aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
blob: 245f755158d38ace39ed47b95c6bbee206ceb40b (plain) (blame)
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
use std::ffi::{c_char, c_int};
use std::io;

/// Helper macro to execute a system call that returns an `io::Result`.
macro_rules! syscall {
    ($fn: ident ( $($arg: expr),* $(,)* ) ) => {{
        #[allow(unused_unsafe)]
        let res = unsafe { libc::$fn($($arg, )*) };
        if res == -1 {
            Err(io::Error::last_os_error())
        } else {
            Ok(res)
        }
    }};
}

#[allow(clippy::missing_safety_doc)]
pub unsafe fn setsockopt(
    fd: c_int,
    opt: c_int,
    val: c_int,
    payload: *const c_char,
    optlen: c_int,
) -> io::Result<()> {
    syscall!(setsockopt(
        fd,
        opt,
        val,
        payload.cast(),
        optlen as libc::socklen_t
    ))
    .map(|_| ())
}