Skip to content

Commit e482b46

Browse files
committed
Library docs
1 parent 00e7f6b commit e482b46

File tree

6 files changed

+88
-16
lines changed

6 files changed

+88
-16
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@ keywords = ["wireguard","onetun","slirp","nat"]
1414
[dependencies]
1515
anyhow = "1.0.72"
1616
argh = "0.1.10"
17-
base64 = "0.21.2"
18-
boringtun = "0.6.0"
19-
bytes = "1.4.0"
20-
hashbrown = "0.14.0"
21-
hex = "0.4.3"
22-
simple-dns = "0.5.3"
23-
smoltcp = { version = "0.10.0", default-features = false, features = ["socket", "socket-tcp", "socket-udp", "std", "proto-ipv4", "proto-ipv4-fragmentation", "proto-ipv6", "fragmentation-buffer-size-65536", "assembler-max-segment-count-32", "log", "medium-ip"] }
24-
tokio = { version = "1.29.1", features = ["rt", "net", "sync", "macros", "io-util", "time"] }
17+
tokio = { version = "1.29.1", features = ["rt", "macros"] }
2518
tracing = "0.1.37"
2619
tracing-subscriber = {version="0.3.17", optional=true}
2720
libwgslirpy = {version = "0.2.0", path="crates/libwgslirpy"}

crates/libwgslirpy/src/channelized_smoltcp_device.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
//! Helper module to create `smoltcp` devices that use single slot for received packet and a tokio::mpsc channel to transmit packets.
12
3+
#![allow(missing_docs)]
24

35
use bytes::BytesMut;
46
use smoltcp::phy::{Device,RxToken,TxToken, Checksum};
@@ -9,12 +11,24 @@ use crate::TEAR_OF_ALLOCATION_SIZE;
911

1012
pub struct ChannelizedDevice {
1113
pub tx : Sender<BytesMut>,
14+
15+
/// Put a packet to be handled by smoltcp here. Channelized device would take it from here.
1216
pub rx: Option<BytesMut>,
17+
18+
/// Moderately big allocation to snip away pieces from it when we need to transmit packets into [`tx`].
19+
///
20+
/// When remaining capacity falls below some threshold, the buffer gets allocated again.
21+
///
22+
/// Idea is to reduce number of allocations at the expense of the size of allocated block.
1323
pub tear_off_buffer: BytesMut,
24+
1425
pub mtu : usize,
1526
}
1627

