Skip to content

Commit 46cca50

Browse files
committed
Fix parsing of link layer address for ndiscoptions
1 parent 20e5455 commit 46cca50

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/wire/ndiscoption.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,14 +433,14 @@ impl<'a> Repr<'a> {
433433
{
434434
match opt.option_type() {
435435
Type::SourceLinkLayerAddr => {
436-
if opt.data_len() == 1 {
436+
if opt.data_len() >= 1 {
437437
Ok(Repr::SourceLinkLayerAddr(opt.link_layer_addr()))
438438
} else {
439439
Err(Error)
440440
}
441441
}
442442
Type::TargetLinkLayerAddr => {
443-
if opt.data_len() == 1 {
443+
if opt.data_len() >= 1 {
444444
Ok(Repr::TargetLinkLayerAddr(opt.link_layer_addr()))
445445
} else {
446446
Err(Error)
@@ -628,13 +628,18 @@ impl<T: AsRef<[u8]>> PrettyPrint for NdiscOption<T> {
628628
}
629629
}
630630

631-
#[cfg(feature = "medium-ethernet")]
631+
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
632632
#[cfg(test)]
633633
mod test {
634634
use super::Error;
635635
use super::{NdiscOption, PrefixInfoFlags, PrefixInformation, Repr, Type};
636636
use crate::time::Duration;
637-
use crate::wire::{EthernetAddress, Ipv6Address};
637+
use crate::wire::Ipv6Address;
638+
639+
#[cfg(feature = "medium-ethernet")]
640+
use crate::wire::EthernetAddress;
641+
#[cfg(all(not(feature = "medium-ethernet"), feature = "medium-ieee802154"))]
642+
use crate::wire::Ieee802154Address;
638643

639644
static PREFIX_OPT_BYTES: [u8; 32] = [
640645
0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x03, 0x84, 0x00, 0x00, 0x03, 0xe8, 0x00, 0x00, 0x00,
@@ -678,8 +683,9 @@ mod test {
678683
assert_eq!(NdiscOption::new_checked(&bytes), Err(Error));
679684
}
680685

686+
#[cfg(feature = "medium-ethernet")]
681687
#[test]
682-
fn test_repr_parse_link_layer_opt() {
688+
fn test_repr_parse_link_layer_opt_ethernet() {
683689
let mut bytes = [0x01, 0x01, 0x54, 0x52, 0x00, 0x12, 0x23, 0x34];
684690
let addr = EthernetAddress([0x54, 0x52, 0x00, 0x12, 0x23, 0x34]);
685691
{
@@ -697,6 +703,29 @@ mod test {
697703
}
698704
}
699705

706+
#[cfg(all(not(feature = "medium-ethernet"), feature = "medium-ieee802154"))]
707+
#[test]
708+
fn test_repr_parse_link_layer_opt_ieee802154() {
709+
let mut bytes = [
710+
0x01, 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
711+
0x00, 0x00,
712+
];
713+
let addr = Ieee802154Address::Extended([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
714+
{
715+
assert_eq!(
716+
Repr::parse(&NdiscOption::new_unchecked(&bytes)),
717+
Ok(Repr::SourceLinkLayerAddr(addr.into()))
718+
);
719+
}
720+
bytes[0] = 0x02;
721+
{
722+
assert_eq!(
723+
Repr::parse(&NdiscOption::new_unchecked(&bytes)),
724+
Ok(Repr::TargetLinkLayerAddr(addr.into()))
725+
);
726+
}
727+
}
728+
700729
#[test]
701730
fn test_repr_parse_prefix_info() {
702731
let repr = Repr::PrefixInformation(PrefixInformation {

0 commit comments

Comments
 (0)