1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Parsing and serialization of DHCP messages

use dhcp_protocol::{AtLeast, AtMostBytes};
use packet::{InnerPacketBuilder, ParseBuffer as _, Serializer};
use packet_formats::ip::IpPacket as _;
use std::{
    net::Ipv4Addr,
    num::{NonZeroU16, NonZeroU32, TryFromIntError},
};

#[derive(thiserror::Error, Debug)]
pub(crate) enum ParseError {
    #[error("parsing IPv4 packet")]
    Ipv4(packet_formats::error::IpParseError<net_types::ip::Ipv4>),
    #[error("IPv4 packet protocol was not UDP")]
    NotUdp,
    #[error("parsing UDP datagram")]
    Udp(packet_formats::error::ParseError),
    #[error("incoming packet destined for wrong port")]
    WrongPort(NonZeroU16),
    #[error("incoming packet has wrong source address")]
    WrongSource(std::net::SocketAddr),
    #[error("parsing DHCP message")]
    Dhcp(dhcp_protocol::ProtocolError),
}

/// Parses a DHCP message from the bytes of an IP packet. This function does not
/// expect to parse a packet with link-layer headers; the buffer may only
/// include bytes for the IP layer and above.
/// NOTE: does not handle IP fragmentation.
pub(crate) fn parse_dhcp_message_from_ip_packet(
    mut bytes: &[u8],
    expected_dst_port: NonZeroU16,
) -> Result<(net_types::ip::Ipv4Addr, dhcp_protocol::Message), ParseError> {
    let ip_packet =
        bytes.parse::<packet_formats::ipv4::Ipv4Packet<_>>().map_err(ParseError::Ipv4)?;

    let src_addr = ip_packet.src_ip();

    match ip_packet.proto() {
        packet_formats::ip::Ipv4Proto::Proto(packet_formats::ip::IpProto::Udp) => (),
        packet_formats::ip::Ipv4Proto::Proto(packet_formats::ip::IpProto::Tcp)
        | packet_formats::ip::Ipv4Proto::Icmp
        | packet_formats::ip::Ipv4Proto::Igmp
        | packet_formats::ip::Ipv4Proto::Other(_) => return Err(ParseError::NotUdp),
    };
    let mut ip_packet_body = ip_packet.body();

    let udp_packet = ip_packet_body
        .parse_with::<_, packet_formats::udp::UdpPacket<_>>(packet_formats::udp::UdpParseArgs::new(
            ip_packet.src_ip(),
            ip_packet.dst_ip(),
        ))
        .map_err(ParseError::Udp)?;
    let dst_port = udp_packet.dst_port();
    if dst_port != expected_dst_port {
        return Err(ParseError::WrongPort(dst_port));
    }
    dhcp_protocol::Message::from_buffer(udp_packet.body())
        .map(|msg| (src_addr, msg))
        .map_err(ParseError::Dhcp)
}

const DEFAULT_TTL: u8 = 64;

/// Serializes a DHCP message to the bytes of an IP packet. Includes IP header
/// but not link-layer headers.
pub(crate) fn serialize_dhcp_message_to_ip_packet(
    message: dhcp_protocol::Message,
    src_ip: impl Into<net_types::ip::Ipv4Addr>,
    src_port: NonZeroU16,
    dst_ip: impl Into<net_types::ip::Ipv4Addr>,
    dst_port: NonZeroU16,
) -> impl AsRef<[u8]> {
    let message = message.serialize();
    let src_ip = src_ip.into();
    let dst_ip = dst_ip.into();

    let udp_builder =
        packet_formats::udp::UdpPacketBuilder::new(src_ip, dst_ip, Some(src_port), dst_port);

    let ipv4_builder = packet_formats::ipv4::Ipv4PacketBuilder::new(
        src_ip,
        dst_ip,
        DEFAULT_TTL,
        packet_formats::ip::Ipv4Proto::Proto(packet_formats::ip::IpProto::Udp),
    );

    match message
        .into_serializer()
        .encapsulate(udp_builder)
        .encapsulate(ipv4_builder)
        .serialize_vec_outer()
    {
        Ok(buf) => buf,
        Err(e) => {
            let (e, _serializer) = e;
            match e {
                packet::SerializeError::Alloc(infallible) => match infallible {},
                packet::SerializeError::SizeLimitExceeded => {
                    unreachable!("no MTU constraints on serializer")
                }
            }
        }
    }
}

#[derive(derive_builder::Builder, Debug, PartialEq)]
#[builder(private, build_fn(error = "CommonIncomingMessageError"))]
struct CommonIncomingMessageFields {
    message_type: dhcp_protocol::MessageType,
    #[builder(setter(custom), default)]
    server_identifier: Option<net_types::SpecifiedAddr<net_types::ip::Ipv4Addr>>,
    #[builder(setter(custom), default)]
    yiaddr: Option<net_types::SpecifiedAddr<net_types::ip::Ipv4Addr>>,
    #[builder(setter(strip_option), default)]
    ip_address_lease_time_secs: Option<NonZeroU32>,
    // While it's nonsensical to have a 0-valued lease time, it's somewhat more
    // reasonable to set the renewal time value to 0 (prompting the client to
    // begin the renewal process immediately).
    #[builder(setter(strip_option), default)]
    renewal_time_value_secs: Option<u32>,
    // Same holds for the rebinding time.
    #[builder(setter(strip_option), default)]
    rebinding_time_value_secs: Option<u32>,
    #[builder(default)]
    parameters: Vec<dhcp_protocol::DhcpOption>,
    #[builder(setter(strip_option), default)]
    message: Option<String>,
    #[builder(setter(strip_option), default)]
    client_identifier: Option<AtLeast<2, AtMostBytes<{ dhcp_protocol::U8_MAX_AS_USIZE }, Vec<u8>>>>,
    #[builder(setter(custom))]
    seen_option_codes: OptionCodeSet,
}

#[derive(thiserror::Error, Debug, PartialEq)]
pub(crate) enum CommonIncomingMessageError {
    #[error("got op = {0}, want op = BOOTREPLY")]
    NotBootReply(dhcp_protocol::OpCode),
    #[error("server identifier was the unspecified address")]
    UnspecifiedServerIdentifier,
    #[error("missing: {0}")]
    BuilderMissingField(&'static str),
    #[error("duplicate option: {0:?}")]
    DuplicateOption(dhcp_protocol::OptionCode),
    #[error("option's inclusion violates protocol: {0:?}")]
    IllegallyIncludedOption(dhcp_protocol::OptionCode),
}

impl From<derive_builder::UninitializedFieldError> for CommonIncomingMessageError {
    fn from(value: derive_builder::UninitializedFieldError) -> Self {
        // `derive_builder::UninitializedFieldError` cannot be destructured
        // because its fields are private.
        Self::BuilderMissingField(value.field_name())
    }
}

impl CommonIncomingMessageFieldsBuilder {
    fn ignore_unused_result(&mut self) {}

