Struct ReusablePacketWriter

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

Wrapper around Vec<u8> that is pre-allocates memory for writing packet bytes

Implementations§

Source§

impl ReusablePacketWriter

Source

pub fn new() -> Self

Creates wrapper around Vec<u8> with pre-allocated u16::MAX (max possible MTU size)

Examples found in repository?
examples/udp_packet.rs (line 12)
7fn main() -> ip_spoofing::Result<()> {
8    //wrapper around raw sockets, requires root privileges
9    let socket = RawSocket::new()?;
10
11    //wrapper for writing packets in pre-allocated memory
12    let mut writer = ReusablePacketWriter::new();
13
14    //sends fake UDP packet
15    socket.send_fake_udp_packet(
16        &mut writer,
17        [8, 8, 8, 8],   //source IPv4 address
18        1234,           //source port
19        [127, 0, 0, 1], //destination IPv4 address
20        5678,           //destination port
21        b"hey",         //data
22        64,             //TTL on most Linux machines is 64
23    )?;
24
25    Ok(())
26}
More examples
Hide additional examples
examples/udp_flood.rs (line 12)
7fn main() -> ip_spoofing::Result<()> {
8    //wrapper around raw sockets, requires root privileges
9    let socket = RawSocket::new()?;
10
11    //wrapper for writing packets in pre-allocated memory
12    let mut writer = ReusablePacketWriter::new();
13    //thread-local pseudorandom number generator
14    let mut rng = thread_rng();
15
16    //endless spam with a randomly generated UDP packet
17    loop {
18        let ret = socket.send_fake_udp_packet(
19            &mut writer,
20            rng.gen(),      //random source IPv4 address
21            rng.gen(),      //random source port
22            [127, 0, 0, 1], //destination IPv4 address
23            5678,           //destination port
24            b"hey",         //data
25            64,             //TTL on most Linux machines is 64
26        );
27
28        if let Err(err) = ret {
29            println!("{err:?}");
30        }
31    }
32}
Source

pub fn as_slice(&self) -> &[u8]

Takes slice of memory from this wrapper

Source

pub fn build_ipv4_header( time_to_live: u8, protocol: IpNumber, source: [u8; 4], destination: [u8; 4], ) -> IpHeaders

Builds IPv4 header with randomized identification field

Source

pub fn write_udp_packet( &mut self, source: [u8; 4], source_port: u16, destination: [u8; 4], destination_port: u16, payload: &[u8], time_to_live: u8, ) -> Result<()>

Writes UDP packet into buffer with given parameters

Source

pub fn write_tcp_syn_packet( &mut self, source: [u8; 4], source_port: u16, destination: [u8; 4], destination_port: u16, payload: &[u8], time_to_live: u8, sequence_number: u32, window_size: u16, ) -> Result<()>

Writes TCP-SYN packet into buffer with given parameters

Trait Implementations§

Source§

impl Debug for ReusablePacketWriter

Source§

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

Formats the value using the given formatter. Read more

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

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