pub struct PacketBuilder {}
Available on crate feature std only.
Expand description

Helper for building packets.

The packet builder allows the easy construction of a packet from the ethernet II layer downwards including ipv6, ipv4, the udp header and the actual payload. The packet builder automatically calculates lengths & checksums for ip & udp and set type identifiers for ethernetII and ip. This makes it easy and less error prone to construct custom packets.

§Example:

Generating a packet that starts with an Ethernet II header:

use etherparse::PacketBuilder;

let builder = PacketBuilder::
    ethernet2([1,2,3,4,5,6],     //source mac
              [7,8,9,10,11,12]) //destination mac
   .ipv4([192,168,1,1], //source ip
         [192,168,1,2], //destination ip
         20)            //time to life
   .udp(21,    //source port
        1234); //destination port

//payload of the udp packet
let payload = [1,2,3,4,5,6,7,8];

//get some memory to store the result
let mut result = Vec::<u8>::with_capacity(
                    builder.size(payload.len()));

//serialize
builder.write(&mut result, &payload).unwrap();
println!("{:?}", result);

§Options

Implementations§

source§

impl PacketBuilder

source

pub fn ethernet2( source: [u8; 6], destination: [u8; 6] ) -> PacketBuilderStep<Ethernet2Header>

Start an packet with an ethernetII header.

§Example

Basic usage:

let builder = PacketBuilder::
    ethernet2([1,2,3,4,5,6],     //source mac
              [7,8,9,10,11,12]) //destination mac
   .ipv4([192,168,1,1], //source ip
         [192,168,1,2], //destination ip
         20)            //time to life
   .udp(21,    //source port
        1234); //destination port

//payload of the udp packet
let payload = [1,2,3,4,5,6,7,8];

//get some memory to store the result
let mut result = Vec::<u8>::with_capacity(
                    builder.size(payload.len()));

//serialize
builder.write(&mut result, &payload).unwrap();
source

pub fn ipv4( source: [u8; 4], destination: [u8; 4], time_to_live: u8 ) -> PacketBuilderStep<IpHeaders>

Starts a packet with an IPv4 header.

§Example

Basic usage:

let builder = PacketBuilder::
   ipv4([192,168,1,1],  //source ip
         [192,168,1,2], //destination ip
         20)            //time to life
   .udp(21,    //source port
        1234); //destination port

//payload of the udp packet
let payload = [1,2,3,4,5,6,7,8];
     
//get some memory to store the result
let mut result = Vec::<u8>::with_capacity(
                    builder.size(payload.len()));

//serialize
builder.write(&mut result, &payload).unwrap();
source

pub fn ipv6( source: [u8; 16], destination: [u8; 16], hop_limit: u8 ) -> PacketBuilderStep<IpHeaders>

Start a packet with an IPv6 header.

§Example

Basic usage:

let builder = PacketBuilder::
    ipv6(
        //source
        [11,12,13,14,15,16,17,18,19,10,21,22,23,24,25,26],
        //destination
        [31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46],
        //hop_limit
        47)
   .udp(21,    //source port
        1234); //destination port

//payload of the udp packet
let payload = [1,2,3,4,5,6,7,8];

//get some memory to store the result
let mut result = Vec::<u8>::with_capacity(
                    builder.size(payload.len()));

//serialize
builder.write(&mut result, &payload).unwrap();
source

pub fn ip(ip_header: IpHeaders) -> PacketBuilderStep<IpHeaders>

Starts a packet with an arbitrary IP header (length, protocol/next_header & checksum fields will be overwritten based on the rest of the packet).

§Examples

With an IPv4 header:

let builder = PacketBuilder::
   //payload_len, protocol & checksum will be replaced during write
   ip(IpHeaders::Ipv4(
       Ipv4Header::new(
           0, //payload_len will be replaced during write
           12, //time_to_live
           ip_number::UDP, //will be replaced during write
           [0,1,2,3], //source
           [4,5,6,7] //destination
       ).unwrap(),
       Default::default()))
   .udp(21,    //source port
        1234); //destination port

//payload of the udp packet
let payload = [1,2,3,4,5,6,7,8];

//get some memory to store the result
let mut result = Vec::<u8>::with_capacity(
                    builder.size(payload.len()));

//serialize
builder.write(&mut result, &payload).unwrap();

With an IPv6 header:

let builder = PacketBuilder::
   ip(IpHeaders::Ipv6(
        Ipv6Header{
            traffic_class: 0,
            flow_label: 0.try_into().unwrap(),
            hop_limit: 4.try_into().unwrap(),
            source: [0;16],
            destination: [0;16],
            // payload_length & next_header will be replaced during write
            ..Default::default()
        },
        Default::default()))
   .udp(21,    //source port
        1234); //destination port

//payload of the udp packet
let payload = [1,2,3,4,5,6,7,8];
     
//get some memory to store the result
let mut result = Vec::<u8>::with_capacity(
                    builder.size(payload.len()));

//serialize
builder.write(&mut result, &payload).unwrap();

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> 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>,

§

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>,

§

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.