Skip to content

Commit 0c9efe1

Browse files
boris-nThomasdezeeuw
authored andcommitted
Added SO_PRIORITY socket option (#587)
1 parent 758a9e2 commit 0c9efe1

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/socket.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,41 @@ impl Socket {
996996
}
997997
}
998998

999+
/// Get value for the `SO_PRIORITY` option on this socket.
1000+
///
1001+
/// For more information about this option, see [`set_priority`].
1002+
///
1003+
/// [`set_priority`]: Socket::set_priority
1004+
#[cfg(all(
1005+
feature = "all",
1006+
any(target_os = "linux", target_os = "android", target_os = "fuchsia")
1007+
))]
1008+
pub fn priority(&self) -> io::Result<u32> {
1009+
unsafe {
1010+
getsockopt::<c_int>(self.as_raw(), sys::SOL_SOCKET, sys::SO_PRIORITY)
1011+
.map(|prio| prio as u32)
1012+
}
1013+
}
1014+
1015+
/// Set value for the `SO_PRIORITY` option on this socket.
1016+
///
1017+
/// Packets with a higher priority may be processed earlier depending on the selected device
1018+
/// queueing discipline.
1019+
#[cfg(all(
1020+
feature = "all",
1021+
any(target_os = "linux", target_os = "android", target_os = "fuchsia")
1022+
))]
1023+
pub fn set_priority(&self, priority: u32) -> io::Result<()> {
1024+
unsafe {
1025+
setsockopt(
1026+
self.as_raw(),
1027+
sys::SOL_SOCKET,
1028+
sys::SO_PRIORITY,
1029+
priority as c_int,
1030+
)
1031+
}
1032+
}
1033+
9991034
/// Get value for the `SO_RCVBUF` option on this socket.
10001035
///
10011036
/// For more information about this option, see [`set_recv_buffer_size`].

src/sys/unix.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ pub(crate) use libc::SO_LINGER;
206206
pub(crate) use libc::SO_LINGER_SEC as SO_LINGER;
207207
#[cfg(any(target_os = "linux", target_os = "cygwin"))]
208208
pub(crate) use libc::SO_PASSCRED;
209+
#[cfg(all(
210+
feature = "all",
211+
any(target_os = "linux", target_os = "android", target_os = "fuchsia")
212+
))]
213+
pub(crate) use libc::SO_PRIORITY;
209214
pub(crate) use libc::{
210215
ip_mreq as IpMreq, linger, IPPROTO_IP, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, IPV6_MULTICAST_IF,
211216
IPV6_MULTICAST_LOOP, IPV6_UNICAST_HOPS, IPV6_V6ONLY, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP,

tests/socket.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,3 +1811,16 @@ fn set_passcred() {
18111811
socket.set_passcred(true).unwrap();
18121812
assert!(socket.passcred().unwrap());
18131813
}
1814+
1815+
#[cfg(all(feature = "all", target_os = "linux"))]
1816+
#[test]
1817+
fn set_priority() {
1818+
let socket = Socket::new(Domain::UNIX, Type::DGRAM, None).unwrap();
1819+
assert!(socket.priority().unwrap() == 0);
1820+
1821+
// test priorities 6 .. 0; values above 6 require additional priviledges
1822+
for i in (0..=6).rev() {
1823+
socket.set_priority(i).unwrap();
1824+
assert!(socket.priority().unwrap() == i);
1825+
}
1826+
}

0 commit comments

Comments
 (0)