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
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
// SPDX-License-Identifier: Apache-2.0

use std::net::Ipv6Addr;

use crate::{
    buffer::{Buffer, BufferMut},
    DhcpError, DhcpV6Option, DhcpV6OptionCode, DhcpV6OptionStatus,
    ErrorContext, ErrorKind,
};

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub struct DhcpV6OptionIaNa {
    pub iaid: u32,
    pub t1_sec: u32,
    pub t2_sec: u32,
    pub address: Option<DhcpV6OptionIaAddr>,
    pub status: Option<DhcpV6OptionStatus>,
}

impl Default for DhcpV6OptionIaNa {
    fn default() -> Self {
        Self {
            iaid: rand::random(),
            t1_sec: 0,
            t2_sec: 0,
            address: None,
            status: None,
        }
    }
}

impl DhcpV6OptionIaNa {
    pub(crate) const fn code() -> DhcpV6OptionCode {
        DhcpV6OptionCode::IANA
    }

    pub(crate) fn new(
        iaid: u32,
        t1_sec: u32,
        t2_sec: u32,
        address: DhcpV6OptionIaAddr,
    ) -> Self {
        Self {
            iaid,
            t1_sec,
            t2_sec,
            address: Some(address),
            status: None,
        }
    }

    pub fn is_success(&self) -> bool {
        self.address.as_ref().map(|addr| addr.is_success()) == Some(true)
            && (self.status.is_none()
                || self.status.as_ref().map(|s| s.is_success()) == Some(true))
    }

    pub(crate) fn parse(buf: &mut Buffer) -> Result<Self, DhcpError> {
        let code = buf.get_u16_be().context("Invalid DHCPv6 option code")?;
        if code != Self::code().into() {
            return Err(DhcpError::new(
                ErrorKind::InvalidArgument,
                format!(
                    "Expecting DHCPv6 option {} code {}, got {}",
                    Self::code(),
                    u16::from(Self::code()),
                    code
                ),
            ));
        }
        let len = buf.get_u16_be().context("Invalid DHCPv6 option len")?;
        let raw = buf
            .get_bytes(len.into())
            .context("Invalid DHCPv6 option OPTION_IA_NA")?;
        let mut buf = Buffer::new(raw);
        let mut ret = Self {
            iaid: buf
                .get_u32_be()
                .context("Invalid DHCPv6 option OPTION_IA_NA IAID")?,
            t1_sec: buf
                .get_u32_be()
                .context("Invalid DHCPv6 option OPTION_IA_NA T1")?,
            t2_sec: buf
                .get_u32_be()
                .context("Invalid DHCPv6 option OPTION_IA_NA T2")?,
            ..Default::default()
        };

        // It could neither OPTION_IAADDR or OPTION_STATUS_CODE
        while !buf.is_empty() {
            let opt = DhcpV6Option::parse(&mut buf)?;
            match opt {
                DhcpV6Option::StatusCode(v) => {
                    ret.status = Some(v);
                }
                DhcpV6Option::IAAddr(v) => {
                    ret.address = Some(v);
                }
                _ => {
                    return Err(DhcpError::new(
                        ErrorKind::InvalidDhcpMessage,
                        format!(
                            "Expecting OPTION_IAADDR or OPTION_STATUS_CODE in \
                             OPTION_IA_NA option field, but got {}",
                            opt.code()
                        ),
                    ));
                }
            }
        }
        Ok(ret)
    }

