diff options
| author | Ulf Lilleengen <[email protected]> | 2023-12-20 12:05:17 +0100 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2023-12-20 12:08:26 +0100 |
| commit | afb01e3fc51d44a637e99db3a16d0243a5921f6b (patch) | |
| tree | 7eed97ed8b32e88ec7afedaa488a9271109d6a44 | |
| parent | 52a801fdb7561c42a5036b30d4807e18580a845f (diff) | |
docs: document embassy-net-tuntap
| -rw-r--r-- | embassy-net-tuntap/src/lib.rs | 13 |
1 files changed, 13 insertions, 0 deletions
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)?)?, |
