#[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 separatelyFields§
§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
impl UdpHdr
Sourcepub fn src_port(&self) -> u16
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.
Sourcepub fn set_src_port(&mut self, src: u16)
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.
Sourcepub fn dst_port(&self) -> u16
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.
Sourcepub fn set_dst_port(&mut self, dst: u16)
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.
Sourcepub fn len(&self) -> u16
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.
Sourcepub fn is_empty(&self) -> bool
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.
Sourcepub fn set_len(&mut self, len: u16)
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.
Sourcepub fn checksum(&self) -> u16
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.
Sourcepub fn set_checksum(&mut self, check: u16)
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.