-
Notifications
You must be signed in to change notification settings - Fork 1
/
ether.rs
35 lines (31 loc) · 828 Bytes
/
ether.rs
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
pub struct EthernetFrame {
dst_mac_addr: Vec<u8>,
source_mac_addr: Vec<u8>,
r#type: Vec<u8>,
}
pub enum EthType {
Ipv4,
Arp,
}
const IPV4: &[u8] = &[0x08, 0x00];
const ARP: &[u8] = &[0x08, 0x06];
impl EthernetFrame {
pub fn new(dst_mac_addr: Vec<u8>, source_mac_addr: Vec<u8>, eth_type: EthType) -> Self {
let ty = match eth_type {
EthType::Ipv4 => IPV4,
EthType::Arp => ARP,
};
EthernetFrame {
dst_mac_addr,
source_mac_addr,
r#type: ty.to_vec(),
}
}
pub fn to_byte_array(&self) -> Vec<u8> {
let mut byte = vec![];
byte.append(&mut self.dst_mac_addr.clone());
byte.append(&mut self.source_mac_addr.clone());
byte.append(&mut self.r#type.clone());
byte
}
}