Skip to content

Commit

Permalink
Add a function for SO_DOMAIN. (#779)
Browse files Browse the repository at this point in the history
Add a `get_socket_domain` function for using `SO_DOMAIN` on a socket.
  • Loading branch information
sunfishcode authored Aug 10, 2023
1 parent eb97b82 commit 5d9bcbc
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 5 deletions.
33 changes: 33 additions & 0 deletions src/backend/libc/net/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,19 @@ pub(crate) mod sockopt {
use super::{c, in6_addr_new, in_addr_new, BorrowedFd};
use crate::io;
use crate::net::sockopt::Timeout;
#[cfg(not(any(
apple,
solarish,
windows,
target_os = "dragonfly",
target_os = "emscripten",
target_os = "espidf",
target_os = "haiku",
target_os = "netbsd",
target_os = "nto",
target_os = "openbsd"
)))]
use crate::net::AddressFamily;
use crate::net::{Ipv4Addr, Ipv6Addr, SocketType};
use crate::utils::as_mut_ptr;
use core::time::Duration;
Expand Down Expand Up @@ -809,6 +822,26 @@ pub(crate) mod sockopt {
getsockopt(fd, c::SOL_SOCKET as _, c::SO_SNDBUF).map(|size: u32| size as usize)
}

#[inline]
#[cfg(not(any(
apple,
solarish,
windows,
target_os = "dragonfly",
target_os = "emscripten",
target_os = "espidf",
target_os = "haiku",
target_os = "netbsd",
target_os = "nto",
target_os = "openbsd"
)))]
pub(crate) fn get_socket_domain(fd: BorrowedFd<'_>) -> io::Result<AddressFamily> {
let domain: c::c_int = getsockopt(fd, c::SOL_SOCKET as _, c::SO_DOMAIN)?;
Ok(AddressFamily(
domain.try_into().map_err(|_| io::Errno::OPNOTSUPP)?,
))
}

#[inline]
pub(crate) fn set_ip_ttl(fd: BorrowedFd<'_>, ttl: u32) -> io::Result<()> {
setsockopt(fd, c::IPPROTO_IP as _, c::IP_TTL, ttl)
Expand Down
8 changes: 4 additions & 4 deletions src/backend/linux_raw/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ pub(crate) use linux_raw_sys::{
IP_MULTICAST_TTL, IP_TTL, MSG_CMSG_CLOEXEC, MSG_CONFIRM, MSG_DONTROUTE, MSG_DONTWAIT,
MSG_EOR, MSG_ERRQUEUE, MSG_MORE, MSG_NOSIGNAL, MSG_OOB, MSG_PEEK, MSG_TRUNC, MSG_WAITALL,
SCM_CREDENTIALS, SCM_RIGHTS, SHUT_RD, SHUT_RDWR, SHUT_WR, SOCK_DGRAM, SOCK_RAW, SOCK_RDM,
SOCK_SEQPACKET, SOCK_STREAM, SOL_SOCKET, SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_LINGER,
SO_PASSCRED, SO_RCVBUF, SO_RCVTIMEO_NEW, SO_RCVTIMEO_NEW as SO_RCVTIMEO, SO_RCVTIMEO_OLD,
SO_REUSEADDR, SO_SNDBUF, SO_SNDTIMEO_NEW, SO_SNDTIMEO_NEW as SO_SNDTIMEO, SO_SNDTIMEO_OLD,
SO_TYPE, TCP_NODELAY,
SOCK_SEQPACKET, SOCK_STREAM, SOL_SOCKET, SO_BROADCAST, SO_DOMAIN, SO_ERROR, SO_KEEPALIVE,
SO_LINGER, SO_PASSCRED, SO_RCVBUF, SO_RCVTIMEO_NEW, SO_RCVTIMEO_NEW as SO_RCVTIMEO,
SO_RCVTIMEO_OLD, SO_REUSEADDR, SO_SNDBUF, SO_SNDTIMEO_NEW, SO_SNDTIMEO_NEW as SO_SNDTIMEO,
SO_SNDTIMEO_OLD, SO_TYPE, TCP_NODELAY,
},
netlink::*,
};
Expand Down
10 changes: 9 additions & 1 deletion src/backend/linux_raw/net/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ pub(crate) mod sockopt {
use super::{c, BorrowedFd};
use crate::io;
use crate::net::sockopt::Timeout;
use crate::net::{Ipv4Addr, Ipv6Addr, SocketType};
use crate::net::{AddressFamily, Ipv4Addr, Ipv6Addr, SocketType};
use c::{SO_RCVTIMEO_NEW, SO_RCVTIMEO_OLD, SO_SNDTIMEO_NEW, SO_SNDTIMEO_OLD};
use core::time::Duration;
use linux_raw_sys::general::{__kernel_timespec, timeval};
Expand Down Expand Up @@ -1271,6 +1271,14 @@ pub(crate) mod sockopt {
getsockopt(fd, c::SOL_SOCKET as _, c::SO_SNDBUF).map(|size: u32| size as usize)
}

#[inline]
pub(crate) fn get_socket_domain(fd: BorrowedFd<'_>) -> io::Result<AddressFamily> {
let domain: c::c_int = getsockopt(fd, c::SOL_SOCKET as _, c::SO_DOMAIN)?;
Ok(AddressFamily(
domain.try_into().map_err(|_| io::Errno::OPNOTSUPP)?,
))
}

#[inline]
pub(crate) fn set_ip_ttl(fd: BorrowedFd<'_>, ttl: u32) -> io::Result<()> {
setsockopt(fd, c::IPPROTO_IP as _, c::IP_TTL, ttl)
Expand Down
64 changes: 64 additions & 0 deletions src/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
#![doc(alias = "getsockopt")]
#![doc(alias = "setsockopt")]

#[cfg(not(any(
apple,
solarish,
windows,
target_os = "dragonfly",
target_os = "emscripten",
target_os = "espidf",
target_os = "haiku",
target_os = "netbsd",
target_os = "nto",
target_os = "openbsd"
)))]
use crate::net::AddressFamily;
use crate::net::{Ipv4Addr, Ipv6Addr, SocketType};
use crate::{backend, io};
use backend::c;
Expand Down Expand Up @@ -712,6 +725,57 @@ pub fn get_socket_send_buffer_size<Fd: AsFd>(fd: Fd) -> io::Result<usize> {
backend::net::syscalls::sockopt::get_socket_send_buffer_size(fd.as_fd())
}

