Skip to content

Commit

Permalink
Make DHCP option 15 optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Fresk committed Aug 20, 2024
1 parent a7d7918 commit 35bfdcb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ defmt = ["dep:defmt", "heapless/defmt-03"]
"proto-sixlowpan" = ["proto-ipv6"]
"proto-sixlowpan-fragmentation" = ["proto-sixlowpan", "_proto-fragmentation"]
"proto-dns" = []
"proto-domainname" = []
"proto-ipsec" = ["proto-ipsec-ah", "proto-ipsec-esp"]
"proto-ipsec-ah" = []
"proto-ipsec-esp" = []
Expand Down Expand Up @@ -96,7 +97,7 @@ default = [
"std", "log", # needed for `cargo test --no-default-features --features default` :/
"medium-ethernet", "medium-ip", "medium-ieee802154",
"phy-raw_socket", "phy-tuntap_interface",
"proto-ipv4", "proto-igmp", "proto-dhcpv4", "proto-ipv6", "proto-dns",
"proto-ipv4", "proto-igmp", "proto-dhcpv4", "proto-ipv6", "proto-dns", "proto-domainname",
"proto-ipv4-fragmentation", "proto-sixlowpan-fragmentation",
"socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4", "socket-dns", "socket-mdns",
"packetmeta-id", "async"
Expand Down
6 changes: 6 additions & 0 deletions src/socket/dhcpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::str::FromStr;
#[cfg(feature = "async")]
use core::task::Waker;

#[cfg(feature = "proto-domainname")]
use crate::config::DHCP_MAX_DOMAIN_NAME_SIZE;
use crate::iface::Context;
use crate::time::{Duration, Instant};
Expand All @@ -24,6 +25,7 @@ const DEFAULT_PARAMETER_REQUEST_LIST: &[u8] = &[
dhcpv4_field::OPT_SUBNET_MASK,
dhcpv4_field::OPT_ROUTER,
dhcpv4_field::OPT_DOMAIN_NAME_SERVER,
#[cfg(feature = "proto-domainname")]
dhcpv4_field::OPT_DOMAIN_NAME,
];

Expand All @@ -42,6 +44,7 @@ pub struct Config<'a> {
/// DNS servers
pub dns_servers: Vec<Ipv4Address, DHCP_MAX_DNS_SERVER_COUNT>,
/// Domain name
#[cfg(feature = "proto-domainname")]
pub domain_name: Option<String<DHCP_MAX_DOMAIN_NAME_SIZE>>,
/// Received DHCP packet
pub packet: Option<DhcpPacket<&'a [u8]>>,
Expand Down Expand Up @@ -499,6 +502,7 @@ impl<'a> Socket<'a> {
address: Ipv4Cidr::new(dhcp_repr.your_ip, prefix_len),
router: dhcp_repr.router,
dns_servers,
#[cfg(feature = "proto-domainname")]
domain_name: dhcp_repr
.domain_name
.map(String::from_str)
Expand Down Expand Up @@ -598,6 +602,7 @@ impl<'a> Socket<'a> {
renew_duration: None,
rebind_duration: None,
dns_servers: None,
#[cfg(feature = "proto-domainname")]
domain_name: None,
additional_options: self.outgoing_options,
};
Expand Down Expand Up @@ -749,6 +754,7 @@ impl<'a> Socket<'a> {
address: state.config.address,
router: state.config.router,
dns_servers: state.config.dns_servers.clone(),
#[cfg(feature = "proto-domainname")]
domain_name: state.config.domain_name.clone(),
packet: self
.receive_packet_buffer
Expand Down
12 changes: 9 additions & 3 deletions src/wire/dhcpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ pub struct Repr<'a> {
/// DNS servers
pub dns_servers: Option<Vec<Ipv4Address, MAX_DNS_SERVER_COUNT>>,
/// Domain name
#[cfg(feature = "proto-domainname")]
pub domain_name: Option<&'a str>,
/// The maximum size dhcp packet the interface can receive
pub max_size: Option<u16>,
Expand Down Expand Up @@ -694,6 +695,7 @@ impl<'a> Repr<'a> {
len += 2;
len += dns_servers.iter().count() * core::mem::size_of::<u32>();
}
#[cfg(feature = "proto-domainname")]
if let Some(domain_name) = &self.domain_name {
len += 2;
len += domain_name.as_bytes().len();
Expand Down Expand Up @@ -744,6 +746,7 @@ impl<'a> Repr<'a> {
let mut subnet_mask = None;
let mut parameter_request_list = None;
let mut dns_servers = None;
#[cfg(feature = "proto-domainname")]
let mut domain_name = None;
let mut max_size = None;
let mut lease_duration = None;
Expand Down Expand Up @@ -809,10 +812,9 @@ impl<'a> Repr<'a> {
net_trace!("DHCP domain name servers contained invalid address");
}
}
#[cfg(feature = "proto-domainname")]
(field::OPT_DOMAIN_NAME, _) => {
if let Ok(name) = core::str::from_utf8(data) {
domain_name = Some(name);
}
domain_name = core::str::from_utf8(data).ok();
}
_ => {}
}
Expand All @@ -836,6 +838,7 @@ impl<'a> Repr<'a> {
client_identifier,
parameter_request_list,
dns_servers,
#[cfg(feature = "proto-domainname")]
domain_name,
max_size,
lease_duration,
Expand Down Expand Up @@ -953,6 +956,7 @@ impl<'a> Repr<'a> {
})?;
}

#[cfg(feature = "proto-domainname")]
if let Some(domain_name) = &self.domain_name {
options.emit(DhcpOption {
kind: field::OPT_DOMAIN_NAME,
Expand Down Expand Up @@ -1187,6 +1191,7 @@ mod test {
server_identifier: None,
parameter_request_list: None,
dns_servers: None,
#[cfg(feature = "proto-domainname")]
domain_name: None,
max_size: None,
renew_duration: None,
Expand Down Expand Up @@ -1218,6 +1223,7 @@ mod test {
server_identifier: None,
parameter_request_list: Some(&[1, 3, 6, 42]),
dns_servers: None,
#[cfg(feature = "proto-domainname")]
domain_name: None,
additional_options: &[],
}
Expand Down

0 comments on commit 35bfdcb

Please sign in to comment.