Skip to content

Commit 03e830b

Browse files
author
Johannes Draaijer
committed
DHCPv4: use a Vec to store DNS server addresses instead of an array of options
1 parent 6b8b667 commit 03e830b

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

examples/dhcp_client.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ fn main() {
7878
}
7979

8080
for (i, s) in config.dns_servers.iter().enumerate() {
81-
if let Some(s) = s {
82-
debug!("DNS server {}: {}", i, s);
83-
}
81+
debug!("DNS server {}: {}", i, s);
8482
}
8583
}
8684
Some(dhcpv4::Event::Deconfigured) => {

src/socket/dhcpv4.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::wire::{
99
UdpRepr, DHCP_CLIENT_PORT, DHCP_MAX_DNS_SERVER_COUNT, DHCP_SERVER_PORT, UDP_HEADER_LEN,
1010
};
1111
use crate::wire::{DhcpOption, HardwareAddress};
12+
use heapless::Vec;
1213

1314
#[cfg(feature = "async")]
1415
use super::WakerRegistration;
@@ -24,7 +25,7 @@ const DEFAULT_PARAMETER_REQUEST_LIST: &[u8] = &[
2425
];
2526

2627
/// IPv4 configuration data provided by the DHCP server.
27-
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
28+
#[derive(Debug, Eq, PartialEq, Clone)]
2829
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2930
pub struct Config<'a> {
3031
/// Information on how to reach the DHCP server that responded with DHCP
@@ -36,7 +37,7 @@ pub struct Config<'a> {
3637
/// match the DHCP server's address.
3738
pub router: Option<Ipv4Address>,
3839
/// DNS servers
39-
pub dns_servers: [Option<Ipv4Address>; DHCP_MAX_DNS_SERVER_COUNT],
40+
pub dns_servers: Vec<Ipv4Address, DHCP_MAX_DNS_SERVER_COUNT>,
4041
/// Received DHCP packet
4142
pub packet: Option<DhcpPacket<&'a [u8]>>,
4243
}
@@ -417,17 +418,20 @@ impl<'a> Socket<'a> {
417418

418419
// Cleanup the DNS servers list, keeping only unicasts/
419420
// TP-Link TD-W8970 sends 0.0.0.0 as second DNS server if there's only one configured :(
420-
let mut dns_servers = [None; DHCP_MAX_DNS_SERVER_COUNT];
421-
if let Some(received) = dhcp_repr.dns_servers {
422-
let mut i = 0;
423-
for addr in received.iter().flatten() {
424-
if addr.is_unicast() {
425-
// This can never be out-of-bounds since both arrays have length DHCP_MAX_DNS_SERVER_COUNT
426-
dns_servers[i] = Some(*addr);
427-
i += 1;
428-
}
429-
}
430-
}
421+
let mut dns_servers = Vec::new();
422+
423+
dhcp_repr
424+
.dns_servers
425+
.iter()
426+
.flatten()
427+
.flatten()
428+
.filter(|s| s.is_unicast())
429+
.for_each(|a| {
430+
// This will never produce an error, as both the arrays and `dns_servers`
431+
// have length DHCP_MAX_DNS_SERVER_COUNT
432+
dns_servers.push(*a).ok();
433+
});
434+
431435
let config = Config {
432436
server,
433437
address: Ipv4Cidr::new(dhcp_repr.your_ip, prefix_len),
@@ -627,7 +631,7 @@ impl<'a> Socket<'a> {
627631
server: state.config.server,
628632
address: state.config.address,
629633
router: state.config.router,
630-
dns_servers: state.config.dns_servers,
634+
dns_servers: state.config.dns_servers.clone(),
631635
packet: self
632636
.receive_packet_buffer
633637
.as_deref()
@@ -775,8 +779,10 @@ mod test {
775779
const DNS_IP_1: Ipv4Address = Ipv4Address([1, 1, 1, 1]);
776780
const DNS_IP_2: Ipv4Address = Ipv4Address([1, 1, 1, 2]);
777781
const DNS_IP_3: Ipv4Address = Ipv4Address([1, 1, 1, 3]);
778-
const DNS_IPS: [Option<Ipv4Address>; DHCP_MAX_DNS_SERVER_COUNT] =
782+
const DNS_IPS_ARR: [Option<Ipv4Address>; DHCP_MAX_DNS_SERVER_COUNT] =
779783
[Some(DNS_IP_1), Some(DNS_IP_2), Some(DNS_IP_3)];
784+
const DNS_IPS: &[Ipv4Address] = &[DNS_IP_1, DNS_IP_2, DNS_IP_3];
785+
780786
const MASK_24: Ipv4Address = Ipv4Address([255, 255, 255, 0]);
781787

782788
const MY_MAC: EthernetAddress = EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]);
@@ -860,7 +866,7 @@ mod test {
860866
your_ip: MY_IP,
861867
router: Some(SERVER_IP),
862868
subnet_mask: Some(MASK_24),
863-
dns_servers: Some(DNS_IPS),
869+
dns_servers: Some(DNS_IPS_ARR),
864870
lease_duration: Some(1000),
865871

866872
..DHCP_DEFAULT
@@ -885,7 +891,7 @@ mod test {
885891
your_ip: MY_IP,
886892
router: Some(SERVER_IP),
887893
subnet_mask: Some(MASK_24),
888-
dns_servers: Some(DNS_IPS),
894+
dns_servers: Some(DNS_IPS_ARR),
889895
lease_duration: Some(1000),
890896

891897
..DHCP_DEFAULT
@@ -931,7 +937,7 @@ mod test {
931937
identifier: SERVER_IP,
932938
},
933939
address: Ipv4Cidr::new(MY_IP, 24),
934-
dns_servers: DNS_IPS,
940+
dns_servers: Vec::from_slice(DNS_IPS).unwrap(),
935941
router: Some(SERVER_IP),
936942
packet: None,
937943
},
@@ -962,7 +968,7 @@ mod test {
962968
identifier: SERVER_IP,
963969
},
964970
address: Ipv4Cidr::new(MY_IP, 24),
965-
dns_servers: DNS_IPS,
971+
dns_servers: Vec::from_slice(DNS_IPS).unwrap(),
966972
router: Some(SERVER_IP),
967973
packet: None,
968974
}))

0 commit comments

Comments
 (0)