File: icmp_packet.rs

package info (click to toggle)
rust-pktparse 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 180 kB
  • sloc: makefile: 4
file content (60 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
extern crate nom;
extern crate pktparse;

mod tests {
    use pktparse::ethernet::{EtherType, EthernetFrame, MacAddress};
    use pktparse::ip::IPProtocol;
    use pktparse::ipv4::IPv4Header;
    use pktparse::{ethernet, ipv4};
    use std::net::Ipv4Addr;

    #[test]
    fn ipfrag_packet() {
        let bytes = [
            0x00, 0x23, 0x54, 0x07, 0x93, 0x6c, // Ethernet destination MAC
            0x00, 0x1b, 0x21, 0x0f, 0x91, 0x9b, // Ethernet source MAC
            0x08, 0x00, // Ethernet ethertype
            0x45, // IP version and IHL
            0x00, // IP Differentiated Services Field
            0x05, 0xdc, // IP total length
            0x1a, 0xe6, // IP id
            0x20, 0x00, // IP flags and fragment offset
            0x40, // IP TTL
            0x01, // IP protocol
            0x22, 0xed, // IP header checksum
            0x0a, 0x0a, 0x01, 0x87, // IP source address
            0x0a, 0x0a, 0x01, 0xb4, // IP dest address
        ];
        let eth_expectation = EthernetFrame {
            source_mac: MacAddress([0x00, 0x1b, 0x21, 0x0f, 0x91, 0x9b]),
            dest_mac: MacAddress([0x00, 0x23, 0x54, 0x07, 0x93, 0x6c]),
            ethertype: EtherType::IPv4,
        };
        let ip_expectation = IPv4Header {
            version: 4,
            ihl: 5,
            tos: 0,
            length: 1500,
            id: 0x1ae6,
            flags: 0x01,
            fragment_offset: 0,
            ttl: 64,
            protocol: IPProtocol::ICMP,
            chksum: 0x22ed,
            source_addr: Ipv4Addr::new(10, 10, 1, 135),
            dest_addr: Ipv4Addr::new(10, 10, 1, 180),
        };
        let parsed_eth_frame = ethernet::parse_ethernet_frame(&bytes);
        if let Ok((remaining_data, eth_frame)) = parsed_eth_frame {
            assert_eq!(eth_frame, eth_expectation);
            let parsed_ip_hdr = ipv4::parse_ipv4_header(&remaining_data);
            if let Ok((_remaining_data, ip_hdr)) = parsed_ip_hdr {
                assert_eq!(ip_hdr, ip_expectation);
            } else {
                assert!(false);
            }
        } else {
            assert!(false);
        }
    }
}