Skip to content

netty channel communicating via TUN devices

License

Notifications You must be signed in to change notification settings

drasyl/netty-tun

Repository files navigation

netty-tun MIT License Maven Central Javadoc

netty channel communicating via TUN devices (tested on macOS, Ubuntu/Linux, and Windows 10/11).

How to use

EventLoopGroup group = new DefaultEventLoopGroup(1);
try {
    final Bootstrap b = new Bootstrap()
        .group(group)
        .channel(TunChannel.class)
        .handler(...);
    final Channel ch = b.bind(new TunAddress()).sync().channel();
    // send/receive messages of type TunPacket...
    ch.closeFuture().sync();
}
finally {
    group.shutdownGracefully();
}

IP Address/Netmask

You can now assign an IP address and netmask to the created network interface (you can query the actual interface name by calling Channel#localAddress()):

# macOS
sudo /sbin/ifconfig utun0 add 10.10.10.10 10.10.10.10
sudo /sbin/ifconfig utun0 up
sudo /sbin/route add -net 10.10.10.0/24 -iface utun0

# Linux
sudo /sbin/ip addr add 10.10.10.10/24 dev utun0
sudo /sbin/ip link set dev utun0 up
# Windows
$InterfaceIndex = Get-NetAdapter -Name utun0 | select ifIndex -expandproperty ifIndex
New-NetIPAddress -InterfaceIndex $InterfaceIndex -IPAddress 10.10.10.10 -PrefixLength 24

# to allow peers access local services, you may mark the tun network as "private"
Set-NetConnectionProfile -InterfaceIndex $InterfaceIndex -NetworkCategory "Private"

MTU

The MTU size of the created network interface is by default 1500 on macOS/Linux and 65535 on Windows.

On macOS/Linux is can be adjusted by passing the channel option TunChannelOption.TUN_MTU to the Bootstrap object.

On Windows you have to use the following command:

netsh interface ipv4 set subinterface tun0 mtu=1234 store=active