forked from smoltcp-rs/smoltcp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathraw_socket.rs
More file actions
139 lines (123 loc) · 3.85 KB
/
Copy pathraw_socket.rs
File metadata and controls
139 lines (123 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::cell::RefCell;
use std::io;
use std::os::unix::io::{AsRawFd, RawFd};
use std::rc::Rc;
use std::vec::Vec;
use crate::phy::{self, sys, Device, DeviceCapabilities, Medium};
use crate::time::Instant;
/// A socket that captures or transmits the complete frame.
#[derive(Debug)]
pub struct RawSocket {
medium: Medium,
lower: Rc<RefCell<sys::RawSocketDesc>>,
mtu: usize,
}
impl AsRawFd for RawSocket {
fn as_raw_fd(&self) -> RawFd {
self.lower.borrow().as_raw_fd()
}
}
impl RawSocket {
/// Creates a raw socket, bound to the interface called `name`.
///
/// This requires superuser privileges or a corresponding capability bit
/// set on the executable.
pub fn new(name: &str, medium: Medium) -> io::Result<RawSocket> {
let mut lower = sys::RawSocketDesc::new(name, medium)?;
lower.bind_interface()?;
let mut mtu = lower.interface_mtu()?;
#[cfg(feature = "medium-ieee802154")]
if medium == Medium::Ieee802154 {
// SIOCGIFMTU returns 127 - (ACK_PSDU - FCS - 1) - FCS.
// 127 - (5 - 2 - 1) - 2 = 123
// For IEEE802154, we want to add (ACK_PSDU - FCS - 1), since that is what SIOCGIFMTU
// uses as the size of the link layer header.
//
// https://github.com/torvalds/linux/blob/7475e51b87969e01a6812eac713a1c8310372e8a/net/mac802154/iface.c#L541
mtu += 2;
}
#[cfg(feature = "medium-ethernet")]
if medium == Medium::Ethernet {
// 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.
mtu += crate::wire::EthernetFrame::<&[u8]>::header_len()
}
Ok(RawSocket {
medium,
lower: Rc::new(RefCell::new(lower)),
mtu,
})
}
}
impl Device for RawSocket {
type RxToken<'a>
= RxToken
where
Self: 'a;
type TxToken<'a>
= TxToken
where
Self: 'a;
fn capabilities(&self) -> DeviceCapabilities {
DeviceCapabilities {
max_transmission_unit: self.mtu,
medium: self.medium,
..DeviceCapabilities::default()
}
}
fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
let mut lower = self.lower.borrow_mut();
let mut buffer = vec![0; self.mtu];
match lower.recv(&mut buffer[..]) {
Ok(size) => {
buffer.resize(size, 0);
let rx = RxToken { buffer };
let tx = TxToken {
lower: self.lower.clone(),
};
Some((rx, tx))
}
Err(err) if err.kind() == io::ErrorKind::WouldBlock => None,
Err(err) => panic!("{}", err),
}
}
fn transmit(&mut self, _timestamp: Instant) -> Option<Self::TxToken<'_>> {
Some(TxToken {
lower: self.lower.clone(),
})
}
}
#[doc(hidden)]
pub struct RxToken {
buffer: Vec<u8>,
}
impl phy::RxToken for RxToken {
fn consume<R, F>(self, f: F) -> R
where
F: FnOnce(&[u8]) -> R,
{
f(&self.buffer[..])
}
}
#[doc(hidden)]
pub struct TxToken {
lower: Rc<RefCell<sys::RawSocketDesc>>,
}
impl phy::TxToken for TxToken {
fn consume<R, F>(self, len: usize, f: F) -> R
where
F: FnOnce(&mut [u8]) -> R,
{
let mut lower = self.lower.borrow_mut();
let mut buffer = vec![0; len];
let result = f(&mut buffer);
match lower.send(&buffer[..]) {
Ok(_) => {}
Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
net_debug!("phy: tx failed due to WouldBlock")
}
Err(err) => panic!("{}", err),
}
result
}
}