pub struct Packet { /* private fields */ }
Expand description
represents a network packet. can be used to build or parse packets.
Implementations§
Source§impl Packet
impl Packet
Sourcepub fn new(protos: Vec<Protocol>) -> Self
pub fn new(protos: Vec<Protocol>) -> Self
creates a new Packet
with the internal buffer capacity set to the appropriate size for the header data.
note that the headers arent created with this method, you still have to add them with add_header.
Sourcepub fn new_empty() -> Self
pub fn new_empty() -> Self
Creates a new Packet
with an empty internal buffer and the capacity is 0
Sourcepub fn add_header(&mut self, buf: impl Header)
pub fn add_header(&mut self, buf: impl Header)
Adds the header into the internal packet buffer.
If the header is TCP or UDP, this method will call the set_pseudo_header
method for you,
as this method is required to be called before calculating the checksum of the header
Sourcepub fn update_header(&mut self, new_buf: impl Header)
pub fn update_header(&mut self, new_buf: impl Header)
If the header already exists in the packet, it will be updated with the one passed to this function.
if the header doesn’t already exist in the packet, it will be added as if you’d called add_header
instead.
Sourcepub fn extend_payload<T: IntoIterator<Item = u8>>(&mut self, buf: T)
pub fn extend_payload<T: IntoIterator<Item = u8>>(&mut self, buf: T)
Appends the given data to the payload of this packet
Sourcepub fn into_vec(self) -> Vec<u8> ⓘ
pub fn into_vec(self) -> Vec<u8> ⓘ
consumes self and returns the buffer which is the cooked data packet.
Sourcepub fn parse(raw_data: &[u8]) -> Result<Self, ParseError>
pub fn parse(raw_data: &[u8]) -> Result<Self, ParseError>
Try to create a Packet
from raw packet data and populate it with the values in the given data packet
Sourcepub fn get_header_as_slice(&self, p: Protocol) -> Option<&[u8]>
pub fn get_header_as_slice(&self, p: Protocol) -> Option<&[u8]>
Returns Option::Some(&[u8])
if the header is found in this packet, else None