Skip to content

Commit 3c6fc4e

Browse files
MaxHearndencathay4t
authored andcommitted
Allow the user to specify their own socket
This is useful when dealing with multiple network/user namespaces. Closes #18
1 parent ce207e4 commit 3c6fc4e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/connection.rs

+24
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,30 @@ where
294294
}
295295
}
296296

297+
impl<T, S, C> Connection<T, S, C>
298+
where
299+
T: Debug + NetlinkSerializable + NetlinkDeserializable + Unpin,
300+
S: AsyncSocket,
301+
C: NetlinkMessageCodec,
302+
{
303+
pub(crate) fn from_socket(
304+
requests_rx: UnboundedReceiver<Request<T>>,
305+
unsolicited_messages_tx: UnboundedSender<(
306+
NetlinkMessage<T>,
307+
SocketAddr,
308+
)>,
309+
socket: S,
310+
) -> Self {
311+
Connection {
312+
socket: NetlinkFramed::new(socket),
313+
protocol: Protocol::new(),
314+
requests_rx: Some(requests_rx),
315+
unsolicited_messages_tx: Some(unsolicited_messages_tx),
316+
socket_closed: false,
317+
}
318+
}
319+
}
320+
297321
impl<T, S, C> Future for Connection<T, S, C>
298322
where
299323
T: Debug + NetlinkSerializable + NetlinkDeserializable + Unpin,

src/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,31 @@ where
284284
messages_rx,
285285
))
286286
}
287+
288+
/// Variant of [`new_connection`] that allows specifying a socket type to use
289+
/// for async handling, a special codec and a socket
290+
#[allow(clippy::type_complexity)]
291+
pub fn from_socket_with_codec<T, S, C>(
292+
socket: S,
293+
) -> (
294+
Connection<T, S, C>,
295+
ConnectionHandle<T>,
296+
UnboundedReceiver<(packet::NetlinkMessage<T>, sys::SocketAddr)>,
297+
)
298+
where
299+
T: Debug
300+
+ packet::NetlinkSerializable
301+
+ packet::NetlinkDeserializable
302+
+ Unpin,
303+
S: sys::AsyncSocket,
304+
C: NetlinkMessageCodec,
305+
{
306+
let (requests_tx, requests_rx) = unbounded::<Request<T>>();
307+
let (messages_tx, messages_rx) =
308+
unbounded::<(packet::NetlinkMessage<T>, sys::SocketAddr)>();
309+
(
310+
Connection::from_socket(requests_rx, messages_tx, socket),
311+
ConnectionHandle::new(requests_tx),
312+
messages_rx,
313+
)
314+
}

0 commit comments

Comments
 (0)