From 5e1318cb140ff4f3e4e455d53c4f790cf5915713 Mon Sep 17 00:00:00 2001 From: MaxHearnden Date: Sun, 17 Nov 2024 13:52:35 +0000 Subject: [PATCH] Allow the user to specify their own socket This is useful when dealing with multiple network/user namespaces. Closes https://github.com/rust-netlink/netlink-proto/issues/18 --- src/connection.rs | 24 ++++++++++++++++++++++++ src/lib.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/connection.rs b/src/connection.rs index cb832d1..692f632 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -294,6 +294,30 @@ where } } +impl Connection +where + T: Debug + NetlinkSerializable + NetlinkDeserializable + Unpin, + S: AsyncSocket, + C: NetlinkMessageCodec, +{ + pub(crate) fn from_socket( + requests_rx: UnboundedReceiver>, + unsolicited_messages_tx: UnboundedSender<( + NetlinkMessage, + SocketAddr, + )>, + socket: S, + ) -> Self { + Connection { + socket: NetlinkFramed::new(socket), + protocol: Protocol::new(), + requests_rx: Some(requests_rx), + unsolicited_messages_tx: Some(unsolicited_messages_tx), + socket_closed: false, + } + } +} + impl Future for Connection where T: Debug + NetlinkSerializable + NetlinkDeserializable + Unpin, diff --git a/src/lib.rs b/src/lib.rs index 7222fc6..3eeab96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -284,3 +284,31 @@ where messages_rx, )) } + +/// Variant of [`new_connection`] that allows specifying a socket type to use +/// for async handling, a special codec and a socket +#[allow(clippy::type_complexity)] +pub fn from_socket_with_codec( + socket: S, +) -> ( + Connection, + ConnectionHandle, + UnboundedReceiver<(packet::NetlinkMessage, sys::SocketAddr)>, +) +where + T: Debug + + packet::NetlinkSerializable + + packet::NetlinkDeserializable + + Unpin, + S: sys::AsyncSocket, + C: NetlinkMessageCodec, +{ + let (requests_tx, requests_rx) = unbounded::>(); + let (messages_tx, messages_rx) = + unbounded::<(packet::NetlinkMessage, sys::SocketAddr)>(); + ( + Connection::from_socket(requests_rx, messages_tx, socket), + ConnectionHandle::new(requests_tx), + messages_rx, + ) +}