Standard library · Network

net.udprelay

Carries UDP datagrams over a byte stream, such as an SSH connection.

import net.udprelay

A relay forwards traffic on behalf of someone else. Some links, such as SSH port forwarding, carry only byte streams, so a UDP packet cannot cross them as it is. net.udprelay puts a short header in front of every datagram, sends it over the stream, and the server half sends it on with net.udp and brings the replies back.

app -> Client == SSH stream ==> Serve -> UDP -> internet

The server is one small program. Build it for the machine it runs on and start it with the command given to NewClient:

import net.udprelay

func main:
    ok := udprelay.Serve(0 as i64, 1 as i64, udprelay.ServerOptions {})
    ret ok ? 0 : 1
end

net.tun uses a client as its udp option when relay_udp is on.

•

The server needs POSIX. Serve returns false on Windows. The client works everywhere.

•

IPv4 only.

•

Datagrams may be lost, as with UDP itself: when the queue is full or the stream is down they are dropped.

The server program

The far side is a program that only calls Serve on its standard input and output. Build it for the server, copy it there, and let the client start it over SSH. It ends when the input ends, so nothing keeps running after the connection closes.

import net.udprelay

func main:
    ok := udprelay.Serve(0 as i64, 1 as i64, udprelay.ServerOptions {})
    ret ok ? 0 : 1
end

Using it with net.tun

Give NewClient the command that starts the server, then pass the client to the tunnel. Blocking port 443 makes browsers use TCP instead of QUIC.

import net.tun
import net.udprelay
import os.process

func options(): tun.Options:
    mut cmd := process.NewCommand("ssh")
    cmd.Arg("user@server")
    cmd.Arg("./udprelay")
    mut blocked := Vector {} as Vector
    blocked.Push(443)
    ret tun.Options {
        relay_udp = true,
        udp = udprelay.NewClient(cmd),
        udp_blocked = blocked
    }
end

Types

Datagram

#
struct Datagram

Where a datagram comes from or goes to.

NameTypeDescription
flowint = 0A number that keeps the datagrams of one local socket together; the tun stack uses the source port.
ipi64 = 0The remote IPv4 address as a number.
portint = 0The remote port.
lengthint = 0Bytes of payload.

PacketRelay

#
interface PacketRelay

Anything that can carry datagrams for another package, such as net.tun.

NoRelay

#
struct NoRelay

A relay that drops everything; the default of net.tun.

Send

#
func Send(_flow: int, _ip: i64, _port: int, _ptr: void*, _n: int): bool

Always false.

Receive

#
func Receive(_buf: void*, _max: int, _info &: Datagram, timeout_ms: int): int

Waits and returns 0.

Close

#
func Close()

Does nothing.

ServerOptions

#
struct ServerOptions

Limits of the server half.

NameTypeDescription
max_flowsint = 256Sockets open at once; the oldest is closed to make room.
idle_msint = 120000A socket without traffic for this long is closed.
allow_loopbackbool = falseLets datagrams reach 127.0.0.0/8 on the server. Off by default.
announce_fdi64 = -1When set, the chosen port is written there as RELAY-PORT n, and Listen accepts port 0 to pick a free one.

Client

#
struct Client

The near half: it queues datagrams, writes them to the stream and reads the replies.

NameTypeDescription
corevoid*Internal state.

Send

#
func Send(flow: int, ip: i64, port: int, ptr: void*, n: int): bool

Queues a datagram. False before the server has answered or when the queue is full.

Receive

#
func Receive(buf: void*, max: int, info &: Datagram, timeout_ms: int): int

Reads the next reply, starting the relay program again if it stopped.

Ready

#
func Ready(): bool

True once the server has greeted and the stream is usable.

Dropped

#
func Dropped(): i64

Datagrams that could not be queued.

Close

#
func Close()

Stops the writer and the relay program.

Constants and variables

header_size

#
const header_size: i32 = 10

Bytes in front of every datagram on the stream: length, flow, address and port.

max_payload

#
const max_payload: i32 = 65507

The largest datagram the relay carries.

max_frame

#
const max_frame: i32 = 65517

The largest frame, header and payload together.

Functions

EncodeHeader

#
func EncodeHeader(dst: void*, info: Datagram)

Writes the header of a datagram into memory.

DecodeHeader

#
func DecodeHeader(src: void*, info &: Datagram)

Reads a header from memory.

ParseIPv4

#
func ParseIPv4(text: str): i64

Turns dotted text into a number; -1 when it is not an IPv4 address.

FormatIPv4

#
func FormatIPv4(ip: i64): str

Turns a number back into dotted text.

NewClient

#
func NewClient(cmd: process.Command): Client

A client that runs a command, usually ssh host relay-program, and talks to it over its input and output.

NewPipeClient

#
func NewPipeClient(read_fd: i64, write_fd: i64): Client

A client over two file descriptors that are already open.

NewSocketClient

#
func NewSocketClient(connect: func(): i64, token: str): Client

A client that connects a socket with the given function, sends the token and talks to a server started with Listen. It connects again after the link drops.

Listen

#
func Listen(port: int, token: str, control_fd: i64, opts: ServerOptions): bool

Runs the server on a loopback port for clients that send the token, until the control descriptor ends. The token needs at least 16 characters. POSIX only.

Serve

#
func Serve(in_fd: i64, out_fd: i64, opts: ServerOptions): bool

Runs the server half on two file descriptors until the input ends. POSIX only: false on Windows.