Skip to content

Commit

Permalink
Add packet length to CaptureResult enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ruieduardolopes committed Oct 14, 2019
1 parent 8ce6c62 commit 51d65e6
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/capture/interfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ pub fn run_capture(
match execute_once((&mut tx, &mut rx)) {
Ok(result) => {
match result.0 {
CaptureResult::Other(timestamp) => continue,
CaptureResult::Other(timestamp, packet_length) => continue,
_ => (),
};
worker_on_queue.push(result.0);
Expand Down
22 changes: 14 additions & 8 deletions src/capture/osi/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ pub fn handle_ipv4_packet(packet: &EthernetPacket) -> Result<CaptureResult, Capt
match ipv4_packet.get_next_level_protocol() {
// This payload corresponds to an IPv4 packet.
IpNextHeaderProtocols::Ipv4 => {
handle_ipv4_tunneled_packet(ipv4_packet.payload(), ipv4_packet.get_destination())
handle_ipv4_tunneled_packet(ipv4_packet.payload(), ipv4_packet.get_destination(), ipv4_packet.get_total_length())
}
// This payload corresponds to an IPv6 packet.
IpNextHeaderProtocols::Ipv6 => {
handle_ipv6_tunneled_packet(ipv4_packet.payload(), ipv4_packet.get_destination())
handle_ipv6_tunneled_packet(ipv4_packet.payload(), ipv4_packet.get_destination(), ipv4_packet.get_total_length())
}
// This payload either is a transport layer packet to be handled or is a malformed packet
_ => transport::handle(
ipv4_packet.get_next_level_protocol(),
ipv4_packet.payload(),
ipv4_packet.get_destination(),
ipv4_packet.get_total_length()
),
}
} else {
Expand All @@ -58,6 +59,7 @@ pub fn handle_ipv4_packet(packet: &EthernetPacket) -> Result<CaptureResult, Capt
pub fn handle_ipv4_tunneled_packet(
packet: &[u8],
_last_dest_address: Ipv4Addr,
packet_length: u16,
) -> Result<CaptureResult, CaptureError> {
// Read the received packet's payload as an IPv4 packet.
let ipv4_packet = Ipv4Packet::new(packet);
Expand All @@ -68,17 +70,18 @@ pub fn handle_ipv4_tunneled_packet(
match ipv4_packet.get_next_level_protocol() {
// This payload corresponds to an IPv4 packet.
IpNextHeaderProtocols::Ipv4 => {
handle_ipv4_tunneled_packet(ipv4_packet.payload(), ipv4_packet.get_destination())
handle_ipv4_tunneled_packet(ipv4_packet.payload(), ipv4_packet.get_destination(), packet_length)
}
// This payload corresponds to an IPv6 packet.
IpNextHeaderProtocols::Ipv6 => {
handle_ipv6_tunneled_packet(ipv4_packet.payload(), ipv4_packet.get_destination())
handle_ipv6_tunneled_packet(ipv4_packet.payload(), ipv4_packet.get_destination(), packet_length)
}
// This payload either is a transport layer packet to be handled or is a malformed packet
_ => transport::handle(
ipv4_packet.get_next_level_protocol(),
ipv4_packet.payload(),
ipv4_packet.get_destination(),
packet_length
),
}
} else {
Expand All @@ -102,17 +105,18 @@ pub fn handle_ipv6_packet(packet: &EthernetPacket) -> Result<CaptureResult, Capt
match ipv6_packet.get_next_header() {
// This payload corresponds to an IPv4 packet.
IpNextHeaderProtocols::Ipv4 => {
handle_ipv4_tunneled_packet(ipv6_packet.payload(), Ipv4Addr::new(0, 0, 0, 0))
handle_ipv4_tunneled_packet(ipv6_packet.payload(), Ipv4Addr::new(0, 0, 0, 0), ipv6_packet.get_payload_length())
}
// This payload corresponds to an IPv6 packet.
IpNextHeaderProtocols::Ipv6 => {
handle_ipv6_tunneled_packet(ipv6_packet.payload(), Ipv4Addr::new(0, 0, 0, 0))
handle_ipv6_tunneled_packet(ipv6_packet.payload(), Ipv4Addr::new(0, 0, 0, 0), ipv6_packet.get_payload_length())
}
// This payload either is a transport layer packet to be handled or is a malformed packet
_ => transport::handle(
ipv6_packet.get_next_header(),
ipv6_packet.payload(),
Ipv4Addr::new(0, 0, 0, 0),
ipv6_packet.get_payload_length()
),
}
} else {
Expand All @@ -129,6 +133,7 @@ pub fn handle_ipv6_packet(packet: &EthernetPacket) -> Result<CaptureResult, Capt
pub fn handle_ipv6_tunneled_packet(
packet: &[u8],
last_dest_address: Ipv4Addr,
packet_length: u16
) -> Result<CaptureResult, CaptureError> {
// Read the received packet's payload as an IPv6 packet.
let ipv6_packet = Ipv6Packet::new(packet);
Expand All @@ -139,17 +144,18 @@ pub fn handle_ipv6_tunneled_packet(
match ipv6_packet.get_next_header() {
// This payload corresponds to an IPv4 packet.
IpNextHeaderProtocols::Ipv4 => {
handle_ipv4_tunneled_packet(ipv6_packet.payload(), last_dest_address)
handle_ipv4_tunneled_packet(ipv6_packet.payload(), last_dest_address, packet_length)
}
// This payload corresponds to an IPv6 packet.
IpNextHeaderProtocols::Ipv6 => {
handle_ipv6_tunneled_packet(ipv6_packet.payload(), last_dest_address)
handle_ipv6_tunneled_packet(ipv6_packet.payload(), last_dest_address, packet_length)
}
// This payload either is a transport layer packet to be handled or is a malformed packet
_ => transport::handle(
ipv6_packet.get_next_header(),
ipv6_packet.payload(),
last_dest_address,
packet_length
),
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/capture/osi/transport/control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn handle_rtcp(
packet: &[u8],
dest_address: Ipv4Addr,
port: u16,
packet_length: u16,
) -> Result<CaptureResult, CaptureError> {
let rtcp_packet = RtcpPacket::new(packet);

Expand All @@ -29,6 +30,7 @@ pub fn handle_rtcp(
sender_report.get_ssrc(),
dest_address,
port,
packet_length,
sender_report.get_rtp_timestamp(),
time::now_utc().to_timespec().sec,
));
Expand Down
13 changes: 8 additions & 5 deletions src/capture/osi/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::net::Ipv4Addr;
/// # Arguments
///
/// * `packet` - A byte-slice reference to a TCP packet
fn handle_tcp(packet: &[u8], _dest_address: Ipv4Addr) -> Result<(), CaptureError> {
fn handle_tcp(packet: &[u8], _dest_address: Ipv4Addr, packet_length: u16) -> Result<(), CaptureError> {
// Create the TCP packet from the raw data given as parameter to this function.
let tcp_packet = TcpPacket::new(packet);

Expand All @@ -43,7 +43,7 @@ fn handle_tcp(packet: &[u8], _dest_address: Ipv4Addr) -> Result<(), CaptureError
/// # Arguments
///
/// * `packet` - A byte-slice reference to a UDP packet
fn handle_udp(packet: &[u8], dest_address: Ipv4Addr) -> Result<CaptureResult, CaptureError> {
fn handle_udp(packet: &[u8], dest_address: Ipv4Addr, packet_length: u16) -> Result<CaptureResult, CaptureError> {
// Create the UDP packet from the raw data given as parameter to this function.
let udp_packet = UdpPacket::new(packet);

Expand All @@ -55,6 +55,7 @@ fn handle_udp(packet: &[u8], dest_address: Ipv4Addr) -> Result<CaptureResult, Ca
udp_packet.payload(),
dest_address,
udp_packet.get_destination(),
packet_length
);
if result.is_ok() {
return result;
Expand All @@ -63,6 +64,7 @@ fn handle_udp(packet: &[u8], dest_address: Ipv4Addr) -> Result<CaptureResult, Ca
udp_packet.payload(),
dest_address,
udp_packet.get_destination(),
packet_length
)
} else {
// If an error occurs, then we consider that this UDP packet was malformed.
Expand All @@ -81,16 +83,17 @@ pub fn handle(
protocol: IpNextHeaderProtocol,
packet: &[u8],
dest_address: Ipv4Addr,
packet_length: u16
) -> Result<CaptureResult, CaptureError> {
// Verify the inner transport protocol to handle it accordingly.
match protocol {
// If this packet is a TCP, handle it as TCP.
IpNextHeaderProtocols::Tcp => {
handle_tcp(packet, dest_address).unwrap();
Ok(OtherResult::launch(0))
handle_tcp(packet, dest_address, packet_length).unwrap();
Ok(OtherResult::launch(packet_length, 0))
}
// If this packet is a UDP, handle it as UDP.
IpNextHeaderProtocols::Udp => match handle_udp(packet, dest_address) {
IpNextHeaderProtocols::Udp => match handle_udp(packet, dest_address, packet_length) {
Ok(packet) => Ok(packet),
Err(_error) => Err(_error),
},
Expand Down
22 changes: 20 additions & 2 deletions src/capture/osi/transport/multimedia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn handle_rtp(
packet: &[u8],
dest_address: Ipv4Addr,
port: u16,
packet_length: u16,
) -> Result<CaptureResult, CaptureError> {
// Create an RTP packet from the raw data given as parameter to this function.
let rtp_packet = RtpPacket::new(packet);
Expand All @@ -39,6 +40,7 @@ pub fn handle_rtp(
rtp_packet.get_ssrc(),
dest_address,
port,
packet_length,
rtp_packet.get_timestamp(),
),
// If this packet has a payload which type is unknown to us, return proper error.
Expand All @@ -61,6 +63,7 @@ pub fn handle_h264(
ssrc: u32,
dest_address: Ipv4Addr,
port: u16,
packet_length: u16,
timestamp: u32,
) -> Result<CaptureResult, CaptureError> {
// Create an RTP's H.264 packet from the raw data given as parameter to this function.
Expand All @@ -71,14 +74,15 @@ pub fn handle_h264(
// Identify this packet's type.
match h264_packet.get_packet_type() {
// If this packet's type is a Type-A Fragment Unit, then handle it accordingly.
nal::FU_A => handle_fu_a(h264_packet.payload(), ssrc, dest_address, port, timestamp),
nal::FU_A => handle_fu_a(h264_packet.payload(), ssrc, dest_address, port, packet_length, timestamp),
// If this packet's type is an IDR Partition Type Unit, then handle it accordingly.
nal::IDR_PARTITION => {
println!("This is a I-frame");
Ok(FrameResult::launch(
ssrc,
dest_address,
port,
packet_length,
timestamp,
MPEGType::I,
time::now_utc().to_timespec().sec,
Expand All @@ -104,6 +108,7 @@ pub fn handle_fu_a(
ssrc: u32,
dest_address: Ipv4Addr,
port: u16,
packet_length: u16,
timestamp: u32,
) -> Result<CaptureResult, CaptureError> {
// Create an RTP's H.264 FU-A packet from the raw data given as parameter to this function.
Expand All @@ -121,6 +126,7 @@ pub fn handle_fu_a(
ssrc,
dest_address,
port,
packet_length,
timestamp,
time::now_utc().to_timespec().sec,
))
Expand All @@ -129,12 +135,14 @@ pub fn handle_fu_a(
ssrc,
dest_address,
port,
packet_length,
timestamp,
time::now_utc().to_timespec().sec,
))
}
} else {
handle_slice(fu_a_packet.payload(), ssrc, dest_address, port, timestamp)
handle_slice(fu_a_packet.payload(), ssrc, dest_address, port,
packet_length,timestamp)
}
}
// If this packet's unit type is an IDR slice (I-slice), handle it.
Expand All @@ -144,6 +152,7 @@ pub fn handle_fu_a(
ssrc,
dest_address,
port,
packet_length,
timestamp,
MPEGType::I,
time::now_utc().to_timespec().sec,
Expand All @@ -155,11 +164,13 @@ pub fn handle_fu_a(
// If this packet's unit type is a SPS, handle it.
nal::SPS => Ok(SequenceParameterSetResult::launch(
dest_address,
packet_length,
time::now_utc().to_timespec().sec,
)),
// If this packet's unit type is a PPS, handle it.
nal::PPS => Ok(PictureParameterSetResult::launch(
dest_address,
packet_length,
time::now_utc().to_timespec().sec,
)),
// If this packet's unit type is unknown to us, return a proper error.
Expand All @@ -182,6 +193,7 @@ pub fn handle_slice(
ssrc: u32,
dest_address: Ipv4Addr,
stream_port: u16,
packet_length: u16,
timestamp: u32,
) -> Result<CaptureResult, CaptureError> {
// Create an RTP's H.264 FU-A Slice packet from the raw data given as parameter to this function.
Expand All @@ -196,6 +208,7 @@ pub fn handle_slice(
ssrc,
dest_address,
stream_port,
packet_length,
timestamp,
MPEGType::P,
time::now_utc().to_timespec().sec,
Expand All @@ -205,6 +218,7 @@ pub fn handle_slice(
ssrc,
dest_address,
stream_port,
packet_length,
timestamp,
MPEGType::B,
time::now_utc().to_timespec().sec,
Expand All @@ -214,6 +228,7 @@ pub fn handle_slice(
ssrc,
dest_address,
stream_port,
packet_length,
timestamp,
MPEGType::I,
time::now_utc().to_timespec().sec,
Expand All @@ -227,6 +242,7 @@ pub fn handle_slice(
ssrc,
dest_address,
stream_port,
packet_length,
timestamp,
MPEGType::P,
time::now_utc().to_timespec().sec,
Expand All @@ -236,6 +252,7 @@ pub fn handle_slice(
ssrc,
dest_address,
stream_port,
packet_length,
timestamp,
MPEGType::B,
time::now_utc().to_timespec().sec,
Expand All @@ -245,6 +262,7 @@ pub fn handle_slice(
ssrc,
dest_address,
stream_port,
packet_length,
timestamp,
MPEGType::I,
time::now_utc().to_timespec().sec,
Expand Down
Loading

0 comments on commit 51d65e6

Please sign in to comment.