[][src]Struct etherparse::PacketBuilder

pub struct PacketBuilder {}

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]) //destionation mac
   .ipv4([192,168,1,1], //source ip
         [192,168,1,2], //desitionation ip
         20)            //time to life
   .udp(21,    //source port 
        1234); //desitnation 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);

Methods

impl PacketBuilder[src]

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

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]) //destionation mac
   .ipv4([192,168,1,1], //source ip
         [192,168,1,2], //desitionation ip
         20)            //time to life
   .udp(21,    //source port 
        1234); //desitnation 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();

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

Starts a packet with an IPv4 header.

Example

Basic usage:

let builder = PacketBuilder::
   ipv4([192,168,1,1],  //source ip
         [192,168,1,2], //desitionation ip
         20)            //time to life
   .udp(21,    //source port 
        1234); //desitnation 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();

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

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); //desitnation 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();

pub fn ip(ip_header: IpHeader) -> PacketBuilderStep<IpHeader>[src]

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(IpHeader::Version4(Ipv4Header::new(
       0, //payload_len will be replaced during write
       12, //time_to_live
       IpTrafficClass::Udp, //will be replaced during write
       [0,1,2,3], //source
       [4,5,6,7] //destination
    )))
   .udp(21,    //source port 
        1234); //desitnation 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(IpHeader::Version6(Ipv6Header{
        traffic_class: 0,
        flow_label: 0,
        payload_length: 0, //will be replaced during write
        next_header: 0, //will be replaced during write
        hop_limit: 4,
        source: [0;16],
        destination: [0;16]
    }))
   .udp(21,    //source port 
        1234); //desitnation 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

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]