cleaning dhcp
This commit is contained in:
parent
2bfca4ad1d
commit
75d280f928
@ -7,8 +7,9 @@ edition = "2024"
|
|||||||
wifi-connect = [
|
wifi-connect = [
|
||||||
"dep:serde-json-core",
|
"dep:serde-json-core",
|
||||||
"dep:serde",
|
"dep:serde",
|
||||||
] # you need to add a wifi.conf file with your wifi credentials (for example : "Example Wifi name:pa$$w0rd")
|
] # you need to add a wifi.conf file for this to work
|
||||||
default = []
|
dhcp = ["dep:dhcparse"]
|
||||||
|
default = ["dhcp"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
embassy-executor = { git = "https://github.com/embassy-rs/embassy", features = [
|
embassy-executor = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||||
@ -53,4 +54,4 @@ serde-json-core = { version = "*", optional = true }
|
|||||||
serde = { version = "*", optional = true, default-features = false, features = [
|
serde = { version = "*", optional = true, default-features = false, features = [
|
||||||
"derive",
|
"derive",
|
||||||
] }
|
] }
|
||||||
dhcparse = { version = "*", default-features = false }
|
dhcparse = { version = "*", default-features = false, optional = true }
|
46
src/dhcp.rs
46
src/dhcp.rs
@ -10,7 +10,7 @@ use embassy_net::{
|
|||||||
};
|
};
|
||||||
use embassy_time::Timer;
|
use embassy_time::Timer;
|
||||||
use heapless::Vec;
|
use heapless::Vec;
|
||||||
use log::info;
|
use log::{info, warn};
|
||||||
use pico_website::unwrap;
|
use pico_website::unwrap;
|
||||||
|
|
||||||
#[embassy_executor::task(pool_size = 1)]
|
#[embassy_executor::task(pool_size = 1)]
|
||||||
@ -23,7 +23,8 @@ pub async fn dhcp_server(stack: Stack<'static>) {
|
|||||||
let mut res_buf = Vec::<u8, 4096>::new();
|
let mut res_buf = Vec::<u8, 4096>::new();
|
||||||
let mut opts = Vec::<u8, 255>::new();
|
let mut opts = Vec::<u8, 255>::new();
|
||||||
let mut current_ip = 10_u8; // add one at each new connection, loop at 250, hope not to get on occupied ip
|
let mut current_ip = 10_u8; // add one at each new connection, loop at 250, hope not to get on occupied ip
|
||||||
loop {
|
|
||||||
|
'listen: loop {
|
||||||
let mut socket = UdpSocket::new(
|
let mut socket = UdpSocket::new(
|
||||||
stack,
|
stack,
|
||||||
&mut rx_meta,
|
&mut rx_meta,
|
||||||
@ -33,31 +34,25 @@ pub async fn dhcp_server(stack: Stack<'static>) {
|
|||||||
);
|
);
|
||||||
unwrap(socket.bind(67)).await;
|
unwrap(socket.bind(67)).await;
|
||||||
|
|
||||||
|
info!("Starting DHCP server");
|
||||||
loop {
|
loop {
|
||||||
info!("receiving");
|
|
||||||
let (n, _) = unwrap(socket.recv_from(&mut buf).await).await;
|
let (n, _) = unwrap(socket.recv_from(&mut buf).await).await;
|
||||||
info!("received");
|
|
||||||
Timer::after_secs(0).await;
|
|
||||||
|
|
||||||
let msg = unwrap(dhcpv4::Message::new(&buf[..n])).await;
|
let msg = unwrap(dhcpv4::Message::new(&buf[..n])).await;
|
||||||
|
|
||||||
let msg_type = unwrap(v4_options!(msg; MessageType required)).await;
|
let msg_type = unwrap(v4_options!(msg; MessageType required)).await;
|
||||||
let mut rapid_commit = false;
|
let mut rapid_commit = false;
|
||||||
for o in unwrap(msg.options()).await {
|
if unwrap(msg.options())
|
||||||
match o {
|
.await
|
||||||
Err(_) => break,
|
.any(|opt| matches!(opt, Ok((DhcpOption::Unknown(80, _), _))))
|
||||||
Ok(o) => {
|
{
|
||||||
if let DhcpOption::Unknown(80, _) = o.0 {
|
if msg_type != DhcpMsgType::DISCOVER {
|
||||||
if msg_type != DhcpMsgType::DISCOVER {
|
warn!("WARN : dhcp rapid commit option on {:?} message", msg_type);
|
||||||
info!("ERROR : rapid commit option on {:?} message", msg_type);
|
continue 'listen;
|
||||||
}
|
|
||||||
rapid_commit = true;
|
|
||||||
}
|
|
||||||
info!("{:?}", o);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
rapid_commit = true;
|
||||||
}
|
}
|
||||||
info!("{:?}", msg_type);
|
info!("Dhcp: received {:?} message", msg_type);
|
||||||
Timer::after_secs(0).await;
|
Timer::after_secs(0).await;
|
||||||
|
|
||||||
match msg_type {
|
match msg_type {
|
||||||
@ -114,17 +109,6 @@ pub async fn dhcp_server(stack: Stack<'static>) {
|
|||||||
unwrap(write_dhcp_opts(&mut res_buf, &opts).await).await;
|
unwrap(write_dhcp_opts(&mut res_buf, &opts).await).await;
|
||||||
res_buf.push(255).unwrap(); // end option
|
res_buf.push(255).unwrap(); // end option
|
||||||
|
|
||||||
for o in unwrap(unwrap(dhcpv4::Message::new(&res_buf)).await.options()).await {
|
|
||||||
info!(
|
|
||||||
"{:?}",
|
|
||||||
match o {
|
|
||||||
Err(_) => break,
|
|
||||||
Ok(o) => o,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
Timer::after_secs(0).await;
|
|
||||||
}
|
|
||||||
|
|
||||||
unwrap(
|
unwrap(
|
||||||
socket
|
socket
|
||||||
.send_to(
|
.send_to(
|
||||||
@ -141,7 +125,7 @@ pub async fn dhcp_server(stack: Stack<'static>) {
|
|||||||
.await,
|
.await,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
info!("offer/ack sent for ip {}", current_ip);
|
info!("Dhcp: offer/ack sent for ip 192.254.0.{}", current_ip);
|
||||||
Timer::after_secs(0).await;
|
Timer::after_secs(0).await;
|
||||||
|
|
||||||
if msg_type == DhcpMsgType::REQUEST || rapid_commit {
|
if msg_type == DhcpMsgType::REQUEST || rapid_commit {
|
||||||
@ -176,7 +160,7 @@ async fn write_dhcp_opts<const N: usize>(buf: &mut Vec<u8, N>, op_codes: &[u8])
|
|||||||
59 => (4, &600_u32.to_be_bytes()), // DhcpOption::Unknown(59, &[0, 0, 0x2, 0x58]), // rebinding time = 600s
|
59 => (4, &600_u32.to_be_bytes()), // DhcpOption::Unknown(59, &[0, 0, 0x2, 0x58]), // rebinding time = 600s
|
||||||
80 => (0, &[]),
|
80 => (0, &[]),
|
||||||
_ => {
|
_ => {
|
||||||
info!("Unhandled requested option : {}", o);
|
info!("Dhcp: unhandled requested option {}", o);
|
||||||
Timer::after_secs(0).await;
|
Timer::after_secs(0).await;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,9 @@ use rand_core::RngCore;
|
|||||||
use static_cell::StaticCell;
|
use static_cell::StaticCell;
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
#[cfg(feature = "dhcp")]
|
||||||
mod dhcp;
|
mod dhcp;
|
||||||
|
|
||||||
mod game;
|
mod game;
|
||||||
mod socket;
|
mod socket;
|
||||||
|
|
||||||
@ -158,6 +160,7 @@ async fn main(spawner: Spawner) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "dhcp")]
|
||||||
unwrap(spawner.spawn(dhcp::dhcp_server(stack))).await;
|
unwrap(spawner.spawn(dhcp::dhcp_server(stack))).await;
|
||||||
|
|
||||||
unwrap(spawner.spawn(socket::listen_task(stack, game::Team::Zero, 80))).await;
|
unwrap(spawner.spawn(socket::listen_task(stack, game::Team::Zero, 80))).await;
|
||||||
|
Loading…
Reference in New Issue
Block a user