udp/
udp.rs

1//! Build a UDP/IP Ethernet packet and get its representation as network bytes
2
3
4
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}
52
53#[test]
54fn test_packet() -> () {
55    main();
56}