/// `getsockopt(fd, SOL_SOCKET, SO_DOMAIN)`
///
/// # References
/// - [POSIX `getsockopt`]
/// - [POSIX `sys/socket.h`]
/// - [Linux `getsockopt`]
/// - [Linux `socket`]
/// - [Winsock2 `getsockopt`]
/// - [Winsock2 `SOL_SOCKET` options]
/// - [Apple]
/// - [FreeBSD]
/// - [NetBSD]
/// - [OpenBSD]
/// - [DragonFly BSD]
/// - [illumos]
/// - [glibc `getsockopt`]
/// - [glibc `SOL_SOCKET` Options]
///
/// [POSIX `getsockopt`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html
/// [POSIX `sys/socket.h`]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html
/// [Linux `getsockopt`]: https://man7.org/linux/man-pages/man2/getsockopt.2.html
/// [Linux `socket`]: https://man7.org/linux/man-pages/man7/socket.7.html
/// [Winsock2 `getsockopt`]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-getsockopt
/// [Winsock2 `SOL_SOCKET` options]: https://docs.microsoft.com/en-us/windows/win32/winsock/sol-socket-socket-options
/// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getsockopt.2.html
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=getsockopt&sektion=2
/// [NetBSD]: https://man.netbsd.org/getsockopt.2
/// [OpenBSD]: https://man.openbsd.org/getsockopt.2
/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=getsockopt&section=2
/// [illumos]: https://illumos.org/man/3SOCKET/getsockopt
/// [glibc `getsockopt`]: https://www.gnu.org/software/libc/manual/html_node/Socket-Option-Functions.html
/// [glibc `SOL_SOCKET` options]: https://www.gnu.org/software/libc/manual/html_node/Socket_002dLevel-Options.html
// TODO: OpenBSD and Solarish support submitted upstream: https://github.com/rust-lang/libc/pull/3316
#[cfg(not(any(
apple,
solarish,
windows,
target_os = "dragonfly",
target_os = "emscripten",
target_os = "espidf",
target_os = "haiku",
target_os = "netbsd",
target_os = "nto",
target_os = "openbsd"
)))]
#[inline]
#[doc(alias = "SO_DOMAIN")]
pub fn get_socket_domain<Fd: AsFd>(fd: Fd) -> io::Result<AddressFamily> {
backend::net::syscalls::sockopt::get_socket_domain(fd.as_fd())
}

/// `setsockopt(fd, IPPROTO_IP, IP_TTL, ttl)`
///
/// # References
Expand Down
16 changes: 16 additions & 0 deletions tests/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ fn test_sockopts_ipv4() {
rustix::net::sockopt::get_socket_send_buffer_size(&s).unwrap(),
0
);
#[cfg(not(any(
apple,
solarish,
windows,
target_os = "dragonfly",
target_os = "emscripten",
target_os = "espidf",
target_os = "haiku",
target_os = "netbsd",
target_os = "nto",
target_os = "openbsd"
)))]
assert_eq!(
rustix::net::sockopt::get_socket_domain(&s).unwrap(),
AddressFamily::INET
);

// Set a timeout.
rustix::net::sockopt::set_socket_timeout(
Expand Down

0 comments on commit 5d9bcbc

Please sign in to comment.