diff options
| author | luwenpeng <[email protected]> | 2023-07-28 11:17:23 +0800 |
|---|---|---|
| committer | luwenpeng <[email protected]> | 2023-08-01 11:44:26 +0800 |
| commit | 993825d24a9d87831043d7a8cf492cf00ab131e6 (patch) | |
| tree | 5bb31ee085cab84d848044c4bafe781d386bfafe /src/protocol/ip.rs | |
| parent | 53c5af3ec08f55933abc8983827a40171374aaf1 (diff) | |
[feature] Support IPProtocol Decode
Diffstat (limited to 'src/protocol/ip.rs')
| -rw-r--r-- | src/protocol/ip.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/protocol/ip.rs b/src/protocol/ip.rs new file mode 100644 index 0000000..3a2f6bd --- /dev/null +++ b/src/protocol/ip.rs @@ -0,0 +1,71 @@ +use nom::number; +use nom::IResult; + +/****************************************************************************** + * Struct + ******************************************************************************/ + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +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), +} + +/****************************************************************************** + * API + ******************************************************************************/ + +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), + } + } +} + +impl IPProtocol { + pub fn decode(input: &[u8]) -> IResult<&[u8], IPProtocol> { + let (input, protocol) = number::streaming::be_u8(input)?; + + Ok((input, protocol.into())) + } +} |
