mozim 0.3.2

DHCP Client Library
Documentation
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
// SPDX-License-Identifier: Apache-2.0

use std::{cmp::Ordering, collections::HashMap, net::Ipv6Addr};

use crate::{
    Buffer, BufferMut, DhcpError, DhcpV6Duid, DhcpV6OptionIaAddr,
    DhcpV6OptionIaNa, DhcpV6OptionIaPd, DhcpV6OptionIaPrefix, DhcpV6OptionIaTa,
    DhcpV6OptionStatus, ErrorContext, ErrorKind,
};

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub(crate) struct DhcpV6Options {
    data: HashMap<DhcpV6OptionCode, Vec<DhcpV6Option>>,
}

impl DhcpV6Options {
    pub(crate) fn new() -> Self {
        Self::default()
    }

    pub(crate) fn get_data_raw(&self, code: u16) -> Option<Vec<Vec<u8>>> {
        let mut ret: Vec<Vec<u8>> = Vec::new();
        if let Some(opts) = self.data.get(&DhcpV6OptionCode::from(code)) {
            for opt in opts {
                let mut buf = BufferMut::new();
                opt.emit(&mut buf);
                ret.push(buf.data);
            }
            Some(ret)
        } else {
            None
        }
    }

    pub(crate) fn get_first(
        &self,
        code: DhcpV6OptionCode,
    ) -> Option<&DhcpV6Option> {
        self.data
            .get(&code)
            .map(|opts| opts.as_slice())
            .and_then(|opts| opts.first())
    }

    pub(crate) fn insert(&mut self, opt: DhcpV6Option) {
        self.data.entry(opt.code()).or_default().push(opt);
    }

    pub(crate) fn remove(&mut self, code: DhcpV6OptionCode) {
        self.data.remove(&code);
    }

    pub(crate) fn parse(buf: &mut Buffer) -> Result<Self, DhcpError> {
        let mut ret = Self::new();
        while !buf.is_empty() {
            match DhcpV6Option::parse(buf) {
                Ok(opt) => {
                    ret.insert(opt);
                }
                Err(e) => {
                    log::info!(
                        "Ignore DHCPv6 option due to parsing error: {e}"
                    );
                    continue;
                }
            }
        }
        Ok(ret)
    }

    pub(crate) fn emit(&self, buf: &mut BufferMut) {
        let mut all_opts: Vec<&DhcpV6Option> = Vec::new();

        for opts in self.data.values() {
            for opt in opts {
                all_opts.push(opt);
            }
        }

        all_opts.sort_unstable();

        for opt in all_opts {
            opt.emit(buf);
        }
    }
}

const OPTION_CLIENTID: u16 = 1;
const OPTION_SERVERID: u16 = 2;
const OPTION_IA_NA: u16 = 3;
const OPTION_IA_TA: u16 = 4;
const OPTION_IAADDR: u16 = 5;
const OPTION_ORO: u16 = 6;
const OPTION_PREFERENCE: u16 = 7;
const OPTION_ELAPSED_TIME: u16 = 8;
const OPTION_UNICAST: u16 = 12;
const OPTION_STATUS_CODE: u16 = 13;
const OPTION_RAPID_COMMIT: u16 = 14;
const OPTION_DNS_SERVERS: u16 = 23;
const OPTION_DOMAIN_LIST: u16 = 24;
const OPTION_IA_PD: u16 = 25;
const OPTION_IAPREFIX: u16 = 26;
const OPTION_NTP_SERVER: u16 = 56;

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Default)]
#[non_exhaustive]
pub enum DhcpV6OptionCode {
    #[default]
    ClientId,
    ServerId,
    IANA,
    IATA,
    IAPD,
    IAAddr,
    IAPrefix,
    OptionRequestOption,
    Preference,
    ElapsedTime,
    ServerUnicast,
    StatusCode,
    RapidCommit,
    DnsServers,
    DomainList,
    NtpServer,
    Other(u16),
}

