$ show heiher/natmap
NATMap is a command-line tool written in C that establishes TCP/UDP port mappings from an ISP NAT public address to a local private address. It uses STUN protocol to discover the mapped public address and maintain NAT bindings through keep-alive sessions. The architecture is built on a cooperative task-based concurrency model (hev-task-system) for efficient non-blocking I/O. It supports two modes: bind mode, where the tool keeps the port alive and notifies the user via a script, and forward mode, where it listens on the mapped port and forwards traffic to an internal target. The codebase is modular, with clear separation of concerns: configuration parsing, socket creation, STUN protocol handling, NAT session keepers (TCP/UDP), port forwarding servers, and script execution. It is highly portable, with cross-compilation support for Linux, Windows, macOS, FreeBSD, and Android across many architectures.
The LLM identified 3 files worth highlighting for reuse:
Provides reusable socket creation helpers for client, STUN, and server sockets with support for binding to interface, fwmark, and address reuse. Encapsulates async I/O with the hev-task-system yielder pattern, making it extractable for other C networking projects.
Implements the STUN binding request/response protocol (RFC 5389) for both TCP and UDP transport. Handles message packing, XOR-MAPPED-ADDRESS decoding, and retry logic. This is a self-contained STUN client implementation that can be reused in any C project needing NAT traversal.
Provides a clean interface to execute an external script with positional arguments (public address, port, etc.) after NAT mapping is established. Handles fork/exec with proper signal handling for SIGCHLD, and includes a Windows-specific workaround for MSYS2 environments. The execution function is decoupled from the rest of the system via a simple callback pattern.