Skip to content

Commit 1ce595f

Browse files
committed
Add IP_TTL/IPV6_UNICAST_HOPS SockOpts
Test: `cargo test --test test test_ttl_opts`
1 parent ccc1434 commit 1ce595f

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
4040
(#[1514](https://github.com/nix-rust/nix/pull/1514))
4141
- Added `AsRawFd` implementation on `PollFd`.
4242
(#[1516](https://github.com/nix-rust/nix/pull/1516))
43+
- Added `Ipv4Ttl` and `Ipv6Ttl` sockopts.
44+
(#[1515](https://github.com/nix-rust/nix/pull/1515))
4345

4446
### Changed
4547

src/sys/socket/sockopt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ sockopt_impl!(Both, Ipv6V6Only, libc::IPPROTO_IPV6, libc::IPV6_V6ONLY, bool);
356356
sockopt_impl!(Both, Ipv4RecvErr, libc::IPPROTO_IP, libc::IP_RECVERR, bool);
357357
#[cfg(any(target_os = "android", target_os = "linux"))]
358358
sockopt_impl!(Both, Ipv6RecvErr, libc::IPPROTO_IPV6, libc::IPV6_RECVERR, bool);
359+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
360+
sockopt_impl!(Both, Ipv4Ttl, libc::IPPROTO_IP, libc::IP_TTL, libc::c_int);
361+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
362+
sockopt_impl!(Both, Ipv6Ttl, libc::IPPROTO_IPV6, libc::IPV6_UNICAST_HOPS, libc::c_int);
359363

360364
#[cfg(any(target_os = "android", target_os = "linux"))]
361365
#[derive(Copy, Clone, Debug)]

test/sys/test_sockopt.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,14 @@ fn test_so_tcp_keepalive() {
186186
assert_eq!(getsockopt(fd, sockopt::TcpKeepInterval).unwrap(), x + 1);
187187
}
188188
}
189+
190+
#[test]
191+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
192+
fn test_ttl_opts() {
193+
let fd4 = socket(AddressFamily::Inet, SockType::Datagram, SockFlag::empty(), None).unwrap();
194+
setsockopt(fd4, sockopt::Ipv4Ttl, &1)
195+
.expect("setting ipv4ttl on an inet socket should succeed");
196+
let fd6 = socket(AddressFamily::Inet6, SockType::Datagram, SockFlag::empty(), None).unwrap();
197+
setsockopt(fd6, sockopt::Ipv6Ttl, &1)
198+
.expect("setting ipv6ttl on an inet6 socket should succeed");
199+
}

0 commit comments

Comments
 (0)