Skip to content
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ readme = "README.md"
repository = "https://github.com/rust-netlink/netlink-sys"
description = "netlink sockets, with optional integration with tokio"

# When running the examples against a local version of `netlink-sys`
# [patch.crates-io]
# netlink-sys = { path = "." }

[dependencies]
bytes = "1.8"
libc = "0.2.164"
Expand Down
2 changes: 1 addition & 1 deletion examples/audit_events_async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const AUDIT_STATUS_PID: u32 = 4;
#[async_std::main]
async fn main() {
let kernel_unicast: SocketAddr = SocketAddr::new(0, 0);
let mut socket = SmolSocket::new(NETLINK_AUDIT).unwrap();
let socket = SmolSocket::new(NETLINK_AUDIT).unwrap();

let mut status = StatusMessage::new();
status.enabled = 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/audit_events_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const AUDIT_STATUS_PID: u32 = 4;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let kernel_unicast: SocketAddr = SocketAddr::new(0, 0);
let mut socket = TokioSocket::new(NETLINK_AUDIT).unwrap();
let socket = TokioSocket::new(NETLINK_AUDIT).unwrap();

let mut status = StatusMessage::new();
status.enabled = 1;
Expand Down
10 changes: 5 additions & 5 deletions src/async_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ pub trait AsyncSocket: Sized + Unpin {

/// Polling wrapper for [`Socket::send`]
fn poll_send(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>>;

/// Polling wrapper for [`Socket::send_to`]
fn poll_send_to(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &[u8],
addr: &SocketAddr,
Expand All @@ -38,7 +38,7 @@ pub trait AsyncSocket: Sized + Unpin {
/// Passes 0 for flags, and ignores the returned length (the buffer will
/// have advanced by the amount read).
fn poll_recv<B>(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<()>>
Expand All @@ -50,7 +50,7 @@ pub trait AsyncSocket: Sized + Unpin {
/// Passes 0 for flags, and ignores the returned length - just returns the
/// address (the buffer will have advanced by the amount read).
fn poll_recv_from<B>(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<SocketAddr>>
Expand All @@ -62,7 +62,7 @@ pub trait AsyncSocket: Sized + Unpin {
/// Passes 0 for flags, and ignores the returned length - just returns the
/// address (the buffer will have advanced by the amount read).
fn poll_recv_from_full(
&mut self,
&self,
cx: &mut Context<'_>,
) -> Poll<io::Result<(Vec<u8>, SocketAddr)>>;
}
33 changes: 15 additions & 18 deletions src/async_socket_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use crate::{AsyncSocket, SocketAddr};
///
/// Provides awaitable variants of the poll functions from [`AsyncSocket`].
pub trait AsyncSocketExt: AsyncSocket {
/// `async fn send(&mut self, buf: &[u8]) -> io::Result<usize>`
fn send<'a, 'b>(&'a mut self, buf: &'b [u8]) -> PollSend<'a, 'b, Self> {
/// `async fn send(&self, buf: &[u8]) -> io::Result<usize>`
fn send<'a, 'b>(&'a self, buf: &'b [u8]) -> PollSend<'a, 'b, Self> {
PollSend { socket: self, buf }
}

/// `async fn send(&mut self, buf: &[u8]) -> io::Result<usize>`
/// `async fn send(&self, buf: &[u8]) -> io::Result<usize>`
fn send_to<'a, 'b>(
&'a mut self,
&'a self,
buf: &'b [u8],
addr: &'b SocketAddr,
) -> PollSendTo<'a, 'b, Self> {
Expand All @@ -31,20 +31,17 @@ pub trait AsyncSocketExt: AsyncSocket {
}
}

/// `async fn recv<B>(&mut self, buf: &mut [u8]) -> io::Result<()>`
fn recv<'a, 'b, B>(
&'a mut self,
buf: &'b mut B,
) -> PollRecv<'a, 'b, Self, B>
/// `async fn recv<B>(&self, buf: &mut [u8]) -> io::Result<()>`
fn recv<'a, 'b, B>(&'a self, buf: &'b mut B) -> PollRecv<'a, 'b, Self, B>
where
B: bytes::BufMut,
{
PollRecv { socket: self, buf }
}

/// `async fn recv<B>(&mut self, buf: &mut [u8]) -> io::Result<SocketAddr>`
/// `async fn recv<B>(&self, buf: &mut [u8]) -> io::Result<SocketAddr>`
fn recv_from<'a, 'b, B>(
&'a mut self,
&'a self,
buf: &'b mut B,
) -> PollRecvFrom<'a, 'b, Self, B>
where
Expand All @@ -53,17 +50,17 @@ pub trait AsyncSocketExt: AsyncSocket {
PollRecvFrom { socket: self, buf }
}

/// `async fn recrecv_from_full(&mut self) -> io::Result<(Vec<u8>,
/// `async fn recrecv_from_full(&self) -> io::Result<(Vec<u8>,
/// SocketAddr)>`
fn recv_from_full(&mut self) -> PollRecvFromFull<'_, Self> {
fn recv_from_full(&self) -> PollRecvFromFull<'_, Self> {
PollRecvFromFull { socket: self }
}
}

impl<S: AsyncSocket> AsyncSocketExt for S {}

pub struct PollSend<'a, 'b, S> {
socket: &'a mut S,
socket: &'a S,
buf: &'b [u8],
}

Expand All @@ -80,7 +77,7 @@ where
}

pub struct PollSendTo<'a, 'b, S> {
socket: &'a mut S,
socket: &'a S,
buf: &'b [u8],
addr: &'b SocketAddr,
}
Expand All @@ -98,7 +95,7 @@ where
}

pub struct PollRecv<'a, 'b, S, B> {
socket: &'a mut S,
socket: &'a S,
buf: &'b mut B,
}

Expand All @@ -116,7 +113,7 @@ where
}

pub struct PollRecvFrom<'a, 'b, S, B> {
socket: &'a mut S,
socket: &'a S,
buf: &'b mut B,
}

Expand All @@ -134,7 +131,7 @@ where
}

pub struct PollRecvFromFull<'a, S> {
socket: &'a mut S,
socket: &'a S,
}

impl<S> Future for PollRecvFromFull<'_, S>
Expand Down
28 changes: 14 additions & 14 deletions src/smol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ impl AsFd for SmolSocket {
// replicate this in these poll functions:
impl SmolSocket {
fn poll_write_with<F, R>(
&mut self,
&self,
cx: &mut Context<'_>,
mut op: F,
) -> Poll<io::Result<R>>
where
F: FnMut(&mut Self) -> io::Result<R>,
F: FnMut(&Self) -> io::Result<R>,
{
loop {
match op(self) {
Expand All @@ -60,12 +60,12 @@ impl SmolSocket {
}

fn poll_read_with<F, R>(
&mut self,
&self,
cx: &mut Context<'_>,
mut op: F,
) -> Poll<io::Result<R>>
where
F: FnMut(&mut Self) -> io::Result<R>,
F: FnMut(&Self) -> io::Result<R>,
{
loop {
match op(self) {
Expand Down Expand Up @@ -94,54 +94,54 @@ impl AsyncSocket for SmolSocket {
}

fn poll_send(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
self.poll_write_with(cx, |this| this.socket_mut().send(buf, 0))
self.poll_write_with(cx, |this| this.socket_ref().send(buf, 0))
}

fn poll_send_to(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &[u8],
addr: &SocketAddr,
) -> Poll<io::Result<usize>> {
self.poll_write_with(cx, |this| this.socket_mut().send_to(buf, addr, 0))
self.poll_write_with(cx, |this| this.socket_ref().send_to(buf, addr, 0))
}

fn poll_recv<B>(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<()>>
where
B: bytes::BufMut,
{
self.poll_read_with(cx, |this| {
this.socket_mut().recv(buf, 0).map(|_len| ())
this.socket_ref().recv(buf, 0).map(|_len| ())
})
}

fn poll_recv_from<B>(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<SocketAddr>>
where
B: bytes::BufMut,
{
self.poll_read_with(cx, |this| {
let x = this.socket_mut().recv_from(buf, 0);
let x = this.socket_ref().recv_from(buf, 0);
trace!("poll_recv_from: {:?}", x);
x.map(|(_len, addr)| addr)
})
}

fn poll_recv_from_full(
&mut self,
&self,
cx: &mut Context<'_>,
) -> Poll<io::Result<(Vec<u8>, SocketAddr)>> {
self.poll_read_with(cx, |this| this.socket_mut().recv_from_full())
self.poll_read_with(cx, |this| this.socket_ref().recv_from_full())
}
}
18 changes: 9 additions & 9 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl Socket {
Ok(res as usize)
}

pub fn set_pktinfo(&mut self, value: bool) -> Result<()> {
pub fn set_pktinfo(&self, value: bool) -> Result<()> {
let value: libc::c_int = value.into();
setsockopt(
self.as_raw_fd(),
Expand All @@ -429,7 +429,7 @@ impl Socket {
Ok(res == 1)
}

pub fn add_membership(&mut self, group: u32) -> Result<()> {
pub fn add_membership(&self, group: u32) -> Result<()> {
setsockopt(
self.as_raw_fd(),
libc::SOL_NETLINK,
Expand All @@ -438,7 +438,7 @@ impl Socket {
)
}

pub fn drop_membership(&mut self, group: u32) -> Result<()> {
pub fn drop_membership(&self, group: u32) -> Result<()> {
setsockopt(
self.as_raw_fd(),
libc::SOL_NETLINK,
Expand All @@ -457,7 +457,7 @@ impl Socket {
/// `NETLINK_BROADCAST_ERROR` (since Linux 2.6.30). When not set,
/// `netlink_broadcast()` only reports `ESRCH` errors and silently
/// ignore `NOBUFS` errors.
pub fn set_broadcast_error(&mut self, value: bool) -> Result<()> {
pub fn set_broadcast_error(&self, value: bool) -> Result<()> {
let value: libc::c_int = value.into();
setsockopt(
self.as_raw_fd(),
Expand All @@ -478,7 +478,7 @@ impl Socket {

/// `NETLINK_NO_ENOBUFS` (since Linux 2.6.30). This flag can be used by
/// unicast and broadcast listeners to avoid receiving `ENOBUFS` errors.
pub fn set_no_enobufs(&mut self, value: bool) -> Result<()> {
pub fn set_no_enobufs(&self, value: bool) -> Result<()> {
let value: libc::c_int = value.into();
setsockopt(
self.as_raw_fd(),
Expand All @@ -502,7 +502,7 @@ impl Socket {
/// have an nsid assigned into the network namespace where the socket
/// has been opened. The nsid is sent to user space via an ancillary
/// data.
pub fn set_listen_all_namespaces(&mut self, value: bool) -> Result<()> {
pub fn set_listen_all_namespaces(&self, value: bool) -> Result<()> {
let value: libc::c_int = value.into();
setsockopt(
self.as_raw_fd(),
Expand All @@ -527,7 +527,7 @@ impl Socket {
/// The netlink message header is still included, so the user can
/// guess from the sequence number which message triggered the
/// acknowledgment.
pub fn set_cap_ack(&mut self, value: bool) -> Result<()> {
pub fn set_cap_ack(&self, value: bool) -> Result<()> {
let value: libc::c_int = value.into();
setsockopt(
self.as_raw_fd(),
Expand All @@ -549,7 +549,7 @@ impl Socket {
/// `NETLINK_EXT_ACK`
/// Extended ACK controls reporting of additional error/warning TLVs in
/// NLMSG_ERROR and NLMSG_DONE messages.
pub fn set_ext_ack(&mut self, value: bool) -> Result<()> {
pub fn set_ext_ack(&self, value: bool) -> Result<()> {
let value: libc::c_int = value.into();
setsockopt(
self.as_raw_fd(),
Expand Down Expand Up @@ -697,7 +697,7 @@ mod test {

#[test]
fn options() {
let mut sock = Socket::new(NETLINK_ROUTE).unwrap();
let sock = Socket::new(NETLINK_ROUTE).unwrap();

sock.set_cap_ack(true).unwrap();
assert!(sock.get_cap_ack().unwrap());
Expand Down
10 changes: 5 additions & 5 deletions src/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl AsyncSocket for TokioSocket {
}

fn poll_send(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Expand All @@ -66,7 +66,7 @@ impl AsyncSocket for TokioSocket {
}

fn poll_send_to(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &[u8],
addr: &SocketAddr,
Expand All @@ -82,7 +82,7 @@ impl AsyncSocket for TokioSocket {
}

fn poll_recv<B>(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<()>>
Expand All @@ -104,7 +104,7 @@ impl AsyncSocket for TokioSocket {
}

fn poll_recv_from<B>(
&mut self,
&self,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<SocketAddr>>
Expand All @@ -130,7 +130,7 @@ impl AsyncSocket for TokioSocket {
}

fn poll_recv_from_full(
&mut self,
&self,
cx: &mut Context<'_>,
) -> Poll<io::Result<(Vec<u8>, SocketAddr)>> {
loop {
Expand Down