Coverage Report

Created: 2026-01-17 06:04

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