Commit 0d4c2ce9 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

add mac address struct

parent f4cf0ec0
pub mod ether_type;
pub mod ethernet;
pub mod packet;
pub mod tools;
pub mod tools;
\ No newline at end of file
pub mod net;
pub use net::MacAddr;
pub struct MacAddr {
mac: [u8; 6],
}
impl MacAddr {
pub fn new(mac: [u8; 6]) -> Self {
Self { mac }
}
}
impl From<MacAddr> for u64 {
fn from(value: MacAddr) -> Self {
let mut byte: u64 = 0;
for i in 0..6 {
byte = byte << 8;
byte += value.mac[i] as u64;
}
byte
}
}
impl From<u64> for MacAddr {
fn from(value: u64) -> Self {
let mut mac = [0u8; 6];
for i in 0..6 {
mac[i] = (value >> (i * 8)) as u8;
}
Self { mac }
}
}
impl From<&str> for MacAddr {
fn from(value: &str) -> Self {
let mac_vec = value
.split(":")
.map(|x| match u8::from_str_radix(x, 16) {
Ok(v) => v,
Err(_) => 0,
})
.collect::<Vec<u8>>();
let mut mac = [0u8; 6];
for i in 0..6 {
mac[i] = mac_vec[i];
}
Self { mac }
}
}
pub mod mac_address;
pub use mac_address::MacAddr;
\ No newline at end of file
......@@ -132,18 +132,18 @@ pub struct MatchFields {
pub icmpv4_type: Option<u8>,
pub icmpv4_code: Option<u8>,
pub arp_op: Option<u16>,
pub arp_spa: Option<IPv4>, // ARP source IPv4 address
pub arp_tpa: Option<IPv4>, // ARP target IPv4 address
pub arp_sha: Option<MacAddr>, // ARP source Mac
pub arp_tha: Option<MacAddr>, // ARP target Mac
pub arp_spa: Option<IPv4>, // ARP source IPv4 address
pub arp_tpa: Option<IPv4>, // ARP target IPv4 address
pub arp_sha: Option<MacAddr>, // ARP source Mac
pub arp_tha: Option<MacAddr>, // ARP target Mac
pub ipv6_src: Option<IPv6>, // IPv6 address
pub ipv6_dst: Option<IPv6>, // IPv6 address
pub ipv6_flabel: Option<u32>, // IPv6 Flow Lable
pub icmpv6_type: Option<u8>, // ICMPv6 type
pub icmpv6_code: Option<u8>, // ICMPv6 code
pub ipv6_nd_target: Option<u128>, // Target address for ND
pub ipv6_nd_sll: Option<MacAddr>, // MAC , source link-layer for ND
pub ipv6_nd_tll: Option<MacAddr>, // Mac , Target link-layer for ND
pub ipv6_nd_sll: Option<MacAddr>, // MAC , source link-layer for ND
pub ipv6_nd_tll: Option<MacAddr>, // Mac , Target link-layer for ND
pub mpls_label: Option<u32>, // MPLS label
pub mpls_tc: Option<u8>, // MPLS TC
pub mpls_bos: Option<u8>, // MPLS Bos bit
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment