Class NullVpnAdapter
- Namespace
- VpnHood.Net.VpnAdapters.Abstractions
- Assembly
- VpnHood.Net.VpnAdapters.Abstractions.dll
Represents a VPN adapter with no interface: it reads no packets and takes every packet written to it, for tests and for running a VPN without a tunnel on the host.
public class NullVpnAdapter : TunVpnAdapter, IVpnAdapter, IPacketTransport, IDisposable
- Inheritance
-
NullVpnAdapter
- Implements
- Inherited Members
Remarks
It needs no rights: its start and stop touch nothing on the host, it supports NAT and app filters, and it neither restarts nor checks its interface when the host's network changes. A packet written to it is disposed: nothing takes it on.
Constructors
NullVpnAdapter(bool)
Initializes a new instance of the NullVpnAdapter class, named "NullAdapter".
public NullVpnAdapter(bool blocking)
Parameters
blockingbooltrue to make a send wait for room when the queue is full; false to drop the oldest packet instead.
NullVpnAdapter(VpnAdapterSettings)
Initializes a new instance of the NullVpnAdapter class with settings of a derived class's own.
protected NullVpnAdapter(VpnAdapterSettings adapterSettings)
Parameters
adapterSettingsVpnAdapterSettingsThe adapter's settings.
Properties
AppPackageId
Gets the ID the app filter knows this app by, which it always lets into the tunnel, or null where there is no app filter.
protected override string AppPackageId { get; }
Property Value
CheckAdapterInterfaceAfterNetworkChanged
Gets a value indicating whether the adapter checks, at each change of the host's network, that its interface is still there, and stops when it is not. The check is IsAdapterInterfaceEnabled(), which by default looks for the interface by name or address only.
protected override bool CheckAdapterInterfaceAfterNetworkChanged { get; }
Property Value
IsAppFilterSupported
Gets a value indicating whether the adapter can include or exclude apps from the tunnel (IncludeApps, ExcludeApps).
public override bool IsAppFilterSupported { get; }
Property Value
IsSocketProtectedByBind
Gets a value indicating whether the adapter keeps a socket out of the tunnel by binding it to the primary address, rather than by an OS call of its own.
protected override bool IsSocketProtectedByBind { get; }
Property Value
Remarks
When true, the routes leave out the probe networks the adapter aims at to find the primary addresses, and that search runs on an unprotected socket after a network change.
RestartAfterNetworkAddressChanged
Gets a value indicating whether the adapter restarts when the host moves to other networks. When false, it takes the new primary addresses and raises PrimaryNetworkChanged.
protected override bool RestartAfterNetworkAddressChanged { get; }
Property Value
Methods
AdapterAdd(CancellationToken)
Makes the interface: the first step of a start.
protected override Task AdapterAdd(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
AdapterClose()
Closes the interface for reading and writing: the first step of a stop, under the base's stop lock. It may run after a partial start.
protected override void AdapterClose()
Remarks
It releases the run's reader, which then leaves.
AdapterOpen(CancellationToken)
Opens the interface for reading and writing packets: the last step of a start, before the reader starts.
protected override Task AdapterOpen(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
AdapterRemove()
Removes the interface and what the start added with it: the last step of a stop, after AdapterClose(). When the base calls it, it runs under the base's stop lock and must not wait for the reader or a sender to take that lock. It may run after a partial start, and after a close that failed.
protected override void AdapterRemove()
AddAddress(IpNetwork, CancellationToken)
Gives the interface its address in a virtual network. A start calls it once per configured IP version.
protected override Task AddAddress(IpNetwork ipNetwork, CancellationToken cancellationToken)
Parameters
ipNetworkIpNetworkThe interface's address with its network.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
AddNat(IpNetwork, CancellationToken)
Translates a virtual network to the host's primary address (NAT). A start calls it with UseNat, for each IP version the adapter's NAT translates and the host has a primary address of.
protected override Task AddNat(IpNetwork ipNetwork, CancellationToken cancellationToken)
Parameters
ipNetworkIpNetworkThe virtual network.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
AddRoute(IpNetwork, CancellationToken)
Routes a network through the interface. A start calls it once per network, for each IP version the interface carries.
protected override Task AddRoute(IpNetwork ipNetwork, CancellationToken cancellationToken)
Parameters
ipNetworkIpNetworkThe network.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
DisposeManaged()
Releases the blocked reader, then disposes the adapter.
protected override void DisposeManaged()
IsNatSupported(IpVersion)
Determines whether the adapter's NAT translates an IP version. A server forwards a version the adapter does not translate by its own forwarders.
public override bool IsNatSupported(IpVersion ipVersion)
Parameters
ipVersionIpVersionThe IP version.
Returns
ReadPacket(byte[])
Reads no packet.
protected override int ReadPacket(byte[] buffer)
Parameters
bufferbyte[]Not used.
Returns
- int
Always 0.
SetAllowedApps(IEnumerable<string>, CancellationToken)
Lets only these apps' traffic into the tunnel. A start calls it with IncludeApps, where IsAppFilterSupported is true.
protected override Task SetAllowedApps(IEnumerable<string> packageIds, CancellationToken cancellationToken)
Parameters
packageIdsIEnumerable<string>The apps' IDs, this app's own included.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
SetDisallowedApps(IEnumerable<string>, CancellationToken)
Keeps these apps' traffic out of the tunnel. A start calls it with ExcludeApps, where IsAppFilterSupported is true.
protected override Task SetDisallowedApps(IEnumerable<string> packageIds, CancellationToken cancellationToken)
Parameters
packageIdsIEnumerable<string>The apps' IDs, never this app's own.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
SetDnsServers(IReadOnlyList<IPAddress>, CancellationToken)
Sets the DNS servers the interface gives the host. A start calls it every time.
protected override Task SetDnsServers(IReadOnlyList<IPAddress> dnsServers, CancellationToken cancellationToken)
Parameters
dnsServersIReadOnlyList<IPAddress>The servers, only of the IP versions the interface carries; may be empty.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
SetMetric(int, bool, bool, CancellationToken)
Sets the interface's route metric. A start calls it when Metric is set.
protected override Task SetMetric(int metric, bool ipV4, bool ipV6, CancellationToken cancellationToken)
Parameters
metricintThe metric.
ipV4booltrue if the interface carries IPv4 in this run.
ipV6booltrue if the interface carries IPv6 in this run.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
SetMtu(int, bool, bool, CancellationToken)
Sets the interface's MTU. A start calls it when Mtu is set.
protected override Task SetMtu(int mtu, bool ipV4, bool ipV6, CancellationToken cancellationToken)
Parameters
mtuintThe MTU in bytes.
ipV4booltrue if the interface carries IPv4 in this run.
ipV6booltrue if the interface carries IPv6 in this run.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
SetSessionName(string, CancellationToken)
Names the session where the OS shows it. A start calls it when SessionName is not empty.
protected override Task SetSessionName(string sessionName, CancellationToken cancellationToken)
Parameters
sessionNamestringThe name.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
StartReadingPackets()
Reads packets from the interface and hands each to the transport's receivers, until a stop retires this run. It runs on the reader's task; ten read errors in a row stop the adapter.
protected override void StartReadingPackets()
WaitForTunRead()
Blocks the reader until its run is stopped, as no packet ever arrives.
protected override void WaitForTunRead()
WaitForTunWrite()
Blocks until the interface may take a packet again, between the retries of a write it refused.
protected override void WaitForTunWrite()
WritePacket(IpPacket)
Takes a packet and drops it; the adapter's transport disposes it, as it does every written packet.
protected override bool WritePacket(IpPacket ipPacket)
Parameters
ipPacketIpPacketThe packet.