summaryrefslogtreecommitdiff
path: root/src/protocol/ip.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/protocol/ip.rs')
-rw-r--r--src/protocol/ip.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/protocol/ip.rs b/src/protocol/ip.rs
new file mode 100644
index 0000000..4c6dddf
--- /dev/null
+++ b/src/protocol/ip.rs
@@ -0,0 +1,61 @@
+use nom::number;
+use nom::IResult;
+
+#[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),
+}
+
+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 protocol(input: &[u8]) -> IResult<&[u8], IPProtocol> {
+ let (input, protocol) = number::streaming::be_u8(input)?;
+
+ Ok((input, protocol.into()))
+}