For a lot of applications today (especially in the IOT and embedded domain), port forwarding is almost unavoidable. Thankfully, there’s a way to automate it.
The Universal Plug and Play protocol (UPnP) provides a feature to automatically install instances of port forwarding in residential Internet gateways. UPnP defines the Internet Gateway Device Protocol (IGD) which is a network service by which an Internet gateway advertises its presence on a private network via the Simple Service Discovery Protocol (SSDP). An application that provides an Internet-based service may discover such gateways and use the UPnP IGD protocol to reserve a port number on the gateway and cause the gateway to forward packets to its listening socket.1
The MiniUPnP project offers software which supports the UPnP Internet Gateway Device (IGD) specifications. This project offers the following softwares:
- MiniUPnP client (
miniupnpc) - an UPnP IGD control point
- MiniUPnP daemon (
miniupnpd) - an implementation of a UPnP IGD + NAT-PMP / PCP gateway
- SSDP managing daemon (
minissdpd): Designed to work with miniupnpc, miniupnpd, minidlna, etc.
miniupnpc-async: Proof of concept for a UPnP IGD control point using asynchronous (non blocking) sockets.
miniupnpc-libevent: UPnP IGD control point using libevent2.
The MiniUPnP client (
miniupnpc) and daemon (
miniupnpd) are portable and should work on any POSIX system.2
I have modified the MiniUPnP client (
upnpc.c) to demonstrate how the MiniUPnP library can be used to setup a port forward.
The code is readable and self-explanatory, thanks to MiniUPnP’s intuitive API.
1. Wikipedia: UPnP 2. Github: MiniUPnP