This repository has been archived by the owner on Nov 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsockopt.rs
339 lines (303 loc) · 10.2 KB
/
sockopt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use crate::error::{msg_from_errno, Error, ErrorKind};
use libzmq_sys as sys;
use sys::errno;
use libc::{c_int, size_t};
use std::{
ffi::CString,
os::raw::c_void,
time::Duration,
{mem, ptr, str},
};
// This is the value `czmq` uses.
// https://github.com/zeromq/czmq/blob/master/src/zsock_option.inc#L389
const MAX_OPTION_SIZE: size_t = 255;
#[derive(Copy, Clone, Debug)]
pub(crate) enum SocketOption {
Backlog = sys::ZMQ_BACKLOG as isize,
ConnectTimeout = sys::ZMQ_CONNECT_TIMEOUT as isize,
FileDescriptor = sys::ZMQ_FD as isize,
HeartbeatInterval = sys::ZMQ_HEARTBEAT_IVL as isize,
HeartbeatTimeout = sys::ZMQ_HEARTBEAT_TIMEOUT as isize,
HeartbeatTtl = sys::ZMQ_HEARTBEAT_TTL as isize,
SendHighWaterMark = sys::ZMQ_SNDHWM as isize,
SendTimeout = sys::ZMQ_SNDTIMEO as isize,
RecvHighWaterMark = sys::ZMQ_RCVHWM as isize,
RecvTimeout = sys::ZMQ_RCVTIMEO as isize,
NoDrop = sys::ZMQ_XPUB_NODROP as isize,
Linger = sys::ZMQ_LINGER as isize,
LastEndpoint = sys::ZMQ_LAST_ENDPOINT as isize,
PlainPassword = sys::ZMQ_PLAIN_PASSWORD as isize,
PlainUsername = sys::ZMQ_PLAIN_USERNAME as isize,
PlainServer = sys::ZMQ_PLAIN_SERVER as isize,
EnforceDomain = sys::ZMQ_ZAP_ENFORCE_DOMAIN as isize,
ZapDomain = sys::ZMQ_ZAP_DOMAIN as isize,
Subscribe = sys::ZMQ_SUBSCRIBE as isize,
Unsubscribe = sys::ZMQ_UNSUBSCRIBE as isize,
CurvePublicKey = sys::ZMQ_CURVE_PUBLICKEY as isize,
CurveSecretKey = sys::ZMQ_CURVE_SECRETKEY as isize,
CurveServer = sys::ZMQ_CURVE_SERVER as isize,
CurveServerKey = sys::ZMQ_CURVE_SERVERKEY as isize,
InBatchSize = sys::ZMQ_IN_BATCH_SIZE as isize,
OutBatchSize = sys::ZMQ_OUT_BATCH_SIZE as isize,
}
impl From<SocketOption> for c_int {
fn from(s: SocketOption) -> c_int {
match s {
SocketOption::Backlog => SocketOption::Backlog as c_int,
SocketOption::ConnectTimeout => {
SocketOption::ConnectTimeout as c_int
}
SocketOption::FileDescriptor => {
SocketOption::FileDescriptor as c_int
}
SocketOption::HeartbeatInterval => {
SocketOption::HeartbeatInterval as c_int
}
SocketOption::HeartbeatTimeout => {
SocketOption::HeartbeatTimeout as c_int
}
SocketOption::HeartbeatTtl => SocketOption::HeartbeatTtl as c_int,
SocketOption::SendHighWaterMark => {
SocketOption::SendHighWaterMark as c_int
}
SocketOption::SendTimeout => SocketOption::SendTimeout as c_int,
SocketOption::RecvHighWaterMark => {
SocketOption::RecvHighWaterMark as c_int
}
SocketOption::RecvTimeout => SocketOption::RecvTimeout as c_int,
SocketOption::NoDrop => SocketOption::NoDrop as c_int,
SocketOption::Linger => SocketOption::Linger as c_int,
SocketOption::LastEndpoint => SocketOption::LastEndpoint as c_int,
SocketOption::PlainPassword => SocketOption::PlainPassword as c_int,
SocketOption::PlainUsername => SocketOption::PlainUsername as c_int,
SocketOption::PlainServer => SocketOption::PlainServer as c_int,
SocketOption::EnforceDomain => SocketOption::EnforceDomain as c_int,
SocketOption::ZapDomain => SocketOption::ZapDomain as c_int,
SocketOption::Subscribe => SocketOption::Subscribe as c_int,
SocketOption::Unsubscribe => SocketOption::Unsubscribe as c_int,
SocketOption::CurvePublicKey => {
SocketOption::CurvePublicKey as c_int
}
SocketOption::CurveSecretKey => {
SocketOption::CurveSecretKey as c_int
}
SocketOption::CurveServer => SocketOption::CurveServer as c_int,
SocketOption::CurveServerKey => {
SocketOption::CurveServerKey as c_int
}
SocketOption::InBatchSize => SocketOption::InBatchSize as c_int,
SocketOption::OutBatchSize => SocketOption::OutBatchSize as c_int,
}
}
}
fn getsockopt(
mut_sock_ptr: *mut c_void,
option: SocketOption,
mut_value_ptr: *mut c_void,
size: &mut size_t,
) -> Result<(), Error> {
let rc = unsafe {
sys::zmq_getsockopt(mut_sock_ptr, option.into(), mut_value_ptr, size)
};
if rc == -1 {
let errno = unsafe { sys::zmq_errno() };
let err = match errno {
errno::EINVAL => panic!("invalid option"),
errno::ETERM => Error::new(ErrorKind::InvalidCtx),
errno::ENOTSOCK => panic!("invalid socket"),
errno::EINTR => Error::new(ErrorKind::Interrupted),
_ => panic!(msg_from_errno(errno)),
};
Err(err)
} else {
Ok(())
}
}
pub(crate) fn getsockopt_bool(
mut_sock_ptr: *mut c_void,
option: SocketOption,
) -> Result<bool, Error> {
let mut value = c_int::default();
let mut size = mem::size_of::<c_int>();
let value_ptr = &mut value as *mut c_int as *mut c_void;
getsockopt(mut_sock_ptr, option, value_ptr, &mut size)?;
Ok(value != 0)
}
pub(crate) fn getsockopt_scalar<T>(
mut_sock_ptr: *mut c_void,
option: SocketOption,
) -> Result<T, Error>
where
T: Default,
{
let mut value = T::default();
let mut size = mem::size_of::<T>();
let value_ptr = &mut value as *mut T as *mut c_void;
getsockopt(mut_sock_ptr, option, value_ptr, &mut size)?;
Ok(value)
}
pub(crate) fn getsockopt_bytes(
mut_sock_ptr: *mut c_void,
option: SocketOption,
) -> Result<Option<Vec<u8>>, Error> {
let mut size = MAX_OPTION_SIZE;
let mut value = vec![0u8; size];
let value_ptr = value.as_mut_ptr() as *mut c_void;
getsockopt(mut_sock_ptr, option, value_ptr, &mut size)?;
if size == 0 {
Ok(None)
} else {
value.truncate(size);
Ok(Some(value))
}
}
pub(crate) fn getsockopt_string(
mut_sock_ptr: *mut c_void,
option: SocketOption,
) -> Result<Option<String>, Error> {
match getsockopt_bytes(mut_sock_ptr, option)? {
Some(mut bytes) => {
// Remove null byte.
bytes.pop();
if bytes.is_empty() {
Ok(None)
} else {
let c_str = unsafe { CString::from_vec_unchecked(bytes) };
Ok(Some(c_str.into_string().unwrap()))
}
}
None => Ok(None),
}
}
pub(crate) fn getsockopt_option_duration(
mut_sock_ptr: *mut c_void,
option: SocketOption,
none_value: i32,
) -> Result<Option<Duration>, Error> {
let ms: i32 = getsockopt_scalar(mut_sock_ptr, option)?;
if ms == none_value {
Ok(None)
} else {
Ok(Some(Duration::from_millis(ms as u64)))
}
}
fn setsockopt(
mut_sock_ptr: *mut c_void,
option: SocketOption,
value_ptr: *const c_void,
size: size_t,
) -> Result<(), Error> {
let rc = unsafe {
sys::zmq_setsockopt(mut_sock_ptr, option.into(), value_ptr, size)
};
if rc == -1 {
let errno = unsafe { sys::zmq_errno() };
let err = match errno {
errno::EINVAL => panic!("invalid option"),
errno::ETERM => Error::new(ErrorKind::InvalidCtx),
errno::ENOTSOCK => panic!("invalid socket"),
errno::EINTR => Error::new(ErrorKind::Interrupted),
_ => panic!(msg_from_errno(errno)),
};
Err(err)
} else {
Ok(())
}
}
pub(crate) fn setsockopt_bool(
mut_sock_ptr: *mut c_void,
option: SocketOption,
value: bool,
) -> Result<(), Error> {
let value = value as c_int;
let size = mem::size_of::<c_int>() as size_t;
let value_ptr = &value as *const c_int as *const c_void;
setsockopt(mut_sock_ptr, option, value_ptr, size)
}
pub(crate) fn setsockopt_scalar<T>(
mut_sock_ptr: *mut c_void,
option: SocketOption,
value: T,
) -> Result<(), Error> {
let size = mem::size_of::<T>() as size_t;
let value_ptr = &value as *const T as *const c_void;
setsockopt(mut_sock_ptr, option, value_ptr, size)
}
pub(crate) fn setsockopt_option_scalar<T>(
mut_sock_ptr: *mut c_void,
option: SocketOption,
maybe: Option<T>,
none_value: T,
) -> Result<(), Error>
where
T: Eq,
{
let size = mem::size_of::<T>() as size_t;
let value_ptr = match maybe {
Some(value) => &value as *const T as *const c_void,
None => &none_value as *const T as *const c_void,
};
setsockopt(mut_sock_ptr, option, value_ptr, size)
}
pub(crate) fn setsockopt_bytes(
mut_sock_ptr: *mut c_void,
option: SocketOption,
maybe: Option<&[u8]>,
) -> Result<(), Error> {
match maybe {
Some(bytes) => {
let size = bytes.len();
let value_ptr = bytes.as_ptr() as *const c_void;
setsockopt(mut_sock_ptr, option, value_ptr, size)
}
None => setsockopt_null(mut_sock_ptr, option),
}
}
pub(crate) fn setsockopt_str(
mut_sock_ptr: *mut c_void,
option: SocketOption,
maybe: Option<&str>,
) -> Result<(), Error> {
// No need to add a terminating zero byte.
// http://api.zeromq.org/master:zmq-setsockopt
setsockopt_bytes(mut_sock_ptr, option, maybe.map(str::as_bytes))
}
pub(crate) fn setsockopt_null(
mut_sock_ptr: *mut c_void,
option: SocketOption,
) -> Result<(), Error> {
setsockopt(mut_sock_ptr, option, ptr::null(), 0)
}
pub(crate) fn setsockopt_option_duration(
mut_sock_ptr: *mut c_void,
option: SocketOption,
maybe: Option<Duration>,
none_value: i32,
) -> Result<(), Error> {
if let Some(duration) = maybe {
check_duration(duration)?;
}
setsockopt_option_scalar(
mut_sock_ptr,
option,
maybe.map(|d| d.as_millis() as i32),
none_value,
)
}
pub(crate) fn setsockopt_duration(
mut_sock_ptr: *mut c_void,
option: SocketOption,
duration: Duration,
) -> Result<(), Error> {
check_duration(duration)?;
setsockopt_scalar(mut_sock_ptr, option, duration.as_millis() as i32)
}
fn check_duration(duration: Duration) -> Result<(), Error> {
if duration.as_millis() > i32::max_value() as u128 {
Err(Error::new(ErrorKind::InvalidInput(
"ms in duration cannot be greater than i32::MAX",
)))
} else {
Ok(())
}
}