impl From<DhcpV6OptionCode> for u16 {
    fn from(v: DhcpV6OptionCode) -> u16 {
        match v {
            DhcpV6OptionCode::ClientId => OPTION_CLIENTID,
            DhcpV6OptionCode::ServerId => OPTION_SERVERID,
            DhcpV6OptionCode::IANA => OPTION_IA_NA,
            DhcpV6OptionCode::IATA => OPTION_IA_TA,
            DhcpV6OptionCode::IAPD => OPTION_IA_PD,
            DhcpV6OptionCode::IAAddr => OPTION_IAADDR,
            DhcpV6OptionCode::IAPrefix => OPTION_IAPREFIX,
            DhcpV6OptionCode::OptionRequestOption => OPTION_ORO,
            DhcpV6OptionCode::Preference => OPTION_PREFERENCE,
            DhcpV6OptionCode::ElapsedTime => OPTION_ELAPSED_TIME,
            DhcpV6OptionCode::ServerUnicast => OPTION_UNICAST,
            DhcpV6OptionCode::StatusCode => OPTION_STATUS_CODE,
            DhcpV6OptionCode::RapidCommit => OPTION_RAPID_COMMIT,
            DhcpV6OptionCode::DnsServers => OPTION_DNS_SERVERS,
            DhcpV6OptionCode::DomainList => OPTION_DOMAIN_LIST,
            DhcpV6OptionCode::NtpServer => OPTION_NTP_SERVER,
            DhcpV6OptionCode::Other(d) => d,
        }
    }
}

impl From<u16> for DhcpV6OptionCode {
    fn from(d: u16) -> Self {
        match d {
            OPTION_CLIENTID => Self::ClientId,
            OPTION_SERVERID => Self::ServerId,
            OPTION_IA_NA => Self::IANA,
            OPTION_IA_TA => Self::IATA,
            OPTION_IA_PD => Self::IAPD,
            OPTION_IAADDR => Self::IAAddr,
            OPTION_IAPREFIX => Self::IAPrefix,
            OPTION_ORO => Self::OptionRequestOption,
            OPTION_PREFERENCE => Self::Preference,
            OPTION_ELAPSED_TIME => Self::ElapsedTime,
            OPTION_UNICAST => Self::ServerUnicast,
            OPTION_STATUS_CODE => Self::StatusCode,
            OPTION_RAPID_COMMIT => Self::RapidCommit,
            OPTION_DNS_SERVERS => Self::DnsServers,
            OPTION_DOMAIN_LIST => Self::DomainList,
            OPTION_NTP_SERVER => Self::NtpServer,
            _ => Self::Other(d),
        }
    }
}

impl Ord for DhcpV6OptionCode {
    fn cmp(&self, other: &Self) -> Ordering {
        u16::from(*self).cmp(&u16::from(*other))
    }
}

