@@ -9,6 +9,7 @@ use crate::wire::{
99 UdpRepr , DHCP_CLIENT_PORT , DHCP_MAX_DNS_SERVER_COUNT , DHCP_SERVER_PORT , UDP_HEADER_LEN ,
1010} ;
1111use crate :: wire:: { DhcpOption , HardwareAddress } ;
12+ use heapless:: Vec ;
1213
1314#[ cfg( feature = "async" ) ]
1415use 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 ) ) ]
2930pub 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,19 @@ 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+ . filter ( |s| s. is_unicast ( ) )
428+ . for_each ( |a| {
429+ // This will never produce an error, as both the arrays and `dns_servers`
430+ // have length DHCP_MAX_DNS_SERVER_COUNT
431+ dns_servers. push ( * a) . ok ( ) ;
432+ } ) ;
433+
431434 let config = Config {
432435 server,
433436 address : Ipv4Cidr :: new ( dhcp_repr. your_ip , prefix_len) ,
@@ -627,7 +630,7 @@ impl<'a> Socket<'a> {
627630 server : state. config . server ,
628631 address : state. config . address ,
629632 router : state. config . router ,
630- dns_servers : state. config . dns_servers ,
633+ dns_servers : state. config . dns_servers . clone ( ) ,
631634 packet : self
632635 . receive_packet_buffer
633636 . as_deref ( )
@@ -775,8 +778,8 @@ mod test {
775778 const DNS_IP_1 : Ipv4Address = Ipv4Address ( [ 1 , 1 , 1 , 1 ] ) ;
776779 const DNS_IP_2 : Ipv4Address = Ipv4Address ( [ 1 , 1 , 1 , 2 ] ) ;
777780 const DNS_IP_3 : Ipv4Address = Ipv4Address ( [ 1 , 1 , 1 , 3 ] ) ;
778- const DNS_IPS : [ Option < Ipv4Address > ; DHCP_MAX_DNS_SERVER_COUNT ] =
779- [ Some ( DNS_IP_1 ) , Some ( DNS_IP_2 ) , Some ( DNS_IP_3 ) ] ;
781+ const DNS_IPS : & [ Ipv4Address ] = & [ DNS_IP_1 , DNS_IP_2 , DNS_IP_3 ] ;
782+
780783 const MASK_24 : Ipv4Address = Ipv4Address ( [ 255 , 255 , 255 , 0 ] ) ;
781784
782785 const MY_MAC : EthernetAddress = EthernetAddress ( [ 0x02 , 0x02 , 0x02 , 0x02 , 0x02 , 0x02 ] ) ;
@@ -852,19 +855,21 @@ mod test {
852855 ..DHCP_DEFAULT
853856 } ;
854857
855- const DHCP_OFFER : DhcpRepr = DhcpRepr {
856- message_type : DhcpMessageType :: Offer ,
857- server_ip : SERVER_IP ,
858- server_identifier : Some ( SERVER_IP ) ,
858+ fn dhcp_offer ( ) -> DhcpRepr < ' static > {
859+ DhcpRepr {
860+ message_type : DhcpMessageType :: Offer ,
861+ server_ip : SERVER_IP ,
862+ server_identifier : Some ( SERVER_IP ) ,
859863
860- your_ip : MY_IP ,
861- router : Some ( SERVER_IP ) ,
862- subnet_mask : Some ( MASK_24 ) ,
863- dns_servers : Some ( DNS_IPS ) ,
864- lease_duration : Some ( 1000 ) ,
864+ your_ip : MY_IP ,
865+ router : Some ( SERVER_IP ) ,
866+ subnet_mask : Some ( MASK_24 ) ,
867+ dns_servers : Some ( Vec :: from_slice ( DNS_IPS ) . unwrap ( ) ) ,
868+ lease_duration : Some ( 1000 ) ,
865869
866- ..DHCP_DEFAULT
867- } ;
870+ ..DHCP_DEFAULT
871+ }
872+ }
868873
869874 const DHCP_REQUEST : DhcpRepr = DhcpRepr {
870875 message_type : DhcpMessageType :: Request ,
@@ -877,19 +882,21 @@ mod test {
877882 ..DHCP_DEFAULT
878883 } ;
879884
880- const DHCP_ACK : DhcpRepr = DhcpRepr {
881- message_type : DhcpMessageType :: Ack ,
882- server_ip : SERVER_IP ,
883- server_identifier : Some ( SERVER_IP ) ,
885+ fn dhcp_ack ( ) -> DhcpRepr < ' static > {
886+ DhcpRepr {
887+ message_type : DhcpMessageType :: Ack ,
888+ server_ip : SERVER_IP ,
889+ server_identifier : Some ( SERVER_IP ) ,
884890
885- your_ip : MY_IP ,
886- router : Some ( SERVER_IP ) ,
887- subnet_mask : Some ( MASK_24 ) ,
888- dns_servers : Some ( DNS_IPS ) ,
889- lease_duration : Some ( 1000 ) ,
891+ your_ip : MY_IP ,
892+ router : Some ( SERVER_IP ) ,
893+ subnet_mask : Some ( MASK_24 ) ,
894+ dns_servers : Some ( Vec :: from_slice ( DNS_IPS ) . unwrap ( ) ) ,
895+ lease_duration : Some ( 1000 ) ,
890896
891- ..DHCP_DEFAULT
892- } ;
897+ ..DHCP_DEFAULT
898+ }
899+ }
893900
894901 const DHCP_NAK : DhcpRepr = DhcpRepr {
895902 message_type : DhcpMessageType :: Nak ,
@@ -931,7 +938,7 @@ mod test {
931938 identifier : SERVER_IP ,
932939 } ,
933940 address : Ipv4Cidr :: new ( MY_IP , 24 ) ,
934- dns_servers : DNS_IPS ,
941+ dns_servers : Vec :: from_slice ( DNS_IPS ) . unwrap ( ) ,
935942 router : Some ( SERVER_IP ) ,
936943 packet : None ,
937944 } ,
@@ -948,11 +955,11 @@ mod test {
948955
949956 recv ! ( s, [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
950957 assert_eq ! ( s. poll( ) , None ) ;
951- send ! ( s, ( IP_RECV , UDP_RECV , DHCP_OFFER ) ) ;
958+ send ! ( s, ( IP_RECV , UDP_RECV , dhcp_offer ( ) ) ) ;
952959 assert_eq ! ( s. poll( ) , None ) ;
953960 recv ! ( s, [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
954961 assert_eq ! ( s. poll( ) , None ) ;
955- send ! ( s, ( IP_RECV , UDP_RECV , DHCP_ACK ) ) ;
962+ send ! ( s, ( IP_RECV , UDP_RECV , dhcp_ack ( ) ) ) ;
956963
957964 assert_eq ! (
958965 s. poll( ) ,
@@ -962,7 +969,7 @@ mod test {
962969 identifier: SERVER_IP ,
963970 } ,
964971 address: Ipv4Cidr :: new( MY_IP , 24 ) ,
965- dns_servers: DNS_IPS ,
972+ dns_servers: Vec :: from_slice ( DNS_IPS ) . unwrap ( ) ,
966973 router: Some ( SERVER_IP ) ,
967974 packet: None ,
968975 } ) )
@@ -988,7 +995,7 @@ mod test {
988995 recv ! ( s, time 20_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
989996
990997 // check after retransmits it still works
991- send ! ( s, time 20_000 , ( IP_RECV , UDP_RECV , DHCP_OFFER ) ) ;
998+ send ! ( s, time 20_000 , ( IP_RECV , UDP_RECV , dhcp_offer ( ) ) ) ;
992999 recv ! ( s, time 20_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
9931000 }
9941001
@@ -997,7 +1004,7 @@ mod test {
9971004 let mut s = socket ( ) ;
9981005
9991006 recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
1000- send ! ( s, time 0 , ( IP_RECV , UDP_RECV , DHCP_OFFER ) ) ;
1007+ send ! ( s, time 0 , ( IP_RECV , UDP_RECV , dhcp_offer ( ) ) ) ;
10011008 recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
10021009 recv ! ( s, time 1_000 , [ ] ) ;
10031010 recv ! ( s, time 5_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
@@ -1007,7 +1014,7 @@ mod test {
10071014 recv ! ( s, time 20_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
10081015
10091016 // check after retransmits it still works
1010- send ! ( s, time 20_000 , ( IP_RECV , UDP_RECV , DHCP_ACK ) ) ;
1017+ send ! ( s, time 20_000 , ( IP_RECV , UDP_RECV , dhcp_ack ( ) ) ) ;
10111018
10121019 match & s. state {
10131020 ClientState :: Renewing ( r) => {
@@ -1023,7 +1030,7 @@ mod test {
10231030 let mut s = socket ( ) ;
10241031
10251032 recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
1026- send ! ( s, time 0 , ( IP_RECV , UDP_RECV , DHCP_OFFER ) ) ;
1033+ send ! ( s, time 0 , ( IP_RECV , UDP_RECV , dhcp_offer ( ) ) ) ;
10271034 recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
10281035 recv ! ( s, time 5_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
10291036 recv ! ( s, time 10_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
@@ -1035,7 +1042,7 @@ mod test {
10351042 recv ! ( s, time 70_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
10361043
10371044 // check it still works
1038- send ! ( s, time 60_000 , ( IP_RECV , UDP_RECV , DHCP_OFFER ) ) ;
1045+ send ! ( s, time 60_000 , ( IP_RECV , UDP_RECV , dhcp_offer ( ) ) ) ;
10391046 recv ! ( s, time 60_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
10401047 }
10411048
@@ -1044,7 +1051,7 @@ mod test {
10441051 let mut s = socket ( ) ;
10451052
10461053 recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
1047- send ! ( s, time 0 , ( IP_RECV , UDP_RECV , DHCP_OFFER ) ) ;
1054+ send ! ( s, time 0 , ( IP_RECV , UDP_RECV , dhcp_offer ( ) ) ) ;
10481055 recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
10491056 send ! ( s, time 0 , ( IP_SERVER_BROADCAST , UDP_RECV , DHCP_NAK ) ) ;
10501057 recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
@@ -1068,7 +1075,7 @@ mod test {
10681075 _ => panic ! ( "Invalid state" ) ,
10691076 }
10701077
1071- send ! ( s, time 500_000 , ( IP_RECV , UDP_RECV , DHCP_ACK ) ) ;
1078+ send ! ( s, time 500_000 , ( IP_RECV , UDP_RECV , dhcp_ack ( ) ) ) ;
10721079 assert_eq ! ( s. poll( ) , None ) ;
10731080
10741081 match & s. state {
@@ -1093,7 +1100,7 @@ mod test {
10931100 recv ! ( s, time 875_000 , [ ( IP_SEND , UDP_SEND , DHCP_RENEW ) ] ) ;
10941101
10951102 // check it still works
1096- send ! ( s, time 875_000 , ( IP_RECV , UDP_RECV , DHCP_ACK ) ) ;
1103+ send ! ( s, time 875_000 , ( IP_RECV , UDP_RECV , dhcp_ack ( ) ) ) ;
10971104 match & s. state {
10981105 ClientState :: Renewing ( r) => {
10991106 // NOW the expiration gets bumped
0 commit comments