UdpHdr

Struct UdpHdr 

Source
#[repr(C)]
pub struct UdpHdr { pub src: [u8; 2], pub dst: [u8; 2], pub len: [u8; 2], pub check: [u8; 2], }
Expand description

UDP header, which is present after the IP header.

This struct represents the User Datagram Protocol (UDP) header as defined in RFC 768. The UDP header is 8 bytes long and contains source and destination ports, length, and checksum fields. All fields are stored in network byte order (big-endian).

§Example

use network_types::udp::UdpHdr;

let mut udp_header = UdpHdr {
    src: [0, 0],
    dst: [0, 0],
    len: [0, 0],
    check: [0, 0],
};

udp_header.set_src_port(12345);
udp_header.set_dst_port(80);
udp_header.set_len(28); // 8 bytes header + 20 bytes payload
udp_header.set_checksum(0); // Checksum calculation would be done separately

Fields§

§src: [u8; 2]

Source port in network byte order (big-endian)

§dst: [u8; 2]

Destination port in network byte order (big-endian)

§len: [u8; 2]

Length of UDP header and data in bytes, in network byte order (big-endian)

§check: [u8; 2]

Checksum of UDP header and data, in network byte order (big-endian)

Implementations§

Source§

impl UdpHdr

Source

pub const LEN: usize = 8usize

The size of the UDP header in bytes (8 bytes).

Source

pub fn src_port(&self) -> u16

Returns the source port number.

This method converts the source port from network byte order (big-endian) to host byte order.

§Returns

The source port as a u16 value.

Source

pub fn set_src_port(&mut self, src: u16)

Sets the source port number.

This method converts the source port from host byte order to network byte order (big-endian).

§Parameters
  • source - The source port number to set.
Source

pub fn dst_port(&self) -> u16

Returns the destination port number.

This method converts the destination port from network byte order (big-endian) to host byte order.

§Returns

The destination port as a u16 value.

Source

pub fn set_dst_port(&mut self, dst: u16)

Sets the destination port number.

This method converts the destination port from host byte order to network byte order (big-endian).

§Parameters
  • dest - The destination port number to set.
Source

pub fn len(&self) -> u16

Returns the length of the UDP datagram in bytes.

The length includes both the UDP header (8 bytes) and the UDP payload. This method converts the length from network byte order (big-endian) to host byte order.

§Returns

The length as a u16 value.

Source

pub fn is_empty(&self) -> bool

Returns true if the UDP length field is zero.

A zero length indicates an invalid or empty UDP datagram, as the minimum valid length is 8 bytes (the size of the UDP header).

§Returns

true if length is zero, false otherwise.

Source

pub fn set_len(&mut self, len: u16)

Sets the length of the UDP datagram in bytes.

The length should include both the UDP header (8 bytes) and the UDP payload. This method converts the length from host byte order to network byte order (big-endian).

§Parameters
  • len - The length to set in bytes.
Source

pub fn checksum(&self) -> u16

Returns the UDP checksum.

The checksum is calculated over the UDP header, the UDP payload, and a pseudo-header derived from the IP header. This method converts the checksum from network byte order (big-endian) to host byte order.

§Returns

The checksum as a u16 value.

Source

pub fn set_checksum(&mut self, check: u16)

Sets the UDP checksum.

The checksum should be calculated over the UDP header, the UDP payload, and a pseudo-header derived from the IP header. This method converts the checksum from host byte order to network byte order (big-endian).

A value of 0 indicates that the checksum is not used (IPv4 only).

§Parameters
  • check - The checksum value to set.

Trait Implementations§

Source§

impl Clone for UdpHdr

Source§

fn clone(&self) -> UdpHdr

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 UdpHdr

Source§

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

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

impl Copy for UdpHdr

Auto Trait Implementations§

§

impl Freeze for UdpHdr

§

impl RefUnwindSafe for UdpHdr

§

impl Send for UdpHdr

§

impl Sync for UdpHdr

§

impl Unpin for UdpHdr

§

impl UnwindSafe for UdpHdr

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.