Table of Contents

Class LocalTcpStack

Namespace
VpnHood.Net.TcpStack
Assembly
VpnHood.Net.TcpStack.dll

Provides a lightweight TCP stack: it terminates the TCP connections carried in the IP packets it is fed, such as those a VPN adapter reads from its interface, and gives each as a LocalTcpClient.

public sealed class LocalTcpStack : ITcpStack, IDisposable
Inheritance
LocalTcpStack
Implements
Inherited Members

Remarks

It is intended for local packet paths, such as a virtual adapter on the same machine, where packets arrive in order and are rarely lost. It retransmits what is lost, with backoff, but has no congestion control, reassembles no out-of-order segment, and advertises no window scaling.

OnPacketSend owns each packet it is given and disposes it once sent, as an adapter made with AutoDisposePackets does; when the callback throws, or with no callback, the stack disposes the packet itself. The callback runs inline on whichever thread drives the stack: ProcessIncoming(IpPacket), a stream's read or write, or the stack's timers, so calls may overlap.

Constructors

LocalTcpStack(LocalTcpStackOptions?)

Initializes a new instance of the LocalTcpStack class.

public LocalTcpStack(LocalTcpStackOptions? options = null)

Parameters

options LocalTcpStackOptions

The stack's options, or null for those of the current platform (ForCurrentPlatform()): small on iOS and tvOS, full-size everywhere else.

Exceptions

ArgumentException

An option is out of its range, or two options conflict.

Properties

ActiveDiagnostics

Gets the diagnostics of the stack made last in the process, or null before any, for a monitor with no reference to the stack, such as the iOS memory monitor.

public static TcpStackDiagnostics? ActiveDiagnostics { get; }

Property Value

TcpStackDiagnostics

Remarks

It turns null when that stack is disposed, even while an older stack is still alive.

Diagnostics

Gets the stack's counters and its logging switch.

public TcpStackDiagnostics Diagnostics { get; }

Property Value

TcpStackDiagnostics

OnPacketSend

Gets or sets the callback that sends the IP packets the stack produces.

public Action<IpPacket>? OnPacketSend { get; set; }

Property Value

Action<IpPacket>

Remarks

The callback owns each packet it returns from and disposes it once sent. When the callback throws, the stack disposes the packet, so the callback must not keep it; with no callback, the stack disposes its packets. The callback runs inline on whichever thread drives the stack: packet processing, a stream's read or write, or the stack's timers, so calls may overlap.

VerboseLogging

Gets or sets a value indicating whether the stack logs verbose diagnostics, such as retransmissions and zero-window probes. It is the same switch as VerboseLogging.

public bool VerboseLogging { get; set; }

Property Value

bool

Methods

Dispose()

Stops the stack: stops its timers, then its listeners, which close the connections in their queues as Dispose() does, and then closes every other connection without telling its peer. An accepted stream can still read what it has buffered. Calling it again does nothing.

public void Dispose()

DropAllConnections()

Aborts every connection: each peer gets a reset, and each stream reads as ended. Listeners stay, and new connections are accepted afterward.

public void DropAllConnections()

Listen(IpEndPointValue)

Gets the listener that accepts the connections to an endpoint: that address and that port exactly. A connection no endpoint listener takes goes to the listener of ListenAny(), or else is answered with a reset.

public LocalTcpListener Listen(IpEndPointValue localEndPoint)

Parameters

localEndPoint IpEndPointValue

The endpoint the connections are made to.

Returns

LocalTcpListener

The listener; each call for the same endpoint returns the same one until it is stopped.

Exceptions

ObjectDisposedException

The stack is disposed.

ListenAny()

Gets the listener that accepts the connections no endpoint listener takes, to any address and port of either IP version, as a transparent proxy needs.

public LocalTcpListener ListenAny()

Returns

LocalTcpListener

The listener; each call returns the same one until it is stopped.

Exceptions

ObjectDisposedException

The stack is disposed.

ProcessIncoming(IpPacket)

Processes an IP packet received for the stack.

public void ProcessIncoming(IpPacket ipPacket)

Parameters

ipPacket IpPacket

The packet, already parsed.

Remarks

The stack copies what it keeps: the caller still owns the packet and disposes it. A packet that is not TCP is ignored.

StopListening(IpEndPointValue)

Stops the listener of an endpoint and removes it from the stack.

public bool StopListening(IpEndPointValue localEndPoint)

Parameters

localEndPoint IpEndPointValue

The listener's endpoint.

Returns

bool

true if a listener was stopped; false if the endpoint had none.