#[repr(C, packed(1))]pub struct IcmpTimestampMsgPart {
pub originate_timestamp: [u8; 4],
pub receive_timestamp: [u8; 4],
pub transmit_timestamp: [u8; 4],
}Expand description
Represents the variable length portion of a Timestamp Request/Reply message (RFC 792) that follows the ICMP header.
Timestamps are milliseconds since midnight UT and are stored in network byte order.
§Example
use core::mem;
use aya_ebpf::programs::TcContext;
use network_types::eth::EthHdr;
use network_types::icmp::{IcmpHdr, IcmpTimestampMsgPart};
use network_types::ip::Ipv4Hdr;
// Assuming aya_log_ebpf is available for logging, as per project dependencies.
// If not, remove or adapt the log lines.
// use aya_log_ebpf::{info, warn};
// This is an adaptation of the example code provided in the doc comment
// for IcmpTimestampMsgPart, corrected to resolve the E0599 error.
// The actual code at src/icmp.rs:355 likely follows this pattern.
fn handle_icmp_timestamp(ctx: &TcContext) -> Result<u32, ()> {
// Parse the ICMP header from start of payload
let icmp_start = ctx.data() + EthHdr::LEN + Ipv4Hdr::LEN;
// Boundary check: Ensure icmp_start is within packet bounds
// This check is simplified; a real check would involve ctx.data_end().
if icmp_start + IcmpHdr::LEN > ctx.data_end() {
// warn!(ctx, "ICMP header out of bounds");
return Err(());
}
let icmp: *const IcmpHdr = icmp_start as *const IcmpHdr;
// Check if it's a Timestamp message (type 13 or 14)
// Reading from a raw pointer is unsafe.
if unsafe { (*icmp).type_ } == 13 || unsafe { (*icmp).type_ } == 14 {
// Calculate pointer to the timestamp part
let timestamps_ptr_location = icmp_start + IcmpHdr::LEN;
// Boundary check: Ensure IcmpTimestampMsgPart is within packet bounds
if timestamps_ptr_location + IcmpTimestampMsgPart::LEN > ctx.data_end() {
// warn!(ctx, "ICMP timestamp message part out of bounds");
return Err(());
}
let timestamps_ptr: *const IcmpTimestampMsgPart = timestamps_ptr_location as *const IcmpTimestampMsgPart;
// Safely dereference the pointer to get a reference
match unsafe { timestamps_ptr.as_ref() } {
Some(timestamps_ref) => {
// Now you can read the timestamps in network byte order
let orig = timestamps_ref.originate_timestamp();
let recv = timestamps_ref.receive_timestamp();
let xmit = timestamps_ref.transmit_timestamp();
// You can now use orig, recv, and xmit. For example, log them:
// info!(ctx, "ICMP Timestamps: O={}, R={}, T={}", orig, recv, xmit);
// Placeholder for further processing:
// For example, return one of the timestamps or 0 for success.
}
None => {
// This case implies timestamps_ptr was null.
// While less common if pointer arithmetic is correct and data_end checks pass,
// it's good practice to handle it.
// warn!(ctx, "Failed to get reference to ICMP timestamps: pointer was null");
return Err(());
}
}
}
Ok(0)
}Fields§
§originate_timestamp: [u8; 4]§receive_timestamp: [u8; 4]§transmit_timestamp: [u8; 4]Implementations§
Source§impl IcmpTimestampMsgPart
impl IcmpTimestampMsgPart
pub const LEN: usize = 12usize
Sourcepub fn originate_timestamp(&self) -> u32
pub fn originate_timestamp(&self) -> u32
Returns the originate timestamp in host byte order (milliseconds since midnight UT)
Sourcepub fn set_originate_timestamp(&mut self, timestamp: u32)
pub fn set_originate_timestamp(&mut self, timestamp: u32)
Sets the originate timestamp field (milliseconds since midnight UT). The value will be stored in network byte order.
Sourcepub fn receive_timestamp(&self) -> u32
pub fn receive_timestamp(&self) -> u32
Returns the receive timestamp in host byte order (milliseconds since midnight UT)
Sourcepub fn set_receive_timestamp(&mut self, timestamp: u32)
pub fn set_receive_timestamp(&mut self, timestamp: u32)
Sets the receive timestamp field (milliseconds since midnight UT). The value will be stored in network byte order.
Sourcepub fn transmit_timestamp(&self) -> u32
pub fn transmit_timestamp(&self) -> u32
Returns the transmit timestamp in host byte order (milliseconds since midnight UT)
Sourcepub fn set_transmit_timestamp(&mut self, timestamp: u32)
pub fn set_transmit_timestamp(&mut self, timestamp: u32)
Sets the transmit timestamp field (milliseconds since midnight UT). The value will be stored in network byte order.
Trait Implementations§
Source§impl Clone for IcmpTimestampMsgPart
impl Clone for IcmpTimestampMsgPart
Source§fn clone(&self) -> IcmpTimestampMsgPart
fn clone(&self) -> IcmpTimestampMsgPart
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more