MacAddr

Type Alias MacAddr 

Source
pub type MacAddr = ByteArray<6>;
Expand description

Standard 6-byte MAC address. Split 24/24 format, Block ID | Device ID . Locally-administered addresses are [0x02, …], [0x06, …], [0x0A, …], [0x0E, …]

Aliased Type§

#[repr(transparent)]
pub struct MacAddr(pub [u8; 6]);

Tuple Fields§

§0: [u8; 6]

Implementations§

Source§

impl MacAddr

Source

pub const BROADCAST: MacAddr

Broadcast address (all ones)

Source

pub const ANY: MacAddr

Any address (all zeroes)

Source

pub fn new(v: [u8; 6]) -> Self

New from bytes

Examples found in repository?
examples/dhcp.rs (line 7)
2fn main() -> () {
3    use catnip::*;
4
5    let dhcp_inform = DhcpFixedPayload::new_inform(
6        IpV4Addr::new([1, 2, 3, 4]),
7        MacAddr::new([5, 6, 7, 8, 9, 10]),
8        12345
9    );
10
11    // Serialize
12    let bytes = dhcp_inform.to_be_bytes();
13    // Deserialize
14    let msg_parsed = DhcpFixedPayload::read_bytes(&bytes);
15
16    assert_eq!(msg_parsed, dhcp_inform);
17}
More examples
Hide additional examples
examples/arp.rs (line 7)
3fn main() -> () {
4    use catnip::*;
5
6    let msg = ArpPayload::new(
7        MacAddr::new([1, 2, 3, 4, 5, 6]),
8        IpV4Addr::new([7, 8, 9, 10]),
9        MacAddr::new([11, 12, 13, 14, 15, 16]),
10        IpV4Addr::new([17, 18, 19, 20]),
11        ArpOperation::Request,
12    );
13
14    // Serialize
15    let bytes: [u8; ArpPayload::BYTE_LEN] = msg.to_be_bytes();
16
17    // Deserialize
18    let msg_parsed = ArpPayload::read_bytes(&bytes);
19
20    assert_eq!(msg, msg_parsed);
21}
examples/udp.rs (line 18)
5fn main() -> () {
6    use catnip::*;
7    
8    // Some made-up data with two 32-bit words' worth of bytes
9    let data: ByteArray<8> = ByteArray([0, 1, 2, 3, 4, 5, 6, 7]);
10
11    // Arbitrary addresses
12    let src_ipaddr: IpV4Addr = IpV4Addr::new([10, 0, 0, 120]);
13    let dst_ipaddr: IpV4Addr = IpV4Addr::new([10, 0, 0, 121]);
14
15    let frame = EthernetFrame::<IpV4Frame<UdpFrame<ByteArray<8>>>> {
16        header: EthernetHeader {
17            dst_macaddr: MacAddr::BROADCAST,
18            src_macaddr: MacAddr::new([0x02, 0xAF, 0xFF, 0x1A, 0xE5, 0x3C]),
19            ethertype: EtherType::IpV4,
20        },
21        data: IpV4Frame::<UdpFrame<ByteArray<8>>> {
22            header: IpV4Header {
23                version_and_header_length: VersionAndHeaderLength::new().with_version(4).with_header_length((IpV4Header::BYTE_LEN / 4) as u8),
24                dscp: DSCP::Standard,
25                total_length: IpV4Frame::<UdpFrame<ByteArray<8>>>::BYTE_LEN as u16,
26                identification: 0,
27                fragmentation: Fragmentation::default(),
28                time_to_live: 10,
29                protocol: Protocol::Udp,
30                checksum: 0,
31                src_ipaddr: src_ipaddr,
32                dst_ipaddr: dst_ipaddr,
33            },
34            data: UdpFrame::<ByteArray<8>> {
35                header: UdpHeader {
36                    src_port: 8123,
37                    dst_port: 8125,
38                    length: UdpFrame::<ByteArray<8>>::BYTE_LEN as u16,
39                    checksum: 0,
40                },
41                data: data,
42            },
43        },
44        checksum: 0_u32,
45    };
46
47    let bytes = frame.to_be_bytes();
48    let frame_parsed = EthernetFrame::<IpV4Frame<UdpFrame<ByteArray<8>>>>::read_bytes(&bytes);
49
50    assert_eq!(frame_parsed, frame);
51}