Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v6: add oro_codes #40

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- internally, v6 DhcpOptions are now kept sorted by OptionCode (may become `HashMap<_, Vec<_>>` in future)
- `DhcpOptions::RelayMsg()` type changed to `RelayMessage`
- moved Duid to duid module
- added oro_codes

### Fixed

Expand Down
77 changes: 77 additions & 0 deletions src/v6/duid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::net::Ipv6Addr;

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

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

/// Duid helper type
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Duid(Vec<u8>);
// TODO: define specific duid types

impl Duid {
/// new DUID link layer address with time
pub fn link_layer_time(htype: HType, time: u32, addr: Ipv6Addr) -> Self {
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_u32(time).unwrap();
e.write_u128(addr.into()).unwrap();
Self(buf)
}
/// new DUID enterprise number
pub fn enterprise(enterprise: u32, id: &[u8]) -> Self {
let mut buf = Vec::new();
let mut e = Encoder::new(&mut buf);
e.write_u16(2).unwrap(); // duid type
e.write_u32(enterprise).unwrap();
e.write_slice(id).unwrap();
Self(buf)
}
/// new link layer DUID
pub fn link_layer(htype: HType, addr: Ipv6Addr) -> Self {
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_u128(addr.into()).unwrap();
Self(buf)
}
/// new DUID-UUID
/// `uuid` must be 16 bytes long
pub fn uuid(uuid: &[u8]) -> Self {
assert!(uuid.len() == 16);
let mut buf = Vec::new();
let mut e = Encoder::new(&mut buf);
e.write_u16(4).unwrap(); // duid type
e.write_slice(uuid).unwrap();
Self(buf)
}
/// create a DUID of unknown type
pub fn unknown(duid: &[u8]) -> Self {
Self(duid.to_vec())
}
/// total length of contained DUID
pub fn len(&self) -> usize {
self.0.len()
}
/// is contained DUID empty
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

impl AsRef<[u8]> for Duid {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl From<Vec<u8>> for Duid {
fn from(v: Vec<u8>) -> Self {
Self(v)
}
}
7 changes: 6 additions & 1 deletion src/v6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@
//! # Ok(()) }
//! ```
//!
pub mod duid;
mod option_codes;
mod options;
mod oro_codes;

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

use std::{convert::TryInto, fmt, net::Ipv6Addr};

// re-export submodules from proto::msg
// re-export submodules from v6
pub use self::option_codes::*;
pub use self::options::*;
pub use self::oro_codes::*;

pub use crate::{
decoder::{Decodable, Decoder},
Expand Down
Loading