IcmpTimestampMsgPart

Struct IcmpTimestampMsgPart 

Source
#[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

Source

pub const LEN: usize = 12usize

Source

pub fn originate_timestamp(&self) -> u32

Returns the originate timestamp in host byte order (milliseconds since midnight UT)

Source

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.

Source

pub fn receive_timestamp(&self) -> u32

Returns the receive timestamp in host byte order (milliseconds since midnight UT)

Source

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.

Source

pub fn transmit_timestamp(&self) -> u32

Returns the transmit timestamp in host byte order (milliseconds since midnight UT)

Source

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

Source§

fn clone(&self) -> IcmpTimestampMsgPart

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for IcmpTimestampMsgPart

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for IcmpTimestampMsgPart

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.