Skip to main content

Ipv4Builder

Struct Ipv4Builder 

Source
pub struct Ipv4Builder { /* private fields */ }
Expand description

Builder for IPv4 packets.

§Example

use stackforge_core::layer::ipv4::{Ipv4Builder, protocol};
use std::net::Ipv4Addr;

let packet = Ipv4Builder::new()
    .src(Ipv4Addr::new(192, 168, 1, 1))
    .dst(Ipv4Addr::new(192, 168, 1, 2))
    .ttl(64)
    .protocol(protocol::TCP)
    .dont_fragment()
    .build();

assert_eq!(packet.len(), 20); // Minimum header, no payload

Implementations§

Source§

impl Ipv4Builder

Source

pub fn new() -> Self

Create a new IPv4 builder with default values.

Source

pub fn from_bytes(data: &[u8]) -> Result<Self, FieldError>

Create a builder initialized from an existing packet.

Source

pub fn version(self, version: u8) -> Self

Set the IP version (should normally be 4).

Source

pub fn ihl(self, ihl: u8) -> Self

Set the Internet Header Length (in 32-bit words). If not set, will be calculated automatically.

Source

pub fn tos(self, tos: u8) -> Self

Set the Type of Service field.

Source

pub fn dscp(self, dscp: u8) -> Self

Set the DSCP (Differentiated Services Code Point).

Source

pub fn ecn(self, ecn: u8) -> Self

Set the ECN (Explicit Congestion Notification).

Source

pub fn total_len(self, len: u16) -> Self

Set the total length field. If not set, will be calculated automatically.

Source

pub fn len(self, len: u16) -> Self

Alias for total_len (Scapy compatibility).

Source

pub fn id(self, id: u16) -> Self

Set the identification field.

Source

pub fn flags(self, flags: Ipv4Flags) -> Self

Set the flags field.

Source

pub fn dont_fragment(self) -> Self

Set the Don’t Fragment flag.

Source

pub fn allow_fragment(self) -> Self

Clear the Don’t Fragment flag.

Source

pub fn more_fragments(self) -> Self

Set the More Fragments flag.

Source

pub fn evil(self) -> Self

Set the reserved/evil bit.

Source

pub fn frag_offset(self, offset: u16) -> Self

Set the fragment offset (in 8-byte units).

Source

pub fn frag_offset_bytes(self, offset: u32) -> Self

Set the fragment offset in bytes (will be divided by 8).

Source

pub fn ttl(self, ttl: u8) -> Self

Set the TTL (Time to Live).

Source

pub fn protocol(self, protocol: u8) -> Self

Set the protocol number.

Source

pub fn proto(self, protocol: u8) -> Self

Alias for protocol (Scapy compatibility).

Source

pub fn checksum(self, checksum: u16) -> Self

Set the checksum manually. If not set, will be calculated automatically.

Source

pub fn chksum(self, checksum: u16) -> Self

Alias for checksum (Scapy compatibility).

Source

pub fn src(self, src: Ipv4Addr) -> Self

Set the source IP address.

Source

pub fn dst(self, dst: Ipv4Addr) -> Self

Set the destination IP address.

Source

pub fn options(self, options: Ipv4Options) -> Self

Set the options.

Source

pub fn option(self, option: Ipv4Option) -> Self

Add a single option.

Source

pub fn with_options<F>(self, f: F) -> Self

Add options using a builder function.

Source

pub fn record_route(self, slots: usize) -> Self

Add a Record Route option.

Source

pub fn lsrr(self, route: Vec<Ipv4Addr>) -> Self

Add a Loose Source Route option.

Source

pub fn ssrr(self, route: Vec<Ipv4Addr>) -> Self

Add a Strict Source Route option.

Source

pub fn router_alert(self, value: u16) -> Self

Add a Router Alert option.

Source

pub fn payload(self, payload: impl Into<Vec<u8>>) -> Self

Set the payload data.

Source

pub fn append_payload(self, data: &[u8]) -> Self

Append data to the payload.

Source

pub fn auto_checksum(self, enabled: bool) -> Self

Enable or disable automatic checksum calculation.

Source

pub fn auto_length(self, enabled: bool) -> Self

Enable or disable automatic length calculation.

Source

pub fn auto_ihl(self, enabled: bool) -> Self

Enable or disable automatic IHL calculation.

Source

pub fn header_size(&self) -> usize

Calculate the header size (including options).

Source

pub fn packet_size(&self) -> usize

Calculate the total packet size.

Source

pub fn build(&self) -> Vec<u8>

Build the IPv4 packet.

Source

pub fn build_into(&self, buf: &mut [u8]) -> Result<usize, FieldError>

Build the IPv4 packet into an existing buffer.

Source

pub fn build_header(&self) -> Vec<u8>

Build only the header (no payload).

Source§

impl Ipv4Builder

Source

pub fn icmp() -> Self

Create an ICMP packet builder.

Source

pub fn tcp() -> Self

Create a TCP packet builder.

Source

pub fn udp() -> Self

Create a UDP packet builder.

Source

pub fn ipip() -> Self

Create an IP-in-IP tunnel packet builder.

Source

pub fn gre() -> Self

Create a GRE tunnel packet builder.

Source

pub fn to(dst: Ipv4Addr) -> Self

Create a packet destined for a specific address.

Source

pub fn from(src: Ipv4Addr) -> Self

Create a packet from a specific source.

Source§

impl Ipv4Builder

Source

pub fn random_id(self) -> Self

Set a random ID.

Trait Implementations§

Source§

impl Clone for Ipv4Builder

Source§

fn clone(&self) -> Ipv4Builder

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 Ipv4Builder

Source§

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

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

impl Default for Ipv4Builder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Ipv4Builder> for LayerStack

Source§

fn from(builder: Ipv4Builder) -> Self

Converts to this type from the input type.
Source§

impl IntoLayerStackEntry for Ipv4Builder

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V