diff --git a/Cargo.toml b/Cargo.toml index 6c11e58..0cc7a77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,9 @@ edition = "2024" wifi-connect = [ "dep:serde-json-core", "dep:serde", -] # you need to add a wifi.conf file with your wifi credentials (for example : "Example Wifi name:pa$$w0rd") -default = [] +] # you need to add a wifi.conf file for this to work +dhcp = ["dep:dhcparse"] +default = ["dhcp"] [dependencies] 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 = [ "derive", ] } -dhcparse = { version = "*", default-features = false } +dhcparse = { version = "*", default-features = false, optional = true } \ No newline at end of file diff --git a/src/dhcp.rs b/src/dhcp.rs index f8f40a2..0103a81 100644 --- a/src/dhcp.rs +++ b/src/dhcp.rs @@ -10,7 +10,7 @@ use embassy_net::{ }; use embassy_time::Timer; use heapless::Vec; -use log::info; +use log::{info, warn}; use pico_website::unwrap; #[embassy_executor::task(pool_size = 1)] @@ -23,7 +23,8 @@ pub async fn dhcp_server(stack: Stack<'static>) { let mut res_buf = Vec::::new(); let mut opts = Vec::::new(); 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( stack, &mut rx_meta, @@ -33,31 +34,25 @@ pub async fn dhcp_server(stack: Stack<'static>) { ); unwrap(socket.bind(67)).await; + info!("Starting DHCP server"); loop { - info!("receiving"); 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_type = unwrap(v4_options!(msg; MessageType required)).await; let mut rapid_commit = false; - for o in unwrap(msg.options()).await { - match o { - Err(_) => break, - Ok(o) => { - if let DhcpOption::Unknown(80, _) = o.0 { - if msg_type != DhcpMsgType::DISCOVER { - info!("ERROR : rapid commit option on {:?} message", msg_type); - } - rapid_commit = true; - } - info!("{:?}", o); - } + if unwrap(msg.options()) + .await + .any(|opt| matches!(opt, Ok((DhcpOption::Unknown(80, _), _)))) + { + if msg_type != DhcpMsgType::DISCOVER { + warn!("WARN : dhcp rapid commit option on {:?} message", msg_type); + continue 'listen; } + rapid_commit = true; } - info!("{:?}", msg_type); + info!("Dhcp: received {:?} message", msg_type); Timer::after_secs(0).await; 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; 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( socket .send_to( @@ -141,7 +125,7 @@ pub async fn dhcp_server(stack: Stack<'static>) { .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; if msg_type == DhcpMsgType::REQUEST || rapid_commit { @@ -176,7 +160,7 @@ async fn write_dhcp_opts(buf: &mut Vec, op_codes: &[u8]) 59 => (4, &600_u32.to_be_bytes()), // DhcpOption::Unknown(59, &[0, 0, 0x2, 0x58]), // rebinding time = 600s 80 => (0, &[]), _ => { - info!("Unhandled requested option : {}", o); + info!("Dhcp: unhandled requested option {}", o); Timer::after_secs(0).await; continue; } diff --git a/src/main.rs b/src/main.rs index bd9a5a0..11deeb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,9 @@ use rand_core::RngCore; use static_cell::StaticCell; use {defmt_rtt as _, panic_probe as _}; +#[cfg(feature = "dhcp")] mod dhcp; + mod game; 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(socket::listen_task(stack, game::Team::Zero, 80))).await;