impl PartialOrd for DhcpV6OptionCode {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl std::fmt::Display for DhcpV6OptionCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ClientId => write!(f, "OPTION_CLIENTID"),
            Self::ServerId => write!(f, "OPTION_SERVERID"),
            Self::IANA => write!(f, "OPTION_IA_NA"),
            Self::IATA => write!(f, "OPTION_IA_TA"),
            Self::IAPD => write!(f, "OPTION_IA_PD"),
            Self::IAAddr => write!(f, "OPTION_IAADDR"),
            Self::IAPrefix => write!(f, "OPTION_IAPREFIX"),
            Self::OptionRequestOption => write!(f, "OPTION_ORO"),
            Self::Preference => write!(f, "OPTION_PREFERENCE"),
            Self::ElapsedTime => write!(f, "OPTION_ELAPSED_TIME"),
            Self::ServerUnicast => write!(f, "OPTION_UNICAST"),
            Self::StatusCode => write!(f, "OPTION_STATUS_CODE"),
            Self::RapidCommit => write!(f, "OPTION_RAPID_COMMIT"),
            Self::DnsServers => write!(f, "OPTION_DNS_SERVERS"),
            Self::DomainList => write!(f, "OPTION_DOMAIN_LIST"),
            Self::NtpServer => write!(f, "OPTION_NTP_SERVER"),
            Self::Other(d) => write!(f, "Unknown({d})"),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum DhcpV6Option {
    ClientId(DhcpV6Duid),
    ServerId(DhcpV6Duid),
    IANA(DhcpV6OptionIaNa),
    IATA(DhcpV6OptionIaTa),
    IAPD(DhcpV6OptionIaPd),
    OptionRequestOption(Vec<DhcpV6OptionCode>),
    IAAddr(DhcpV6OptionIaAddr),
    IAPrefix(DhcpV6OptionIaPrefix),
    Preference(u8),
    ElapsedTime(u16),
    ServerUnicast(Ipv6Addr),
    StatusCode(DhcpV6OptionStatus),
    RapidCommit,
    /// RFC 3646
    DnsServers(Vec<Ipv6Addr>),
    /// RFC 3646
    DomainList(Vec<String>),
    /// RFC 5908
    NtpServer(Vec<DhcpV6OptionNtpServer>),
    Unknown(DhcpV6OptionUnknown),
}

impl Default for DhcpV6Option {
    fn default() -> Self {
        Self::Unknown(DhcpV6OptionUnknown::default())
    }
}

impl Ord for DhcpV6Option {
    fn cmp(&self, other: &Self) -> Ordering {
        self.code().cmp(&other.code())
    }
}

impl PartialOrd for DhcpV6Option {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl DhcpV6Option {
    pub fn code(&self) -> DhcpV6OptionCode {
        match self {
            DhcpV6Option::ClientId(_) => DhcpV6OptionCode::ClientId,
            DhcpV6Option::ServerId(_) => DhcpV6OptionCode::ServerId,
            DhcpV6Option::IANA(_) => DhcpV6OptionCode::IANA,
            DhcpV6Option::IATA(_) => DhcpV6OptionCode::IATA,
            DhcpV6Option::OptionRequestOption(_) => {
                DhcpV6OptionCode::OptionRequestOption
            }
            DhcpV6Option::Preference(_) => DhcpV6OptionCode::Preference,
            DhcpV6Option::ElapsedTime(_) => DhcpV6OptionCode::ElapsedTime,
            DhcpV6Option::ServerUnicast(_) => DhcpV6OptionCode::ServerUnicast,
            DhcpV6Option::StatusCode(_) => DhcpV6OptionCode::StatusCode,
            DhcpV6Option::RapidCommit => DhcpV6OptionCode::RapidCommit,
            DhcpV6Option::DnsServers(_) => DhcpV6OptionCode::DnsServers,
            DhcpV6Option::DomainList(_) => DhcpV6OptionCode::DomainList,
            DhcpV6Option::IAPD(_) => DhcpV6OptionCode::IAPD,
            DhcpV6Option::NtpServer(_) => DhcpV6OptionCode::NtpServer,
            DhcpV6Option::IAAddr(_) => DhcpV6OptionCode::IAAddr,
            DhcpV6Option::IAPrefix(_) => DhcpV6OptionCode::IAPrefix,
            DhcpV6Option::Unknown(u) => u.code(),
        }
    }

