From 60d94e4dcd9c4ce2b6dff413ca9bde93b41c6e8d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 02:06:27 +0000 Subject: [PATCH 1/7] Initial plan From 4d7e047784cdb67528cd6d3d7258e0ca49ebb3f2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 02:12:52 +0000 Subject: [PATCH 2/7] Add comprehensive Rust documentation for public APIs Co-authored-by: SajjadPourali <20374762+SajjadPourali@users.noreply.github.com> --- src/error.rs | 16 ++++ src/lib.rs | 184 +++++++++++++++++++++++++++++++++++++++++- src/stream/mod.rs | 52 ++++++++++++ src/stream/tcp.rs | 53 ++++++++++++ src/stream/udp.rs | 48 +++++++++++ src/stream/unknown.rs | 100 +++++++++++++++++++++++ 6 files changed, 452 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index def2c2d..839b8b0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,26 +1,37 @@ +/// Error types for the IP stack. +/// +/// This enum represents all possible errors that can occur when working with the IP stack. #[derive(thiserror::Error, Debug)] pub enum IpStackError { + /// The transport protocol is not supported. #[error("The transport protocol is not supported")] UnsupportedTransportProtocol, + /// The packet is invalid or malformed. #[error("The packet is invalid")] InvalidPacket, + /// A value is too large to fit in a u16. #[error("ValueTooBigError {0}")] ValueTooBigErrorU16(#[from] etherparse::err::ValueTooBigError), + /// A value is too large to fit in a usize. #[error("ValueTooBigError {0}")] ValueTooBigErrorUsize(#[from] etherparse::err::ValueTooBigError), + /// The TCP packet is invalid. #[error("Invalid Tcp packet")] InvalidTcpPacket, + /// An I/O error occurred. #[error("IO error: {0}")] IoError(#[from] std::io::Error), + /// Error accepting a new stream. #[error("Accept Error")] AcceptError, + /// Error sending data through a channel. #[error("Send Error {0}")] SendError(#[from] Box>), } @@ -48,4 +59,9 @@ impl From for std::io::Error { } } +/// A specialized [`Result`] type for IP stack operations. +/// +/// This type is used throughout the IP stack for any operation which may produce an error. +/// +/// [`Result`]: std::result::Result pub type Result = std::result::Result; diff --git a/src/lib.rs b/src/lib.rs index eb01e94..3d09d44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,11 +42,34 @@ const TUN_PROTO_IP6: [u8; 2] = [0x00, 0x0A]; #[cfg(any(target_os = "macos", target_os = "ios"))] const TUN_PROTO_IP4: [u8; 2] = [0x00, 0x02]; +/// Configuration for the IP stack. +/// +/// This structure holds configuration parameters that control the behavior of the IP stack, +/// including network settings and protocol-specific timeouts. +/// +/// # Examples +/// +/// ``` +/// use ipstack::IpStackConfig; +/// use std::time::Duration; +/// +/// let mut config = IpStackConfig::default(); +/// config.mtu(1500) +/// .udp_timeout(Duration::from_secs(60)) +/// .packet_information(false); +/// ``` #[non_exhaustive] pub struct IpStackConfig { + /// Maximum Transmission Unit (MTU) size in bytes. + /// Default is `u16::MAX` (65535). pub mtu: u16, + /// Whether to include packet information headers (Unix platforms only). + /// Default is `false`. pub packet_information: bool, + /// TCP-specific configuration parameters. pub tcp_config: Arc, + /// Timeout for UDP connections. + /// Default is 30 seconds. pub udp_timeout: Duration, } @@ -62,31 +85,157 @@ impl Default for IpStackConfig { } impl IpStackConfig { - /// Set custom TCP configuration + /// Set custom TCP configuration. + /// + /// # Arguments + /// + /// * `config` - The TCP configuration to use + /// + /// # Examples + /// + /// ``` + /// use ipstack::{IpStackConfig, TcpConfig}; + /// + /// let mut config = IpStackConfig::default(); + /// config.with_tcp_config(TcpConfig::default()); + /// ``` pub fn with_tcp_config(&mut self, config: TcpConfig) -> &mut Self { self.tcp_config = Arc::new(config); self } + + /// Set the UDP connection timeout. + /// + /// # Arguments + /// + /// * `timeout` - The timeout duration for UDP connections + /// + /// # Examples + /// + /// ``` + /// use ipstack::IpStackConfig; + /// use std::time::Duration; + /// + /// let mut config = IpStackConfig::default(); + /// config.udp_timeout(Duration::from_secs(60)); + /// ``` pub fn udp_timeout(&mut self, timeout: Duration) -> &mut Self { self.udp_timeout = timeout; self } + + /// Set the Maximum Transmission Unit (MTU) size. + /// + /// # Arguments + /// + /// * `mtu` - The MTU size in bytes + /// + /// # Examples + /// + /// ``` + /// use ipstack::IpStackConfig; + /// + /// let mut config = IpStackConfig::default(); + /// config.mtu(1500); + /// ``` pub fn mtu(&mut self, mtu: u16) -> &mut Self { self.mtu = mtu; self } + + /// Enable or disable packet information headers (Unix platforms only). + /// + /// When enabled on Unix platforms, the TUN device will include 4-byte packet + /// information headers. + /// + /// # Arguments + /// + /// * `packet_information` - Whether to include packet information headers + /// + /// # Examples + /// + /// ``` + /// use ipstack::IpStackConfig; + /// + /// let mut config = IpStackConfig::default(); + /// config.packet_information(true); + /// ``` pub fn packet_information(&mut self, packet_information: bool) -> &mut Self { self.packet_information = packet_information; self } } +/// 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 +/// +/// ```no_run +/// use ipstack::{IpStack, IpStackConfig, IpStackStream}; +/// use std::net::Ipv4Addr; +/// +/// #[tokio::main] +/// async fn main() -> Result<(), Box> { +/// // 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(()) +/// } +/// ``` pub struct IpStack { accept_receiver: UnboundedReceiver, handle: JoinHandle>, } impl IpStack { + /// Create a new IP stack instance. + /// + /// # Arguments + /// + /// * `config` - Configuration for the IP stack + /// * `device` - An async TUN device implementing `AsyncRead` + `AsyncWrite` + /// + /// # Examples + /// + /// ```no_run + /// use ipstack::{IpStack, IpStackConfig}; + /// use std::net::Ipv4Addr; + /// + /// # async fn example() -> Result<(), Box> { + /// 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)?); + /// # Ok(()) + /// # } + /// ``` pub fn new(config: IpStackConfig, device: Device) -> IpStack where Device: AsyncRead + AsyncWrite + Unpin + Send + 'static, @@ -98,6 +247,39 @@ impl IpStack { } } + /// 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 + /// + /// ```no_run + /// use ipstack::{IpStack, IpStackConfig, IpStackStream}; + /// + /// # async fn example(mut ip_stack: IpStack) -> Result<(), Box> { + /// 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()); + /// } + /// } + /// # Ok(()) + /// # } + /// ``` pub async fn accept(&mut self) -> Result { self.accept_receiver.recv().await.ok_or(IpStackError::AcceptError) } diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 467191f..ed7d6bc 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -11,14 +11,43 @@ mod tcp; mod udp; mod unknown; +/// A network stream accepted by the IP stack. +/// +/// This enum represents different types of network streams that can be accepted from the TUN device. +/// Each variant provides appropriate abstractions for handling specific protocol types. +/// +/// # Variants +/// +/// * `Tcp` - A TCP connection stream implementing `AsyncRead` + `AsyncWrite` +/// * `Udp` - A UDP stream implementing `AsyncRead` + `AsyncWrite` +/// * `UnknownTransport` - A stream for unknown transport layer protocols (e.g., ICMP, IGMP) +/// * `UnknownNetwork` - Raw network layer packets that couldn't be parsed pub enum IpStackStream { + /// A TCP connection stream. Tcp(IpStackTcpStream), + /// A UDP stream. Udp(IpStackUdpStream), + /// A stream for unknown transport protocols. UnknownTransport(IpStackUnknownTransport), + /// Raw network packets that couldn't be parsed. UnknownNetwork(Vec), } impl IpStackStream { + /// Returns the local socket address for this stream. + /// + /// For TCP and UDP streams, this returns the source address of the connection. + /// For unknown transport and network streams, this returns an unspecified address. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::{IpStack, IpStackStream}; + /// # async fn example(stream: IpStackStream) { + /// let local_addr = stream.local_addr(); + /// println!("Local address: {}", local_addr); + /// # } + /// ``` pub fn local_addr(&self) -> SocketAddr { match self { IpStackStream::Tcp(tcp) => tcp.local_addr(), @@ -30,6 +59,21 @@ impl IpStackStream { }, } } + + /// Returns the remote socket address for this stream. + /// + /// For TCP and UDP streams, this returns the destination address of the connection. + /// For unknown transport and network streams, this returns an unspecified address. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::{IpStack, IpStackStream}; + /// # async fn example(stream: IpStackStream) { + /// let peer_addr = stream.peer_addr(); + /// println!("Peer address: {}", peer_addr); + /// # } + /// ``` pub fn peer_addr(&self) -> SocketAddr { match self { IpStackStream::Tcp(tcp) => tcp.peer_addr(), @@ -42,6 +86,14 @@ impl IpStackStream { } } + /// Returns a packet sender for this stream. + /// + /// This is an internal method used to send packets back to the stream. + /// Only available for TCP and UDP streams. + /// + /// # Errors + /// + /// Returns an error if called on `UnknownTransport` or `UnknownNetwork` variants. pub fn stream_sender(&self) -> Result { match self { IpStackStream::Tcp(tcp) => Ok(tcp.stream_sender()), diff --git a/src/stream/tcp.rs b/src/stream/tcp.rs index c1ad80e..53e1e7d 100644 --- a/src/stream/tcp.rs +++ b/src/stream/tcp.rs @@ -109,6 +109,35 @@ static SESSION_COUNTER: std::sync::atomic::AtomicUsize = std::sync::atomic::Atom type TcbPtr = std::sync::Arc>; +/// A TCP stream in the IP stack. +/// +/// This type represents a TCP connection and implements `AsyncRead` and `AsyncWrite` +/// for bidirectional data transfer. It handles TCP state management, flow control, +/// and retransmission automatically. +/// +/// # Examples +/// +/// ```no_run +/// use ipstack::{IpStack, IpStackConfig, IpStackStream}; +/// use tokio::io::{AsyncReadExt, AsyncWriteExt}; +/// +/// # async fn example(mut ip_stack: IpStack) -> Result<(), Box> { +/// if let IpStackStream::Tcp(mut tcp_stream) = ip_stack.accept().await? { +/// println!("New TCP connection from {}", tcp_stream.peer_addr()); +/// +/// // Read data +/// let mut buffer = [0u8; 1024]; +/// let n = tcp_stream.read(&mut buffer).await?; +/// +/// // Write data +/// tcp_stream.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await?; +/// +/// // Shutdown the stream +/// tcp_stream.shutdown().await?; +/// } +/// # Ok(()) +/// # } +/// ``` #[derive(Debug)] pub struct IpStackTcpStream { src_addr: SocketAddr, @@ -194,12 +223,36 @@ impl IpStackTcpStream { NetworkTuple::new(self.src_addr, self.dst_addr, true) } + /// Returns the local socket address of the TCP connection. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::IpStackTcpStream; + /// # fn example(tcp_stream: &IpStackTcpStream) { + /// let local_addr = tcp_stream.local_addr(); + /// println!("Local address: {}", local_addr); + /// # } + /// ``` pub fn local_addr(&self) -> SocketAddr { self.src_addr } + + /// Returns the remote socket address of the TCP connection. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::IpStackTcpStream; + /// # fn example(tcp_stream: &IpStackTcpStream) { + /// let peer_addr = tcp_stream.peer_addr(); + /// println!("Peer address: {}", peer_addr); + /// # } + /// ``` pub fn peer_addr(&self) -> SocketAddr { self.dst_addr } + pub fn stream_sender(&self) -> PacketSender { self.stream_sender.clone() } diff --git a/src/stream/udp.rs b/src/stream/udp.rs index aa1fdf6..87925b1 100644 --- a/src/stream/udp.rs +++ b/src/stream/udp.rs @@ -10,6 +10,32 @@ use tokio::{ time::Sleep, }; +/// A UDP stream in the IP stack. +/// +/// This type represents a UDP connection and implements `AsyncRead` and `AsyncWrite` +/// for bidirectional data transfer. UDP streams have a configurable timeout and +/// automatically handle packet fragmentation based on MTU. +/// +/// # Examples +/// +/// ```no_run +/// use ipstack::{IpStack, IpStackConfig, IpStackStream}; +/// use tokio::io::{AsyncReadExt, AsyncWriteExt}; +/// +/// # async fn example(mut ip_stack: IpStack) -> Result<(), Box> { +/// if let IpStackStream::Udp(mut udp_stream) = ip_stack.accept().await? { +/// println!("New UDP stream from {}", udp_stream.peer_addr()); +/// +/// // Read data +/// let mut buffer = [0u8; 1024]; +/// let n = udp_stream.read(&mut buffer).await?; +/// +/// // Write data +/// udp_stream.write_all(b"Response").await?; +/// } +/// # Ok(()) +/// # } +/// ``` #[derive(Debug)] pub struct IpStackUdpStream { src_addr: SocketAddr, @@ -97,10 +123,32 @@ impl IpStackUdpStream { } } + /// Returns the local socket address of the UDP stream. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::IpStackUdpStream; + /// # fn example(udp_stream: &IpStackUdpStream) { + /// let local_addr = udp_stream.local_addr(); + /// println!("Local address: {}", local_addr); + /// # } + /// ``` pub fn local_addr(&self) -> SocketAddr { self.src_addr } + /// Returns the remote socket address of the UDP stream. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::IpStackUdpStream; + /// # fn example(udp_stream: &IpStackUdpStream) { + /// let peer_addr = udp_stream.peer_addr(); + /// println!("Peer address: {}", peer_addr); + /// # } + /// ``` pub fn peer_addr(&self) -> SocketAddr { self.dst_addr } diff --git a/src/stream/unknown.rs b/src/stream/unknown.rs index be67591..fbcce2f 100644 --- a/src/stream/unknown.rs +++ b/src/stream/unknown.rs @@ -5,6 +5,30 @@ use crate::{ use etherparse::{IpNumber, Ipv4Header, Ipv6FlowLabel, Ipv6Header}; use std::net::IpAddr; +/// A stream for unknown transport layer protocols. +/// +/// This type handles network packets with transport protocols that are not TCP or UDP +/// (e.g., ICMP, IGMP, ESP, etc.). It provides methods to inspect the packet details +/// and send responses. +/// +/// # Examples +/// +/// ```no_run +/// use ipstack::{IpStack, IpStackConfig, IpStackStream}; +/// +/// # async fn example(mut ip_stack: IpStack) -> Result<(), Box> { +/// if let IpStackStream::UnknownTransport(unknown) = ip_stack.accept().await? { +/// println!("Unknown transport protocol: {:?}", unknown.ip_protocol()); +/// println!("Source: {}", unknown.src_addr()); +/// println!("Destination: {}", unknown.dst_addr()); +/// println!("Payload: {} bytes", unknown.payload().len()); +/// +/// // Send a response +/// unknown.send(vec![0x08, 0x00, 0x00, 0x00])?; +/// } +/// # Ok(()) +/// # } +/// ``` pub struct IpStackUnknownTransport { src_addr: IpAddr, dst_addr: IpAddr, @@ -29,18 +53,90 @@ impl IpStackUnknownTransport { packet_sender, } } + + /// Returns the source IP address of the packet. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::IpStackUnknownTransport; + /// # fn example(unknown: &IpStackUnknownTransport) { + /// let src = unknown.src_addr(); + /// println!("Source: {}", src); + /// # } + /// ``` pub fn src_addr(&self) -> IpAddr { self.src_addr } + + /// Returns the destination IP address of the packet. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::IpStackUnknownTransport; + /// # fn example(unknown: &IpStackUnknownTransport) { + /// let dst = unknown.dst_addr(); + /// println!("Destination: {}", dst); + /// # } + /// ``` pub fn dst_addr(&self) -> IpAddr { self.dst_addr } + + /// Returns the payload of the packet. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::IpStackUnknownTransport; + /// # fn example(unknown: &IpStackUnknownTransport) { + /// let payload = unknown.payload(); + /// println!("Payload: {} bytes", payload.len()); + /// # } + /// ``` pub fn payload(&self) -> &[u8] { &self.payload } + + /// Returns the IP protocol number of the packet. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::IpStackUnknownTransport; + /// # fn example(unknown: &IpStackUnknownTransport) { + /// let protocol = unknown.ip_protocol(); + /// println!("Protocol: {:?}", protocol); + /// # } + /// ``` pub fn ip_protocol(&self) -> IpNumber { self.protocol } + + /// Send a response packet. + /// + /// This method sends one or more packets with the given payload, automatically + /// fragmenting the data if it exceeds the MTU. + /// + /// # Arguments + /// + /// * `payload` - The payload to send + /// + /// # Errors + /// + /// Returns an error if the packet cannot be sent. + /// + /// # Examples + /// + /// ```no_run + /// # use ipstack::IpStackUnknownTransport; + /// # fn example(unknown: &IpStackUnknownTransport) -> std::io::Result<()> { + /// // Send an ICMP echo reply + /// unknown.send(vec![0x08, 0x00, 0x00, 0x00])?; + /// # Ok(()) + /// # } + /// ``` pub fn send(&self, mut payload: Vec) -> std::io::Result<()> { loop { let packet = self.create_rev_packet(&mut payload)?; @@ -53,6 +149,10 @@ impl IpStackUnknownTransport { } } + /// Create a reverse packet for sending a response. + /// + /// This is an internal method used by `send()` to create packets with swapped + /// source and destination addresses. pub fn create_rev_packet(&self, payload: &mut Vec) -> std::io::Result { match (self.dst_addr, self.src_addr) { (std::net::IpAddr::V4(dst), std::net::IpAddr::V4(src)) => { From 7ee8a1385ef8b46226d6de452968db09195dee3f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 02:15:34 +0000 Subject: [PATCH 3/7] Improve documentation for create_rev_packet method Co-authored-by: SajjadPourali <20374762+SajjadPourali@users.noreply.github.com> --- src/stream/unknown.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/stream/unknown.rs b/src/stream/unknown.rs index fbcce2f..8d75719 100644 --- a/src/stream/unknown.rs +++ b/src/stream/unknown.rs @@ -151,8 +151,21 @@ impl IpStackUnknownTransport { /// Create a reverse packet for sending a response. /// - /// This is an internal method used by `send()` to create packets with swapped - /// source and destination addresses. + /// This method creates packets with swapped source and destination addresses, + /// suitable for sending responses to received packets. The payload is automatically + /// fragmented if it exceeds the MTU. + /// + /// # Arguments + /// + /// * `payload` - A mutable reference to the payload. Data will be drained from this vector. + /// + /// # Returns + /// + /// Returns a `NetworkPacket` with the reversed addresses and the payload. + /// + /// # Errors + /// + /// Returns an error if the packet cannot be constructed. pub fn create_rev_packet(&self, payload: &mut Vec) -> std::io::Result { match (self.dst_addr, self.src_addr) { (std::net::IpAddr::V4(dst), std::net::IpAddr::V4(src)) => { From 115946bf0c378b3fd04bbdfcbade7101e1248cc3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 02:16:49 +0000 Subject: [PATCH 4/7] Refine create_rev_packet documentation for accuracy Co-authored-by: SajjadPourali <20374762+SajjadPourali@users.noreply.github.com> --- src/stream/unknown.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/stream/unknown.rs b/src/stream/unknown.rs index 8d75719..882e20f 100644 --- a/src/stream/unknown.rs +++ b/src/stream/unknown.rs @@ -151,17 +151,18 @@ impl IpStackUnknownTransport { /// Create a reverse packet for sending a response. /// - /// This method creates packets with swapped source and destination addresses, - /// suitable for sending responses to received packets. The payload is automatically - /// fragmented if it exceeds the MTU. + /// This method creates a packet with swapped source and destination addresses, + /// suitable for sending responses to received packets. If the payload exceeds + /// the MTU, only a portion of the payload is consumed and included in the packet. /// /// # Arguments /// - /// * `payload` - A mutable reference to the payload. Data will be drained from this vector. + /// * `payload` - A mutable reference to the payload vector. If the payload exceeds + /// the MTU, data is drained from the front. Otherwise, the entire vector is taken. /// /// # Returns /// - /// Returns a `NetworkPacket` with the reversed addresses and the payload. + /// Returns a `NetworkPacket` with the reversed addresses and up to MTU bytes of payload. /// /// # Errors /// From 0a3a881114d10f09a79ea9f4e4672b1f54f9304b Mon Sep 17 00:00:00 2001 From: SajjadPourali Date: Tue, 14 Oct 2025 19:20:24 -0700 Subject: [PATCH 5/7] Fix format error --- src/lib.rs | 6 +++--- src/stream/mod.rs | 2 +- src/stream/tcp.rs | 4 ++-- src/stream/unknown.rs | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3d09d44..eb6225f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,7 +103,7 @@ impl IpStackConfig { self.tcp_config = Arc::new(config); self } - + /// Set the UDP connection timeout. /// /// # Arguments @@ -123,7 +123,7 @@ impl IpStackConfig { self.udp_timeout = timeout; self } - + /// Set the Maximum Transmission Unit (MTU) size. /// /// # Arguments @@ -142,7 +142,7 @@ impl IpStackConfig { self.mtu = mtu; self } - + /// Enable or disable packet information headers (Unix platforms only). /// /// When enabled on Unix platforms, the TUN device will include 4-byte packet diff --git a/src/stream/mod.rs b/src/stream/mod.rs index ed7d6bc..8a97472 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -59,7 +59,7 @@ impl IpStackStream { }, } } - + /// Returns the remote socket address for this stream. /// /// For TCP and UDP streams, this returns the destination address of the connection. diff --git a/src/stream/tcp.rs b/src/stream/tcp.rs index 53e1e7d..267978f 100644 --- a/src/stream/tcp.rs +++ b/src/stream/tcp.rs @@ -237,7 +237,7 @@ impl IpStackTcpStream { pub fn local_addr(&self) -> SocketAddr { self.src_addr } - + /// Returns the remote socket address of the TCP connection. /// /// # Examples @@ -252,7 +252,7 @@ impl IpStackTcpStream { pub fn peer_addr(&self) -> SocketAddr { self.dst_addr } - + pub fn stream_sender(&self) -> PacketSender { self.stream_sender.clone() } diff --git a/src/stream/unknown.rs b/src/stream/unknown.rs index 882e20f..97d542a 100644 --- a/src/stream/unknown.rs +++ b/src/stream/unknown.rs @@ -53,7 +53,7 @@ impl IpStackUnknownTransport { packet_sender, } } - + /// Returns the source IP address of the packet. /// /// # Examples @@ -68,7 +68,7 @@ impl IpStackUnknownTransport { pub fn src_addr(&self) -> IpAddr { self.src_addr } - + /// Returns the destination IP address of the packet. /// /// # Examples @@ -83,7 +83,7 @@ impl IpStackUnknownTransport { pub fn dst_addr(&self) -> IpAddr { self.dst_addr } - + /// Returns the payload of the packet. /// /// # Examples @@ -98,7 +98,7 @@ impl IpStackUnknownTransport { pub fn payload(&self) -> &[u8] { &self.payload } - + /// Returns the IP protocol number of the packet. /// /// # Examples @@ -113,7 +113,7 @@ impl IpStackUnknownTransport { pub fn ip_protocol(&self) -> IpNumber { self.protocol } - + /// Send a response packet. /// /// This method sends one or more packets with the given payload, automatically From ccd628b366c31cbf99a2299ae31c86846d5b5f14 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 02:32:47 +0000 Subject: [PATCH 6/7] Change stream_sender visibility to pub(crate) Co-authored-by: SajjadPourali <20374762+SajjadPourali@users.noreply.github.com> --- src/stream/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 8a97472..71fefba 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -94,7 +94,7 @@ impl IpStackStream { /// # Errors /// /// Returns an error if called on `UnknownTransport` or `UnknownNetwork` variants. - pub fn stream_sender(&self) -> Result { + pub(crate) fn stream_sender(&self) -> Result { match self { IpStackStream::Tcp(tcp) => Ok(tcp.stream_sender()), IpStackStream::Udp(udp) => Ok(udp.stream_sender()), From ab8e1232f020ee6f3b395187234837b82f1cbde6 Mon Sep 17 00:00:00 2001 From: SajjadPourali Date: Tue, 14 Oct 2025 19:34:38 -0700 Subject: [PATCH 7/7] Removed stream_sender documentations --- src/stream/mod.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 71fefba..777053f 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -86,14 +86,6 @@ impl IpStackStream { } } - /// Returns a packet sender for this stream. - /// - /// This is an internal method used to send packets back to the stream. - /// Only available for TCP and UDP streams. - /// - /// # Errors - /// - /// Returns an error if called on `UnknownTransport` or `UnknownNetwork` variants. pub(crate) fn stream_sender(&self) -> Result { match self { IpStackStream::Tcp(tcp) => Ok(tcp.stream_sender()),