Skip to content

Commit

Permalink
Add ip_mtu and ipv6_mtu on linux platform
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Feb 10, 2025
1 parent 2f489cc commit 3f52ea2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/backend/libc/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,18 @@ pub(crate) fn ipv6_v6only(fd: BorrowedFd<'_>) -> io::Result<bool> {
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_V6ONLY).map(to_bool)
}

#[cfg(linux_kernel)]
#[inline]
pub(crate) fn ip_mtu(fd: BorrowedFd<'_>) -> io::Result<u32> {
getsockopt(fd, c::IPPROTO_IP, c::IP_MTU)
}

#[cfg(linux_kernel)]
#[inline]
pub(crate) fn ipv6_mtu(fd: BorrowedFd<'_>) -> io::Result<u32> {
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_MTU)
}

#[inline]
pub(crate) fn set_ip_multicast_if(fd: BorrowedFd<'_>, value: &Ipv4Addr) -> io::Result<()> {
setsockopt(fd, c::IPPROTO_IP, c::IP_MULTICAST_IF, to_imr_addr(value))
Expand Down
10 changes: 10 additions & 0 deletions src/backend/linux_raw/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,16 @@ pub(crate) fn ipv6_v6only(fd: BorrowedFd<'_>) -> io::Result<bool> {
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_V6ONLY).map(to_bool)
}

#[inline]
pub(crate) fn ip_mtu(fd: BorrowedFd<'_>) -> io::Result<u32> {
getsockopt(fd, c::IPPROTO_IP, c::IP_MTU)
}

#[inline]
pub(crate) fn ipv6_mtu(fd: BorrowedFd<'_>) -> io::Result<u32> {
getsockopt(fd, c::IPPROTO_IPV6, c::IPV6_MTU)
}

#[inline]
pub(crate) fn set_ip_multicast_if_with_ifindex(
fd: BorrowedFd<'_>,
Expand Down
24 changes: 24 additions & 0 deletions src/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,30 @@ pub fn ipv6_v6only<Fd: AsFd>(fd: Fd) -> io::Result<bool> {
backend::net::sockopt::ipv6_v6only(fd.as_fd())
}

/// `getsockopt(fd, IPPROTO_IP, IP_MTU)`
///
/// See the [module-level documentation] for more.
///
/// [module-level documentation]: self#references-for-get_ip_-and-set_ip_-functions
#[inline]
#[cfg(linux_kernel)]
#[doc(alias = "IP_MTU")]
pub fn ip_mtu<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
backend::net::sockopt::ip_mtu(fd.as_fd())
}

/// `getsockopt(fd, IPPROTO_IPV6, IPV6_MTU)`
///
/// See the [module-level documentation] for more.
///
/// [module-level documentation]: self#references-for-get_ip_-and-set_ip_-functions
#[inline]
#[cfg(linux_kernel)]
#[doc(alias = "IPV6_MTU")]
pub fn ipv6_mtu<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
backend::net::sockopt::ipv6_mtu(fd.as_fd())
}

/// `setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, value)`
///
/// See the [module-level documentation] for more.
Expand Down
22 changes: 22 additions & 0 deletions tests/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,28 @@ fn test_sockopts_ipv6() {
test_sockopts_tcp(&s);
}

#[cfg(linux_kernel)]
#[test]
fn test_socketopts_ip_mtu() {
crate::init();

let s = rustix::net::socket(AddressFamily::INET, SocketType::DGRAM, None).unwrap();
rustix::net::bind(&s, &"127.0.0.1:0".parse().unwrap()).unwrap();
rustix::net::connect(&s, &"127.0.0.1:0".parse().unwrap()).unwrap();
assert!(rustix::net::sockopt::ip_mtu(&s).unwrap() > 0);
}

#[cfg(linux_kernel)]
#[test]
fn test_socketopts_ipv6_mtu() {
crate::init();

let s = rustix::net::socket(AddressFamily::INET, SocketType::DGRAM, None).unwrap();
rustix::net::bind(&s, &"[::1]:0".parse().unwrap()).unwrap();
rustix::net::connect(&s, &"[::1]:0".parse().unwrap()).unwrap();
assert!(rustix::net::sockopt::ipv6_mtu(&s).unwrap() > 0);
}

#[test]
fn test_sockopts_multicast_ifv4() {
crate::init();
Expand Down

0 comments on commit 3f52ea2

Please sign in to comment.