summaryrefslogtreecommitdiff
path: root/src/packet/packet.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/packet/packet.rs')
-rw-r--r--src/packet/packet.rs89
1 files changed, 26 insertions, 63 deletions
diff --git a/src/packet/packet.rs b/src/packet/packet.rs
index 82dd4f1..59dfce1 100644
--- a/src/packet/packet.rs
+++ b/src/packet/packet.rs
@@ -32,35 +32,11 @@ pub enum Encapsulation<'a> {
UNSUPPORTED(&'a [u8]),
}
-#[allow(non_camel_case_types)]
-#[derive(Clone, Debug, PartialEq)]
-pub enum PacketEvent {
- // L2 Layer Event
- L2_EVENT,
- L2_ETH_EVENT,
- L2_VLAN_EVENT,
- L2_MPLS_EVENT,
- L2_PWETH_EVENT,
-
- // L3 Layer Event
- L3_EVENT,
- L3_IPV4_EVENT,
- L3_IPV6_EVENT,
-
- // L4 Layer Event
- L4_EVENT,
- L4_TCP_EVENT,
- L4_UDP_EVENT,
- L4_ICMP_EVENT,
- L4_ICMPV6_EVENT,
-}
-
#[derive(Debug)]
pub struct Packet<'a> {
pub orig_data: &'a [u8],
pub orig_len: u32,
pub encapsulation: Vec<Encapsulation<'a>>,
- pub event: Vec<PacketEvent>,
}
impl Packet<'_> {
@@ -69,7 +45,6 @@ impl Packet<'_> {
orig_data: data,
orig_len: len,
encapsulation: vec![],
- event: vec![],
}
}
@@ -376,14 +351,12 @@ fn handle_eth<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet
if let Ok((payload, header)) = result {
dbg!(&header);
+ let next_proto = header.ether_type;
packet
.encapsulation
.push(Encapsulation::L2_ETH(header, payload));
- packet.event.push(PacketEvent::L2_EVENT);
- packet.event.push(PacketEvent::L2_ETH_EVENT);
-
- return handle_l3(packet, payload, header.ether_type);
+ return handle_l3(packet, payload, next_proto);
} else {
return Err(PacketError::IncompleteEthernetFrame);
}
@@ -394,14 +367,12 @@ fn handle_vlan<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe
if let Ok((payload, header)) = result {
dbg!(&header);
+ let next_proto = header.ether_type;
packet
.encapsulation
.push(Encapsulation::L2_VLAN(header, payload));
- packet.event.push(PacketEvent::L2_EVENT);
- packet.event.push(PacketEvent::L2_VLAN_EVENT);
-
- return handle_l3(packet, payload, header.ether_type);
+ return handle_l3(packet, payload, next_proto);
} else {
return Err(PacketError::IncompleteVlanHeader);
}
@@ -412,14 +383,12 @@ fn handle_mpls<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe
if let Ok((payload, header)) = result {
dbg!(&header);
+ let bottom_of_stack = header.bottom_of_stack;
packet
.encapsulation
.push(Encapsulation::L2_MPLS(header, payload));
- packet.event.push(PacketEvent::L2_EVENT);
- packet.event.push(PacketEvent::L2_MPLS_EVENT);
-
- if header.bottom_of_stack {
+ if bottom_of_stack {
if payload.len() < 1 {
return Ok(());
}
@@ -448,9 +417,6 @@ fn handle_pw_eth<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Pac
.encapsulation
.push(Encapsulation::L2_PWETH(header, payload));
- packet.event.push(PacketEvent::L2_EVENT);
- packet.event.push(PacketEvent::L2_PWETH_EVENT);
-
return handle_eth(packet, payload);
} else {
return Err(PacketError::IncompletePwEthHeader);
@@ -462,16 +428,14 @@ fn handle_ipv4<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe
if let Ok((payload, header)) = result {
dbg!(&header);
+ let next_proto = header.protocol;
packet
.encapsulation
.push(Encapsulation::L3_IPV4(header, payload));
// TODO IPv4 Fragment
- packet.event.push(PacketEvent::L3_EVENT);
- packet.event.push(PacketEvent::L3_IPV4_EVENT);
-
- return handle_l4(packet, payload, header.protocol);
+ return handle_l4(packet, payload, next_proto);
} else {
return Err(PacketError::IncompleteIpv4Header);
}
@@ -482,16 +446,20 @@ fn handle_ipv6<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe
if let Ok((payload, header)) = result {
dbg!(&header);
+ let mut next_proto = header.next_header;
+ for extension in header.extensions.iter() {
+ next_proto = extension.next_header;
+
+ if next_proto == IPProtocol::IPV6_FRAGMENT_HDR {
+ // TODO IPv6 Fragment
+ }
+ }
+
packet
.encapsulation
.push(Encapsulation::L3_IPV6(header, payload));
- // TODO IPv6 Fragment
-
- packet.event.push(PacketEvent::L3_EVENT);
- packet.event.push(PacketEvent::L3_IPV6_EVENT);
-
- return handle_l4(packet, payload, header.next_header);
+ return handle_l4(packet, payload, next_proto);
} else {
return Err(PacketError::IncompleteIpv6Header);
}
@@ -508,9 +476,6 @@ fn handle_tcp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet
// TODO TCP Reassembly
- packet.event.push(PacketEvent::L4_EVENT);
- packet.event.push(PacketEvent::L4_TCP_EVENT);
-
return Ok(());
} else {
return Err(PacketError::IncompleteTcpHeader);
@@ -526,9 +491,6 @@ fn handle_udp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet
.encapsulation
.push(Encapsulation::L4_UDP(header, payload));
- packet.event.push(PacketEvent::L4_EVENT);
- packet.event.push(PacketEvent::L4_UDP_EVENT);
-
return Ok(());
} else {
return Err(PacketError::IncompleteUdpHeader);
@@ -544,9 +506,6 @@ fn handle_icmp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe
.encapsulation
.push(Encapsulation::L4_ICMP(header, payload));
- packet.event.push(PacketEvent::L4_EVENT);
- packet.event.push(PacketEvent::L4_ICMP_EVENT);
-
return Ok(());
} else {
return Err(PacketError::IncompleteIcmpHeader);
@@ -562,9 +521,6 @@ fn handle_icmpv6<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Pac
.encapsulation
.push(Encapsulation::L4_ICMPV6(header, payload));
- packet.event.push(PacketEvent::L4_EVENT);
- packet.event.push(PacketEvent::L4_ICMPV6_EVENT);
-
return Ok(());
} else {
return Err(PacketError::IncompleteIcmpv6Header);
@@ -662,6 +618,7 @@ mod tests {
dest_address: Ipv6Addr::new(
0x2409, 0x8034, 0x4040, 0x5301, 0x0000, 0x0000, 0x0000, 0x0204,
),
+ extensions: Vec::new(),
};
let tcp_hdr = TcpHeader {
source_port: 50081,
@@ -929,7 +886,6 @@ mod tests {
&bytes[42..]
)
);
-
assert_eq!(
packet.encapsulation[4],
Encapsulation::L4_TCP(
@@ -954,6 +910,7 @@ mod tests {
&bytes[62..]
)
);
+
// assert_eq!(1, 0);
}
@@ -1152,6 +1109,8 @@ mod tests {
&bytes[62..]
)
)
+
+ // assert_eq!(1, 0);
}
#[test]
@@ -1348,6 +1307,7 @@ mod tests {
hop_limit: 64,
source_address: Ipv6Addr::new(0x2001, 0x0, 0x0, 0x0, 0x192, 0x168, 0x40, 0x134),
dest_address: Ipv6Addr::new(0x2001, 0x0, 0x0, 0x0, 0x192, 0x168, 0x40, 0x133),
+ extensions: Vec::new(),
},
&bytes[54..]
)
@@ -1574,6 +1534,7 @@ mod tests {
dest_address: Ipv6Addr::new(
0x2600, 0x140e, 0x0006, 0x0000, 0x0000, 0x0000, 0x1702, 0x1058
),
+ extensions: Vec::new(),
},
&bytes[74..]
)
@@ -1720,6 +1681,7 @@ mod tests {
dest_address: Ipv6Addr::new(
0x2001, 0x04f8, 0x0004, 0x0007, 0x02e0, 0x81ff, 0xfe52, 0x9a6b
),
+ extensions: Vec::new(),
},
&bytes[54..]
)
@@ -1741,6 +1703,7 @@ mod tests {
dest_address: Ipv6Addr::new(
0xcafe, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xbabe
),
+ extensions: Vec::new(),
},
&bytes[94..]
)