aboutsummaryrefslogtreecommitdiff
path: root/examples/std/src/tuntap.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/std/src/tuntap.rs')
-rw-r--r--examples/std/src/tuntap.rs224
1 files changed, 0 insertions, 224 deletions
diff --git a/examples/std/src/tuntap.rs b/examples/std/src/tuntap.rs
deleted file mode 100644
index 167c3da5f..000000000
--- a/examples/std/src/tuntap.rs
+++ /dev/null
@@ -1,224 +0,0 @@
1use std::io;
2use std::io::{Read, Write};
3use std::os::unix::io::{AsRawFd, RawFd};
4use std::task::Context;
5
6use async_io::Async;
7use embassy_net_driver::{self, Capabilities, Driver, HardwareAddress, LinkState};
8use log::*;
9
10pub const SIOCGIFMTU: libc::c_ulong = 0x8921;
11pub const _SIOCGIFINDEX: libc::c_ulong = 0x8933;
12pub const _ETH_P_ALL: libc::c_short = 0x0003;
13pub const TUNSETIFF: libc::c_ulong = 0x400454CA;
14pub const _IFF_TUN: libc::c_int = 0x0001;
15pub const IFF_TAP: libc::c_int = 0x0002;
16pub const IFF_NO_PI: libc::c_int = 0x1000;
17
18const ETHERNET_HEADER_LEN: usize = 14;
19
20#[repr(C)]
21#[derive(Debug)]
22struct ifreq {
23 ifr_name: [libc::c_char; libc::IF_NAMESIZE],
24 ifr_data: libc::c_int, /* ifr_ifindex or ifr_mtu */
25}
26
27fn ifreq_for(name: &str) -> ifreq {
28 let mut ifreq = ifreq {
29 ifr_name: [0; libc::IF_NAMESIZE],
30 ifr_data: 0,
31 };
32 for (i, byte) in name.as_bytes().iter().enumerate() {
33 ifreq.ifr_name[i] = *byte as libc::c_char
34 }
35 ifreq
36}
37
38fn ifreq_ioctl(lower: libc::c_int, ifreq: &mut ifreq, cmd: libc::c_ulong) -> io::Result<libc::c_int> {
39 unsafe {
40 let res = libc::ioctl(lower, cmd as _, ifreq as *mut ifreq);
41 if res == -1 {
42 return Err(io::Error::last_os_error());
43 }
44 }
45
46 Ok(ifreq.ifr_data)
47}
48
49#[derive(Debug)]
50pub struct TunTap {
51 fd: libc::c_int,
52 mtu: usize,
53}
54
55impl AsRawFd for TunTap {
56 fn as_raw_fd(&self) -> RawFd {
57 self.fd
58 }
59}
60
61impl TunTap {
62 pub fn new(name: &str) -> io::Result<TunTap> {
63 unsafe {
64 let fd = libc::open(
65 "/dev/net/tun\0".as_ptr() as *const libc::c_char,
66 libc::O_RDWR | libc::O_NONBLOCK,
67 );
68 if fd == -1 {
69 return Err(io::Error::last_os_error());
70 }
71
72 let mut ifreq = ifreq_for(name);
73 ifreq.ifr_data = IFF_TAP | IFF_NO_PI;
74 ifreq_ioctl(fd, &mut ifreq, TUNSETIFF)?;
75
76 let socket = libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_IP);
77 if socket == -1 {
78 return Err(io::Error::last_os_error());
79 }
80
81 let ip_mtu = ifreq_ioctl(socket, &mut ifreq, SIOCGIFMTU);
82 libc::close(socket);
83 let ip_mtu = ip_mtu? as usize;
84
85 // SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
86 // smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
87 let mtu = ip_mtu + ETHERNET_HEADER_LEN;
88
89 Ok(TunTap { fd, mtu })
90 }
91 }
92}
93
94impl Drop for TunTap {
95 fn drop(&mut self) {
96 unsafe {
97 libc::close(self.fd);
98 }
99 }
100}
101
102impl io::Read for TunTap {
103 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
104 let len = unsafe { libc::read(self.fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len()) };
105 if len == -1 {
106 Err(io::Error::last_os_error())
107 } else {
108 Ok(len as usize)
109 }
110 }
111}
112
113impl io::Write for TunTap {
114 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
115 let len = unsafe { libc::write(self.fd, buf.as_ptr() as *mut libc::c_void, buf.len()) };
116 if len == -1 {
117 Err(io::Error::last_os_error())
118 } else {
119 Ok(len as usize)
120 }
121 }
122
123 fn flush(&mut self) -> io::Result<()> {
124 Ok(())
125 }
126}
127
128pub struct TunTapDevice {
129 device: Async<TunTap>,
130}
131
132impl TunTapDevice {
133 pub fn new(name: &str) -> io::Result<TunTapDevice> {
134 Ok(Self {
135 device: Async::new(TunTap::new(name)?)?,
136 })
137 }
138}
139
140impl Driver for TunTapDevice {
141 type RxToken<'a> = RxToken where Self: 'a;
142 type TxToken<'a> = TxToken<'a> where Self: 'a;
143
144 fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
145 let mut buf = vec![0; self.device.get_ref().mtu];
146 loop {
147 match self.device.get_mut().read(&mut buf) {
148 Ok(n) => {
149 buf.truncate(n);
150 return Some((
151 RxToken { buffer: buf },
152 TxToken {
153 device: &mut self.device,
154 },
155 ));
156 }
157 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
158 if !self.device.poll_readable(cx).is_ready() {
159 return None;
160 }
161 }
162 Err(e) => panic!("read error: {:?}", e),
163 }
164 }
165 }
166
167 fn transmit(&mut self, _cx: &mut Context) -> Option<Self::TxToken<'_>> {
168 Some(TxToken {
169 device: &mut self.device,
170 })
171 }
172
173 fn capabilities(&self) -> Capabilities {
174 let mut caps = Capabilities::default();
175 caps.max_transmission_unit = self.device.get_ref().mtu;
176 caps
177 }
178
179 fn link_state(&mut self, _cx: &mut Context) -> LinkState {
180 LinkState::Up
181 }
182
183 fn hardware_address(&self) -> HardwareAddress {
184 HardwareAddress::Ethernet([0x02, 0x03, 0x04, 0x05, 0x06, 0x07])
185 }
186}
187
188#[doc(hidden)]
189pub struct RxToken {
190 buffer: Vec<u8>,
191}
192
193impl embassy_net_driver::RxToken for RxToken {
194 fn consume<R, F>(mut self, f: F) -> R
195 where
196 F: FnOnce(&mut [u8]) -> R,
197 {
198 f(&mut self.buffer)
199 }
200}
201
202#[doc(hidden)]
203pub struct TxToken<'a> {
204 device: &'a mut Async<TunTap>,
205}
206
207impl<'a> embassy_net_driver::TxToken for TxToken<'a> {
208 fn consume<R, F>(self, len: usize, f: F) -> R
209 where
210 F: FnOnce(&mut [u8]) -> R,
211 {
212 let mut buffer = vec![0; len];
213 let result = f(&mut buffer);
214
215 // todo handle WouldBlock with async
216 match self.device.get_mut().write(&buffer) {
217 Ok(_) => {}
218 Err(e) if e.kind() == io::ErrorKind::WouldBlock => info!("transmit WouldBlock"),
219 Err(e) => panic!("transmit error: {:?}", e),
220 }
221
222 result
223 }
224}