renet/src/phy/sys/raw_socket.rs

123 lines
3.3 KiB
Rust
Raw Normal View History

2016-12-11 07:15:26 +08:00
use super::*;
use crate::wire::EthernetFrame;
2021-06-27 15:31:59 +08:00
use std::os::unix::io::{AsRawFd, RawFd};
use std::{io, mem};
2016-12-11 07:15:26 +08:00
#[derive(Debug)]
pub struct RawSocketDesc {
lower: libc::c_int,
2021-06-27 15:31:59 +08:00
ifreq: ifreq,
2016-12-11 07:15:26 +08:00
}
impl AsRawFd for RawSocketDesc {
fn as_raw_fd(&self) -> RawFd {
self.lower
}
}
2016-12-11 07:15:26 +08:00
impl RawSocketDesc {
pub fn new(name: &str) -> io::Result<RawSocketDesc> {
let lower = unsafe {
2021-04-29 18:04:46 +08:00
// TODO(thvdveld)
//#[cfg(feature = "medium-ieee802154")]
//let protocol = imp::ETH_P_IEEE802154;
#[cfg(feature = "medium-ethernet")]
let protocol = imp::ETH_P_ALL;
2021-06-27 15:31:59 +08:00
let lower = libc::socket(
libc::AF_PACKET,
libc::SOCK_RAW | libc::SOCK_NONBLOCK,
2021-04-29 18:04:46 +08:00
protocol.to_be() as i32,
2021-06-27 15:31:59 +08:00
);
if lower == -1 {
return Err(io::Error::last_os_error());
}
2016-12-11 07:15:26 +08:00
lower
};
Ok(RawSocketDesc {
lower: lower,
2021-06-27 15:31:59 +08:00
ifreq: ifreq_for(name),
2016-12-11 07:15:26 +08:00
})
}
pub fn interface_mtu(&mut self) -> io::Result<usize> {
// SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
// smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
2021-06-27 15:31:59 +08:00
let ip_mtu =
ifreq_ioctl(self.lower, &mut self.ifreq, imp::SIOCGIFMTU).map(|mtu| mtu as usize)?;
Ok(ip_mtu + EthernetFrame::<&[u8]>::header_len())
2016-12-11 07:15:26 +08:00
}
pub fn bind_interface(&mut self) -> io::Result<()> {
2021-04-29 18:04:46 +08:00
// TODO(thvdveld)
//#[cfg(feature = "medium-ieee802154")]
//let protocol = imp::ETH_P_IEEE802154;
#[cfg(feature = "medium-ethernet")]
let protocol = imp::ETH_P_ALL;
2016-12-11 07:15:26 +08:00
let sockaddr = libc::sockaddr_ll {
2021-06-27 15:31:59 +08:00
sll_family: libc::AF_PACKET as u16,
2021-04-29 18:04:46 +08:00
sll_protocol: protocol.to_be() as u16,
2021-06-27 15:31:59 +08:00
sll_ifindex: ifreq_ioctl(self.lower, &mut self.ifreq, imp::SIOCGIFINDEX)?,
sll_hatype: 1,
sll_pkttype: 0,
sll_halen: 6,
sll_addr: [0; 8],
2016-12-11 07:15:26 +08:00
};
unsafe {
2021-06-27 15:31:59 +08:00
let res = libc::bind(
self.lower,
&sockaddr as *const libc::sockaddr_ll as *const libc::sockaddr,
mem::size_of::<libc::sockaddr_ll>() as u32,
);
if res == -1 {
return Err(io::Error::last_os_error());
}
2016-12-11 07:15:26 +08:00
}
Ok(())
}
pub fn recv(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
unsafe {
2021-06-27 15:31:59 +08:00
let len = libc::recv(
self.lower,
buffer.as_mut_ptr() as *mut libc::c_void,
buffer.len(),
0,
);
if len == -1 {
return Err(io::Error::last_os_error());
}
2016-12-11 07:15:26 +08:00
Ok(len as usize)
}
}
pub fn send(&mut self, buffer: &[u8]) -> io::Result<usize> {
unsafe {
2021-06-27 15:31:59 +08:00
let len = libc::send(
self.lower,
buffer.as_ptr() as *const libc::c_void,
buffer.len(),
0,
);
if len == -1 {
Err(io::Error::last_os_error()).unwrap()
}
2016-12-11 07:15:26 +08:00
Ok(len as usize)
}
}
}
impl Drop for RawSocketDesc {
fn drop(&mut self) {
2021-06-27 15:31:59 +08:00
unsafe {
libc::close(self.lower);
}
2016-12-11 07:15:26 +08:00
}
}