Skip to main content

dope_sys/io/socket/option/
source.rs

1use std::os::fd;
2
3use crate::{
4    Errno,
5    io::{
6        socket,
7        socket::option::{Kind, Value},
8    },
9};
10
11/// Native ABI selection for the same validated scalar options.
12pub trait Source: socket::Socket {
13    const KEEP_IDLE: libc::c_int;
14
15    fn option(kind: Kind) -> (libc::c_int, libc::c_int) {
16        match kind {
17            Kind::NoDelay => (libc::IPPROTO_TCP, libc::TCP_NODELAY),
18            Kind::KeepAlive => (libc::SOL_SOCKET, libc::SO_KEEPALIVE),
19            Kind::RecvBuffer => (libc::SOL_SOCKET, libc::SO_RCVBUF),
20            Kind::SendBuffer => (libc::SOL_SOCKET, libc::SO_SNDBUF),
21            Kind::KeepAliveIdle => (libc::IPPROTO_TCP, Self::KEEP_IDLE),
22            Kind::KeepAliveInterval => (libc::IPPROTO_TCP, libc::TCP_KEEPINTVL),
23            Kind::KeepAliveRetries => (libc::IPPROTO_TCP, libc::TCP_KEEPCNT),
24        }
25    }
26
27    fn set(fd: fd::BorrowedFd<'_>, kind: Kind, value: &Value) -> Result<(), Errno> {
28        let (level, name) = Self::option(kind);
29        Self::set_option(fd, level, name, &value.raw())
30    }
31}