    pub(crate) fn parse(buf: &mut Buffer) -> Result<DhcpV6Option, DhcpError> {
        let code: DhcpV6OptionCode = buf
            .peek_u16_be()
            .context("Invalid DHCPv6 option code")?
            .into();
        let len: usize = buf
            .peek_u16_be_offset(2)
            .context("Invalid DHCPv6 option length")?
            .into();
        let opt_raw = buf.get_bytes(len + 4).context(format!(
            "Invalid DHCPv6 option {code} with length {len}"
        ))?;
        let mut opt_buf = Buffer::new(opt_raw);

        Ok(match code {
            DhcpV6OptionCode::IAAddr => {
                Self::IAAddr(DhcpV6OptionIaAddr::parse(&mut opt_buf)?)
            }
            DhcpV6OptionCode::IAPrefix => {
                Self::IAPrefix(DhcpV6OptionIaPrefix::parse(&mut opt_buf)?)
            }
            DhcpV6OptionCode::ClientId => {
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                Self::ClientId(DhcpV6Duid::parse(&mut opt_buf, len)?)
            }
            DhcpV6OptionCode::ServerId => {
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                Self::ServerId(DhcpV6Duid::parse(&mut opt_buf, len)?)
            }
            DhcpV6OptionCode::IANA => {
                Self::IANA(DhcpV6OptionIaNa::parse(&mut opt_buf)?)
            }
            DhcpV6OptionCode::IATA => {
                Self::IATA(DhcpV6OptionIaTa::parse(&mut opt_buf)?)
            }
            DhcpV6OptionCode::IAPD => {
                Self::IAPD(DhcpV6OptionIaPd::parse(&mut opt_buf)?)
            }
            DhcpV6OptionCode::OptionRequestOption => {
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                let mut opts: Vec<DhcpV6OptionCode> = Vec::new();
                for _ in 0..len / 2 {
                    opts.push(
                        opt_buf
                            .get_u16_be()
                            .context("Invalid DHCPv6 option OPTION_ORO")?
                            .into(),
                    );
                }
                Self::OptionRequestOption(opts)
            }
            DhcpV6OptionCode::Preference => Self::Preference({
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                opt_buf
                    .get_u8()
                    .context("Invalid DHCPv6 option OPTION_PREFERENCE")?
            }),
            DhcpV6OptionCode::ElapsedTime => Self::ElapsedTime({
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option OPTION_ELAPSED_TIME")?
            }),
            DhcpV6OptionCode::ServerUnicast => Self::ServerUnicast({
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                opt_buf
                    .get_ipv6()
                    .context("Invalid DHCPv6 option OPTION_UNICAST")?
            }),
            DhcpV6OptionCode::StatusCode => {
                Self::StatusCode(DhcpV6OptionStatus::parse(&mut opt_buf)?)
            }
            DhcpV6OptionCode::RapidCommit => {
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                Self::RapidCommit
            }
            DhcpV6OptionCode::DnsServers => {
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                let mut addrs = Vec::new();
                for _ in 0..len / 16 {
                    addrs.push(
                        opt_buf.get_ipv6().context(
                            "Invalid DHCPv6 option OPTION_DNS_SERVERS",
                        )?,
                    );
                }
                Self::DnsServers(addrs)
            }
            DhcpV6OptionCode::DomainList => {
                // RFC 1035: 3.1. Name space definitions
                //      Domain names in messages are expressed in terms of a
                //      sequence of labels.  Each label is represented as a one
                //      octet length field followed by that number of octets.
                //      Since every domain name ends with the null label of the
                //      root, a domain name is terminated by a length byte of
                //      zero.  The high order two bits of every length octet
                //      must be zero, and the remaining six bits of the length
                //      field limit the label to 63 octets or less.
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                let raw = opt_buf
                    .get_bytes(len)
                    .context("Invalid DHCPv6 option OPTION_DOMAIN_LIST")?;
                let mut tmp_opt_buf = Buffer::new(raw);
                let mut domains = Vec::new();
                while !tmp_opt_buf.is_empty() {
                    let str_len = tmp_opt_buf.get_u8().context(
                        "Invalid DHCPv6 option OPTION_DOMAIN_LIST length",
                    )?;
                    domains.push(
                        tmp_opt_buf
                            .get_string_with_null(str_len.into())
                            .context(
                                "Invalid DHCPv6 option OPTION_DOMAIN_LIST \
                                 domain",
                            )?,
                    );
                }
                Self::DomainList(domains)
            }
            DhcpV6OptionCode::NtpServer => {
                // RFC 5908
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                let raw = opt_buf
                    .get_bytes(len)
                    .context("Invalid DHCPv6 option OPTION_NTP_SERVER")?;
                let mut tmp_opt_buf = Buffer::new(raw);
                let mut srvs: Vec<DhcpV6OptionNtpServer> = Vec::new();
                while !tmp_opt_buf.is_empty() {
                    srvs.push(DhcpV6OptionNtpServer::parse(&mut tmp_opt_buf)?);
                }
                Self::NtpServer(srvs)
            }
            DhcpV6OptionCode::Other(d) => Self::Unknown({
                opt_buf.get_u16_be().context("Invalid DHCPv6 option code")?;
                opt_buf
                    .get_u16_be()
                    .context("Invalid DHCPv6 option length")?;
                DhcpV6OptionUnknown {
                    code: d,
                    raw: opt_buf
                        .get_bytes(len)
                        .context(format!("Invalid DHCPv6 option {d}"))?
                        .to_vec(),
                }
            }),
        })
    }

