Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[34] calculate ip extension lenght based on version #68

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/ip_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::packet::ipv4::Ipv4Packet;
use pnet::packet::ipv6::Ipv6Packet;

pub struct IpOptions;
use pnet::packet::Packet;

/// Utility struct for handling IP header options and extension headers.
/// Provides methods to calculate the length of optional headers in both IPv4 and IPv6 packets.
impl IpOptions {
pub fn calculate_ipv4_length(packet: &Ipv4Packet) -> u8 {
// IHL (Internet Header Length) is in 32-bit words
// Subtract minimum header length (20 bytes = 5 words)
let ihl = packet.get_header_length();
let options_length: u8 = if ihl > 5 {
(ihl - 5) * 4 // Convert words to bytes
} else {
0 // No options: standard header only
};
options_length
}

pub fn calculate_ipv6_length(packet: &Ipv6Packet) -> u8 {
// Most packets will be direct TCP
if packet.get_next_header() == IpNextHeaderProtocols::Tcp {
return 0;
}

let payload = packet.payload();
if payload.is_empty() {
return 0;
}

let len = match packet.get_next_header() {
IpNextHeaderProtocols::Ipv6Frag => 8,
_ => {
if payload.len() >= 2 {
(payload[1] as usize + 1) * 8
} else {
0
}
}
};

len as u8
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ipv4_options_length() {
let mut data = vec![0u8; 24];
data[0] = 0x46; // Version 4, IHL 6
let packet = Ipv4Packet::new(&data).unwrap();

assert_eq!(IpOptions::calculate_ipv4_length(&packet), 4);
}

#[test]
fn test_ipv6_direct_tcp() {
let mut data = vec![0u8; 40];
data[0] = 0x60; // Version 6
data[6] = IpNextHeaderProtocols::Tcp.0; // Next Header = TCP

let packet = Ipv6Packet::new(&data).unwrap();
assert_eq!(IpOptions::calculate_ipv6_length(&packet), 0);
}

#[test]
fn test_ipv6_fragment() {
let mut data = vec![0u8; 48];
data[0] = 0x60; // Version 6
data[6] = IpNextHeaderProtocols::Ipv6Frag.0; // Next Header = Fragment
data[4] = 0; // Length high byte
data[5] = 8; // Length low byte
data[40] = IpNextHeaderProtocols::Tcp.0; // Next Header = TCP

let packet = Ipv6Packet::new(&data).unwrap();
assert_eq!(IpOptions::calculate_ipv6_length(&packet), 8);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod display;
mod http;
mod http_languages;
mod http_process;
mod ip_options;
mod mtu;
mod p0f_output;
mod process;
Expand Down
5 changes: 3 additions & 2 deletions src/tcp_process.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ip_options::IpOptions;
use crate::mtu::ObservableMtu;
use crate::process::IpPort;
use crate::tcp;
Expand Down Expand Up @@ -73,7 +74,7 @@ pub fn process_tcp_ipv4(
let version = IpVersion::V4;
let ttl_observed: u8 = packet.get_ttl();
let ttl: Ttl = ttl::calculate_ttl(ttl_observed);
let olen: u8 = packet.get_options_raw().len() as u8;
let olen: u8 = IpOptions::calculate_ipv4_length(packet);
let mut quirks = vec![];

if (packet.get_ecn() & (IP_TOS_CE | IP_TOS_ECT)) != 0 {
Expand Down Expand Up @@ -128,7 +129,7 @@ pub fn process_tcp_ipv6(
let version = IpVersion::V6;
let ttl_observed: u8 = packet.get_hop_limit();
let ttl: Ttl = ttl::calculate_ttl(ttl_observed);
let olen = 0; // TODO handle extensions
let olen: u8 = IpOptions::calculate_ipv6_length(packet);
let mut quirks = vec![];

if packet.get_flow_label() != 0 {
Expand Down