aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2023-12-20 12:44:06 +0000
committerGitHub <[email protected]>2023-12-20 12:44:06 +0000
commit70ea805af3fa80f815cbd2f1170b7488da0e52fe (patch)
tree6af5d1f6d8c51ec6f8524aff89ef9117eebbf501
parentc8eb128a563850788722a14c242246c089434030 (diff)
parent73f8cd7ade632a9905252698d09f71bd7ac3714c (diff)
Merge pull request #2332 from embassy-rs/embassy-net-tuntap-docs
docs: document embassy-net-tuntap
-rw-r--r--embassy-net-tuntap/Cargo.toml3
-rw-r--r--embassy-net-tuntap/src/lib.rs13
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
6categories = ["embedded", "hardware-support", "no-std", "network-programming", "async"] 6categories = ["embedded", "hardware-support", "no-std", "network-programming", "async"]
7license = "MIT OR Apache-2.0" 7license = "MIT OR Apache-2.0"
8edition = "2021" 8edition = "2021"
9repository = "https://github.com/embassy-rs/embassy"
9 10
10[dependencies] 11[dependencies]
11embassy-net-driver = { version = "0.2.0", path = "../embassy-net-driver" } 12embassy-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]
17src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-tuntap-v$VERSION/embassy-net-tuntap/src/" 18src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-tuntap-v$VERSION/embassy-net-tuntap/src/"
18src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-tuntap/src/" 19src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-tuntap/src/"
19target = "thumbv7em-none-eabi" \ No newline at end of file 20target = "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")]
1use std::io; 3use std::io;
2use std::io::{Read, Write}; 4use std::io::{Read, Write};
3use std::os::unix::io::{AsRawFd, RawFd}; 5use std::os::unix::io::{AsRawFd, RawFd};
@@ -7,12 +9,19 @@ use async_io::Async;
7use embassy_net_driver::{self, Capabilities, Driver, HardwareAddress, LinkState}; 9use embassy_net_driver::{self, Capabilities, Driver, HardwareAddress, LinkState};
8use log::*; 10use log::*;
9 11
12/// Get the MTU of the given interface.
10pub const SIOCGIFMTU: libc::c_ulong = 0x8921; 13pub const SIOCGIFMTU: libc::c_ulong = 0x8921;
14/// Get the index of the given interface.
11pub const _SIOCGIFINDEX: libc::c_ulong = 0x8933; 15pub const _SIOCGIFINDEX: libc::c_ulong = 0x8933;
16/// Capture all packages.
12pub const _ETH_P_ALL: libc::c_short = 0x0003; 17pub const _ETH_P_ALL: libc::c_short = 0x0003;
18/// Set the interface flags.
13pub const TUNSETIFF: libc::c_ulong = 0x400454CA; 19pub const TUNSETIFF: libc::c_ulong = 0x400454CA;
20/// TUN device.
14pub const _IFF_TUN: libc::c_int = 0x0001; 21pub const _IFF_TUN: libc::c_int = 0x0001;
22/// TAP device.
15pub const IFF_TAP: libc::c_int = 0x0002; 23pub const IFF_TAP: libc::c_int = 0x0002;
24/// No packet information.
16pub const IFF_NO_PI: libc::c_int = 0x1000; 25pub const IFF_NO_PI: libc::c_int = 0x1000;
17 26
18const ETHERNET_HEADER_LEN: usize = 14; 27const 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)]
51pub struct TunTap { 61pub struct TunTap {
52 fd: libc::c_int, 62 fd: libc::c_int,
@@ -60,6 +70,7 @@ impl AsRawFd for TunTap {
60} 70}
61 71
62impl TunTap { 72impl 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.
129pub struct TunTapDevice { 141pub struct TunTapDevice {
130 device: Async<TunTap>, 142 device: Async<TunTap>,
131} 143}
132 144
133impl TunTapDevice { 145impl 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)?)?,