From e4a6d2063d6b2f8dd084dd0cd4f6d11c17decbba Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Fri, 9 Aug 2024 22:01:06 +0200 Subject: [PATCH] Make domain name length settable, use reference in DhcpRepr --- Cargo.toml | 4 ++++ build.rs | 1 + gen_config.py | 1 + src/lib.rs | 1 + src/socket/dhcpv4.rs | 16 ++++++++++------ src/wire/dhcpv4.rs | 15 +++++---------- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2f3e266bd..9f9fba422 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -249,6 +249,10 @@ dns-max-name-size-64 = [] dns-max-name-size-128 = [] dns-max-name-size-255 = [] # Default +dhcp-max-domain-name-size-32 = [] +dhcp-max-domain-name-size-64 = [] # Default +dhcp-max-domain-name-size-128 = [] + rpl-relations-buffer-count-1 = [] rpl-relations-buffer-count-2 = [] rpl-relations-buffer-count-4 = [] diff --git a/build.rs b/build.rs index e1746d23f..69098aee5 100644 --- a/build.rs +++ b/build.rs @@ -19,6 +19,7 @@ static CONFIGS: &[(&str, usize)] = &[ ("DNS_MAX_RESULT_COUNT", 1), ("DNS_MAX_SERVER_COUNT", 1), ("DNS_MAX_NAME_SIZE", 255), + ("DHCP_MAX_DOMAIN_NAME_SIZE", 64), ("RPL_RELATIONS_BUFFER_COUNT", 16), ("RPL_PARENTS_BUFFER_COUNT", 8), // END AUTOGENERATED CONFIG FEATURES diff --git a/gen_config.py b/gen_config.py index 1407ca2d6..31993cdd5 100644 --- a/gen_config.py +++ b/gen_config.py @@ -40,6 +40,7 @@ def feature(name, default, min, max, pow2=None): feature("dns_max_result_count", default=1, min=1, max=32, pow2=4) feature("dns_max_server_count", default=1, min=1, max=32, pow2=4) feature("dns_max_name_size", default=255, min=64, max=255, pow2=True) +feature("dhcp_max_domain_name_size", default=64, min=32, max=255, pow2=True) feature("rpl_relations_buffer_count", default=16, min=1, max=128, pow2=True) feature("rpl_parents_buffer_count", default=8, min=2, max=32, pow2=True) diff --git a/src/lib.rs b/src/lib.rs index 2e99c9576..a6a10edc4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -136,6 +136,7 @@ pub mod config { pub const DNS_MAX_NAME_SIZE: usize = 255; pub const DNS_MAX_RESULT_COUNT: usize = 1; pub const DNS_MAX_SERVER_COUNT: usize = 1; + pub const DHCP_MAX_DOMAIN_NAME_SIZE: usize = 64; pub const FRAGMENTATION_BUFFER_SIZE: usize = 1500; pub const IFACE_MAX_ADDR_COUNT: usize = 8; pub const IFACE_MAX_MULTICAST_GROUP_COUNT: usize = 4; diff --git a/src/socket/dhcpv4.rs b/src/socket/dhcpv4.rs index 109386e54..fda4c5783 100644 --- a/src/socket/dhcpv4.rs +++ b/src/socket/dhcpv4.rs @@ -1,13 +1,14 @@ +use core::str::FromStr; #[cfg(feature = "async")] use core::task::Waker; +use crate::config::DHCP_MAX_DOMAIN_NAME_SIZE; use crate::iface::Context; use crate::time::{Duration, Instant}; use crate::wire::dhcpv4::field as dhcpv4_field; use crate::wire::{ DhcpMessageType, DhcpPacket, DhcpRepr, IpAddress, IpProtocol, Ipv4Address, Ipv4Cidr, Ipv4Repr, - UdpRepr, DHCP_CLIENT_PORT, DHCP_MAX_DNS_SERVER_COUNT, DHCP_MAX_DOMAIN_NAME_LEN, - DHCP_SERVER_PORT, UDP_HEADER_LEN, + UdpRepr, DHCP_CLIENT_PORT, DHCP_MAX_DNS_SERVER_COUNT, DHCP_SERVER_PORT, UDP_HEADER_LEN, }; use crate::wire::{DhcpOption, HardwareAddress}; use heapless::{String, Vec}; @@ -41,7 +42,7 @@ pub struct Config<'a> { /// DNS servers pub dns_servers: Vec, /// Domain name - pub domain_name: Option>, + pub domain_name: Option>, /// Received DHCP packet pub packet: Option>, } @@ -498,7 +499,10 @@ impl<'a> Socket<'a> { address: Ipv4Cidr::new(dhcp_repr.your_ip, prefix_len), router: dhcp_repr.router, dns_servers, - domain_name: dhcp_repr.domain_name.clone(), + domain_name: dhcp_repr + .domain_name + .map(String::from_str) + .and_then(Result::ok), packet: None, }; @@ -1004,7 +1008,7 @@ mod test { router: Some(SERVER_IP), subnet_mask: Some(MASK_24), dns_servers: Some(Vec::from_slice(DNS_IPS).unwrap()), - domain_name: Some(String::from_str(DOMAIN_NAME).unwrap()), + domain_name: Some(DOMAIN_NAME), lease_duration: Some(1000), ..DHCP_DEFAULT @@ -1032,7 +1036,7 @@ mod test { router: Some(SERVER_IP), subnet_mask: Some(MASK_24), dns_servers: Some(Vec::from_slice(DNS_IPS).unwrap()), - domain_name: Some(String::from_str(DOMAIN_NAME).unwrap()), + domain_name: Some(DOMAIN_NAME), lease_duration: Some(1000), ..DHCP_DEFAULT diff --git a/src/wire/dhcpv4.rs b/src/wire/dhcpv4.rs index 3b7ff331c..8ac5a8d3a 100644 --- a/src/wire/dhcpv4.rs +++ b/src/wire/dhcpv4.rs @@ -3,7 +3,7 @@ use bitflags::bitflags; use byteorder::{ByteOrder, NetworkEndian}; use core::iter; -use heapless::{String, Vec}; +use heapless::Vec; use super::{Error, Result}; use crate::wire::arp::Hardware; @@ -649,7 +649,7 @@ pub struct Repr<'a> { /// DNS servers pub dns_servers: Option>, /// Domain name - pub domain_name: Option>, + pub domain_name: Option<&'a str>, /// The maximum size dhcp packet the interface can receive pub max_size: Option, /// The DHCP IP lease duration, specified in seconds. @@ -697,7 +697,7 @@ impl<'a> Repr<'a> { } if let Some(domain_name) = &self.domain_name { len += 2; - len += domain_name.len(); + len += domain_name.as_bytes().len(); } if let Some(list) = self.parameter_request_list { len += list.len() + 2; @@ -811,13 +811,8 @@ impl<'a> Repr<'a> { } } (field::OPT_DOMAIN_NAME, _) => { - let mut name = String::new(); - - if let Ok(n) = core::str::from_utf8(data) { - if data.len() <= MAX_DOMAIN_NAME_LEN { - name.push_str(n).ok(); - domain_name = Some(name); - } + if let Ok(name) = core::str::from_utf8(data) { + domain_name = Some(name); } } _ => {}