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.

Requires rust-netlink/netlink-proto#25
  • Loading branch information
MaxHearnden committed Nov 20, 2024
1 parent 5fca904 commit a6703f1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ ipnetwork = "0.18.0"
tokio = { version = "1.0.1", features = ["macros", "rt", "rt-multi-thread"] }
async-std = { version = "1.9.0", features = ["attributes"]}
macaddr = "1.0"

[patch.crates-io]
netlink-proto = {path = "../netlink-proto"}
16 changes: 16 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,19 @@ where
netlink_proto::new_connection_with_socket(NETLINK_ROUTE)?;
Ok((conn, Handle::new(handle), messages))
}

#[allow(clippy::type_complexity)]
pub fn from_socket<S>(
socket: S,
) -> (
Connection<RouteNetlinkMessage, S>,
Handle,
UnboundedReceiver<(NetlinkMessage<RouteNetlinkMessage>, SocketAddr)>,
)
where
S: AsyncSocket,
{
let (conn, handle, messages) =
netlink_proto::from_socket_with_codec(socket);
(conn, Handle::new(handle), messages)
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod traffic_control;
pub use crate::addr::{
AddressAddRequest, AddressDelRequest, AddressGetRequest, AddressHandle,
};
pub use crate::connection::from_socket;
#[cfg(feature = "tokio_socket")]
pub use crate::connection::new_connection;
pub use crate::connection::new_connection_with_socket;
Expand Down

0 comments on commit a6703f1

Please sign in to comment.