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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! Handles parsing of Internet Protocol fields (shared between ipv4 and ipv6)

use nom::bits;
use nom::error::Error;
use nom::number;
use nom::sequence;
use nom::IResult;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum IPProtocol {
    HOPOPT,
    ICMP,
    IGMP,
    GGP,
    IPINIP,
    ST,
    TCP,
    CBT,
    EGP,
    IGP,
    BBNRCCMON,
    NVPII,
    PUP,
    ARGUS,
    EMCON,
    XNET,
    CHAOS,
    UDP,
    IPV6,
    ICMP6,
    Other(u8),
}

impl From<u8> for IPProtocol {
    fn from(raw: u8) -> Self {
        match raw {
            0 => IPProtocol::HOPOPT,
            1 => IPProtocol::ICMP,
            2 => IPProtocol::IGMP,
            3 => IPProtocol::GGP,
            4 => IPProtocol::IPINIP,
            5 => IPProtocol::ST,
            6 => IPProtocol::TCP,
            7 => IPProtocol::CBT,
            8 => IPProtocol::EGP,
            9 => IPProtocol::IGP,
            10 => IPProtocol::BBNRCCMON,
            11 => IPProtocol::NVPII,
            12 => IPProtocol::PUP,
            13 => IPProtocol::ARGUS,
            14 => IPProtocol::EMCON,
            15 => IPProtocol::XNET,
            16 => IPProtocol::CHAOS,
            17 => IPProtocol::UDP,
            41 => IPProtocol::IPV6,
            58 => IPProtocol::ICMP6,
            other => IPProtocol::Other(other),
        }
    }
}

pub(crate) fn two_nibbles(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
    bits::bits::<_, _, Error<_>, _, _>(sequence::pair(
        bits::streaming::take(4u8),
        bits::streaming::take(4u8),
    ))(input)
}

pub(crate) fn protocol(input: &[u8]) -> IResult<&[u8], IPProtocol> {
    let (input, protocol) = number::streaming::be_u8(input)?;

    Ok((input, protocol.into()))
}