    pub(crate) fn emit(&self, buf: &mut BufferMut) {
        let mut value_buf = BufferMut::new();
        value_buf.write_u32_be(self.iaid);
        value_buf.write_u32_be(self.t1_sec);
        value_buf.write_u32_be(self.t2_sec);
        if let Some(address) = self.address.as_ref() {
            address.emit(&mut value_buf);
        }
        if let Some(status) = self.status.as_ref() {
            status.emit(&mut value_buf);
        }

        buf.write_u16_be(DhcpV6OptionCode::IANA.into());
        buf.write_u16_be(value_buf.len() as u16);
        buf.write_bytes(&value_buf.data);
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub struct DhcpV6OptionIaTa {
    pub iaid: u32,
    pub address: Option<DhcpV6OptionIaAddr>,
    pub status: Option<DhcpV6OptionStatus>,
}

impl Default for DhcpV6OptionIaTa {
    fn default() -> Self {
        Self {
            iaid: rand::random(),
            address: None,
            status: None,
        }
    }
}

impl DhcpV6OptionIaTa {
    pub(crate) fn new(iaid: u32, address: DhcpV6OptionIaAddr) -> Self {
        Self {
            iaid,
            address: Some(address),
            status: None,
        }
    }

    pub fn is_success(&self) -> bool {
        self.address.as_ref().map(|addr| addr.is_success()) == Some(true)
            && (self.status.is_none()
                || self.status.as_ref().map(|s| s.is_success()) == Some(true))
    }

    pub(crate) const fn code() -> DhcpV6OptionCode {
        DhcpV6OptionCode::IATA
    }

    pub(crate) fn parse(buf: &mut Buffer) -> Result<Self, DhcpError> {
        let code = buf.get_u16_be().context("Invalid DHCPv6 option code")?;
        if code != Self::code().into() {
            return Err(DhcpError::new(
                ErrorKind::InvalidArgument,
                format!(
                    "Expecting DHCPv6 option {} code {}, got {}",
                    Self::code(),
                    u16::from(Self::code()),
                    code
                ),
            ));
        }
        let len = buf.get_u16_be().context("Invalid DHCPv6 option len")?;
        let raw = buf
            .get_bytes(len.into())
            .context("Invalid DHCPv6 option OPTION_IA_TA")?;
        let mut buf = Buffer::new(raw);
        let mut ret = Self {
            iaid: buf
                .get_u32_be()
                .context("Invalid DHCPv6 option OPTION_IA_TA IAID")?,
            ..Default::default()
        };

        while !buf.is_empty() {
            // It could neither OPTION_IAADDR or OPTION_STATUS_CODE
            let opt = DhcpV6Option::parse(&mut buf)?;
            match opt {
                DhcpV6Option::StatusCode(v) => ret.status = Some(v),
                DhcpV6Option::IAAddr(v) => ret.address = Some(v),
                _ => {
                    return Err(DhcpError::new(
                        ErrorKind::InvalidDhcpMessage,
                        format!(
                            "Expecting OPTION_IAADDR or OPTION_STATUS_CODE in \
                             OPTION_IA_TA option field, but got {}",
                            opt.code()
                        ),
                    ));
                }
            }
        }
        Ok(ret)
    }

    pub(crate) fn emit(&self, buf: &mut BufferMut) {
        let mut value_buf = BufferMut::new();
        value_buf.write_u32_be(self.iaid);
        if let Some(address) = self.address.as_ref() {
            address.emit(&mut value_buf);
        }
        if let Some(status) = self.status.as_ref() {
            status.emit(&mut value_buf);
        }

        buf.write_u16_be(DhcpV6OptionCode::IATA.into());
        buf.write_u16_be(value_buf.len() as u16);
        buf.write_bytes(&value_buf.data);
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub struct DhcpV6OptionIaPd {
    pub iaid: u32,
    pub t1_sec: u32,
    pub t2_sec: u32,
    pub prefix: Option<DhcpV6OptionIaPrefix>,
    pub status: Option<DhcpV6OptionStatus>,
}

impl Default for DhcpV6OptionIaPd {
    fn default() -> Self {
        Self {
            iaid: rand::random(),
            t1_sec: 0,
            t2_sec: 0,
            prefix: None,
            status: None,
        }
    }
}

impl DhcpV6OptionIaPd {
    pub fn new_with_hint(prefix_len: u8) -> Self {
        Self {
            prefix: Some(DhcpV6OptionIaPrefix {
                prefix: Ipv6Addr::UNSPECIFIED,
                prefix_len,
                ..Default::default()
            }),
            ..Default::default()
        }
    }

    pub fn new(
        iaid: u32,
        t1_sec: u32,
        t2_sec: u32,
        prefix: DhcpV6OptionIaPrefix,
    ) -> Self {
        Self {
            iaid,
            t1_sec,
            t2_sec,
            prefix: Some(prefix),
            status: None,
        }
    }

    pub fn is_success(&self) -> bool {
        self.prefix.as_ref().map(|prefix| prefix.is_success()) == Some(true)
            && (self.status.is_none()
                || self.status.as_ref().map(|s| s.is_success()) == Some(true))
    }

    pub(crate) const fn code() -> DhcpV6OptionCode {
        DhcpV6OptionCode::IAPD
    }

    pub(crate) fn parse(buf: &mut Buffer) -> Result<Self, DhcpError> {
        let code = buf.get_u16_be().context("Invalid DHCPv6 option code")?;
        if code != Self::code().into() {
            return Err(DhcpError::new(
                ErrorKind::InvalidArgument,
                format!(
                    "Expecting DHCPv6 option {} code {}, got {}",
                    Self::code(),
                    u16::from(Self::code()),
                    code
                ),
            ));
        }
        let len = buf.get_u16_be().context("Invalid DHCPv6 option len")?;
        let raw = buf
            .get_bytes(len.into())
            .context("Invalid DHCPv6 option OPTION_IA_PD")?;
        let mut buf = Buffer::new(raw);
        let mut ret = Self {
            iaid: buf
                .get_u32_be()
                .context("Invalid DHCPv6 option OPTION_IA_PD IAID")?,
            t1_sec: buf
                .get_u32_be()
                .context("Invalid DHCPv6 option OPTION_IA_PD T1")?,
            t2_sec: buf
                .get_u32_be()
                .context("Invalid DHCPv6 option OPTION_IA_PD T2")?,
            ..Default::default()
        };

        // It could neither OPTION_IAPREFIX or OPTION_STATUS_CODE
        while !buf.is_empty() {
            let opt = DhcpV6Option::parse(&mut buf)?;
            match opt {
                DhcpV6Option::StatusCode(v) => ret.status = Some(v),
                DhcpV6Option::IAPrefix(v) => ret.prefix = Some(v),
                _ => {
                    return Err(DhcpError::new(
                        ErrorKind::InvalidDhcpMessage,
                        format!(
                            "Expecting OPTION_IAPREFIX or OPTION_STATUS_CODE \
                             in OPTION_IA_PD option field, but got {}",
                            opt.code()
                        ),
                    ));
                }
            }
        }
        Ok(ret)
    }

    pub(crate) fn emit(&self, buf: &mut BufferMut) {
        let mut value_buf = BufferMut::new();
        value_buf.write_u32_be(self.iaid);
        value_buf.write_u32_be(self.t1_sec);
        value_buf.write_u32_be(self.t2_sec);
        if let Some(prefix) = self.prefix.as_ref() {
            prefix.emit(&mut value_buf);
        }
        if let Some(status) = self.status.as_ref() {
            status.emit(&mut value_buf);
        }

        buf.write_u16_be(DhcpV6OptionCode::IAPD.into());
        buf.write_u16_be(value_buf.len() as u16);
        buf.write_bytes(&value_buf.data);
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub struct DhcpV6OptionIaAddr {
    pub address: Ipv6Addr,
    pub preferred_time_sec: u32,
    pub valid_time_sec: u32,
    pub status: Option<DhcpV6OptionStatus>,
}

impl Default for DhcpV6OptionIaAddr {
    fn default() -> Self {
        Self {
            address: Ipv6Addr::UNSPECIFIED,
            preferred_time_sec: 0,
            valid_time_sec: 0,
            status: None,
        }
    }
}

impl DhcpV6OptionIaAddr {
    pub fn new(
        address: Ipv6Addr,
        preferred_time_sec: u32,
        valid_time_sec: u32,
    ) -> Self {
        Self {
            address,
            preferred_time_sec,
            valid_time_sec,
            ..Default::default()
        }
    }

    pub(crate) const fn code() -> DhcpV6OptionCode {
        DhcpV6OptionCode::IAAddr
    }

    // RFC 8415: If the Status Code option (see Section 21.13) does not appear
    // in a message in which the option could appear, the status of the message
    // is assumed to be Success.
    pub(crate) fn is_success(&self) -> bool {
        if let Some(s) = self.status.as_ref() {
            s.is_success()
        } else {
            true
        }
    }

    pub(crate) fn parse(buf: &mut Buffer) -> Result<Self, DhcpError> {
        let code = buf.get_u16_be().context("Invalid DHCPv6 option code")?;
        if code != Self::code().into() {
            return Err(DhcpError::new(
                ErrorKind::InvalidArgument,
                format!(
                    "Expecting DHCPv6 option {} code {}, got {}",
                    Self::code(),
                    u16::from(Self::code()),
                    code
                ),
            ));
        }
        let len = buf.get_u16_be().context("Invalid DHCPv6 option len")?;
        let raw = buf
            .get_bytes(len.into())
            .context("Invalid DHCPv6 option OPTION_IAADDR")?;
        let mut buf = Buffer::new(raw);
        Ok(Self {
            address: buf
                .get_ipv6()
                .context("Invalid DHCPv6 option OPTION_IAADDR address")?,
            preferred_time_sec: buf.get_u32_be().context(
                "Invalid DHCPv6 option OPTION_IAADDR preferred time",
            )?,
            valid_time_sec: buf
                .get_u32_be()
                .context("Invalid DHCPv6 option OPTION_IAADDR valid time")?,
            status: {
                if buf.is_empty() {
                    None
                } else {
                    Some(DhcpV6OptionStatus::parse(&mut buf)?)
                }
            },
        })
    }

    pub(crate) fn emit(&self, buf: &mut BufferMut) {
        let mut value_buf = BufferMut::new();
        value_buf.write_ipv6(self.address);
        value_buf.write_u32_be(self.preferred_time_sec);
        value_buf.write_u32_be(self.valid_time_sec);
        if let Some(status) = self.status.as_ref() {
            status.emit(&mut value_buf);
        }

        buf.write_u16_be(DhcpV6OptionCode::IAAddr.into());
        buf.write_u16_be(value_buf.len() as u16);
        buf.write_bytes(&value_buf.data);
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub struct DhcpV6OptionIaPrefix {
    pub preferred_time_sec: u32,
    pub valid_time_sec: u32,
    pub prefix_len: u8,
    pub prefix: Ipv6Addr,
    pub status: Option<DhcpV6OptionStatus>,
}

impl Default for DhcpV6OptionIaPrefix {
    fn default() -> Self {
        Self {
            preferred_time_sec: 0,
            valid_time_sec: 0,
            prefix_len: 0,
            prefix: Ipv6Addr::UNSPECIFIED,
            status: None,
        }
    }
}

impl DhcpV6OptionIaPrefix {
    pub fn new(
        prefix: Ipv6Addr,
        prefix_len: u8,
        preferred_time_sec: u32,
        valid_time_sec: u32,
    ) -> Self {
        Self {
            preferred_time_sec,
            valid_time_sec,
            prefix_len,
            prefix,
            status: None,
        }
    }

    pub(crate) fn is_success(&self) -> bool {
        if let Some(s) = self.status.as_ref() {
            s.is_success()
        } else {
            true
        }
    }

    pub(crate) const fn code() -> DhcpV6OptionCode {
        DhcpV6OptionCode::IAPrefix
    }

    pub(crate) fn parse(buf: &mut Buffer) -> Result<Self, DhcpError> {
        let code = buf
            .get_u16_be()
            .context("Invalid DHCPv6 option OPTION_IAPREFIX code")?;
        if code != DhcpV6OptionCode::IAPrefix.into() {
            return Err(DhcpError::new(
                ErrorKind::InvalidDhcpMessage,
                format!(
                    "Expecting DHCPv6 option {} code {}, got {}",
                    Self::code(),
                    u16::from(Self::code()),
                    code
                ),
            ));
        }
        let len = buf
            .get_u16_be()
            .context("Invalid DHCPv6 option OPTION_IAPREFIX length")?;
        let raw = buf
            .get_bytes(len.into())
            .context("Invalid DHCPv6 option OPTION_IA_NA")?;
        let mut buf = Buffer::new(raw);
        Ok(Self {
            preferred_time_sec: buf.get_u32_be().context(
                "Invalid DHCPv6 option OPTION_IAPREFIX preferred time",
            )?,
            valid_time_sec: buf
                .get_u32_be()
                .context("Invalid DHCPv6 option OPTION_IAPREFIX valid time")?,
            prefix_len: buf.get_u8().context(
                "Invalid DHCPv6 option OPTION_IAPREFIX prefix length",
            )?,
            prefix: buf
                .get_ipv6()
                .context("Invalid DHCPv6 option OPTION_IAPREFIX address")?,
            status: {
                if buf.is_empty() {
                    None
                } else {
                    Some(DhcpV6OptionStatus::parse(&mut buf)?)
                }
            },
        })
    }

    pub(crate) fn emit(&self, buf: &mut BufferMut) {
        let mut value_buf = BufferMut::new();
        value_buf.write_u32_be(self.preferred_time_sec);
        value_buf.write_u32_be(self.valid_time_sec);
        value_buf.write_u8(self.prefix_len);
        value_buf.write_ipv6(self.prefix);
        if let Some(status) = self.status.as_ref() {
            status.emit(&mut value_buf);
        }

        buf.write_u16_be(DhcpV6OptionCode::IAPrefix.into());
        buf.write_u16_be(value_buf.len() as u16);
        buf.write_bytes(&value_buf.data);
    }
}

#[cfg(test)]
mod test {
    use std::str::FromStr;

    use super::*;
    use crate::DhcpV6OptionStatusCode;

    #[test]
    fn parse_iana() -> Result<(), DhcpError> {
        let raw = &[
            0x00, 0x03, 0x00, 0x28, 0xfd, 0x2a, 0xbc, 0x8e, 0x00, 0x00, 0x00,
            0x3c, 0x00, 0x00, 0x00, 0x69, 0x00, 0x05, 0x00, 0x18, 0x20, 0x01,
            0x0d, 0xb8, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x07, 0x6d, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x78,
        ];
        let mut buf = Buffer::new(raw.as_slice());

        let opt = DhcpV6Option::parse(&mut buf)?;

        assert_eq!(
            opt,
            DhcpV6Option::IANA(DhcpV6OptionIaNa {
                iaid: 0xfd2abc8e,
                t1_sec: 60,
                t2_sec: 105,
                address: Some(DhcpV6OptionIaAddr {
                    preferred_time_sec: 120,
                    valid_time_sec: 120,
                    address: Ipv6Addr::from_str("2001:db8:a::76d").unwrap(),
                    status: None,
                }),
                status: None,
            })
        );

        let mut buf = BufferMut::new();
        opt.emit(&mut buf);

        assert_eq!(buf.data.as_slice(), raw);
        Ok(())
    }

    #[test]
    fn parse_pd_msg() -> Result<(), DhcpError> {
        let raw = &[
            0x00, 0x19, 0x00, 0x36, 0x32, 0xaa, 0xbe, 0x4e, 0x00, 0x00, 0xa8,
            0xc0, 0x00, 0x01, 0x0e, 0x00, 0x00, 0x1a, 0x00, 0x19, 0x00, 0x01,
            0x51, 0x80, 0x00, 0x01, 0x51, 0x80, 0x3c, 0x24, 0x0e, 0x03, 0x9c,
            0x0e, 0x29, 0xdb, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x0d, 0x00, 0x09, 0x00, 0x00, 0x53, 0x55, 0x43, 0x43,
            0x45, 0x53, 0x53,
        ];

        let mut buf = Buffer::new(raw);

        let opt = DhcpV6Option::parse(&mut buf)?;

        assert_eq!(
            opt,
            DhcpV6Option::IAPD(DhcpV6OptionIaPd {
                iaid: 0x32aabe4e,
                t1_sec: 43200,
                t2_sec: 69120,
                prefix: Some(DhcpV6OptionIaPrefix {
                    preferred_time_sec: 86400,
                    valid_time_sec: 86400,
                    prefix_len: 60,
                    prefix: Ipv6Addr::from_str("240e:39c:e29:dbf0::").unwrap(),
                    status: None,
                }),
                status: Some(DhcpV6OptionStatus {
                    status: DhcpV6OptionStatusCode::Success,
                    message: "SUCCESS".into(),
                })
            })
        );

        let mut buf = BufferMut::new();
        opt.emit(&mut buf);

        assert_eq!(buf.data.as_slice(), raw);
        Ok(())
    }

    #[test]
    fn parse_pd_no_address() -> Result<(), DhcpError> {
        let raw = &[
            0x00, 0x19, 0x00, 0x1f, 0xc1, 0xdb, 0x20, 0x5c, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x0f, 0x00, 0x06,
            0x4e, 0x4f, 0x50, 0x52, 0x45, 0x46, 0x49, 0x58, 0x41, 0x56, 0x41,
            0x49, 0x4c,
        ];

        let mut buf = Buffer::new(raw);

        let opt = DhcpV6Option::parse(&mut buf)?;

        assert_eq!(
            opt,
            DhcpV6Option::IAPD(DhcpV6OptionIaPd {
                iaid: 0xc1db205c,
                t1_sec: 0,
                t2_sec: 0,
                prefix: None,
                status: Some(DhcpV6OptionStatus {
                    status: DhcpV6OptionStatusCode::NoPrefixAvail,
                    message: "NOPREFIXAVAIL".into(),
                })
            })
        );

        let mut buf = BufferMut::new();
        opt.emit(&mut buf);

        assert_eq!(buf.data.as_slice(), raw);

        Ok(())
    }
}