    fn add_requested_parameter(&mut self, option: dhcp_protocol::DhcpOption) {
        let parameters = self.parameters.get_or_insert_with(Default::default);
        parameters.push(option)
    }

    fn add_seen_option_and_return_whether_newly_added(
        &mut self,
        option_code: dhcp_protocol::OptionCode,
    ) -> bool {
        self.seen_option_codes.get_or_insert_with(Default::default).insert(option_code)
    }

    fn server_identifier(&mut self, addr: Ipv4Addr) -> Result<(), CommonIncomingMessageError> {
        self.server_identifier = Some(Some(
            net_types::SpecifiedAddr::new(net_types::ip::Ipv4Addr::from(addr))
                .ok_or(CommonIncomingMessageError::UnspecifiedServerIdentifier)?,
        ));
        Ok(())
    }

    fn yiaddr(&mut self, addr: Ipv4Addr) {
        match net_types::SpecifiedAddr::new(net_types::ip::Ipv4Addr::from(addr)) {
            None => {
                // Unlike with the Server Identifier option, it is not an error
                // to set `yiaddr` to the unspecified address, as there is no
                // other way to indicate its absence (it has its own field in
                // the DHCP message rather than appearing in the list of
                // options).
            }
            Some(specified_addr) => {
                self.yiaddr = Some(Some(specified_addr));
            }
        }
    }
}

/// Represents a `Map<OptionCode, T>` as an array of booleans.
#[derive(Clone, PartialEq, Debug)]
pub struct OptionCodeMap<T> {
    inner: [Option<T>; dhcp_protocol::U8_MAX_AS_USIZE],
}

impl<T: Copy> OptionCodeMap<T> {
    /// Constructs an empty `OptionCodeMap`.
    pub fn new() -> Self {
        OptionCodeMap { inner: [None; dhcp_protocol::U8_MAX_AS_USIZE] }
    }

    /// Puts `(option_code, value)` into the map, returning the previously-associated
    /// value if is one.
    pub fn put(&mut self, option_code: dhcp_protocol::OptionCode, value: T) -> Option<T> {
        std::mem::replace(&mut self.inner[usize::from(u8::from(option_code))], Some(value))
    }

    /// Gets the value associated with `option_code` from the map, if there is one.
    pub fn get(&self, option_code: dhcp_protocol::OptionCode) -> Option<T> {
        self.inner[usize::from(u8::from(option_code))]
    }

    /// Checks if `option_code` is present in the map.
    pub fn contains(&self, option_code: dhcp_protocol::OptionCode) -> bool {
        self.get(option_code).is_some()
    }

    pub(crate) fn iter(&self) -> impl Iterator<Item = (dhcp_protocol::OptionCode, T)> + '_ {
        self.inner.iter().enumerate().filter_map(|(index, value)| {
            let option_code = u8::try_from(index)
                .ok()
                .and_then(|i| dhcp_protocol::OptionCode::try_from(i).ok())?;
            let value = *value.as_ref()?;
            Some((option_code, value))
        })
    }

    pub(crate) fn iter_keys(&self) -> impl Iterator<Item = dhcp_protocol::OptionCode> + '_ {
        self.iter().map(|(key, _)| key)
    }
}

impl<V: Copy> FromIterator<(dhcp_protocol::OptionCode, V)> for OptionCodeMap<V> {
    fn from_iter<T: IntoIterator<Item = (dhcp_protocol::OptionCode, V)>>(iter: T) -> Self {
        let mut map = Self::new();
        for (option_code, value) in iter {
            let _: Option<_> = map.put(option_code, value);
        }
        map
    }
}

impl<T: Copy> Default for OptionCodeMap<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl OptionCodeMap<OptionRequested> {
    fn iter_required(&self) -> impl Iterator<Item = dhcp_protocol::OptionCode> + '_ {
        self.iter().filter_map(|(key, val)| match val {
            OptionRequested::Required => Some(key),
            OptionRequested::Optional => None,
        })
    }

    /// Converts `self` into the representation required for
    /// `DhcpOption::ParameterRequestList`.
    ///
    /// Returns None if `self` is empty.
    pub(crate) fn try_to_parameter_request_list(
        &self,
    ) -> Option<
        AtLeast<1, AtMostBytes<{ dhcp_protocol::U8_MAX_AS_USIZE }, Vec<dhcp_protocol::OptionCode>>>,
    > {
        match AtLeast::try_from(self.iter_keys().collect::<Vec<_>>()) {
            Ok(parameters) => Some(parameters),
            Err((dhcp_protocol::SizeConstrainedError::SizeConstraintViolated, parameters)) => {
                // This can only have happened because parameters is empty.
                assert_eq!(parameters, Vec::new());
                // Thus, we must omit the ParameterRequestList option.
                None
            }
        }
    }
}

/// Represents a set of OptionCodes as an array of booleans.
pub type OptionCodeSet = OptionCodeMap<()>;

impl OptionCodeSet {
    /// Inserts `option_code` into the set, returning whether it was newly added.
    pub fn insert(&mut self, option_code: dhcp_protocol::OptionCode) -> bool {
        self.put(option_code, ()).is_none()
    }
}

impl FromIterator<dhcp_protocol::OptionCode> for OptionCodeSet {
    fn from_iter<T: IntoIterator<Item = dhcp_protocol::OptionCode>>(iter: T) -> Self {
        let mut set = Self::new();
        for code in iter {
            let _: bool = set.insert(code);
        }
        set
    }
}

/// Denotes whether a requested option is required or optional.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum OptionRequested {
    /// The option is required; incoming DHCPOFFERs and DHCPACKs lacking this
    /// option will be discarded.
    Required,
    /// The option is optional.
    Optional,
}

