Skip to content

Commit 040769b

Browse files
authored
Add Socket::(set_)recv_tos
Gets or set the IP_RECVTOS option.
1 parent 5436228 commit 040769b

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/socket.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,52 @@ impl Socket {
13881388
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, sys::IP_TOS).map(|tos| tos as u32)
13891389
}
13901390
}
1391+
1392+
/// Set the value of the `IP_RECVTOS` option for this socket.
1393+
///
1394+
/// If enabled, the IP_TOS ancillary message is passed with
1395+
/// incoming packets. It contains a byte which specifies the
1396+
/// Type of Service/Precedence field of the packet header.
1397+
#[cfg(not(any(
1398+
target_os = "fuschia",
1399+
target_os = "illumos",
1400+
target_os = "netbsd",
1401+
target_os = "redox",
1402+
target_os = "solaris",
1403+
target_os = "windows",
1404+
)))]
1405+
pub fn set_recv_tos(&self, recv_tos: bool) -> io::Result<()> {
1406+
let recv_tos = if recv_tos { 1 } else { 0 };
1407+
1408+
unsafe {
1409+
setsockopt(
1410+
self.as_raw(),
1411+
sys::IPPROTO_IP,
1412+
sys::IP_RECVTOS,
1413+
recv_tos as c_int,
1414+
)
1415+
}
1416+
}
1417+
1418+
/// Get the value of the `IP_RECVTOS` option for this socket.
1419+
///
1420+
/// For more information about this option, see [`set_recv_tos`].
1421+
///
1422+
/// [`set_recv_tos`]: Socket::set_recv_tos
1423+
#[cfg(not(any(
1424+
target_os = "fuschia",
1425+
target_os = "illumos",
1426+
target_os = "netbsd",
1427+
target_os = "redox",
1428+
target_os = "solaris",
1429+
target_os = "windows",
1430+
)))]
1431+
pub fn recv_tos(&self) -> io::Result<bool> {
1432+
unsafe {
1433+
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, sys::IP_RECVTOS)
1434+
.map(|recv_tos| recv_tos > 0)
1435+
}
1436+
}
13911437
}
13921438

13931439
/// Socket options for IPv6 sockets, get/set using `IPPROTO_IPV6`.

src/sys/unix.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ pub(crate) use libc::{MSG_TRUNC, SO_OOBINLINE};
7777
// Used in `Socket`.
7878
#[cfg(all(feature = "all", not(target_os = "redox")))]
7979
pub(crate) use libc::IP_HDRINCL;
80+
#[cfg(not(any(
81+
target_os = "fuschia",
82+
target_os = "illumos",
83+
target_os = "netbsd",
84+
target_os = "redox",
85+
target_os = "solaris",
86+
)))]
87+
pub(crate) use libc::IP_RECVTOS;
8088
#[cfg(not(any(
8189
target_os = "fuschia",
8290
target_os = "redox",

tests/socket.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,17 @@ test!(IPv4 ttl, set_ttl(40));
11721172
target_os = "illumos",
11731173
)))]
11741174
test!(IPv4 tos, set_tos(96));
1175+
1176+
#[cfg(not(any(
1177+
target_os = "fuschia",
1178+
target_os = "illumos",
1179+
target_os = "netbsd",
1180+
target_os = "redox",
1181+
target_os = "solaris",
1182+
target_os = "windows",
1183+
)))]
1184+
test!(IPv4 recv_tos, set_recv_tos(true));
1185+
11751186
#[cfg(not(windows))] // TODO: returns `WSAENOPROTOOPT` (10042) on Windows.
11761187
test!(IPv4 broadcast, set_broadcast(true));
11771188

0 commit comments

Comments
 (0)