IpStack

Struct IpStack 

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

The main IP stack instance.

IpStack provides a userspace TCP/IP stack implementation for TUN devices. It processes network packets and creates stream abstractions for TCP, UDP, and unknown transport protocols.

§Examples

use ipstack::{IpStack, IpStackConfig, IpStackStream};
use std::net::Ipv4Addr;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Configure TUN device
    let mut config = tun::Configuration::default();
    config
        .address(Ipv4Addr::new(10, 0, 0, 1))
        .netmask(Ipv4Addr::new(255, 255, 255, 0))
        .up();

    // Create IP stack
    let ipstack_config = IpStackConfig::default();
    let mut ip_stack = IpStack::new(ipstack_config, tun::create_as_async(&config)?);

    // Accept incoming streams
    while let Ok(stream) = ip_stack.accept().await {
        match stream {
            IpStackStream::Tcp(tcp) => {
                // Handle TCP connection
            }
            IpStackStream::Udp(udp) => {
                // Handle UDP connection
            }
            _ => {}
        }
    }
    Ok(())
}

Implementations§

Source§

impl IpStack

Source

pub fn new<Device>(config: IpStackConfig, device: Device) -> IpStack
where Device: AsyncRead + AsyncWrite + Unpin + Send + 'static,

Create a new IP stack instance.

§Arguments
  • config - Configuration for the IP stack
  • device - An async TUN device implementing AsyncRead + AsyncWrite
§Examples
use ipstack::{IpStack, IpStackConfig};
use std::net::Ipv4Addr;

let mut tun_config = tun::Configuration::default();
tun_config.address(Ipv4Addr::new(10, 0, 0, 1))
          .netmask(Ipv4Addr::new(255, 255, 255, 0))
          .up();

let ipstack_config = IpStackConfig::default();
let ip_stack = IpStack::new(ipstack_config, tun::create_as_async(&tun_config)?);
Source

pub async fn accept(&mut self) -> Result<IpStackStream, IpStackError>

Accept an incoming network stream.

This method waits for and returns the next incoming network connection or packet. The returned IpStackStream enum indicates the type of stream (TCP, UDP, or unknown).

§Returns
  • Ok(IpStackStream) - The next incoming stream
  • Err(IpStackError::AcceptError) - If the IP stack has been shut down
§Examples
use ipstack::{IpStack, IpStackConfig, IpStackStream};

match ip_stack.accept().await? {
    IpStackStream::Tcp(tcp) => {
        println!("New TCP connection from {}", tcp.peer_addr());
    }
    IpStackStream::Udp(udp) => {
        println!("New UDP stream from {}", udp.peer_addr());
    }
    IpStackStream::UnknownTransport(unknown) => {
        println!("Unknown transport protocol: {:?}", unknown.ip_protocol());
    }
    IpStackStream::UnknownNetwork(data) => {
        println!("Unknown network packet: {} bytes", data.len());
    }
}

Trait Implementations§

Source§

impl Drop for IpStack

Source§

fn drop(&mut self)

Executes the destructor for this type. 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.