1728
impl ChannelizedDevice {
29+
/// Create the device using given queue for transmitting packets, retuning given `mtu` as MTU.
30+
///
31+
/// Checksumming is enabled, menium is "IP".
1832
pub fn new(tx: Sender<BytesMut>, mtu: usize) -> Self {
1933
ChannelizedDevice {
2034
tx,

crates/libwgslirpy/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#![warn(missing_docs)]
2+
//! Library part of wgslirpy - Tokio-, smoltcp- and boringtun-based user-space router.
3+
//! See main (i.e. CLI tool) documentation for what is it.
4+
5+
16
const TEAR_OF_ALLOCATION_SIZE : usize = 65536;
27

38
pub mod wg;
@@ -8,6 +13,14 @@ use tracing::warn;
813

914
pub use wg::parsebase64_32;
1015

16+
pub extern crate bytes;
17+
pub extern crate boringtun;
18+
pub extern crate smoltcp;
19+
pub extern crate tokio;
20+
21+
/// Start the application using given Wireguard and routing options.
22+
///
23+
/// Aboring `Future` returned by this function should abort all tasks spawned related by it and close all sockets.
1124
pub async fn run(wireguard_options: wg::Opts, router_options: router::Opts, transmit_queue_capacity: usize) -> anyhow::Result<()> {
1225
let (tx_towg, rx_towg) = tokio::sync::mpsc::channel(transmit_queue_capacity);
1326
let (tx_fromwg, rx_fromwg) = tokio::sync::mpsc::channel(4);

crates/libwgslirpy/src/router.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//! Routing and smoltcp-facing part of the library.
2+
//!
3+
//! This moduile allows a pair of tokio::sync::mpsc channels that represent sent and transmitted IP bytes to be used to access outer
4+
//! network using operating system's sockets.
5+
//!
6+
//! "host" network represents operating system's sockets and communication counterparts that are accessed using them.
7+
//!
8+
//! "internal" network represents senders and receivers of `BytesMut`-wrapped IP packet queues.
9+
110
use std::net::{IpAddr, SocketAddr};
211

312
use bytes::BytesMut;
@@ -42,18 +51,40 @@ impl std::fmt::Display for NatKey {
4251
}
4352
}
4453

54+
/// Options regarding interaction with smoltcp and host sockets.
4555
pub struct Opts {
56+
/// If UDP datagrams are directed at this socket address then attempt to reply to a DNS request internally instead of forwarding the datagram properly
4657
pub dns_addr: Option<SocketAddr>,
58+
59+
/// If ICMP or ICMPv6 packet is directed at this address, route it to smoltcp's interface (which will reply to ICMP echo requests) instead of dropping it.
4760
pub pingable: Option<IpAddr>,
61+
62+
/// Maximum transfer unit to use for smoltcp stack
4863
pub mtu: usize,
64+
65+
/// Receive and send smoltcp TCP socket buffer sizes. Does not affect host socket buffer sizes.
4966
pub tcp_buffer_size: usize,
67+
68+
/// Listen these UDP ports and direct content into the internal network.
69+
5070
pub incoming_udp: Vec<PortForward>,
71+
72+
/// Listen these TCP ports on host and direct connections into the internal network.
5173
pub incoming_tcp: Vec<PortForward>,
5274
}
5375

76+
/// Instructions how to forward a port from host to our internal IP network.
5477
pub struct PortForward {
78+
/// Which socket address to use to bind the host socket to.
5579
pub host: SocketAddr,
80+
81+
/// Typically incoming connection or datagram address is also used as a source address within internal network.
82+
/// Setting `src` allows you to override this and just use source fixed address.
83+
///
84+
/// If port part is `0` and this port forward is used for `incoming_tcp`, the port number is filled in with nonzero value is some way.
5685
pub src: Option<SocketAddr>,
86+
87+
/// Where within our internal network to forward connections or datagrams to.
5788
pub dst: SocketAddr,
5889
}
5990

@@ -62,6 +93,9 @@ mod serve_pingable;
6293
mod serve_tcp;
6394
mod serve_udp;
6495

96+
/// Start the router using given options and a part of channels that represent internal network.
97+
///
98+
/// Dropping the resulting `Future` should abort the entire router.
6599
pub async fn run(
66100
mut rx_from_wg: Receiver<BytesMut>,
67101
tx_to_wg: Sender<BytesMut>,

crates/libwgslirpy/src/wg.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
1+
//! Simplified interface to `boringtun` based on Tokio's mpsc channels.
2+
13
use std::{net::SocketAddr, time::Duration};
24

35
use base64::Engine;
46
use boringtun::noise::TunnResult;
5-
use boringtun::x25519::{PublicKey, StaticSecret};
7+
pub use boringtun::x25519::{PublicKey, StaticSecret};
68
use bytes::BytesMut;
79
use tokio::sync::mpsc::{Receiver, Sender};
810
use tracing::{error, warn};
911

1012
use crate::TEAR_OF_ALLOCATION_SIZE;
1113

14+
/// Options for being a Wireguard peer
1215
pub struct Opts {
16+
/// Private key of this Wireguard node.
17+
///
18+
/// Use `parsebase64_32` and `into()` to specify it from a string.
1319
pub private_key: StaticSecret,
20+
21+
22+
/// Public key of single Wireguard peer we are connecting to.
23+
///
24+
/// Use `parsebase64_32` and `into()` to specify it from a string.
1425
pub peer_key: PublicKey,
26+
27+
/// Static socket address of Wireguard peer to connect to.
28+
///
29+
/// If it is missing, it listens for incoming datagrams and remembers last seen `from` address
30+
/// (data from which Wireguard implementation recognized) for `sendto` purposes.
1531
pub peer_endpoint: Option<SocketAddr>,
32+
33+
/// How often to send keepalive packets to the peer.
1634
pub keepalive_interval: Option<u16>,
35+
36+
/// Socket address to bind local UDP port to.
1737
pub bind_ip_port: SocketAddr,
1838
}
1939

2040
impl Opts {
41+
/// Start Wireguard implementation using this options set.
42+
///
43+
/// Received IP packets would appear at `tx_fromwg`'s channel.
44+
/// IP packets to be sent to Wireguard tunnel is to be written to `rx_towg`'s channel.
2145
pub async fn start(
2246
&self,
2347
tx_fromwg: Sender<BytesMut>,
@@ -111,6 +135,7 @@ impl Opts {
111135
}
112136
}
113137

138+
/// Helper funtion to simplify creating [`StaticSecret`]s and [`PublicKey`]s.
114139
pub fn parsebase64_32(x: &str) -> anyhow::Result<[u8; 32]> {
115140
let b = base64::engine::general_purpose::STANDARD.decode(x)?;
116141
Ok(b[..].try_into()?)

0 commit comments

Comments
 (0)