libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
mechanism.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #ifndef __ZMQ_MECHANISM_HPP_INCLUDED__
31 #define __ZMQ_MECHANISM_HPP_INCLUDED__
32 
33 #include "stdint.hpp"
34 #include "options.hpp"
35 #include "blob.hpp"
36 #include "metadata.hpp"
37 
38 namespace zmq
39 {
40 
41  // Abstract class representing security mechanism.
42  // Different mechanism extends this class.
43 
44  class msg_t;
45 
47  {
48  public:
49 
50  enum status_t {
54  };
55 
56  mechanism_t (const options_t &options_);
57 
58  virtual ~mechanism_t ();
59 
60  // Prepare next handshake command that is to be sent to the peer.
61  virtual int next_handshake_command (msg_t *msg_) = 0;
62 
63  // Process the handshake command received from the peer.
64  virtual int process_handshake_command (msg_t *msg_) = 0;
65 
66  virtual int encode (msg_t *) { return 0; }
67 
68  virtual int decode (msg_t *) { return 0; }
69 
70  // Notifies mechanism about availability of ZAP message.
71  virtual int zap_msg_available () { return 0; }
72 
73  // Returns the status of this mechanism.
74  virtual status_t status () const = 0;
75 
76  void set_peer_identity (const void *id_ptr, size_t id_size);
77 
78  void peer_identity (msg_t *msg_);
79 
80  void set_user_id (const void *user_id, size_t size);
81 
82  blob_t get_user_id () const;
83 
85  return zmtp_properties;
86  }
87 
89  return zap_properties;
90  }
91 
92  protected:
93 
94  // Only used to identify the socket for the Socket-Type
95  // property in the wire protocol.
96  const char *socket_type_string (int socket_type) const;
97 
98  size_t add_property (unsigned char *ptr, const char *name,
99  const void *value, size_t value_len) const;
100 
101  // Parses a metadata.
102  // Metadata consists of a list of properties consisting of
103  // name and value as size-specified strings.
104  // Returns 0 on success and -1 on error, in which case errno is set.
105  int parse_metadata (
106  const unsigned char *ptr_, size_t length, bool zap_flag = false);
107 
108  // This is called by parse_property method whenever it
109  // parses a new property. The function should return 0
110  // on success and -1 on error, in which case it should
111  // set errno. Signaling error prevents parser from
112  // parsing remaining data.
113  // Derived classes are supposed to override this
114  // method to handle custom processing.
115  virtual int property (const std::string& name_,
116  const void *value_, size_t length_);
117 
118  // Properties received from ZMTP peer.
120 
121  // Properties received from ZAP server.
123 
125 
126  private:
127 
129 
131 
132  // Returns true iff socket associated with the mechanism
133  // is compatible with a given socket type 'type_'.
134  bool check_socket_type (const std::string& type_) const;
135  };
136 
137 }
138 
139 #endif
#define size
void set_peer_identity(const void *id_ptr, size_t id_size)
Definition: mechanism.cpp:48
mechanism_t(const options_t &options_)
Definition: mechanism.cpp:39
virtual ~mechanism_t()
Definition: mechanism.cpp:44
std::map< std::string, std::string > dict_t
Definition: metadata.hpp:43
virtual int process_handshake_command(msg_t *msg_)=0
bool check_socket_type(const std::string &type_) const
Definition: mechanism.cpp:169
virtual status_t status() const =0
metadata_t::dict_t zmtp_properties
Definition: mechanism.hpp:119
options_t options
Definition: mechanism.hpp:124
metadata_t::dict_t zap_properties
Definition: mechanism.hpp:122
int parse_metadata(const unsigned char *ptr_, size_t length, bool zap_flag=false)
Definition: mechanism.cpp:102
virtual int encode(msg_t *)
Definition: mechanism.hpp:66
std::basic_string< unsigned char > blob_t
Definition: blob.hpp:134
void peer_identity(msg_t *msg_)
Definition: mechanism.cpp:53
const char * socket_type_string(int socket_type) const
Definition: mechanism.cpp:74
virtual int zap_msg_available()
Definition: mechanism.hpp:71
virtual int property(const std::string &name_, const void *value_, size_t length_)
Definition: mechanism.cpp:161
virtual int next_handshake_command(msg_t *msg_)=0
virtual int decode(msg_t *)
Definition: mechanism.hpp:68
size_t add_property(unsigned char *ptr, const char *name, const void *value, size_t value_len) const
Definition: mechanism.cpp:86
Definition: address.hpp:35
void set_user_id(const void *user_id, size_t size)
Definition: mechanism.cpp:61
blob_t get_user_id() const
Definition: mechanism.cpp:69
const metadata_t::dict_t & get_zap_properties()
Definition: mechanism.hpp:88
const metadata_t::dict_t & get_zmtp_properties()
Definition: mechanism.hpp:84