    pub(crate) fn emit(&self, buf: &mut BufferMut) {
        match self {
            Self::ClientId(id) | Self::ServerId(id) => {
                let mut value_buf = BufferMut::new();
                id.emit(&mut value_buf);
                buf.write_u16_be(self.code().into());
                buf.write_u16_be(value_buf.len() as u16);
                buf.write_bytes(value_buf.data.as_slice());
            }
            Self::IAAddr(v) => v.emit(buf),
            Self::IAPrefix(v) => v.emit(buf),
            Self::IANA(v) => v.emit(buf),
            Self::IATA(v) => v.emit(buf),
            Self::IAPD(v) => v.emit(buf),
            Self::OptionRequestOption(opts) => {
                buf.write_u16_be(self.code().into());
                buf.write_u16_be((opts.len() * 2) as u16);
                for opt in opts {
                    buf.write_u16_be((*opt).into());
                }
            }
            Self::Preference(d) => {
                buf.write_u16_be(self.code().into());
                buf.write_u16_be(1);
                buf.write_u8(*d);
            }
            Self::ElapsedTime(d) => {
                buf.write_u16_be(self.code().into());
                buf.write_u16_be(2);
                buf.write_u16_be(*d);
            }
            Self::ServerUnicast(i) => {
                buf.write_u16_be(self.code().into());
                buf.write_u16_be(16);
                buf.write_ipv6(*i);
            }
            Self::StatusCode(v) => {
                v.emit(buf);
            }
            Self::RapidCommit => {
                buf.write_u16_be(self.code().into());
                buf.write_u16_be(0);
            }
            Self::DnsServers(addrs) => {
                buf.write_u16_be(self.code().into());
                buf.write_u16_be((addrs.len() * 16) as u16);
                for addr in addrs {
                    buf.write_ipv6(*addr);
                }
            }
            Self::DomainList(domains) => {
                let mut value_buf = BufferMut::new();
                for domain in domains {
                    value_buf.write_u8((domain.len() + 1) as u8);
                    value_buf.write_string_with_null(domain, domain.len() + 1);
                }
                buf.write_u16_be(self.code().into());
                buf.write_u16_be(value_buf.len() as u16);
                buf.write_bytes(value_buf.data.as_slice());
            }
            Self::NtpServer(srvs) => {
                let mut value_buf = BufferMut::new();
                for srv in srvs {
                    srv.emit(&mut value_buf);
                }
                buf.write_u16_be(self.code().into());
                buf.write_u16_be(value_buf.len() as u16);
                buf.write_bytes(value_buf.data.as_slice());
            }
            Self::Unknown(v) => {
                buf.write_u16_be(self.code().into());
                buf.write_u16_be(v.raw.len() as u16);
                buf.write_bytes(v.raw.as_slice());
            }
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct DhcpV6OptionUnknown {
    pub code: u16,
    pub raw: Vec<u8>,
}

impl DhcpV6OptionUnknown {
    pub fn code(&self) -> DhcpV6OptionCode {
        self.code.into()
    }
}

/// DHCPv6 Option for NTP Server
///
/// Defined by RFC 5908
#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum DhcpV6OptionNtpServer {
    ServerAddr(Ipv6Addr),
    MulticastAddr(Ipv6Addr),
    ServerFqdn(String),
    Other((u16, Vec<u8>)),
}

const NTP_SUBOPTION_SRV_ADDR: u16 = 1;
const NTP_SUBOPTION_MC_ADDR: u16 = 2;
const NTP_SUBOPTION_SRV_FQDN: u16 = 3;

impl DhcpV6OptionNtpServer {
    pub(crate) fn parse(buf: &mut Buffer) -> Result<Self, DhcpError> {
        let subopt_type = buf
            .get_u16_be()
            .context("Invalid OPTION_NTP_SERVER suboption")?;
        let subopt_len = buf
            .get_u16_be()
            .context("Invalid OPTION_NTP_SERVER suboption-len")?;
        Ok(match subopt_type {
            NTP_SUBOPTION_SRV_ADDR => {
                Self::ServerAddr(buf.get_ipv6().context(
                    "Invalid OPTION_NTP_SERVER NTP_SUBOPTION_SRV_ADDR",
                )?)
            }
            NTP_SUBOPTION_MC_ADDR => {
                Self::MulticastAddr(buf.get_ipv6().context(
                    "Invalid OPTION_NTP_SERVER NTP_SUBOPTION_MC_ADDR",
                )?)
            }
            NTP_SUBOPTION_SRV_FQDN => Self::ServerFqdn({
                let mut lables = Vec::new();
                let raw = buf.get_bytes(subopt_len.into()).context(
                    "Invalid OPTION_NTP_SERVER NTP_SUBOPTION_SRV_FQDN",
                )?;
                let mut fqdn_buf = Buffer::new(raw);
                while !fqdn_buf.is_empty() {
                    let lable_len = fqdn_buf.get_u8().context(
                        "Invalid OPTION_NTP_SERVER NTP_SUBOPTION_SRV_FQDN",
                    )?;
                    if lable_len == 0 {
                        break;
                    }
                    let lable_raw =
                        fqdn_buf.get_bytes(lable_len as usize).context(
                            "Invalid OPTION_NTP_SERVER NTP_SUBOPTION_SRV_FQDN",
                        )?;
                    match std::str::from_utf8(lable_raw) {
                        Ok(l) => lables.push(l.to_string()),
                        Err(e) => {
                            return Err(DhcpError::new(
                                ErrorKind::InvalidDhcpMessage,
                                format!(
                                    "Invalid OPTION_NTP_SERVER \
                                     NTP_SUBOPTION_SRV_FQDN: {e}"
                                ),
                            ));
                        }
                    }
                }
                lables.join(".").to_string()
            }),
            _ => Self::Other((
                subopt_type,
                buf.get_bytes(subopt_len.into())
                    .context(format!(
                        "Invalid OPTION_NTP_SERVER {}",
                        subopt_type
                    ))?
                    .to_vec(),
            )),
        })
    }

    pub(crate) fn emit(&self, buf: &mut BufferMut) {
        match self {
            Self::ServerAddr(ip) => {
                buf.write_u16_be(NTP_SUBOPTION_SRV_ADDR);
                buf.write_u16_be(16);
                buf.write_ipv6(*ip);
            }
            Self::MulticastAddr(ip) => {
                buf.write_u16_be(NTP_SUBOPTION_MC_ADDR);
                buf.write_u16_be(16);
                buf.write_ipv6(*ip);
            }
            Self::ServerFqdn(name) => {
                buf.write_u16_be(NTP_SUBOPTION_SRV_FQDN);
                buf.write_u16_be((name.len() + 1) as u16);
                buf.write_string_with_null(name, name.len() + 1);
            }
            Self::Other((subopt_type, v)) => {
                buf.write_u16_be(*subopt_type);
                buf.write_u16_be(v.len() as u16);
                buf.write_bytes(v.as_slice());
            }
        }
    }
}