Skip to content

Commit

Permalink
datalink(linux): add feature to pass the fd (socket) to ::channel()
Browse files Browse the repository at this point in the history
This can be useful if the caller want to manipulate the
socket differently at creating. For example setting a
BPF filter on the socket before passing it in the channel:

```
datalink::linux::channel(interface, config, socket_fd)
```

Signed-off-by: Martin Andre <martin.andre@tessares.net>
  • Loading branch information
Martichou committed Sep 12, 2022
1 parent 05db0f1 commit 26e965a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ travis = []
serde = ["pnet_base/serde", "pnet_datalink?/serde"]
std = ["pnet_base/std", "pnet_sys", "pnet_datalink", "pnet_transport", "ipnetwork"]
default = ["pnet_base/default", "pnet_datalink/default", "pnet_transport/default", "pnet_packet/default"]
psocket = ["pnet_datalink/psocket"]

[dependencies]
ipnetwork = { version = "0.19.0", optional = true }
Expand Down
1 change: 1 addition & 0 deletions pnet_datalink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ edition = "2021"
netmap = []
std = ["pnet_base/std"]
default = ["std"]
psocket = []

[dependencies]
libc = "0.2.126"
Expand Down
14 changes: 12 additions & 2 deletions pnet_datalink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,18 @@ impl Default for Config {
/// When matching on the returned channel, make sure to include a catch-all so that code doesn't
/// break when new channel types are added.
#[inline]
pub fn channel(network_interface: &NetworkInterface, configuration: Config) -> io::Result<Channel> {
backend::channel(network_interface, (&configuration).into())
pub fn channel(
network_interface: &NetworkInterface,
configuration: Config,
#[cfg(feature = "psocket")]
socket: i32
) -> io::Result<Channel> {
backend::channel(
network_interface,
(&configuration).into(),
#[cfg(feature = "psocket")]
socket
)
}

/// Trait to enable sending `$packet` packets.
Expand Down
28 changes: 17 additions & 11 deletions pnet_datalink/src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,25 @@ impl Default for Config {

/// Create a data link channel using the Linux's `AF_PACKET` socket type.
#[inline]
pub fn channel(network_interface: &NetworkInterface, config: Config) -> io::Result<super::Channel> {
let eth_p_all = 0x0003;
let (typ, proto) = match config.channel_type {
super::ChannelType::Layer2 => (libc::SOCK_RAW, eth_p_all),
super::ChannelType::Layer3(proto) => (libc::SOCK_DGRAM, proto),
pub fn channel(
network_interface: &NetworkInterface,
config: Config,
#[cfg(feature = "psocket")]
socket: i32
) -> io::Result<super::Channel> {
let (_typ, proto) = match config.channel_type {
super::ChannelType::Layer2 => (libc::SOCK_RAW, libc::ETH_P_ALL),
super::ChannelType::Layer3(proto) => (libc::SOCK_DGRAM, proto as i32),
};
let socket = unsafe { libc::socket(libc::AF_PACKET, typ, proto.to_be() as i32) };
if socket == -1 {
return Err(io::Error::last_os_error());
}
let mut addr: libc::sockaddr_storage = unsafe { mem::zeroed() };
let len = network_addr_to_sockaddr(network_interface, &mut addr, proto as i32);

#[cfg(not(feature = "psocket"))]
let socket = match unsafe { libc::socket(libc::AF_PACKET, _typ, proto.to_be()) } {
-1 => return Err(io::Error::last_os_error()),
fd => fd
};

let mut addr: libc::sockaddr_storage = unsafe { mem::zeroed() };
let len = network_addr_to_sockaddr(network_interface, &mut addr, proto);
let send_addr = (&addr as *const libc::sockaddr_storage) as *const libc::sockaddr;

// Bind to interface
Expand Down

0 comments on commit 26e965a

Please sign in to comment.