Skip to content

Added SO_PRIORITY socket option (#587) #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,41 @@ impl Socket {
}
}

/// Get value for the `SO_PRIORITY` option on this socket.
///
/// For more information about this option, see [`set_priority`].
///
/// [`set_priority`]: Socket::set_priority
#[cfg(all(
feature = "all",
any(target_os = "linux", target_os = "android", target_os = "fuchsia")
))]
pub fn priority(&self) -> io::Result<u32> {
unsafe {
getsockopt::<c_int>(self.as_raw(), sys::SOL_SOCKET, sys::SO_PRIORITY)
.map(|prio| prio as u32)
}
}

/// Set value for the `SO_PRIORITY` option on this socket.
///
/// Packets with a higher priority may be processed earlier depending on the selected device
/// queueing discipline.
#[cfg(all(
feature = "all",
any(target_os = "linux", target_os = "android", target_os = "fuchsia")
))]
pub fn set_priority(&self, priority: u32) -> io::Result<()> {
unsafe {
setsockopt(
self.as_raw(),
sys::SOL_SOCKET,
sys::SO_PRIORITY,
priority as c_int,
)
}
}

/// Get value for the `SO_RCVBUF` option on this socket.
///
/// For more information about this option, see [`set_recv_buffer_size`].
Expand Down
5 changes: 5 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ pub(crate) use libc::SO_LINGER;
pub(crate) use libc::SO_LINGER_SEC as SO_LINGER;
#[cfg(any(target_os = "linux", target_os = "cygwin"))]
pub(crate) use libc::SO_PASSCRED;
#[cfg(all(
feature = "all",
any(target_os = "linux", target_os = "android", target_os = "fuchsia")
))]
pub(crate) use libc::SO_PRIORITY;
pub(crate) use libc::{
ip_mreq as IpMreq, linger, IPPROTO_IP, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, IPV6_MULTICAST_IF,
IPV6_MULTICAST_LOOP, IPV6_UNICAST_HOPS, IPV6_V6ONLY, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP,
Expand Down
13 changes: 13 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1811,3 +1811,16 @@ fn set_passcred() {
socket.set_passcred(true).unwrap();
assert!(socket.passcred().unwrap());
}

#[cfg(all(feature = "all", target_os = "linux"))]
#[test]
fn set_priority() {
let socket = Socket::new(Domain::UNIX, Type::DGRAM, None).unwrap();
assert!(socket.priority().unwrap() == 0);

// test priorities 6 .. 0; values above 6 require additional priviledges
for i in (0..=6).rev() {
socket.set_priority(i).unwrap();
assert!(socket.priority().unwrap() == i);
}
}