2023-05-25 13:36:45 +02:00
|
|
|
use core::fmt::Write as _;
|
|
|
|
use std::default::Default;
|
|
|
|
|
|
|
|
use clap::Parser;
|
|
|
|
use embassy_executor::{Executor, Spawner};
|
|
|
|
use embassy_net::tcp::TcpSocket;
|
|
|
|
use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources};
|
2023-08-03 14:23:11 +02:00
|
|
|
use embassy_net_tuntap::TunTapDevice;
|
2023-05-25 13:36:45 +02:00
|
|
|
use embassy_time::{Duration, Timer};
|
2023-08-07 13:43:09 +02:00
|
|
|
use embedded_io_async::Write as _;
|
2023-05-25 13:36:45 +02:00
|
|
|
use heapless::Vec;
|
|
|
|
use log::*;
|
|
|
|
use rand_core::{OsRng, RngCore};
|
2023-12-21 08:50:54 +01:00
|
|
|
use static_cell::StaticCell;
|
2023-05-25 13:36:45 +02:00
|
|
|
|
|
|
|
#[derive(Parser)]
|
|
|
|
#[clap(version = "1.0")]
|
|
|
|
struct Opts {
|
|
|
|
/// TAP device name
|
|
|
|
#[clap(long, default_value = "tap0")]
|
|
|
|
tap: String,
|
|
|
|
/// use a static IP instead of DHCP
|
|
|
|
#[clap(long)]
|
|
|
|
static_ip: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
async fn net_task(stack: &'static Stack<TunTapDevice>) -> ! {
|
|
|
|
stack.run().await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct StrWrite(pub heapless::Vec<u8, 30>);
|
|
|
|
|
|
|
|
impl core::fmt::Write for StrWrite {
|
|
|
|
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> {
|
|
|
|
self.0.extend_from_slice(s.as_bytes()).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
async fn main_task(spawner: Spawner) {
|
|
|
|
let opts: Opts = Opts::parse();
|
|
|
|
|
|
|
|
// Init network device
|
|
|
|
let device = TunTapDevice::new(&opts.tap).unwrap();
|
|
|
|
|
|
|
|
// Choose between dhcp or static ip
|
|
|
|
let config = if opts.static_ip {
|
2023-06-07 12:04:15 +02:00
|
|
|
Config::ipv4_static(embassy_net::StaticConfigV4 {
|
2023-05-25 13:36:45 +02:00
|
|
|
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24),
|
|
|
|
dns_servers: Vec::new(),
|
|
|
|
gateway: Some(Ipv4Address::new(192, 168, 69, 1)),
|
|
|
|
})
|
|
|
|
} else {
|
2023-06-07 12:04:15 +02:00
|
|
|
Config::dhcpv4(Default::default())
|
2023-05-25 13:36:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Generate random seed
|
|
|
|
let mut seed = [0; 8];
|
|
|
|
OsRng.fill_bytes(&mut seed);
|
|
|
|
let seed = u64::from_le_bytes(seed);
|
|
|
|
|
|
|
|
// Init network stack
|
2023-12-21 08:50:54 +01:00
|
|
|
static STACK: StaticCell<Stack<TunTapDevice>> = StaticCell::new();
|
|
|
|
static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
|
|
|
|
let stack = &*STACK.init(Stack::new(
|
2023-06-01 01:32:11 +02:00
|
|
|
device,
|
|
|
|
config,
|
2023-12-21 08:50:54 +01:00
|
|
|
RESOURCES.init(StackResources::<3>::new()),
|
|
|
|
seed,
|
2023-06-01 01:32:11 +02:00
|
|
|
));
|
2023-05-25 13:36:45 +02:00
|
|
|
|
|
|
|
// Launch network task
|
|
|
|
spawner.spawn(net_task(stack)).unwrap();
|
|
|
|
|
|
|
|
// Then we can use it!
|
|
|
|
let mut rx_buffer = [0; 4096];
|
|
|
|
let mut tx_buffer = [0; 4096];
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
|
|
|
socket.set_timeout(Some(Duration::from_secs(10)));
|
|
|
|
info!("Listening on TCP:9999...");
|
|
|
|
if let Err(_) = socket.accept(9999).await {
|
|
|
|
warn!("accept error");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("Accepted a connection");
|
|
|
|
|
|
|
|
// Write some quick output
|
|
|
|
for i in 1..=5 {
|
|
|
|
let mut w = StrWrite::default();
|
|
|
|
write!(w, "{}! ", i).unwrap();
|
|
|
|
let r = socket.write_all(&w.0).await;
|
|
|
|
if let Err(e) = r {
|
|
|
|
warn!("write error: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(500).await;
|
2023-05-25 13:36:45 +02:00
|
|
|
}
|
|
|
|
info!("Closing the connection");
|
|
|
|
socket.abort();
|
|
|
|
info!("Flushing the RST out...");
|
2023-06-01 02:22:46 +02:00
|
|
|
_ = socket.flush().await;
|
2023-05-25 13:36:45 +02:00
|
|
|
info!("Finished with the socket");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static EXECUTOR: StaticCell<Executor> = StaticCell::new();
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
env_logger::builder()
|
|
|
|
.filter_level(log::LevelFilter::Debug)
|
|
|
|
.filter_module("async_io", log::LevelFilter::Info)
|
|
|
|
.format_timestamp_nanos()
|
|
|
|
.init();
|
|
|
|
|
|
|
|
let executor = EXECUTOR.init(Executor::new());
|
|
|
|
executor.run(|spawner| {
|
|
|
|
spawner.spawn(main_task(spawner)).unwrap();
|
|
|
|
});
|
|
|
|
}
|