Skip to content

Commit e9f07ee

Browse files
add IpMtu sockopt
1 parent a75a349 commit e9f07ee

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
3838
([#1857](https://github.com/nix-rust/nix/pull/1857))
3939
- Added `SockProtocol::Raw` for raw sockets
4040
([#1848](https://github.com/nix-rust/nix/pull/1848))
41+
- added `IP_MTU` (`IpMtu`) `IPPROTO_IP` sockopt on Linux and Android.
42+
([#1865](https://github.com/nix-rust/nix/pull/1865))
4143

4244
### Changed
4345

src/sys/socket/sockopt.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,15 @@ sockopt_impl!(
925925
libc::IPV6_RECVERR,
926926
bool
927927
);
928+
#[cfg(any(target_os = "android", target_os = "linux"))]
929+
sockopt_impl!(
930+
/// Fetch the current system-estimated Path MTU.
931+
IpMtu,
932+
GetOnly,
933+
libc::IPPROTO_IP,
934+
libc::IP_MTU,
935+
libc::c_int
936+
);
928937
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
929938
sockopt_impl!(
930939
/// Set or retrieve the current time-to-live field that is used in every

test/sys/test_sockopt.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,33 @@ fn test_so_tcp_keepalive() {
236236
}
237237
}
238238

239+
#[test]
240+
#[cfg(any(target_os = "android", target_os = "linux"))]
241+
#[cfg_attr(qemu, ignore)]
242+
fn test_get_mtu() {
243+
use nix::sys::socket::{bind, connect, SockaddrIn};
244+
use std::net::SocketAddrV4;
245+
use std::str::FromStr;
246+
247+
let std_sa = SocketAddrV4::from_str("127.0.0.1:4001").unwrap();
248+
let std_sb = SocketAddrV4::from_str("127.0.0.1:4002").unwrap();
249+
250+
let usock = socket(
251+
AddressFamily::Inet,
252+
SockType::Datagram,
253+
SockFlag::empty(),
254+
SockProtocol::Udp,
255+
)
256+
.unwrap();
257+
258+
// Bind and initiate connection
259+
bind(usock, &SockaddrIn::from(std_sa)).unwrap();
260+
connect(usock, &SockaddrIn::from(std_sb)).unwrap();
261+
262+
// Loopback connections have 2^16 - the maximum - MTU
263+
assert_eq!(getsockopt(usock, sockopt::IpMtu), Ok(u16::MAX as i32))
264+
}
265+
239266
#[test]
240267
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
241268
fn test_ttl_opts() {

0 commit comments

Comments
 (0)