fn collect_common_fields<T: Copy>(
    requested_parameters: &OptionCodeMap<T>,
    dhcp_protocol::Message {
        op,
        xid: _,
        secs: _,
        bdcast_flag: _,
        ciaddr: _,
        yiaddr,
        siaddr: _,
        giaddr: _,
        chaddr: _,
        sname: _,
        file: _,
        options,
    }: dhcp_protocol::Message,
) -> Result<CommonIncomingMessageFields, CommonIncomingMessageError> {
    use dhcp_protocol::DhcpOption;

    match op {
        dhcp_protocol::OpCode::BOOTREQUEST => {
            return Err(CommonIncomingMessageError::NotBootReply(op))
        }
        dhcp_protocol::OpCode::BOOTREPLY => (),
    };

    let mut builder = CommonIncomingMessageFieldsBuilder::default();
    builder.yiaddr(yiaddr);

    for option in options {
        let newly_seen = builder.add_seen_option_and_return_whether_newly_added(option.code());
        if !newly_seen {
            return Err(CommonIncomingMessageError::DuplicateOption(option.code()));
        }

        // From RFC 2131 section 4.3.1:
        // """
        // Option                    DHCPOFFER    DHCPACK               DHCPNAK
        // ------                    ---------    -------               -------
        // Requested IP address      MUST NOT     MUST NOT              MUST NOT
        // IP address lease time     MUST         MUST (DHCPREQUEST)    MUST NOT
        //                                        MUST NOT (DHCPINFORM)
        // Use 'file'/'sname' fields MAY          MAY                   MUST NOT
        // DHCP message type         DHCPOFFER    DHCPACK               DHCPNAK
        // Parameter request list    MUST NOT     MUST NOT              MUST NOT
        // Message                   SHOULD       SHOULD                SHOULD
        // Client identifier         MUST NOT     MUST NOT              MAY
        // Vendor class identifier   MAY          MAY                   MAY
        // Server identifier         MUST         MUST                  MUST
        // Maximum message size      MUST NOT     MUST NOT              MUST NOT
        // All others                MAY          MAY                   MUST NOT
        //
        //            Table 3:  Fields and options used by DHCP servers
        // """

        match &option {
            DhcpOption::IpAddressLeaseTime(value) => match NonZeroU32::try_from(*value) {
                Err(e) => {
                    let _: TryFromIntError = e;
                    tracing::warn!("dropping 0 lease time");
                }
                Ok(value) => {
                    builder.ip_address_lease_time_secs(value).ignore_unused_result();
                }
            },
            DhcpOption::DhcpMessageType(message_type) => {
                builder.message_type(*message_type).ignore_unused_result()
            }
            DhcpOption::ServerIdentifier(value) => {
                builder.server_identifier(*value)?;
            }
            DhcpOption::Message(message) => builder.message(message.clone()).ignore_unused_result(),
            DhcpOption::RenewalTimeValue(value) => {
                builder.renewal_time_value_secs(*value).ignore_unused_result()
            }
            DhcpOption::RebindingTimeValue(value) => {
                builder.rebinding_time_value_secs(*value).ignore_unused_result()
            }
            DhcpOption::ClientIdentifier(value) => {
                builder.client_identifier(value.clone()).ignore_unused_result();
            }
            DhcpOption::ParameterRequestList(_)
            | DhcpOption::RequestedIpAddress(_)
            | DhcpOption::MaxDhcpMessageSize(_) => {
                return Err(CommonIncomingMessageError::IllegallyIncludedOption(option.code()))
            }
            DhcpOption::Pad()
            | DhcpOption::End()
            | DhcpOption::SubnetMask(_)
            | DhcpOption::TimeOffset(_)
            | DhcpOption::Router(_)
            | DhcpOption::TimeServer(_)
            | DhcpOption::NameServer(_)
            | DhcpOption::DomainNameServer(_)
            | DhcpOption::LogServer(_)
            | DhcpOption::CookieServer(_)
            | DhcpOption::LprServer(_)
            | DhcpOption::ImpressServer(_)
            | DhcpOption::ResourceLocationServer(_)
            | DhcpOption::HostName(_)
            | DhcpOption::BootFileSize(_)
            | DhcpOption::MeritDumpFile(_)
            | DhcpOption::DomainName(_)
            | DhcpOption::SwapServer(_)
            | DhcpOption::RootPath(_)
            | DhcpOption::ExtensionsPath(_)
            | DhcpOption::IpForwarding(_)
            | DhcpOption::NonLocalSourceRouting(_)
            | DhcpOption::PolicyFilter(_)
            | DhcpOption::MaxDatagramReassemblySize(_)
            | DhcpOption::DefaultIpTtl(_)
            | DhcpOption::PathMtuAgingTimeout(_)
            | DhcpOption::PathMtuPlateauTable(_)
            | DhcpOption::InterfaceMtu(_)
            | DhcpOption::AllSubnetsLocal(_)
            | DhcpOption::BroadcastAddress(_)
            | DhcpOption::PerformMaskDiscovery(_)
            | DhcpOption::MaskSupplier(_)
            | DhcpOption::PerformRouterDiscovery(_)
            | DhcpOption::RouterSolicitationAddress(_)
            | DhcpOption::StaticRoute(_)
            | DhcpOption::TrailerEncapsulation(_)
            | DhcpOption::ArpCacheTimeout(_)
            | DhcpOption::EthernetEncapsulation(_)
            | DhcpOption::TcpDefaultTtl(_)
            | DhcpOption::TcpKeepaliveInterval(_)
            | DhcpOption::TcpKeepaliveGarbage(_)
            | DhcpOption::NetworkInformationServiceDomain(_)
            | DhcpOption::NetworkInformationServers(_)
            | DhcpOption::NetworkTimeProtocolServers(_)
            | DhcpOption::VendorSpecificInformation(_)
            | DhcpOption::NetBiosOverTcpipNameServer(_)
            | DhcpOption::NetBiosOverTcpipDatagramDistributionServer(_)
            | DhcpOption::NetBiosOverTcpipNodeType(_)
            | DhcpOption::NetBiosOverTcpipScope(_)
            | DhcpOption::XWindowSystemFontServer(_)
            | DhcpOption::XWindowSystemDisplayManager(_)
            | DhcpOption::NetworkInformationServicePlusDomain(_)
            | DhcpOption::NetworkInformationServicePlusServers(_)
            | DhcpOption::MobileIpHomeAgent(_)
            | DhcpOption::SmtpServer(_)
            | DhcpOption::Pop3Server(_)
            | DhcpOption::NntpServer(_)
            | DhcpOption::DefaultWwwServer(_)
            | DhcpOption::DefaultFingerServer(_)
            | DhcpOption::DefaultIrcServer(_)
            | DhcpOption::StreetTalkServer(_)
            | DhcpOption::StreetTalkDirectoryAssistanceServer(_)
            | DhcpOption::OptionOverload(_)
            | DhcpOption::TftpServerName(_)
            | DhcpOption::BootfileName(_)
            | DhcpOption::VendorClassIdentifier(_) => (),
        };

        if requested_parameters.contains(option.code()) {
            builder.add_requested_parameter(option);
        }
    }
    builder.build()
}

