/src/libzmq/src/mechanism.hpp
Line | Count | Source |
1 | | /* SPDX-License-Identifier: MPL-2.0 */ |
2 | | |
3 | | #ifndef __ZMQ_MECHANISM_HPP_INCLUDED__ |
4 | | #define __ZMQ_MECHANISM_HPP_INCLUDED__ |
5 | | |
6 | | #include "stdint.hpp" |
7 | | #include "options.hpp" |
8 | | #include "blob.hpp" |
9 | | #include "metadata.hpp" |
10 | | |
11 | | namespace zmq |
12 | | { |
13 | | class msg_t; |
14 | | class session_base_t; |
15 | | |
16 | | // Abstract class representing security mechanism. |
17 | | // Different mechanism extends this class. |
18 | | |
19 | | class mechanism_t |
20 | | { |
21 | | public: |
22 | | enum status_t |
23 | | { |
24 | | handshaking, |
25 | | ready, |
26 | | error |
27 | | }; |
28 | | |
29 | | mechanism_t (const options_t &options_); |
30 | | |
31 | | virtual ~mechanism_t (); |
32 | | |
33 | | // Prepare next handshake command that is to be sent to the peer. |
34 | | virtual int next_handshake_command (msg_t *msg_) = 0; |
35 | | |
36 | | // Process the handshake command received from the peer. |
37 | | virtual int process_handshake_command (msg_t *msg_) = 0; |
38 | | |
39 | 0 | virtual int encode (msg_t *) { return 0; } |
40 | | |
41 | 0 | virtual int decode (msg_t *) { return 0; } |
42 | | |
43 | | // Notifies mechanism about availability of ZAP message. |
44 | 0 | virtual int zap_msg_available () { return 0; } |
45 | | |
46 | | // Returns the status of this mechanism. |
47 | | virtual status_t status () const = 0; |
48 | | |
49 | | void set_peer_routing_id (const void *id_ptr_, size_t id_size_); |
50 | | |
51 | | void peer_routing_id (msg_t *msg_); |
52 | | |
53 | | void set_user_id (const void *user_id_, size_t size_); |
54 | | |
55 | | const blob_t &get_user_id () const; |
56 | | |
57 | | const metadata_t::dict_t &get_zmtp_properties () const |
58 | 0 | { |
59 | 0 | return _zmtp_properties; |
60 | 0 | } |
61 | | |
62 | | const metadata_t::dict_t &get_zap_properties () const |
63 | 0 | { |
64 | 0 | return _zap_properties; |
65 | 0 | } |
66 | | |
67 | | protected: |
68 | | // Only used to identify the socket for the Socket-Type |
69 | | // property in the wire protocol. |
70 | | static const char *socket_type_string (int socket_type_); |
71 | | |
72 | | static size_t add_property (unsigned char *ptr_, |
73 | | size_t ptr_capacity_, |
74 | | const char *name_, |
75 | | const void *value_, |
76 | | size_t value_len_); |
77 | | static size_t property_len (const char *name_, size_t value_len_); |
78 | | |
79 | | size_t add_basic_properties (unsigned char *ptr_, |
80 | | size_t ptr_capacity_) const; |
81 | | size_t basic_properties_len () const; |
82 | | |
83 | | void make_command_with_basic_properties (msg_t *msg_, |
84 | | const char *prefix_, |
85 | | size_t prefix_len_) const; |
86 | | |
87 | | // Parses a metadata. |
88 | | // Metadata consists of a list of properties consisting of |
89 | | // name and value as size-specified strings. |
90 | | // Returns 0 on success and -1 on error, in which case errno is set. |
91 | | int parse_metadata (const unsigned char *ptr_, |
92 | | size_t length_, |
93 | | bool zap_flag_ = false); |
94 | | |
95 | | // This is called by parse_property method whenever it |
96 | | // parses a new property. The function should return 0 |
97 | | // on success and -1 on error, in which case it should |
98 | | // set errno. Signaling error prevents parser from |
99 | | // parsing remaining data. |
100 | | // Derived classes are supposed to override this |
101 | | // method to handle custom processing. |
102 | | virtual int |
103 | | property (const std::string &name_, const void *value_, size_t length_); |
104 | | |
105 | | const options_t options; |
106 | | |
107 | | private: |
108 | | // Properties received from ZMTP peer. |
109 | | metadata_t::dict_t _zmtp_properties; |
110 | | |
111 | | // Properties received from ZAP server. |
112 | | metadata_t::dict_t _zap_properties; |
113 | | |
114 | | blob_t _routing_id; |
115 | | |
116 | | blob_t _user_id; |
117 | | |
118 | | // Returns true iff socket associated with the mechanism |
119 | | // is compatible with a given socket type 'type_'. |
120 | | bool check_socket_type (const char *type_, size_t len_) const; |
121 | | }; |
122 | | } |
123 | | |
124 | | #endif |