options.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_OPTIONS_HPP_INCLUDED__
  25. #define __ZMQ_OPTIONS_HPP_INCLUDED__
  26. #include <string>
  27. #include <vector>
  28. #include <map>
  29. #include "atomic_ptr.hpp"
  30. #include "stddef.h"
  31. #include "stdint.hpp"
  32. #include "tcp_address.hpp"
  33. #if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
  34. #include <set>
  35. #include <sys/types.h>
  36. #endif
  37. #ifdef ZMQ_HAVE_LOCAL_PEERCRED
  38. #include <sys/ucred.h>
  39. #endif
  40. #if __cplusplus >= 201103L || (defined _MSC_VER && _MSC_VER >= 1700)
  41. #include <type_traits>
  42. #endif
  43. // Normal base 256 key is 32 bytes
  44. #define CURVE_KEYSIZE 32
  45. // Key encoded using Z85 is 40 bytes
  46. #define CURVE_KEYSIZE_Z85 40
  47. namespace zmq
  48. {
  49. struct options_t
  50. {
  51. options_t ();
  52. int set_curve_key (uint8_t *destination_,
  53. const void *optval_,
  54. size_t optvallen_);
  55. int setsockopt (int option_, const void *optval_, size_t optvallen_);
  56. int getsockopt (int option_, void *optval_, size_t *optvallen_) const;
  57. // High-water marks for message pipes.
  58. int sndhwm;
  59. int rcvhwm;
  60. // I/O thread affinity.
  61. uint64_t affinity;
  62. // Socket routing id.
  63. unsigned char routing_id_size;
  64. unsigned char routing_id[256];
  65. // Maximum transfer rate [kb/s]. Default 100kb/s.
  66. int rate;
  67. // Reliability time interval [ms]. Default 10 seconds.
  68. int recovery_ivl;
  69. // Sets the time-to-live field in every multicast packet sent.
  70. int multicast_hops;
  71. // Sets the maximum transport data unit size in every multicast
  72. // packet sent.
  73. int multicast_maxtpdu;
  74. // SO_SNDBUF and SO_RCVBUF to be passed to underlying transport sockets.
  75. int sndbuf;
  76. int rcvbuf;
  77. // Type of service (containing DSCP and ECN socket options)
  78. int tos;
  79. // Protocol-defined priority
  80. int priority;
  81. // Socket type.
  82. int8_t type;
  83. // Linger time, in milliseconds.
  84. atomic_value_t linger;
  85. // Maximum interval in milliseconds beyond which userspace will
  86. // timeout connect().
  87. // Default 0 (unused)
  88. int connect_timeout;
  89. // Maximum interval in milliseconds beyond which TCP will timeout
  90. // retransmitted packets.
  91. // Default 0 (unused)
  92. int tcp_maxrt;
  93. // Disable reconnect under certain conditions
  94. // Default 0
  95. int reconnect_stop;
  96. // Minimum interval between attempts to reconnect, in milliseconds.
  97. // Default 100ms
  98. int reconnect_ivl;
  99. // Maximum interval between attempts to reconnect, in milliseconds.
  100. // Default 0 (unused)
  101. int reconnect_ivl_max;
  102. // Maximum backlog for pending connections.
  103. int backlog;
  104. // Maximal size of message to handle.
  105. int64_t maxmsgsize;
  106. // The timeout for send/recv operations for this socket, in milliseconds.
  107. int rcvtimeo;
  108. int sndtimeo;
  109. // If true, IPv6 is enabled (as well as IPv4)
  110. bool ipv6;
  111. // If 1, connecting pipes are not attached immediately, meaning a send()
  112. // on a socket with only connecting pipes would block
  113. int immediate;
  114. // If 1, (X)SUB socket should filter the messages. If 0, it should not.
  115. bool filter;
  116. // If true, the subscription matching on (X)PUB and (X)SUB sockets
  117. // is reversed. Messages are sent to and received by non-matching
  118. // sockets.
  119. bool invert_matching;
  120. // If true, the routing id message is forwarded to the socket.
  121. bool recv_routing_id;
  122. // if true, router socket accepts non-zmq tcp connections
  123. bool raw_socket;
  124. bool raw_notify; // Provide connect notifications
  125. // Address of SOCKS proxy
  126. std::string socks_proxy_address;
  127. // Credentials for SOCKS proxy.
  128. // Connection method will be basic auth if username
  129. // is not empty, no auth otherwise.
  130. std::string socks_proxy_username;
  131. std::string socks_proxy_password;
  132. // TCP keep-alive settings.
  133. // Defaults to -1 = do not change socket options
  134. int tcp_keepalive;
  135. int tcp_keepalive_cnt;
  136. int tcp_keepalive_idle;
  137. int tcp_keepalive_intvl;
  138. // TCP accept() filters
  139. typedef std::vector<tcp_address_mask_t> tcp_accept_filters_t;
  140. tcp_accept_filters_t tcp_accept_filters;
  141. // IPC accept() filters
  142. #if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
  143. typedef std::set<uid_t> ipc_uid_accept_filters_t;
  144. ipc_uid_accept_filters_t ipc_uid_accept_filters;
  145. typedef std::set<gid_t> ipc_gid_accept_filters_t;
  146. ipc_gid_accept_filters_t ipc_gid_accept_filters;
  147. #endif
  148. #if defined ZMQ_HAVE_SO_PEERCRED
  149. typedef std::set<pid_t> ipc_pid_accept_filters_t;
  150. ipc_pid_accept_filters_t ipc_pid_accept_filters;
  151. #endif
  152. // Security mechanism for all connections on this socket
  153. int mechanism;
  154. // If peer is acting as server for PLAIN or CURVE mechanisms
  155. int as_server;
  156. // ZAP authentication domain
  157. std::string zap_domain;
  158. // Security credentials for PLAIN mechanism
  159. std::string plain_username;
  160. std::string plain_password;
  161. // Security credentials for CURVE mechanism
  162. uint8_t curve_public_key[CURVE_KEYSIZE];
  163. uint8_t curve_secret_key[CURVE_KEYSIZE];
  164. uint8_t curve_server_key[CURVE_KEYSIZE];
  165. // Principals for GSSAPI mechanism
  166. std::string gss_principal;
  167. std::string gss_service_principal;
  168. // Name types GSSAPI principals
  169. int gss_principal_nt;
  170. int gss_service_principal_nt;
  171. // If true, gss encryption will be disabled
  172. bool gss_plaintext;
  173. // ID of the socket.
  174. int socket_id;
  175. // If true, socket conflates outgoing/incoming messages.
  176. // Applicable to dealer, push/pull, pub/sub socket types.
  177. // Cannot receive multi-part messages.
  178. // Ignores hwm
  179. bool conflate;
  180. // If connection handshake is not done after this many milliseconds,
  181. // close socket. Default is 30 secs. 0 means no handshake timeout.
  182. int handshake_ivl;
  183. bool connected;
  184. // If remote peer receives a PING message and doesn't receive another
  185. // message within the ttl value, it should close the connection
  186. // (measured in tenths of a second)
  187. uint16_t heartbeat_ttl;
  188. // Time in milliseconds between sending heartbeat PING messages.
  189. int heartbeat_interval;
  190. // Time in milliseconds to wait for a PING response before disconnecting
  191. int heartbeat_timeout;
  192. #if defined ZMQ_HAVE_VMCI
  193. uint64_t vmci_buffer_size;
  194. uint64_t vmci_buffer_min_size;
  195. uint64_t vmci_buffer_max_size;
  196. int vmci_connect_timeout;
  197. #endif
  198. // When creating a new ZMQ socket, if this option is set the value
  199. // will be used as the File Descriptor instead of allocating a new
  200. // one via the socket () system call.
  201. int use_fd;
  202. // Device to bind the underlying socket to, eg. VRF or interface
  203. std::string bound_device;
  204. // Enforce a non-empty ZAP domain requirement for PLAIN auth
  205. bool zap_enforce_domain;
  206. // Use of loopback fastpath.
  207. bool loopback_fastpath;
  208. // Loop sent multicast packets to local sockets
  209. bool multicast_loop;
  210. // Maximal batching size for engines with receiving functionality.
  211. // So, if there are 10 messages that fit into the batch size, all of
  212. // them may be read by a single 'recv' system call, thus avoiding
  213. // unnecessary network stack traversals.
  214. int in_batch_size;
  215. // Maximal batching size for engines with sending functionality.
  216. // So, if there are 10 messages that fit into the batch size, all of
  217. // them may be written by a single 'send' system call, thus avoiding
  218. // unnecessary network stack traversals.
  219. int out_batch_size;
  220. // Use zero copy strategy for storing message content when decoding.
  221. bool zero_copy;
  222. // Router socket ZMQ_NOTIFY_CONNECT/ZMQ_NOTIFY_DISCONNECT notifications
  223. int router_notify;
  224. // Application metadata
  225. std::map<std::string, std::string> app_metadata;
  226. // Version of monitor events to emit
  227. int monitor_event_version;
  228. // WSS Keys
  229. std::string wss_key_pem;
  230. std::string wss_cert_pem;
  231. std::string wss_trust_pem;
  232. std::string wss_hostname;
  233. bool wss_trust_system;
  234. // Hello msg
  235. std::vector<unsigned char> hello_msg;
  236. bool can_send_hello_msg;
  237. // Disconnect msg
  238. std::vector<unsigned char> disconnect_msg;
  239. bool can_recv_disconnect_msg;
  240. // Hiccup msg
  241. std::vector<unsigned char> hiccup_msg;
  242. bool can_recv_hiccup_msg;
  243. // This option removes several delays caused by scheduling, interrupts and context switching.
  244. int busy_poll;
  245. };
  246. inline bool get_effective_conflate_option (const options_t &options)
  247. {
  248. // conflate is only effective for some socket types
  249. return options.conflate
  250. && (options.type == ZMQ_DEALER || options.type == ZMQ_PULL
  251. || options.type == ZMQ_PUSH || options.type == ZMQ_PUB
  252. || options.type == ZMQ_SUB);
  253. }
  254. int do_getsockopt (void *optval_,
  255. size_t *optvallen_,
  256. const void *value_,
  257. size_t value_len_);
  258. template <typename T>
  259. int do_getsockopt (void *const optval_, size_t *const optvallen_, T value_)
  260. {
  261. #if __cplusplus >= 201103L && (!defined(__GNUC__) || __GNUC__ > 5)
  262. static_assert (std::is_trivially_copyable<T>::value,
  263. "invalid use of do_getsockopt");
  264. #endif
  265. return do_getsockopt (optval_, optvallen_, &value_, sizeof (T));
  266. }
  267. int do_getsockopt (void *optval_,
  268. size_t *optvallen_,
  269. const std::string &value_);
  270. int do_setsockopt_int_as_bool_strict (const void *optval_,
  271. size_t optvallen_,
  272. bool *out_value_);
  273. int do_setsockopt_int_as_bool_relaxed (const void *optval_,
  274. size_t optvallen_,
  275. bool *out_value_);
  276. }
  277. #endif