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_OPTIONS_HPP_INCLUDED__ |
31 | #define __ZMQ_OPTIONS_HPP_INCLUDED__ |
32 | |
33 | #include <string> |
34 | #include <vector> |
35 | #include <map> |
36 | |
37 | #include "atomic_ptr.hpp" |
38 | #include "stddef.h" |
39 | #include "stdint.hpp" |
40 | #include "tcp_address.hpp" |
41 | |
42 | #if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED |
43 | #include <set> |
44 | #include <sys/types.h> |
45 | #endif |
46 | #ifdef ZMQ_HAVE_LOCAL_PEERCRED |
47 | #include <sys/ucred.h> |
48 | #endif |
49 | |
50 | #if __cplusplus >= 201103L |
51 | #include <type_traits> |
52 | #endif |
53 | |
54 | // Normal base 256 key is 32 bytes |
55 | #define CURVE_KEYSIZE 32 |
56 | // Key encoded using Z85 is 40 bytes |
57 | #define CURVE_KEYSIZE_Z85 40 |
58 | |
59 | namespace zmq |
60 | { |
61 | struct options_t |
62 | { |
63 | options_t (); |
64 | |
65 | int set_curve_key (uint8_t *destination_, |
66 | const void *optval_, |
67 | size_t optvallen_); |
68 | |
69 | int setsockopt (int option_, const void *optval_, size_t optvallen_); |
70 | int getsockopt (int option_, void *optval_, size_t *optvallen_) const; |
71 | |
72 | // High-water marks for message pipes. |
73 | int sndhwm; |
74 | int rcvhwm; |
75 | |
76 | // I/O thread affinity. |
77 | uint64_t affinity; |
78 | |
79 | // Socket routing id. |
80 | unsigned char routing_id_size; |
81 | unsigned char routing_id[256]; |
82 | |
83 | // Maximum transfer rate [kb/s]. Default 100kb/s. |
84 | int rate; |
85 | |
86 | // Reliability time interval [ms]. Default 10 seconds. |
87 | int recovery_ivl; |
88 | |
89 | // Sets the time-to-live field in every multicast packet sent. |
90 | int multicast_hops; |
91 | |
92 | // Sets the maximum transport data unit size in every multicast |
93 | // packet sent. |
94 | int multicast_maxtpdu; |
95 | |
96 | // SO_SNDBUF and SO_RCVBUF to be passed to underlying transport sockets. |
97 | int sndbuf; |
98 | int rcvbuf; |
99 | |
100 | // Type of service (containing DSCP and ECN socket options) |
101 | int tos; |
102 | |
103 | // Socket type. |
104 | int8_t type; |
105 | |
106 | // Linger time, in milliseconds. |
107 | atomic_value_t linger; |
108 | |
109 | // Maximum interval in milliseconds beyond which userspace will |
110 | // timeout connect(). |
111 | // Default 0 (unused) |
112 | int connect_timeout; |
113 | |
114 | // Maximum interval in milliseconds beyond which TCP will timeout |
115 | // retransmitted packets. |
116 | // Default 0 (unused) |
117 | int tcp_maxrt; |
118 | |
119 | // Minimum interval between attempts to reconnect, in milliseconds. |
120 | // Default 100ms |
121 | int reconnect_ivl; |
122 | |
123 | // Maximum interval between attempts to reconnect, in milliseconds. |
124 | // Default 0 (unused) |
125 | int reconnect_ivl_max; |
126 | |
127 | // Maximum backlog for pending connections. |
128 | int backlog; |
129 | |
130 | // Maximal size of message to handle. |
131 | int64_t maxmsgsize; |
132 | |
133 | // The timeout for send/recv operations for this socket, in milliseconds. |
134 | int rcvtimeo; |
135 | int sndtimeo; |
136 | |
137 | // If true, IPv6 is enabled (as well as IPv4) |
138 | bool ipv6; |
139 | |
140 | // If 1, connecting pipes are not attached immediately, meaning a send() |
141 | // on a socket with only connecting pipes would block |
142 | int immediate; |
143 | |
144 | // If 1, (X)SUB socket should filter the messages. If 0, it should not. |
145 | bool filter; |
146 | |
147 | // If true, the subscription matching on (X)PUB and (X)SUB sockets |
148 | // is reversed. Messages are sent to and received by non-matching |
149 | // sockets. |
150 | bool invert_matching; |
151 | |
152 | // If true, the routing id message is forwarded to the socket. |
153 | bool recv_routing_id; |
154 | |
155 | // if true, router socket accepts non-zmq tcp connections |
156 | bool raw_socket; |
157 | bool raw_notify; // Provide connect notifications |
158 | |
159 | // Address of SOCKS proxy |
160 | std::string socks_proxy_address; |
161 | |
162 | // Credentials for SOCKS proxy. |
163 | // Conneciton method will be basic auth if username |
164 | // is not empty, no auth otherwise. |
165 | std::string socks_proxy_username; |
166 | std::string socks_proxy_password; |
167 | |
168 | // TCP keep-alive settings. |
169 | // Defaults to -1 = do not change socket options |
170 | int tcp_keepalive; |
171 | int tcp_keepalive_cnt; |
172 | int tcp_keepalive_idle; |
173 | int tcp_keepalive_intvl; |
174 | |
175 | // TCP accept() filters |
176 | typedef std::vector<tcp_address_mask_t> tcp_accept_filters_t; |
177 | tcp_accept_filters_t tcp_accept_filters; |
178 | |
179 | // IPC accept() filters |
180 | #if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED |
181 | typedef std::set<uid_t> ipc_uid_accept_filters_t; |
182 | ipc_uid_accept_filters_t ipc_uid_accept_filters; |
183 | typedef std::set<gid_t> ipc_gid_accept_filters_t; |
184 | ipc_gid_accept_filters_t ipc_gid_accept_filters; |
185 | #endif |
186 | #if defined ZMQ_HAVE_SO_PEERCRED |
187 | typedef std::set<pid_t> ipc_pid_accept_filters_t; |
188 | ipc_pid_accept_filters_t ipc_pid_accept_filters; |
189 | #endif |
190 | |
191 | // Security mechanism for all connections on this socket |
192 | int mechanism; |
193 | |
194 | // If peer is acting as server for PLAIN or CURVE mechanisms |
195 | int as_server; |
196 | |
197 | // ZAP authentication domain |
198 | std::string zap_domain; |
199 | |
200 | // Security credentials for PLAIN mechanism |
201 | std::string plain_username; |
202 | std::string plain_password; |
203 | |
204 | // Security credentials for CURVE mechanism |
205 | uint8_t curve_public_key[CURVE_KEYSIZE]; |
206 | uint8_t curve_secret_key[CURVE_KEYSIZE]; |
207 | uint8_t curve_server_key[CURVE_KEYSIZE]; |
208 | |
209 | // Principals for GSSAPI mechanism |
210 | std::string gss_principal; |
211 | std::string gss_service_principal; |
212 | |
213 | // Name types GSSAPI principals |
214 | int gss_principal_nt; |
215 | int gss_service_principal_nt; |
216 | |
217 | // If true, gss encryption will be disabled |
218 | bool gss_plaintext; |
219 | |
220 | // ID of the socket. |
221 | int socket_id; |
222 | |
223 | // If true, socket conflates outgoing/incoming messages. |
224 | // Applicable to dealer, push/pull, pub/sub socket types. |
225 | // Cannot receive multi-part messages. |
226 | // Ignores hwm |
227 | bool conflate; |
228 | |
229 | // If connection handshake is not done after this many milliseconds, |
230 | // close socket. Default is 30 secs. 0 means no handshake timeout. |
231 | int handshake_ivl; |
232 | |
233 | bool connected; |
234 | // If remote peer receives a PING message and doesn't receive another |
235 | // message within the ttl value, it should close the connection |
236 | // (measured in tenths of a second) |
237 | uint16_t heartbeat_ttl; |
238 | // Time in milliseconds between sending heartbeat PING messages. |
239 | int heartbeat_interval; |
240 | // Time in milliseconds to wait for a PING response before disconnecting |
241 | int heartbeat_timeout; |
242 | |
243 | #if defined ZMQ_HAVE_VMCI |
244 | uint64_t vmci_buffer_size; |
245 | uint64_t vmci_buffer_min_size; |
246 | uint64_t vmci_buffer_max_size; |
247 | int vmci_connect_timeout; |
248 | #endif |
249 | |
250 | // When creating a new ZMQ socket, if this option is set the value |
251 | // will be used as the File Descriptor instead of allocating a new |
252 | // one via the socket () system call. |
253 | int use_fd; |
254 | |
255 | // Device to bind the underlying socket to, eg. VRF or interface |
256 | std::string bound_device; |
257 | |
258 | // Enforce a non-empty ZAP domain requirement for PLAIN auth |
259 | bool zap_enforce_domain; |
260 | |
261 | // Use of loopback fastpath. |
262 | bool loopback_fastpath; |
263 | |
264 | // Loop sent multicast packets to local sockets |
265 | bool multicast_loop; |
266 | |
267 | // Maximal batching size for engines with receiving functionality. |
268 | // So, if there are 10 messages that fit into the batch size, all of |
269 | // them may be read by a single 'recv' system call, thus avoiding |
270 | // unnecessary network stack traversals. |
271 | int in_batch_size; |
272 | // Maximal batching size for engines with sending functionality. |
273 | // So, if there are 10 messages that fit into the batch size, all of |
274 | // them may be written by a single 'send' system call, thus avoiding |
275 | // unnecessary network stack traversals. |
276 | int out_batch_size; |
277 | |
278 | // Use zero copy strategy for storing message content when decoding. |
279 | bool zero_copy; |
280 | |
281 | // Router socket ZMQ_NOTIFY_CONNECT/ZMQ_NOTIFY_DISCONNECT notifications |
282 | int router_notify; |
283 | |
284 | // Application metadata |
285 | std::map<std::string, std::string> app_metadata; |
286 | |
287 | // Version of monitor events to emit |
288 | int monitor_event_version; |
289 | |
290 | // WSS Keys |
291 | std::string wss_key_pem; |
292 | std::string wss_cert_pem; |
293 | std::string wss_trust_pem; |
294 | std::string wss_hostname; |
295 | bool wss_trust_system; |
296 | }; |
297 | |
298 | inline bool get_effective_conflate_option (const options_t &options) |
299 | { |
300 | // conflate is only effective for some socket types |
301 | return options.conflate |
302 | && (options.type == ZMQ_DEALER || options.type == ZMQ_PULL |
303 | || options.type == ZMQ_PUSH || options.type == ZMQ_PUB |
304 | || options.type == ZMQ_SUB); |
305 | } |
306 | |
307 | int do_getsockopt (void *const optval_, |
308 | size_t *const optvallen_, |
309 | const void *value_, |
310 | const size_t value_len_); |
311 | |
312 | template <typename T> |
313 | int do_getsockopt (void *const optval_, size_t *const optvallen_, T value_) |
314 | { |
315 | #if __cplusplus >= 201103L && (!defined(__GNUC__) || __GNUC__ > 5) |
316 | static_assert (std::is_trivially_copyable<T>::value, |
317 | "invalid use of do_getsockopt" ); |
318 | #endif |
319 | return do_getsockopt (optval_, optvallen_, &value_, sizeof (T)); |
320 | } |
321 | |
322 | int do_getsockopt (void *const optval_, |
323 | size_t *const optvallen_, |
324 | const std::string &value_); |
325 | |
326 | int do_setsockopt_int_as_bool_strict (const void *const optval_, |
327 | const size_t optvallen_, |
328 | bool *const out_value_); |
329 | |
330 | int do_setsockopt_int_as_bool_relaxed (const void *const optval_, |
331 | const size_t optvallen_, |
332 | bool *const out_value_); |
333 | } |
334 | |
335 | #endif |
336 | |