/// Reasons that an incoming DHCP message might be discarded during Selecting
/// state.
#[derive(thiserror::Error, Debug, PartialEq)]
pub(crate) enum SelectingIncomingMessageError {
    #[error("{0}")]
    CommonError(#[from] CommonIncomingMessageError),
    /// Note that `NoServerIdentifier` is intentionally distinct from
    /// `CommonIncomingMessageError::UnspecifiedServerIdentifier`, as the latter
    /// refers to the Server Identifier being explicitly populated as the
    /// unspecified address, rather than simply omitted.
    #[error("no server identifier")]
    NoServerIdentifier,
    #[error("got DHCP message type = {0}, wanted DHCPOFFER")]
    NotDhcpOffer(dhcp_protocol::MessageType),
    #[error("yiaddr was the unspecified address")]
    UnspecifiedYiaddr,
    #[error("missing required option: {0:?}")]
    MissingRequiredOption(dhcp_protocol::OptionCode),
}

/// Extracts the fields from a DHCP message incoming during Selecting state that
/// should be used during Requesting state.
pub(crate) fn fields_to_retain_from_selecting(
    requested_parameters: &OptionCodeMap<OptionRequested>,
    message: dhcp_protocol::Message,
) -> Result<FieldsFromOfferToUseInRequest, SelectingIncomingMessageError> {
    let CommonIncomingMessageFields {
        message_type,
        server_identifier,
        yiaddr,
        ip_address_lease_time_secs,
        renewal_time_value_secs: _,
        rebinding_time_value_secs: _,
        parameters: _,
        seen_option_codes,
        message: _,
        client_identifier: _,
    } = collect_common_fields(requested_parameters, message)?;

    match message_type {
        dhcp_protocol::MessageType::DHCPOFFER => (),
        dhcp_protocol::MessageType::DHCPDISCOVER
        | dhcp_protocol::MessageType::DHCPREQUEST
        | dhcp_protocol::MessageType::DHCPDECLINE
        | dhcp_protocol::MessageType::DHCPACK
        | dhcp_protocol::MessageType::DHCPNAK
        | dhcp_protocol::MessageType::DHCPRELEASE
        | dhcp_protocol::MessageType::DHCPINFORM => {
            return Err(SelectingIncomingMessageError::NotDhcpOffer(message_type))
        }
    };

    if let Some(missing_option_code) =
        requested_parameters.iter_required().find(|code| !seen_option_codes.contains(*code))
    {
        return Err(SelectingIncomingMessageError::MissingRequiredOption(missing_option_code));
    }

    Ok(FieldsFromOfferToUseInRequest {
        server_identifier: server_identifier
            .ok_or(SelectingIncomingMessageError::NoServerIdentifier)?,
        ip_address_lease_time_secs,
        ip_address_to_request: yiaddr.ok_or(SelectingIncomingMessageError::UnspecifiedYiaddr)?,
    })
}

#[derive(Debug, Clone, PartialEq)]
/// Fields from a DHCPOFFER that should be used while building a DHCPREQUEST.
pub(crate) struct FieldsFromOfferToUseInRequest {
    pub(crate) server_identifier: net_types::SpecifiedAddr<net_types::ip::Ipv4Addr>,
    pub(crate) ip_address_lease_time_secs: Option<NonZeroU32>,
    pub(crate) ip_address_to_request: net_types::SpecifiedAddr<net_types::ip::Ipv4Addr>,
}

#[derive(Debug, PartialEq)]
// `ServerIdentifier`` is generic in order to allow for it to be optional for
// DHCPACKs received while in RENEWING state (since we already know the
// identifier of the server we're directly communicating with) but required in
// REBINDING state (because we've broadcast the request and need to record
// which server to send renewal requests to in the future).
pub(crate) enum IncomingResponseToRequest<ServerIdentifier> {
    Ack(FieldsToRetainFromAck<ServerIdentifier>),
    Nak(FieldsToRetainFromNak),
}

/// Reasons that an incoming response to a DHCPREQUEST might be discarded.
#[derive(thiserror::Error, Debug, PartialEq)]
pub(crate) enum IncomingResponseToRequestError {
    #[error("{0}")]
    CommonError(#[from] CommonIncomingMessageError),
    #[error("got DHCP message type = {0}, wanted DHCPACK or DHCPNAK")]
    NotDhcpAckOrNak(dhcp_protocol::MessageType),
    #[error("yiaddr was the unspecified address")]
    UnspecifiedYiaddr,
    #[error("no IP address lease time")]
    NoLeaseTime,
    #[error("no server identifier")]
    NoServerIdentifier,
    #[error("missing required option: {0:?}")]
    MissingRequiredOption(dhcp_protocol::OptionCode),
}

#[derive(Debug, PartialEq)]
pub(crate) struct FieldsToRetainFromAck<ServerIdentifier> {
    pub(crate) yiaddr: net_types::SpecifiedAddr<net_types::ip::Ipv4Addr>,
    pub(crate) server_identifier: ServerIdentifier,
    pub(crate) ip_address_lease_time_secs: NonZeroU32,
    pub(crate) renewal_time_value_secs: Option<u32>,
    pub(crate) rebinding_time_value_secs: Option<u32>,
    pub(crate) parameters: Vec<dhcp_protocol::DhcpOption>,
}

impl<ServerIdentifier> FieldsToRetainFromAck<ServerIdentifier> {
    pub(crate) fn map_server_identifier<T, E>(
        self,
        f: impl FnOnce(ServerIdentifier) -> Result<T, E>,
    ) -> Result<FieldsToRetainFromAck<T>, E> {
        let Self {
            yiaddr,
            server_identifier,
            ip_address_lease_time_secs,
            renewal_time_value_secs,
            rebinding_time_value_secs,
            parameters,
        } = self;
        Ok(FieldsToRetainFromAck {
            yiaddr,
            server_identifier: f(server_identifier)?,
            ip_address_lease_time_secs,
            renewal_time_value_secs,
            rebinding_time_value_secs,
            parameters,
        })
    }
}

#[derive(Debug, PartialEq)]
pub(crate) struct FieldsToRetainFromNak {
    pub(crate) server_identifier: net_types::SpecifiedAddr<net_types::ip::Ipv4Addr>,
    pub(crate) message: Option<String>,
    pub(crate) client_identifier: Option<
        AtLeast<
            { dhcp_protocol::CLIENT_IDENTIFIER_MINIMUM_LENGTH },
            AtMostBytes<{ dhcp_protocol::U8_MAX_AS_USIZE }, Vec<u8>>,
        >,
    >,
}

pub(crate) fn fields_to_retain_from_response_to_request(
    requested_parameters: &OptionCodeMap<OptionRequested>,
    message: dhcp_protocol::Message,
) -> Result<
    IncomingResponseToRequest<
        // Strictly according to RFC 2131, the Server Identifier MUST be included in
        // the DHCPACK. However, we've observed DHCP servers in the field fail to
        // set the Server Identifier, instead expecting the client to remember it
        // from the DHCPOFFER (https://fxbug.dev/42064504). Thus, we treat Server
        // Identifier as optional for DHCPACK.
        Option<net_types::SpecifiedAddr<net_types::ip::Ipv4Addr>>,
    >,
    IncomingResponseToRequestError,
> {
    let CommonIncomingMessageFields {
        message_type,
        server_identifier,
        yiaddr,
        ip_address_lease_time_secs,
        renewal_time_value_secs,
        rebinding_time_value_secs,
        parameters,
        seen_option_codes,
        message,
        client_identifier,
    } = collect_common_fields(requested_parameters, message)?;

    match message_type {
        dhcp_protocol::MessageType::DHCPACK => {
            // Only enforce required parameters for ACKs, since NAKs aren't
            // expected to include any configuration at all.

            if let Some(missing_option_code) =
                requested_parameters.iter_required().find(|code| !seen_option_codes.contains(*code))
            {
                return Err(IncomingResponseToRequestError::MissingRequiredOption(
                    missing_option_code,
                ));
            }
            Ok(IncomingResponseToRequest::Ack(FieldsToRetainFromAck {
                yiaddr: yiaddr.ok_or(IncomingResponseToRequestError::UnspecifiedYiaddr)?,
                server_identifier,
                ip_address_lease_time_secs: ip_address_lease_time_secs
                    .ok_or(IncomingResponseToRequestError::NoLeaseTime)?,
                renewal_time_value_secs,
                rebinding_time_value_secs,
                parameters,
            }))
        }
        dhcp_protocol::MessageType::DHCPNAK => {
            Ok(IncomingResponseToRequest::Nak(FieldsToRetainFromNak {
                server_identifier: server_identifier
                    .ok_or(IncomingResponseToRequestError::NoServerIdentifier)?,
                message,
                client_identifier,
            }))
        }
        dhcp_protocol::MessageType::DHCPDISCOVER
        | dhcp_protocol::MessageType::DHCPOFFER
        | dhcp_protocol::MessageType::DHCPREQUEST
        | dhcp_protocol::MessageType::DHCPDECLINE
        | dhcp_protocol::MessageType::DHCPRELEASE
        | dhcp_protocol::MessageType::DHCPINFORM => {
            Err(IncomingResponseToRequestError::NotDhcpAckOrNak(message_type))
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use assert_matches::assert_matches;
    use const_unwrap::const_unwrap_option;
    use dhcp_protocol::{CLIENT_PORT, SERVER_PORT};
    use net_declare::{net::prefix_length_v4, net_ip_v4, net_mac, std_ip_v4};
    use net_types::ip::{Ip, Ipv4, PrefixLength};
    use std::net::Ipv4Addr;
    use test_case::test_case;

    #[test]
    fn serialize_parse_roundtrip() {
        let make_message = || dhcp_protocol::Message {
            op: dhcp_protocol::OpCode::BOOTREQUEST,
            xid: 124,
            secs: 99,
            bdcast_flag: false,
            ciaddr: net_ip_v4!("1.2.3.4").into(),
            yiaddr: net_ip_v4!("5.6.7.8").into(),
            siaddr: net_ip_v4!("9.10.11.12").into(),
            giaddr: net_ip_v4!("13.14.15.16").into(),
            chaddr: net_mac!("17:18:19:20:21:22"),
            sname: "this is a sname".to_owned(),
            file: "this is the boot filename".to_owned(),
            options: vec![
                dhcp_protocol::DhcpOption::DhcpMessageType(
                    dhcp_protocol::MessageType::DHCPDISCOVER,
                ),
                dhcp_protocol::DhcpOption::RequestedIpAddress(net_ip_v4!("5.6.7.8").into()),
            ],
        };
        let packet = serialize_dhcp_message_to_ip_packet(
            make_message(),
            Ipv4Addr::UNSPECIFIED,
            CLIENT_PORT,
            Ipv4Addr::BROADCAST,
            SERVER_PORT,
        );
        let (src_addr, parsed_message) =
            parse_dhcp_message_from_ip_packet(packet.as_ref(), SERVER_PORT).unwrap();

        assert_eq!(net_types::ip::Ipv4::UNSPECIFIED_ADDRESS, src_addr);
        assert_eq!(make_message(), parsed_message);
    }

    #[test]
    fn nonsense() {
        assert_matches!(
            parse_dhcp_message_from_ip_packet(
                &[0xD, 0xE, 0xA, 0xD, 0xB, 0xE, 0xE, 0xF],
                const_unwrap_option(NonZeroU16::new(1))
            ),
            Err(ParseError::Ipv4(parse_error)) => {
                assert_eq!(parse_error, packet_formats::error::IpParseError::Parse { error: packet_formats::error::ParseError::Format })
            }
        )
    }

    #[test]
    fn not_udp() {
        let src_ip = Ipv4Addr::UNSPECIFIED.into();
        let dst_ip = Ipv4Addr::BROADCAST.into();
        let tcp_builder: packet_formats::tcp::TcpSegmentBuilder<net_types::ip::Ipv4Addr> =
            packet_formats::tcp::TcpSegmentBuilder::new(
                src_ip,
                dst_ip,
                CLIENT_PORT,
                SERVER_PORT,
                0,
                None,
                0,
            );
        let ipv4_builder = packet_formats::ipv4::Ipv4PacketBuilder::new(
            src_ip,
            dst_ip,
            DEFAULT_TTL,
            packet_formats::ip::Ipv4Proto::Proto(packet_formats::ip::IpProto::Tcp),
        );
        let bytes = vec![1, 2, 3, 4, 5]
            .into_serializer()
            .encapsulate(tcp_builder)
            .encapsulate(ipv4_builder)
            .serialize_vec_outer()
            .expect("serialize error");

        assert_matches!(
            parse_dhcp_message_from_ip_packet(
                bytes.as_ref(),
                const_unwrap_option(NonZeroU16::new(1))
            ),
            Err(ParseError::NotUdp)
        );
    }

    #[test]
    fn wrong_port() {
        let src_ip = Ipv4Addr::UNSPECIFIED.into();
        let dst_ip = Ipv4Addr::BROADCAST.into();

        let udp_builder: packet_formats::udp::UdpPacketBuilder<net_types::ip::Ipv4Addr> =
            packet_formats::udp::UdpPacketBuilder::new(
                src_ip,
                dst_ip,
                Some(CLIENT_PORT),
                SERVER_PORT,
            );
        let ipv4_builder = packet_formats::ipv4::Ipv4PacketBuilder::new(
            src_ip,
            dst_ip,
            DEFAULT_TTL,
            packet_formats::ip::Ipv4Proto::Proto(packet_formats::ip::IpProto::Udp),
        );

        let bytes = "hello_world"
            .bytes()
            .collect::<Vec<_>>()
            .into_serializer()
            .encapsulate(udp_builder)
            .encapsulate(ipv4_builder)
            .serialize_vec_outer()
            .expect("serialize error");

        let result = parse_dhcp_message_from_ip_packet(bytes.as_ref(), CLIENT_PORT);
        assert_matches!(result, Err(ParseError::WrongPort(port)) => assert_eq!(port, SERVER_PORT));
    }

    struct VaryingOfferFields {
        op: dhcp_protocol::OpCode,
        yiaddr: Ipv4Addr,
        message_type: Option<dhcp_protocol::MessageType>,
        server_identifier: Option<Ipv4Addr>,
        subnet_mask: Option<PrefixLength<Ipv4>>,
        lease_length_secs: Option<u32>,
        include_duplicate_option: bool,
    }

    const SERVER_IP: Ipv4Addr = std_ip_v4!("192.168.1.1");
    const TEST_SUBNET_MASK: PrefixLength<Ipv4> = prefix_length_v4!(24);
    const LEASE_LENGTH_SECS: u32 = 100;
    const LEASE_LENGTH_SECS_NONZERO: NonZeroU32 =
        const_unwrap_option(NonZeroU32::new(LEASE_LENGTH_SECS));
    const YIADDR: Ipv4Addr = std_ip_v4!("192.168.1.5");

    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPOFFER),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        include_duplicate_option: false,
    } => Ok(FieldsFromOfferToUseInRequest {
        server_identifier: net_types::ip::Ipv4Addr::from(SERVER_IP)
            .try_into()
            .expect("should be specified"),
        ip_address_lease_time_secs: Some(LEASE_LENGTH_SECS_NONZERO),
        ip_address_to_request: net_types::ip::Ipv4Addr::from(YIADDR)
            .try_into()
            .expect("should be specified"),
    }); "accepts good offer with lease time")]
    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPOFFER),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: None,
        include_duplicate_option: false,
    } => Ok(FieldsFromOfferToUseInRequest {
        server_identifier: net_types::ip::Ipv4Addr::from(SERVER_IP)
            .try_into()
            .expect("should be specified"),
        ip_address_lease_time_secs: None,
        ip_address_to_request: net_types::ip::Ipv4Addr::from(YIADDR)
            .try_into()
            .expect("should be specified"),
    }); "accepts good offer without lease time")]
    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPOFFER),
        server_identifier: Some(Ipv4Addr::UNSPECIFIED),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        include_duplicate_option: false,
    } => Err(SelectingIncomingMessageError::CommonError(
        CommonIncomingMessageError::UnspecifiedServerIdentifier,
    )); "rejects offer with unspecified server identifier")]
    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPOFFER),
        server_identifier: Some(SERVER_IP),
        subnet_mask: None,
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        include_duplicate_option: false,
    } => Err(SelectingIncomingMessageError::MissingRequiredOption(
        dhcp_protocol::OptionCode::SubnetMask,
    )); "rejects offer without required subnet mask")]
    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPOFFER),
        server_identifier: None,
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        include_duplicate_option: false,
    } => Err(SelectingIncomingMessageError::NoServerIdentifier); "rejects offer with no server identifier option")]
    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: Ipv4Addr::UNSPECIFIED,
        message_type: Some(dhcp_protocol::MessageType::DHCPOFFER),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        include_duplicate_option: false,
    } => Err(SelectingIncomingMessageError::UnspecifiedYiaddr) ; "rejects offer with unspecified yiaddr")]
    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREQUEST,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPOFFER),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        include_duplicate_option: false,
    } => Err(SelectingIncomingMessageError::CommonError(
        CommonIncomingMessageError::NotBootReply(dhcp_protocol::OpCode::BOOTREQUEST),
    )); "rejects offer that isn't a bootreply")]
    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPACK),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        include_duplicate_option: false,
    } => Err(
        SelectingIncomingMessageError::NotDhcpOffer(dhcp_protocol::MessageType::DHCPACK),
    ); "rejects offer with wrong DHCP message type")]
    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: None,
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        include_duplicate_option: false,
    } => Err(SelectingIncomingMessageError::CommonError(
        CommonIncomingMessageError::BuilderMissingField("message_type"),
    )); "rejects offer with no DHCP message type option")]
    #[test_case(VaryingOfferFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPOFFER),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        include_duplicate_option: true,
    } => Err(SelectingIncomingMessageError::CommonError(
        CommonIncomingMessageError::DuplicateOption(
            dhcp_protocol::OptionCode::DomainName,
        ),
    )); "rejects offer with duplicate DHCP option")]
    fn fields_from_offer_to_use_in_request(
        offer_fields: VaryingOfferFields,
    ) -> Result<FieldsFromOfferToUseInRequest, SelectingIncomingMessageError> {
        use super::fields_to_retain_from_selecting as fields;
        use dhcp_protocol::DhcpOption;

        let VaryingOfferFields {
            op,
            yiaddr,
            message_type,
            server_identifier,
            subnet_mask,
            lease_length_secs,
            include_duplicate_option,
        } = offer_fields;

        let message = dhcp_protocol::Message {
            op,
            xid: 1,
            secs: 0,
            bdcast_flag: false,
            ciaddr: Ipv4Addr::UNSPECIFIED,
            yiaddr,
            siaddr: Ipv4Addr::UNSPECIFIED,
            giaddr: Ipv4Addr::UNSPECIFIED,
            chaddr: net_mac!("01:02:03:04:05:06"),
            sname: String::new(),
            file: String::new(),
            options: message_type
                .map(DhcpOption::DhcpMessageType)
                .into_iter()
                .chain(server_identifier.map(DhcpOption::ServerIdentifier))
                .chain(subnet_mask.map(DhcpOption::SubnetMask))
                .chain(lease_length_secs.map(DhcpOption::IpAddressLeaseTime))
                .chain(
                    include_duplicate_option
                        .then_some([
                            dhcp_protocol::DhcpOption::DomainName("example.com".to_owned()),
                            dhcp_protocol::DhcpOption::DomainName("example.com".to_owned()),
                        ])
                        .into_iter()
                        .flatten(),
                )
                .collect(),
        };

        fields(
            &std::iter::once((dhcp_protocol::OptionCode::SubnetMask, OptionRequested::Required))
                .collect(),
            message,
        )
    }

    struct VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode,
        yiaddr: Ipv4Addr,
        message_type: Option<dhcp_protocol::MessageType>,
        server_identifier: Option<Ipv4Addr>,
        subnet_mask: Option<PrefixLength<Ipv4>>,
        lease_length_secs: Option<u32>,
        renewal_time_secs: Option<u32>,
        rebinding_time_secs: Option<u32>,
        message: Option<String>,
        include_duplicate_option: bool,
    }

    const DOMAIN_NAME: &str = "example.com";
    const MESSAGE: &str = "message explaining why the DHCPNAK was sent";
    const RENEWAL_TIME_SECS: u32 = LEASE_LENGTH_SECS / 2;
    const REBINDING_TIME_SECS: u32 = LEASE_LENGTH_SECS * 3 / 4;

    #[test_case(
        VaryingReplyToRequestFields {
            op: dhcp_protocol::OpCode::BOOTREPLY,
            yiaddr: YIADDR,
            message_type: Some(dhcp_protocol::MessageType::DHCPACK),
            server_identifier: Some(SERVER_IP),
            subnet_mask: Some(TEST_SUBNET_MASK),
            lease_length_secs: Some(LEASE_LENGTH_SECS),
            renewal_time_secs: None,
            rebinding_time_secs: None,
            message: None,
            include_duplicate_option: false,
        } => Ok(IncomingResponseToRequest::Ack(FieldsToRetainFromAck {
            yiaddr: net_types::ip::Ipv4Addr::from(YIADDR)
                .try_into()
                .expect("should be specified"),
            server_identifier: Some(
                net_types::ip::Ipv4Addr::from(SERVER_IP)
                    .try_into()
                    .expect("should be specified"),
            ),
            ip_address_lease_time_secs: LEASE_LENGTH_SECS_NONZERO,
            parameters: vec![
                dhcp_protocol::DhcpOption::SubnetMask(TEST_SUBNET_MASK),
                dhcp_protocol::DhcpOption::DomainName(DOMAIN_NAME.to_owned())
            ],
            renewal_time_value_secs: None,
            rebinding_time_value_secs: None,
        })); "accepts good DHCPACK")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPACK),
        server_identifier: None,
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        renewal_time_secs: None,
        rebinding_time_secs: None,
        message: None,
        include_duplicate_option: false,
    } => Ok(IncomingResponseToRequest::Ack(FieldsToRetainFromAck {
        yiaddr: net_types::ip::Ipv4Addr::from(YIADDR)
            .try_into()
            .expect("should be specified"),
        server_identifier: None,
        ip_address_lease_time_secs: LEASE_LENGTH_SECS_NONZERO,
        parameters: vec![
            dhcp_protocol::DhcpOption::SubnetMask(TEST_SUBNET_MASK),
            dhcp_protocol::DhcpOption::DomainName(DOMAIN_NAME.to_owned())
        ],
        renewal_time_value_secs: None,
        rebinding_time_value_secs: None,
    })); "accepts DHCPACK with no server identifier")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPACK),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        renewal_time_secs: Some(RENEWAL_TIME_SECS),
        rebinding_time_secs: Some(REBINDING_TIME_SECS),
        message: None,
        include_duplicate_option: false,
    } => Ok(IncomingResponseToRequest::Ack(FieldsToRetainFromAck {
        yiaddr: net_types::ip::Ipv4Addr::from(YIADDR)
            .try_into()
            .expect("should be specified"),
        server_identifier: Some(
            net_types::ip::Ipv4Addr::from(SERVER_IP)
                .try_into()
                .expect("should be specified"),
        ),
        ip_address_lease_time_secs: LEASE_LENGTH_SECS_NONZERO,
        parameters: vec![
            dhcp_protocol::DhcpOption::SubnetMask(TEST_SUBNET_MASK),
            dhcp_protocol::DhcpOption::DomainName(DOMAIN_NAME.to_owned())
        ],
        renewal_time_value_secs: Some(RENEWAL_TIME_SECS),
        rebinding_time_value_secs: Some(REBINDING_TIME_SECS),
    })); "accepts DHCPACK with renew and rebind times")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: Ipv4Addr::UNSPECIFIED,
        message_type: Some(dhcp_protocol::MessageType::DHCPNAK),
        server_identifier: Some(SERVER_IP),
        subnet_mask: None,
        lease_length_secs: None,
        renewal_time_secs: None,
        rebinding_time_secs: None,
        message: Some(MESSAGE.to_owned()),
        include_duplicate_option: false,
    } => Ok(IncomingResponseToRequest::Nak(FieldsToRetainFromNak {
        server_identifier: net_types::ip::Ipv4Addr::from(SERVER_IP)
            .try_into()
            .expect("should be specified"),
        message: Some(MESSAGE.to_owned()),
        client_identifier: None,
    })); "accepts good DHCPNAK")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPACK),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: None,
        renewal_time_secs: Some(RENEWAL_TIME_SECS),
        rebinding_time_secs: Some(REBINDING_TIME_SECS),
        message: None,
        include_duplicate_option: false,
    } =>  Err(IncomingResponseToRequestError::NoLeaseTime); "rejects DHCPACK with no lease time")]
    #[test_case(
        VaryingReplyToRequestFields {
            op: dhcp_protocol::OpCode::BOOTREPLY,
            yiaddr: YIADDR,
            message_type: Some(dhcp_protocol::MessageType::DHCPACK),
            server_identifier: Some(SERVER_IP),
            subnet_mask: None,
            lease_length_secs: Some(LEASE_LENGTH_SECS),
            renewal_time_secs: None,
            rebinding_time_secs: None,
            message: None,
            include_duplicate_option: false,
        } => Err(IncomingResponseToRequestError::MissingRequiredOption(
            dhcp_protocol::OptionCode::SubnetMask
        )); "rejects DHCPACK without required subnet mask")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPACK),
        server_identifier: Some(Ipv4Addr::UNSPECIFIED),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        renewal_time_secs: Some(RENEWAL_TIME_SECS),
        rebinding_time_secs: Some(REBINDING_TIME_SECS),
        message: None,
        include_duplicate_option: false,
    } => Err(IncomingResponseToRequestError::CommonError(
        CommonIncomingMessageError::UnspecifiedServerIdentifier,
    )); "rejects DHCPACK with unspecified server identifier")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: Ipv4Addr::UNSPECIFIED,
        message_type: Some(dhcp_protocol::MessageType::DHCPACK),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        renewal_time_secs: Some(RENEWAL_TIME_SECS),
        rebinding_time_secs: Some(REBINDING_TIME_SECS),
        message: None,
        include_duplicate_option: false,
    } => Err(IncomingResponseToRequestError::UnspecifiedYiaddr); "rejects DHCPACK with unspecified yiaddr")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: Ipv4Addr::UNSPECIFIED,
        message_type: Some(dhcp_protocol::MessageType::DHCPNAK),
        server_identifier: Some(Ipv4Addr::UNSPECIFIED),
        subnet_mask: None,
        lease_length_secs: None,
        renewal_time_secs: None,
        rebinding_time_secs: None,
        message: Some(MESSAGE.to_owned()),
        include_duplicate_option: false,
    } => Err(IncomingResponseToRequestError::CommonError(
        CommonIncomingMessageError::UnspecifiedServerIdentifier,
    )); "rejects DHCPNAK with unspecified server identifier")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: Ipv4Addr::UNSPECIFIED,
        message_type: Some(dhcp_protocol::MessageType::DHCPNAK),
        server_identifier: None,
        subnet_mask: None,
        lease_length_secs: None,
        renewal_time_secs: None,
        rebinding_time_secs: None,
        message: Some(MESSAGE.to_owned()),
        include_duplicate_option: false,
    } => Err(IncomingResponseToRequestError::NoServerIdentifier) ; "rejects DHCPNAK with no server identifier")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREQUEST,
        yiaddr: Ipv4Addr::UNSPECIFIED,
        message_type: Some(dhcp_protocol::MessageType::DHCPNAK),
        server_identifier: Some(SERVER_IP),
        subnet_mask: None,
        lease_length_secs: None,
        renewal_time_secs: None,
        rebinding_time_secs: None,
        message: Some(MESSAGE.to_owned()),
        include_duplicate_option: false,
    } => Err(IncomingResponseToRequestError::CommonError(
        CommonIncomingMessageError::NotBootReply(dhcp_protocol::OpCode::BOOTREQUEST),
    )) ; "rejects non-bootreply")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: Ipv4Addr::UNSPECIFIED,
        message_type: Some(dhcp_protocol::MessageType::DHCPOFFER),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: None,
        renewal_time_secs: None,
        rebinding_time_secs: None,
        message: Some(MESSAGE.to_owned()),
        include_duplicate_option: false,
    } => Err(IncomingResponseToRequestError::NotDhcpAckOrNak(
        dhcp_protocol::MessageType::DHCPOFFER,
    )) ; "rejects non-DHCPACK or DHCPNAK")]
    #[test_case(VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: Ipv4Addr::UNSPECIFIED,
        message_type: None,
        server_identifier: Some(SERVER_IP),
        subnet_mask: None,
        lease_length_secs: None,
        renewal_time_secs: None,
        rebinding_time_secs: None,
        message: Some(MESSAGE.to_owned()),
        include_duplicate_option: false,
    } => Err(IncomingResponseToRequestError::CommonError(
        CommonIncomingMessageError::BuilderMissingField("message_type"),
    )) ; "rejects missing DHCP message type")]
    #[test_case( VaryingReplyToRequestFields {
        op: dhcp_protocol::OpCode::BOOTREPLY,
        yiaddr: YIADDR,
        message_type: Some(dhcp_protocol::MessageType::DHCPACK),
        server_identifier: Some(SERVER_IP),
        subnet_mask: Some(TEST_SUBNET_MASK),
        lease_length_secs: Some(LEASE_LENGTH_SECS),
        renewal_time_secs: Some(RENEWAL_TIME_SECS),
        rebinding_time_secs: Some(REBINDING_TIME_SECS),
        message: None,
        include_duplicate_option: true,
    } => Err(IncomingResponseToRequestError::CommonError(
        CommonIncomingMessageError::DuplicateOption(
            dhcp_protocol::OptionCode::DomainName,
        ),
    )); "rejects duplicate option")]
    fn fields_to_retain_during_requesting(
        incoming_fields: VaryingReplyToRequestFields,
    ) -> Result<
        IncomingResponseToRequest<Option<net_types::SpecifiedAddr<net_types::ip::Ipv4Addr>>>,
        IncomingResponseToRequestError,
    > {
        use super::fields_to_retain_from_response_to_request as fields;
        use dhcp_protocol::DhcpOption;

        let VaryingReplyToRequestFields {
            op,
            yiaddr,
            message_type,
            server_identifier,
            subnet_mask,
            lease_length_secs,
            renewal_time_secs,
            rebinding_time_secs,
            message,
            include_duplicate_option,
        } = incoming_fields;

        let message = dhcp_protocol::Message {
            op,
            xid: 1,
            secs: 0,
            bdcast_flag: false,
            ciaddr: Ipv4Addr::UNSPECIFIED,
            yiaddr,
            siaddr: Ipv4Addr::UNSPECIFIED,
            giaddr: Ipv4Addr::UNSPECIFIED,
            chaddr: net_mac!("01:02:03:04:05:06"),
            sname: String::new(),
            file: String::new(),
            options: std::iter::empty()
                .chain(message_type.map(DhcpOption::DhcpMessageType))
                .chain(server_identifier.map(DhcpOption::ServerIdentifier))
                .chain(subnet_mask.map(DhcpOption::SubnetMask))
                .chain(lease_length_secs.map(DhcpOption::IpAddressLeaseTime))
                .chain(renewal_time_secs.map(DhcpOption::RenewalTimeValue))
                .chain(rebinding_time_secs.map(DhcpOption::RebindingTimeValue))
                .chain(message.map(DhcpOption::Message))
                // Include a parameter that the client didn't request so that we can
                // assert that the client ignored it.
                .chain(std::iter::once(dhcp_protocol::DhcpOption::InterfaceMtu(1)))
                // Include a parameter that the client did request so that we can
                // check that it's included in the acquired parameters map.
                .chain(std::iter::once(dhcp_protocol::DhcpOption::DomainName(
                    DOMAIN_NAME.to_owned(),
                )))
                .chain(
                    include_duplicate_option
                        .then_some(dhcp_protocol::DhcpOption::DomainName(DOMAIN_NAME.to_owned())),
                )
                .collect(),
        };

        fields(
            &[
                (dhcp_protocol::OptionCode::SubnetMask, OptionRequested::Required),
                (dhcp_protocol::OptionCode::DomainName, OptionRequested::Optional),
            ]
            .into_iter()
            .collect(),
            message,
        )
    }
}