@@ -9,6 +9,7 @@ use crate::wire::{
9
9
UdpRepr , DHCP_CLIENT_PORT , DHCP_MAX_DNS_SERVER_COUNT , DHCP_SERVER_PORT , UDP_HEADER_LEN ,
10
10
} ;
11
11
use crate :: wire:: { DhcpOption , HardwareAddress } ;
12
+ use heapless:: Vec ;
12
13
13
14
#[ cfg( feature = "async" ) ]
14
15
use super :: WakerRegistration ;
@@ -24,7 +25,7 @@ const DEFAULT_PARAMETER_REQUEST_LIST: &[u8] = &[
24
25
] ;
25
26
26
27
/// IPv4 configuration data provided by the DHCP server.
27
- #[ derive( Debug , Eq , PartialEq , Clone , Copy ) ]
28
+ #[ derive( Debug , Eq , PartialEq , Clone ) ]
28
29
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
29
30
pub struct Config < ' a > {
30
31
/// Information on how to reach the DHCP server that responded with DHCP
@@ -36,7 +37,7 @@ pub struct Config<'a> {
36
37
/// match the DHCP server's address.
37
38
pub router : Option < Ipv4Address > ,
38
39
/// DNS servers
39
- pub dns_servers : [ Option < Ipv4Address > ; DHCP_MAX_DNS_SERVER_COUNT ] ,
40
+ pub dns_servers : Vec < Ipv4Address , DHCP_MAX_DNS_SERVER_COUNT > ,
40
41
/// Received DHCP packet
41
42
pub packet : Option < DhcpPacket < & ' a [ u8 ] > > ,
42
43
}
@@ -417,17 +418,19 @@ impl<'a> Socket<'a> {
417
418
418
419
// Cleanup the DNS servers list, keeping only unicasts/
419
420
// 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
+
431
434
let config = Config {
432
435
server,
433
436
address : Ipv4Cidr :: new ( dhcp_repr. your_ip , prefix_len) ,
@@ -627,7 +630,7 @@ impl<'a> Socket<'a> {
627
630
server : state. config . server ,
628
631
address : state. config . address ,
629
632
router : state. config . router ,
630
- dns_servers : state. config . dns_servers ,
633
+ dns_servers : state. config . dns_servers . clone ( ) ,
631
634
packet : self
632
635
. receive_packet_buffer
633
636
. as_deref ( )
@@ -775,8 +778,8 @@ mod test {
775
778
const DNS_IP_1 : Ipv4Address = Ipv4Address ( [ 1 , 1 , 1 , 1 ] ) ;
776
779
const DNS_IP_2 : Ipv4Address = Ipv4Address ( [ 1 , 1 , 1 , 2 ] ) ;
777
780
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
+
780
783
const MASK_24 : Ipv4Address = Ipv4Address ( [ 255 , 255 , 255 , 0 ] ) ;
781
784
782
785
const MY_MAC : EthernetAddress = EthernetAddress ( [ 0x02 , 0x02 , 0x02 , 0x02 , 0x02 , 0x02 ] ) ;
@@ -852,19 +855,21 @@ mod test {
852
855
..DHCP_DEFAULT
853
856
} ;
854
857
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 ) ,
859
863
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 ) ,
865
869
866
- ..DHCP_DEFAULT
867
- } ;
870
+ ..DHCP_DEFAULT
871
+ }
872
+ }
868
873
869
874
const DHCP_REQUEST : DhcpRepr = DhcpRepr {
870
875
message_type : DhcpMessageType :: Request ,
@@ -877,19 +882,21 @@ mod test {
877
882
..DHCP_DEFAULT
878
883
} ;
879
884
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 ) ,
884
890
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 ) ,
890
896
891
- ..DHCP_DEFAULT
892
- } ;
897
+ ..DHCP_DEFAULT
898
+ }
899
+ }
893
900
894
901
const DHCP_NAK : DhcpRepr = DhcpRepr {
895
902
message_type : DhcpMessageType :: Nak ,
@@ -931,7 +938,7 @@ mod test {
931
938
identifier : SERVER_IP ,
932
939
} ,
933
940
address : Ipv4Cidr :: new ( MY_IP , 24 ) ,
934
- dns_servers : DNS_IPS ,
941
+ dns_servers : Vec :: from_slice ( DNS_IPS ) . unwrap ( ) ,
935
942
router : Some ( SERVER_IP ) ,
936
943
packet : None ,
937
944
} ,
@@ -948,11 +955,11 @@ mod test {
948
955
949
956
recv ! ( s, [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
950
957
assert_eq ! ( s. poll( ) , None ) ;
951
- send ! ( s, ( IP_RECV , UDP_RECV , DHCP_OFFER ) ) ;
958
+ send ! ( s, ( IP_RECV , UDP_RECV , dhcp_offer ( ) ) ) ;
952
959
assert_eq ! ( s. poll( ) , None ) ;
953
960
recv ! ( s, [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
954
961
assert_eq ! ( s. poll( ) , None ) ;
955
- send ! ( s, ( IP_RECV , UDP_RECV , DHCP_ACK ) ) ;
962
+ send ! ( s, ( IP_RECV , UDP_RECV , dhcp_ack ( ) ) ) ;
956
963
957
964
assert_eq ! (
958
965
s. poll( ) ,
@@ -962,7 +969,7 @@ mod test {
962
969
identifier: SERVER_IP ,
963
970
} ,
964
971
address: Ipv4Cidr :: new( MY_IP , 24 ) ,
965
- dns_servers: DNS_IPS ,
972
+ dns_servers: Vec :: from_slice ( DNS_IPS ) . unwrap ( ) ,
966
973
router: Some ( SERVER_IP ) ,
967
974
packet: None ,
968
975
} ) )
@@ -988,7 +995,7 @@ mod test {
988
995
recv ! ( s, time 20_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
989
996
990
997
// 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 ( ) ) ) ;
992
999
recv ! ( s, time 20_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
993
1000
}
994
1001
@@ -997,7 +1004,7 @@ mod test {
997
1004
let mut s = socket ( ) ;
998
1005
999
1006
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 ( ) ) ) ;
1001
1008
recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
1002
1009
recv ! ( s, time 1_000 , [ ] ) ;
1003
1010
recv ! ( s, time 5_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
@@ -1007,7 +1014,7 @@ mod test {
1007
1014
recv ! ( s, time 20_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
1008
1015
1009
1016
// 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 ( ) ) ) ;
1011
1018
1012
1019
match & s. state {
1013
1020
ClientState :: Renewing ( r) => {
@@ -1023,7 +1030,7 @@ mod test {
1023
1030
let mut s = socket ( ) ;
1024
1031
1025
1032
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 ( ) ) ) ;
1027
1034
recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
1028
1035
recv ! ( s, time 5_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
1029
1036
recv ! ( s, time 10_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
@@ -1035,7 +1042,7 @@ mod test {
1035
1042
recv ! ( s, time 70_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
1036
1043
1037
1044
// 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 ( ) ) ) ;
1039
1046
recv ! ( s, time 60_000 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
1040
1047
}
1041
1048
@@ -1044,7 +1051,7 @@ mod test {
1044
1051
let mut s = socket ( ) ;
1045
1052
1046
1053
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 ( ) ) ) ;
1048
1055
recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_REQUEST ) ] ) ;
1049
1056
send ! ( s, time 0 , ( IP_SERVER_BROADCAST , UDP_RECV , DHCP_NAK ) ) ;
1050
1057
recv ! ( s, time 0 , [ ( IP_BROADCAST , UDP_SEND , DHCP_DISCOVER ) ] ) ;
@@ -1068,7 +1075,7 @@ mod test {
1068
1075
_ => panic ! ( "Invalid state" ) ,
1069
1076
}
1070
1077
1071
- send ! ( s, time 500_000 , ( IP_RECV , UDP_RECV , DHCP_ACK ) ) ;
1078
+ send ! ( s, time 500_000 , ( IP_RECV , UDP_RECV , dhcp_ack ( ) ) ) ;
1072
1079
assert_eq ! ( s. poll( ) , None ) ;
1073
1080
1074
1081
match & s. state {
@@ -1093,7 +1100,7 @@ mod test {
1093
1100
recv ! ( s, time 875_000 , [ ( IP_SEND , UDP_SEND , DHCP_RENEW ) ] ) ;
1094
1101
1095
1102
// 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 ( ) ) ) ;
1097
1104
match & s. state {
1098
1105
ClientState :: Renewing ( r) => {
1099
1106
// NOW the expiration gets bumped
0 commit comments