Skip to content

Commit

Permalink
modify v6 Htype (#69)
Browse files Browse the repository at this point in the history
* modify v6 Htype

* revert v4 htype back

---------

Co-authored-by: chenwanqq <[email protected]>
  • Loading branch information
chenwanqq and chenwanqq authored Nov 27, 2023
1 parent d736010 commit b543b57
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/v6/duid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::net::Ipv6Addr;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{v4::HType, Encoder};
use crate::v6::HType;
use crate::Encoder;

/// Duid helper type
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand All @@ -17,7 +18,7 @@ impl Duid {
let mut buf = Vec::new();
let mut e = Encoder::new(&mut buf);
e.write_u16(1).unwrap(); // duid type
e.write_u16(u8::from(htype) as u16).unwrap();
e.write_u16(u16::from(htype)).unwrap();
e.write_u32(time).unwrap();
e.write_u128(addr.into()).unwrap();
Self(buf)
Expand All @@ -36,7 +37,7 @@ impl Duid {
let mut buf = Vec::new();
let mut e = Encoder::new(&mut buf);
e.write_u16(3).unwrap(); // duid type
e.write_u16(u8::from(htype) as u16).unwrap();
e.write_u16(u16::from(htype)).unwrap();
e.write_u128(addr.into()).unwrap();
Self(buf)
}
Expand Down
198 changes: 198 additions & 0 deletions src/v6/htype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/// In DHCPv6, the Hardware Types are expanded from u8 to u16, referring to https://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml for implementation.
use crate::{
decoder::{Decodable, Decoder},
encoder::{Encodable, Encoder},
error::{DecodeResult, EncodeResult},
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Hardware type of message
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Hash, Clone, PartialEq, Eq)]
pub enum HType {
/// 1 Ethernet
Eth,
/// 2 Experimental Ethernet
ExperimentalEth,
/// 3 Amateur Radio AX25
AmRadioAX25,
/// 4 Proteon Token Ring
ProteonTokenRing,
/// 5 Chaos
Chaos,
/// 6 IEEE.802
IEEE802,
/// 7 ARCNET
ARCNET,
/// 8 Hyperchannel
Hyperchannel,
/// 9 LANSTAR
Lanstar,
/// 10 Autonet Short Addr
AutonetShortAddr,
/// 11 LocalTalk
LocalTalk,
/// 12 LocalNet
LocalNet,
/// 13 Ultralink
Ultralink,
/// 14 SMDS
SMDS,
/// 15 FrameRelay
FrameRelay,
/// 17 HDLC
HDLC,
/// 18 FibreChannel
FibreChannel,
/// 20 SerialLine
SerialLine,
/// 22 Mil STD
MilStd188220,
/// 23 Metricom
Metricom,
/// 24 IEEE1394.1995
IEEE13941995,
/// 25 MAPOS
MAPOS,
/// 26 Twinaxial
Twinaxial,
/// 27 EUI64
EUI64,
/// 28 HIPARP
HIPARP,
/// 29 IP and ARP over ISO 7816-3
IPandARPoverISO78163,
/// 30 ARPSec
ARPSec,
/// 31 IPsec tunnel
IPsecTunnel,
/// 32 Infiniband
Infiniband,
/// 33 TIA-102 Project 25 Common Air Interface (CAI)
CAI,
/// 34 WeigandInt
WiegandInt,
/// 35 PureIP
PureIP,
/// 36 HW_EXP1
HWExp1,
/// 37 HFI
HFI,
/// 38 Unified BUS(UB),
UB,
/// 256 HW_EXP2
HWExp2,
/// 257 AEthernet
AEthernet,
/// 65535 Reserved
Reserved,
/// Unknown or not yet implemented htype
Unknown(u16),
}
impl From<u16> for HType {
fn from(n: u16) -> Self {
use HType::*;
match n {
1 => Eth,
2 => ExperimentalEth,
3 => AmRadioAX25,
4 => ProteonTokenRing,
5 => Chaos,
6 => IEEE802,
7 => ARCNET,
8 => Hyperchannel,
9 => Lanstar,
10 => AutonetShortAddr,
11 => LocalTalk,
12 => LocalNet,
13 => Ultralink,
14 => SMDS,
15 => FrameRelay,
17 => HDLC,
18 => FibreChannel,
20 => SerialLine,
22 => MilStd188220,
23 => Metricom,
24 => IEEE13941995,
25 => MAPOS,
26 => Twinaxial,
27 => EUI64,
28 => HIPARP,
29 => IPandARPoverISO78163,
30 => ARPSec,
31 => IPsecTunnel,
32 => Infiniband,
33 => CAI,
34 => WiegandInt,
35 => PureIP,
36 => HWExp1,
37 => HFI,
38 => UB,
256 => HWExp2,
257 => AEthernet,
65535 => Reserved,
n => Unknown(n),
}
}
}

impl From<HType> for u16 {
fn from(n: HType) -> Self {
use HType as H;
match n {
H::Eth => 1,
H::ExperimentalEth => 2,
H::AmRadioAX25 => 3,
H::ProteonTokenRing => 4,
H::Chaos => 5,
H::IEEE802 => 6,
H::ARCNET => 7,
H::Hyperchannel => 8,
H::Lanstar => 9,
H::AutonetShortAddr => 10,
H::LocalTalk => 11,
H::LocalNet => 12,
H::Ultralink => 13,
H::SMDS => 14,
H::FrameRelay => 15,
H::HDLC => 17,
H::FibreChannel => 18,
H::SerialLine => 20,
H::MilStd188220 => 22,
H::Metricom => 23,
H::IEEE13941995 => 24,
H::MAPOS => 25,
H::Twinaxial => 26,
H::EUI64 => 27,
H::HIPARP => 28,
H::IPandARPoverISO78163 => 29,
H::ARPSec => 30,
H::IPsecTunnel => 31,
H::Infiniband => 32,
H::CAI => 33,
H::WiegandInt => 34,
H::PureIP => 35,
H::HWExp1 => 36,
H::HFI => 37,
H::UB => 38,
H::HWExp2 => 256,
H::AEthernet => 257,
H::Reserved => 65535,
H::Unknown(n) => n,
}
}
}

impl Decodable for HType {
fn decode(decoder: &mut Decoder<'_>) -> DecodeResult<Self> {
Ok(decoder.read_u16()?.into())
}
}

impl Encodable for HType {
fn encode(&self, e: &mut Encoder<'_>) -> EncodeResult<()> {
e.write_u16((*self).into())
}
}
2 changes: 2 additions & 0 deletions src/v6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
//! ```
//!
pub mod duid;
mod htype;
mod option_codes;
mod options;
mod oro_codes;
Expand All @@ -63,6 +64,7 @@ use serde::{Deserialize, Serialize};
use std::{convert::TryInto, fmt, net::Ipv6Addr};

// re-export submodules from v6
pub use self::htype::*;
pub use self::option_codes::*;
pub use self::options::*;
pub use self::oro_codes::*;
Expand Down

0 comments on commit b543b57

Please sign in to comment.