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
38namespace zmq
39{
40class msg_t;
41class session_base_t;
42
43// Abstract class representing security mechanism.
44// Different mechanism extends this class.
45
46class mechanism_t
47{
48 public:
49 enum status_t
50 {
51 handshaking,
52 ready,
53 error
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_routing_id (const void *id_ptr_, size_t id_size_);
77
78 void peer_routing_id (msg_t *msg_);
79
80 void set_user_id (const void *user_id_, size_t size_);
81
82 const blob_t &get_user_id () const;
83
84 const metadata_t::dict_t &get_zmtp_properties ()
85 {
86 return _zmtp_properties;
87 }
88
89 const metadata_t::dict_t &get_zap_properties () { return _zap_properties; }
90
91 protected:
92 // Only used to identify the socket for the Socket-Type
93 // property in the wire protocol.
94 const char *socket_type_string (int socket_type_) const;
95
96 static size_t add_property (unsigned char *ptr_,
97 size_t ptr_capacity_,
98 const char *name_,
99 const void *value_,
100 size_t value_len_);
101 static size_t property_len (const char *name_, size_t value_len_);
102
103 size_t add_basic_properties (unsigned char *ptr_,
104 size_t ptr_capacity_) const;
105 size_t basic_properties_len () const;
106
107 void make_command_with_basic_properties (msg_t *msg_,
108 const char *prefix_,
109 size_t prefix_len_) const;
110
111 // Parses a metadata.
112 // Metadata consists of a list of properties consisting of
113 // name and value as size-specified strings.
114 // Returns 0 on success and -1 on error, in which case errno is set.
115 int parse_metadata (const unsigned char *ptr_,
116 size_t length_,
117 bool zap_flag_ = false);
118
119 // This is called by parse_property method whenever it
120 // parses a new property. The function should return 0
121 // on success and -1 on error, in which case it should
122 // set errno. Signaling error prevents parser from
123 // parsing remaining data.
124 // Derived classes are supposed to override this
125 // method to handle custom processing.
126 virtual int
127 property (const std::string &name_, const void *value_, size_t length_);
128
129 const options_t options;
130
131 private:
132 // Properties received from ZMTP peer.
133 metadata_t::dict_t _zmtp_properties;
134
135 // Properties received from ZAP server.
136 metadata_t::dict_t _zap_properties;
137
138 blob_t _routing_id;
139
140 blob_t _user_id;
141
142 // Returns true iff socket associated with the mechanism
143 // is compatible with a given socket type 'type_'.
144 bool check_socket_type (const char *type_, size_t len_) const;
145};
146}
147
148#endif
149