Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VNT — Virtual Network Tool

A high-performance virtual LAN tool built on async_net, creating virtual networks where devices can communicate as if on the same local network.

Features

  • Virtual LAN — Create a virtual network where all clients share the same subnet and can ping/access each other
  • TUN Device — Standard OS virtual network interface (no custom drivers needed)
  • Cross-Platform TUN — Supports Linux (/dev/net/tun), macOS (utun), and Windows (TAP-Windows driver)
  • Async I/O — Built on async_net's C++20 coroutine-based async network library
  • UDP Transport — Efficient UDP-based relay protocol between server and clients
  • Auto Peer Discovery — Server automatically pushes peer list updates to all clients
  • Heartbeat & Cleanup — Automatic stale client detection and removal
  • Robust Restart — Auto-cleans stale TUN devices from previous crashed runs

Architecture

┌─────────────┐         ┌─────────────┐
│  Client A   │         │  Client B   │
│  10.26.0.2  │         │  10.26.0.3  │
│  TUN: vnt0  │         │  TUN: vnt0  │
└──────┬──────┘         └──────┬──────┘
       │ UDP                   │ UDP
       │                       │
       ▼                       ▼
┌──────────────────────────────────────┐
│            VNT Server (vnts)         │
│         UDP Relay + Registry         │
│         Virtual Net: 10.26.0.0/24    │
└──────────────────────────────────────┘

Flow:

  1. Client creates a TUN device with the assigned virtual IP
  2. Client registers with the server via UDP (sends desired virtual IP)
  3. Server assigns IP, registers the client, and pushes peer list to all clients
  4. IP packets from TUN → wrapped in UDP frames → sent to server → forwarded to destination client → written to destination's TUN
  5. Heartbeat keeps the registration alive; stale clients are auto-cleaned after 90s

Wire Protocol

Frame format:
  ┌──────────┬────────────┬───────────────┬─────────┐
  │ type (1) │ client_id  │ payload_len   │ payload │
  │          │   (2)      │    (2)        │  (var)  │
  └──────────┴────────────┴───────────────┴─────────┘
  All multi-byte fields in network byte order (big-endian).

Message types:
  0x01  Register      Client → Server  Register with virtual IP
  0x02  RegisterAck   Server → Client  Registration result + assigned IP
  0x03  Data          Bidirectional    Forwarded IP packet
  0x04  Heartbeat     Client → Server  Keep-alive
  0x05  PeerNotify    Server → Client  Peer list update

Building

Prerequisites

  • C++20 compiler (GCC 10+, Clang 14+, or MSVC 19.28+)
  • CMake 3.20+
  • async_net library (pre-built)

Build

# 1. Build async_net first (if not already built)
cd /path/to/async_net
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . -j$(nproc)

# 2. Build VNT
cd /path/to/vnt
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DASYNC_NET_DIR=/path/to/async_net
cmake --build . -j$(nproc)

Platform Notes

Platform TUN Backend Notes
Linux /dev/net/tun + TUNSETIFF Uses ip command for interface config
macOS utun via AF_SYSTEM Uses ifconfig for interface config
Windows TAP-Windows driver Requires TAP-Windows adapter installed

Usage

Start the Server

./vnts [options]
  -p <port>     UDP listen port       (default: 22688)
  -n <name>     Virtual device name   (default: vnt)
  -i <ip>       Server virtual IP     (default: 10.26.0.1)
  -m <mask>     Network mask          (default: 255.255.255.0)
  -P <password> Network password      (optional, reserved for encryption)

Example:

./vnts -p 22688 -i 10.26.0.1 -m 255.255.255.0

Start a Client

Requires root/Administrator privileges (for TUN device creation).

sudo ./vnt [options]
  -s <host>     Server address        (default: 127.0.0.1)
  -p <port>     Server UDP port       (default: 22688)
  -n <name>     Client name           (default: vnt-client)
  -i <ip>       Desired virtual IP    (default: 10.26.0.2)
  -m <mask>     Network mask          (default: 255.255.255.0)
  -d <device>   TUN device name       (default: vnt)
  -P <password> Network password      (optional, reserved for encryption)

Example — start two clients on the same machine:

# Client A
sudo ./vnt -s 192.168.1.100 -n "machine-a" -i 10.26.0.2 -d vnt0

# Client B (on another machine or with different device name)
sudo ./vnt -s 192.168.1.100 -n "machine-b" -i 10.26.0.3 -d vnt0

After both clients connect, they can ping each other:

# From Client A
ping 10.26.0.3

# From Client B
ping 10.26.0.2

Project Structure

vnt/
├── CMakeLists.txt          # Build configuration
├── README.md               # This file (English)
├── README_CN.md            # Chinese README
└── src/
    ├── protocol.hpp        # Wire protocol (frame format, message types, codecs)
    ├── tun_device.hpp      # Cross-platform TUN device (Linux/macOS/Windows)
    ├── vnts_server.hpp     # Relay server (client registry, packet routing, heartbeat)
    ├── vnt_client.hpp      # Client (TUN ↔ UDP bridge, registration, heartbeat)
    ├── vnts_main.cpp       # Server entry point
    └── vnt_main.cpp        # Client entry point

License

MIT License.

About

基于 async_net 构建的高性能虚拟局域网工具,创建虚拟网络使设备可以像在同一局域网中一样互相通信

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages