Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ['Narrowlink <opensource@narrowlink.com>']
description = 'Asynchronous lightweight userspace implementation of TCP/IP stack for Tun device'
name = "ipstack"
version = "0.4.0"
version = "0.5.0"
edition = "2024"
license = "Apache-2.0"
repository = 'https://github.com/narrowlink/ipstack'
Expand Down
5 changes: 4 additions & 1 deletion examples/tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let mut ipstack_config = ipstack::IpStackConfig::default();
ipstack_config.mtu(MTU);
ipstack_config.tcp_timeout(std::time::Duration::from_secs(args.tcp_timeout));
let mut tcp_config = ipstack::TcpConfig::default();
tcp_config.timeout = std::time::Duration::from_secs(args.tcp_timeout);
tcp_config.options = Some(vec![ipstack::TcpOptions::MaximumSegmentSize(1460)]);
ipstack_config.with_tcp_config(tcp_config);
ipstack_config.udp_timeout(std::time::Duration::from_secs(args.udp_timeout));

let mut ip_stack = ipstack::IpStack::new(ipstack_config, tun::create_as_async(&tun_config)?);
Expand Down
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use ahash::AHashMap;
use packet::{NetworkPacket, NetworkTuple, TransportHeader};
use std::time::Duration;
use std::{sync::Arc, time::Duration};
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
select,
Expand All @@ -20,7 +20,8 @@ mod stream;

pub use self::error::{IpStackError, Result};
pub use self::stream::{IpStackStream, IpStackTcpStream, IpStackUdpStream, IpStackUnknownTransport};
pub use ::etherparse::IpNumber;
pub use self::stream::{TcpConfig, TcpOptions};
pub use etherparse::IpNumber;

#[cfg(unix)]
const TTL: u8 = 64;
Expand All @@ -41,10 +42,11 @@ const TUN_PROTO_IP6: [u8; 2] = [0x00, 0x0A];
#[cfg(any(target_os = "macos", target_os = "ios"))]
const TUN_PROTO_IP4: [u8; 2] = [0x00, 0x02];

#[non_exhaustive]
pub struct IpStackConfig {
pub mtu: u16,
pub packet_information: bool,
pub tcp_timeout: Duration,
pub tcp_config: Arc<TcpConfig>,
pub udp_timeout: Duration,
}

Expand All @@ -53,15 +55,16 @@ impl Default for IpStackConfig {
IpStackConfig {
mtu: u16::MAX,
packet_information: false,
tcp_timeout: Duration::from_secs(60),
tcp_config: Arc::new(TcpConfig::default()),
udp_timeout: Duration::from_secs(30),
}
}
}

impl IpStackConfig {
pub fn tcp_timeout(&mut self, timeout: Duration) -> &mut Self {
self.tcp_timeout = timeout;
/// Set custom TCP configuration
pub fn with_tcp_config(&mut self, config: TcpConfig) -> &mut Self {
self.tcp_config = Arc::new(config);
self
}
pub fn udp_timeout(&mut self, timeout: Duration) -> &mut Self {
Expand Down Expand Up @@ -194,7 +197,7 @@ fn create_stream(
let dst_addr = packet.dst_addr();
match packet.transport_header() {
TransportHeader::Tcp(h) => {
let stream = IpStackTcpStream::new(src_addr, dst_addr, h.clone(), up_pkt_sender, cfg.mtu, cfg.tcp_timeout, msgr)?;
let stream = IpStackTcpStream::new(src_addr, dst_addr, h.clone(), up_pkt_sender, cfg.mtu, msgr, cfg.tcp_config.clone())?;
Ok(IpStackStream::Tcp(stream))
}
TransportHeader::Udp(_) => {
Expand Down
1 change: 1 addition & 0 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

pub use self::tcp::IpStackTcpStream;
pub use self::tcp::{TcpConfig, TcpOptions};
pub use self::udp::IpStackUdpStream;
pub use self::unknown::IpStackUnknownTransport;

Expand Down
Loading