|
@@ -1,10 +1,12 @@
|
|
|
use super::*;
|
|
|
+use crate::phy::Medium;
|
|
|
use crate::wire::EthernetFrame;
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
|
|
use std::{io, mem};
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
pub struct RawSocketDesc {
|
|
|
+ protocol: libc::c_short,
|
|
|
lower: libc::c_int,
|
|
|
ifreq: ifreq,
|
|
|
}
|
|
@@ -16,15 +18,17 @@ impl AsRawFd for RawSocketDesc {
|
|
|
}
|
|
|
|
|
|
impl RawSocketDesc {
|
|
|
- pub fn new(name: &str) -> io::Result<RawSocketDesc> {
|
|
|
- let lower = unsafe {
|
|
|
- // TODO(thvdveld)
|
|
|
- //#[cfg(feature = "medium-ieee802154")]
|
|
|
- //let protocol = imp::ETH_P_IEEE802154;
|
|
|
-
|
|
|
+ pub fn new(name: &str, medium: Medium) -> io::Result<RawSocketDesc> {
|
|
|
+ let protocol = match medium {
|
|
|
#[cfg(feature = "medium-ethernet")]
|
|
|
- let protocol = imp::ETH_P_ALL;
|
|
|
+ Medium::Ethernet => imp::ETH_P_ALL,
|
|
|
+ #[cfg(feature = "medium-ip")]
|
|
|
+ Medium::Ip => imp::ETH_P_ALL,
|
|
|
+ #[cfg(feature = "medium-ieee802154")]
|
|
|
+ Medium::Ieee802154 => imp::ETH_P_IEEE802154,
|
|
|
+ };
|
|
|
|
|
|
+ let lower = unsafe {
|
|
|
let lower = libc::socket(
|
|
|
libc::AF_PACKET,
|
|
|
libc::SOCK_RAW | libc::SOCK_NONBLOCK,
|
|
@@ -37,7 +41,8 @@ impl RawSocketDesc {
|
|
|
};
|
|
|
|
|
|
Ok(RawSocketDesc {
|
|
|
- lower: lower,
|
|
|
+ protocol,
|
|
|
+ lower,
|
|
|
ifreq: ifreq_for(name),
|
|
|
})
|
|
|
}
|
|
@@ -51,16 +56,9 @@ impl RawSocketDesc {
|
|
|
}
|
|
|
|
|
|
pub fn bind_interface(&mut self) -> io::Result<()> {
|
|
|
- // TODO(thvdveld)
|
|
|
- //#[cfg(feature = "medium-ieee802154")]
|
|
|
- //let protocol = imp::ETH_P_IEEE802154;
|
|
|
-
|
|
|
- #[cfg(feature = "medium-ethernet")]
|
|
|
- let protocol = imp::ETH_P_ALL;
|
|
|
-
|
|
|
let sockaddr = libc::sockaddr_ll {
|
|
|
sll_family: libc::AF_PACKET as u16,
|
|
|
- sll_protocol: protocol.to_be() as u16,
|
|
|
+ sll_protocol: self.protocol.to_be() as u16,
|
|
|
sll_ifindex: ifreq_ioctl(self.lower, &mut self.ifreq, imp::SIOCGIFINDEX)?,
|
|
|
sll_hatype: 1,
|
|
|
sll_pkttype: 0,
|