Skip to content

Commit

Permalink
Make domain name length settable, use reference in DhcpRepr
Browse files Browse the repository at this point in the history
  • Loading branch information
korken89 committed Aug 9, 2024
1 parent cf5b0a7 commit e4a6d20
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions gen_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions src/socket/dhcpv4.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -41,7 +42,7 @@ pub struct Config<'a> {
/// DNS servers
pub dns_servers: Vec<Ipv4Address, DHCP_MAX_DNS_SERVER_COUNT>,
/// Domain name
pub domain_name: Option<String<DHCP_MAX_DOMAIN_NAME_LEN>>,
pub domain_name: Option<String<DHCP_MAX_DOMAIN_NAME_SIZE>>,
/// Received DHCP packet
pub packet: Option<DhcpPacket<&'a [u8]>>,
}
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions src/wire/dhcpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -649,7 +649,7 @@ pub struct Repr<'a> {
/// DNS servers
pub dns_servers: Option<Vec<Ipv4Address, MAX_DNS_SERVER_COUNT>>,
/// Domain name
pub domain_name: Option<String<MAX_DOMAIN_NAME_LEN>>,
pub domain_name: Option<&'a str>,
/// The maximum size dhcp packet the interface can receive
pub max_size: Option<u16>,
/// The DHCP IP lease duration, specified in seconds.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
_ => {}
Expand Down

0 comments on commit e4a6d20

Please sign in to comment.