userspace tcp/ip stack
cargo buildcargo run -p cli -- [--tap] <name>- linux:
<name>is a tun interface name liketcpip0 - windows:
<name>is a wintun adapter name liketcpip
create a tun interface by running the cli (needs root):
sudo cargo run -p cli -- tcpip0in another terminal, bring it up and assign an address:
sudo ip link set tcpip0 up
sudo ip addr add 10.0.0.1/24 dev tcpip0test:
ping 10.0.0.2udp echo (port 9000):
python3 - <<'PY'
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1)
s.sendto(b"hi", ("10.0.0.2", 9000))
print(s.recvfrom(2048))
PYrun (needs root):
sudo cargo run -p cli -- --tap tcpip0in another terminal, bring it up and assign an address:
sudo ip link set tcpip0 up
sudo ip addr add 10.0.0.1/24 dev tcpip0test:
ping 10.0.0.2tcp echo (port 9001):
python3 - <<'PY'
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect(("10.0.0.2", 9001))
s.sendall(b"hi")
print(s.recv(2048))
s.close()
PYthis uses wintun
requirements:
- install wireguard (it installs the driver)
- put
wintun.dllnext to the built exe, or inpath
run as admin:
cargo run -p cli -- tcpipassign ip:
netsh interface ipv4 set address name="tcpip" static 10.0.0.1 255.255.255.0test:
ping 10.0.0.2the stack responds to fd00::2 on the tap interface
assign ipv6 to the host (note: the prefix must be /64, not /128):
New-NetIPAddress -InterfaceAlias "tcpip-tap" -IPAddress "fd00::1" -PrefixLength 64verify:
Get-NetIPAddress -InterfaceAlias "tcpip-tap" -AddressFamily IPv6 | Select IPAddress, PrefixLengthrun the stack (admin):
cargo run -p cli -- --tap tcpip-tapin another terminal:
ping -6 fd00::2optional neighbor cache check:
Get-NetNeighbor -InterfaceAlias "tcpip-tap" -AddressFamily IPv6 |
Where-Object { $_.IPAddress -eq "fd00::2" } |
Select IPAddress, LinkLayerAddress, Stateudp echo (port 9000):
$udp = New-Object System.Net.Sockets.UdpClient
$udp.Client.ReceiveTimeout = 1000
$udp.Connect("10.0.0.2", 9000)
$buf = [System.Text.Encoding]::ASCII.GetBytes("hi")
$udp.Send($buf, $buf.Length) | Out-Null
$rem = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any, 0)
$out = $udp.Receive([ref]$rem)
[System.Text.Encoding]::ASCII.GetString($out)tcp echo (port 9001):
$tcp = New-Object System.Net.Sockets.TcpClient
$tcp.ReceiveTimeout = 2000
$tcp.SendTimeout = 2000
$tcp.Connect("10.0.0.2", 9001)
$st = $tcp.GetStream()
$buf = [System.Text.Encoding]::ASCII.GetBytes("hi")
$st.Write($buf, 0, $buf.Length)
$out = New-Object byte[] 2048
$n = $st.Read($out, 0, $out.Length)
[System.Text.Encoding]::ASCII.GetString($out, 0, $n)
$tcp.Close()this uses tap-windows6
requirements:
- install a package that actually installs the tap-windows6 network adapter
- openvpn community (msi) works, or a standalone tap-windows6 installer
- create a tap adapter and name it (example name:
tcpip-tap)
verify you have it:
ncpa.cpl(network connections) should show something likeTAP-Windows Adapter V9- or powershell:
Get-NetAdapter | Select Name, InterfaceDescription
run as admin:
cargo run -p cli -- --tap tcpip-tapassign ip:
netsh interface ipv4 set address name="tcpip-tap" static 10.0.0.1 255.255.255.0test:
ping 10.0.0.2