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
impl IpStack
Sourcepub fn new<Device>(config: IpStackConfig, device: Device) -> IpStack
pub fn new<Device>(config: IpStackConfig, device: Device) -> IpStack
Create a new IP stack instance.
§Arguments
config- Configuration for the IP stackdevice- An async TUN device implementingAsyncRead+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)?);Sourcepub async fn accept(&mut self) -> Result<IpStackStream, IpStackError>
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 streamErr(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§
Auto Trait Implementations§
impl Freeze for IpStack
impl RefUnwindSafe for IpStack
impl Send for IpStack
impl Sync for IpStack
impl Unpin for IpStack
impl UnwindSafe for IpStack
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more