Skip to content

Commit

Permalink
Allow the user to specify their own socket
Browse files Browse the repository at this point in the history
This is useful when dealing with multiple network/user namespaces.

Closes #18
  • Loading branch information
MaxHearnden committed Nov 20, 2024
1 parent 67c9ca7 commit 992a5f3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,30 @@ where
}
}

impl<T, S, C> Connection<T, S, C>
where
T: Debug + NetlinkSerializable + NetlinkDeserializable + Unpin,
S: AsyncSocket,
C: NetlinkMessageCodec,
{
pub(crate) fn from_socket(
requests_rx: UnboundedReceiver<Request<T>>,
unsolicited_messages_tx: UnboundedSender<(
NetlinkMessage<T>,
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<T, S, C> Future for Connection<T, S, C>
where
T: Debug + NetlinkSerializable + NetlinkDeserializable + Unpin,
Expand Down
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, S, C>(
socket: S,
) -> (
Connection<T, S, C>,
ConnectionHandle<T>,
UnboundedReceiver<(packet::NetlinkMessage<T>, sys::SocketAddr)>,
)
where
T: Debug
+ packet::NetlinkSerializable
+ packet::NetlinkDeserializable
+ Unpin,
S: sys::AsyncSocket,
C: NetlinkMessageCodec,
{
let (requests_tx, requests_rx) = unbounded::<Request<T>>();
let (messages_tx, messages_rx) =
unbounded::<(packet::NetlinkMessage<T>, sys::SocketAddr)>();
(
Connection::from_socket(requests_rx, messages_tx, socket),
ConnectionHandle::new(requests_tx),
messages_rx,
)
}

0 comments on commit 992a5f3

Please sign in to comment.