diff options
| -rw-r--r-- | embassy-net-tuntap/Cargo.toml | 3 | ||||
| -rw-r--r-- | embassy-net-tuntap/src/lib.rs | 13 |
2 files changed, 15 insertions, 1 deletions
diff --git a/embassy-net-tuntap/Cargo.toml b/embassy-net-tuntap/Cargo.toml index 4e374c365..7e2c7bfd5 100644 --- a/embassy-net-tuntap/Cargo.toml +++ b/embassy-net-tuntap/Cargo.toml | |||
| @@ -6,6 +6,7 @@ keywords = ["embedded", "tuntap", "embassy-net", "embedded-hal-async", "ethernet | |||
| 6 | categories = ["embedded", "hardware-support", "no-std", "network-programming", "async"] | 6 | categories = ["embedded", "hardware-support", "no-std", "network-programming", "async"] |
| 7 | license = "MIT OR Apache-2.0" | 7 | license = "MIT OR Apache-2.0" |
| 8 | edition = "2021" | 8 | edition = "2021" |
| 9 | repository = "https://github.com/embassy-rs/embassy" | ||
| 9 | 10 | ||
| 10 | [dependencies] | 11 | [dependencies] |
| 11 | embassy-net-driver = { version = "0.2.0", path = "../embassy-net-driver" } | 12 | embassy-net-driver = { version = "0.2.0", path = "../embassy-net-driver" } |
| @@ -16,4 +17,4 @@ libc = "0.2.101" | |||
| 16 | [package.metadata.embassy_docs] | 17 | [package.metadata.embassy_docs] |
| 17 | src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-tuntap-v$VERSION/embassy-net-tuntap/src/" | 18 | src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-tuntap-v$VERSION/embassy-net-tuntap/src/" |
| 18 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-tuntap/src/" | 19 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-tuntap/src/" |
| 19 | target = "thumbv7em-none-eabi" \ No newline at end of file | 20 | target = "thumbv7em-none-eabi" |
diff --git a/embassy-net-tuntap/src/lib.rs b/embassy-net-tuntap/src/lib.rs index 75c54c487..de30934eb 100644 --- a/embassy-net-tuntap/src/lib.rs +++ b/embassy-net-tuntap/src/lib.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | #![warn(missing_docs)] | ||
| 2 | #![doc = include_str!("../README.md")] | ||
| 1 | use std::io; | 3 | use std::io; |
| 2 | use std::io::{Read, Write}; | 4 | use std::io::{Read, Write}; |
| 3 | use std::os::unix::io::{AsRawFd, RawFd}; | 5 | use std::os::unix::io::{AsRawFd, RawFd}; |
| @@ -7,12 +9,19 @@ use async_io::Async; | |||
| 7 | use embassy_net_driver::{self, Capabilities, Driver, HardwareAddress, LinkState}; | 9 | use embassy_net_driver::{self, Capabilities, Driver, HardwareAddress, LinkState}; |
| 8 | use log::*; | 10 | use log::*; |
| 9 | 11 | ||
| 12 | /// Get the MTU of the given interface. | ||
| 10 | pub const SIOCGIFMTU: libc::c_ulong = 0x8921; | 13 | pub const SIOCGIFMTU: libc::c_ulong = 0x8921; |
| 14 | /// Get the index of the given interface. | ||
| 11 | pub const _SIOCGIFINDEX: libc::c_ulong = 0x8933; | 15 | pub const _SIOCGIFINDEX: libc::c_ulong = 0x8933; |
| 16 | /// Capture all packages. | ||
| 12 | pub const _ETH_P_ALL: libc::c_short = 0x0003; | 17 | pub const _ETH_P_ALL: libc::c_short = 0x0003; |
| 18 | /// Set the interface flags. | ||
| 13 | pub const TUNSETIFF: libc::c_ulong = 0x400454CA; | 19 | pub const TUNSETIFF: libc::c_ulong = 0x400454CA; |
| 20 | /// TUN device. | ||
| 14 | pub const _IFF_TUN: libc::c_int = 0x0001; | 21 | pub const _IFF_TUN: libc::c_int = 0x0001; |
| 22 | /// TAP device. | ||
| 15 | pub const IFF_TAP: libc::c_int = 0x0002; | 23 | pub const IFF_TAP: libc::c_int = 0x0002; |
| 24 | /// No packet information. | ||
| 16 | pub const IFF_NO_PI: libc::c_int = 0x1000; | 25 | pub const IFF_NO_PI: libc::c_int = 0x1000; |
| 17 | 26 | ||
| 18 | const ETHERNET_HEADER_LEN: usize = 14; | 27 | const ETHERNET_HEADER_LEN: usize = 14; |
| @@ -47,6 +56,7 @@ fn ifreq_ioctl(lower: libc::c_int, ifreq: &mut ifreq, cmd: libc::c_ulong) -> io: | |||
| 47 | Ok(ifreq.ifr_data) | 56 | Ok(ifreq.ifr_data) |
| 48 | } | 57 | } |
| 49 | 58 | ||
| 59 | /// A TUN/TAP device. | ||
| 50 | #[derive(Debug)] | 60 | #[derive(Debug)] |
| 51 | pub struct TunTap { | 61 | pub struct TunTap { |
| 52 | fd: libc::c_int, | 62 | fd: libc::c_int, |
| @@ -60,6 +70,7 @@ impl AsRawFd for TunTap { | |||
| 60 | } | 70 | } |
| 61 | 71 | ||
| 62 | impl TunTap { | 72 | impl TunTap { |
| 73 | /// Create a new TUN/TAP device. | ||
| 63 | pub fn new(name: &str) -> io::Result<TunTap> { | 74 | pub fn new(name: &str) -> io::Result<TunTap> { |
| 64 | unsafe { | 75 | unsafe { |
| 65 | let fd = libc::open( | 76 | let fd = libc::open( |
| @@ -126,11 +137,13 @@ impl io::Write for TunTap { | |||
| 126 | } | 137 | } |
| 127 | } | 138 | } |
| 128 | 139 | ||
| 140 | /// A TUN/TAP device, wrapped in an async interface. | ||
| 129 | pub struct TunTapDevice { | 141 | pub struct TunTapDevice { |
| 130 | device: Async<TunTap>, | 142 | device: Async<TunTap>, |
| 131 | } | 143 | } |
| 132 | 144 | ||
| 133 | impl TunTapDevice { | 145 | impl TunTapDevice { |
| 146 | /// Create a new TUN/TAP device. | ||
| 134 | pub fn new(name: &str) -> io::Result<TunTapDevice> { | 147 | pub fn new(name: &str) -> io::Result<TunTapDevice> { |
| 135 | Ok(Self { | 148 | Ok(Self { |
| 136 | device: Async::new(TunTap::new(name)?)?, | 149 | device: Async::new(TunTap::new(name)?)?, |
