Skip to content

Commit

Permalink
DHCP server bugfixing and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Dec 3, 2023
1 parent eaa4d77 commit 93a807f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
24 changes: 14 additions & 10 deletions src/asynch/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ pub trait Socket {
pub struct RawSocketFactory<R> {
stack: R,
interface: Option<u32>,
client_port: Option<u16>,
server_port: Option<u16>,
local_port: Option<u16>,
remote_port: Option<u16>,
}

impl<R> RawSocketFactory<R>
Expand All @@ -98,18 +98,18 @@ where
pub const fn new(
stack: R,
interface: Option<u32>,
client_port: Option<u16>,
server_port: Option<u16>,
local_port: Option<u16>,
remote_port: Option<u16>,
) -> Self {
if client_port.is_none() && server_port.is_none() {
panic!("Either the client, or the sererver port, or both should be specified");
if local_port.is_none() && remote_port.is_none() {
panic!("Either the local, or the remote port, or both should be specified");
}

Self {
stack,
interface,
client_port,
server_port,
local_port,
remote_port,
}
}
}
Expand All @@ -123,7 +123,7 @@ where
type Socket = R::Socket;

fn raw_ports(&self) -> (Option<u16>, Option<u16>) {
(self.client_port, self.server_port)
(self.local_port, self.remote_port)
}

async fn connect(&self) -> Result<Self::Socket, Self::Error> {
Expand Down Expand Up @@ -501,6 +501,8 @@ pub mod server {

use embedded_nal_async::Ipv4Addr;

use log::info;

pub use super::*;

#[derive(Clone, Debug)]
Expand All @@ -525,6 +527,8 @@ pub mod server {

impl<const N: usize> Server<N> {
pub fn new(conf: &Configuration) -> Self {
info!("Creating DHCP server with configuration {conf:?}");

Self {
server: dhcp::server::Server {
ip: conf.ip,
Expand Down Expand Up @@ -555,7 +559,7 @@ pub mod server {

if let Some(reply) = self
.server
.handle_bootp_request(f.raw_ports().1, buf, len)?
.handle_bootp_request(f.raw_ports().0, buf, len)?
{
socket.send(reply).await.map_err(Error::Io)?;
}
Expand Down
11 changes: 10 additions & 1 deletion src/asynch/stdnal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use futures_lite::io::{AsyncReadExt, AsyncWriteExt};

use embedded_io::ErrorType;
use embedded_io_async::{Read, Write};
use log::warn;
use no_std_net::{Ipv4Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

use embedded_nal_async::{
Expand Down Expand Up @@ -165,6 +166,8 @@ impl UdpStack for StdUdpStack {
) -> Result<(SocketAddr, Self::UniquelyBound), Self::Error> {
let socket = Async::<UdpSocket>::bind(to_std_addr(local))?;

socket.as_ref().set_broadcast(true)?;

Ok((
to_nal_addr(socket.as_ref().local_addr()?),
StdUdpSocket(socket),
Expand Down Expand Up @@ -359,11 +362,15 @@ impl UnconnectedUdp for StdUdpSocket {
async fn send(
&mut self,
local: SocketAddr,
remote: SocketAddr,
mut remote: SocketAddr,
data: &[u8],
) -> Result<(), Self::Error> {
assert!(local == to_nal_addr(self.0.as_ref().local_addr()?));

remote = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::BROADCAST, 68));

warn!("Send to: {remote}");

let mut offset = 0;

loop {
Expand All @@ -383,6 +390,8 @@ impl UnconnectedUdp for StdUdpSocket {
) -> Result<(usize, SocketAddr, SocketAddr), Self::Error> {
let (len, addr) = self.0.recv_from(buffer).await?;

warn!("Received from: {addr}");

Ok((
len,
to_nal_addr(self.0.as_ref().local_addr()?),
Expand Down
19 changes: 14 additions & 5 deletions src/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,8 @@ impl<'a> Options<'a> {
let option = match *code {
DhcpOption::CODE_ROUTER => (!gateways.is_empty())
.then_some(DhcpOption::Router(Ipv4Addrs::new(gateways))),
DhcpOption::CODE_DNS => {
(!dns.is_empty()).then_some(DhcpOption::Router(Ipv4Addrs::new(dns)))
}
DhcpOption::CODE_DNS => (!dns.is_empty())
.then_some(DhcpOption::DomainNameServer(Ipv4Addrs::new(dns))),
DhcpOption::CODE_SUBNET => subnet.map(DhcpOption::SubnetMask),
_ => None,
};
Expand Down Expand Up @@ -959,6 +958,8 @@ pub mod server {

use embassy_time::{Duration, Instant};

use log::{info, trace};

use super::*;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -1000,6 +1001,8 @@ pub mod server {
};

if let Some((raw_hdrs, request)) = request {
trace!("Got packet {request:?}");

if !request.reply {
let mt = request.options.iter().find_map(|option| {
if let DhcpOption::MessageType(mt) = option {
Expand All @@ -1021,6 +1024,8 @@ pub mod server {
if server_identifier == Some(self.ip)
|| server_identifier.is_none() && matches!(mt, MessageType::Discover)
{
info!("Packet is for us, will process, message type {mt:?}");

let mut opt_buf = Options::buf();

let reply = match mt {
Expand Down Expand Up @@ -1124,7 +1129,7 @@ pub mod server {
ip: Option<Ipv4Addr>,
buf: &'a mut [DhcpOption<'a>],
) -> Packet<'a> {
request.new_reply(
let reply = request.new_reply(
ip,
request.options.reply(
mt,
Expand All @@ -1135,7 +1140,11 @@ pub mod server {
&self.dns,
buf,
),
)
);

info!("Reply: {reply:?}");

reply
}

fn is_available(&self, mac: &[u8; 16], addr: Ipv4Addr) -> bool {
Expand Down

0 comments on commit 93a807f

Please sign in to comment.