Back to Site
Loading...
Searching...
No Matches
port_mapping_service.h File Reference

Automatic NAT port forwarding (UPnP IGD + NAT-PMP) as a Subsystem. More...

#include "librats/util/rats_export.h"
#include "librats/node/peer_network.h"
#include "librats/nat/port_mapping.h"
#include <cstdint>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <utility>
Include dependency graph for port_mapping_service.h:

Go to the source code of this file.

Classes

class  librats::PortMappingService
 Maps the node's TCP listen port through the home router via UPnP and/or NAT-PMP. More...

Namespaces

namespace  librats

Detailed Description

Automatic NAT port forwarding (UPnP IGD + NAT-PMP) as a Subsystem.

Wraps the standalone UpnpClient (src/upnp.h) and NatPmpClient (src/natpmp.h) backends into the Node lifecycle. On start() it asks the home router to forward the node's listen port so peers behind NAT can accept inbound connections; on stop() it removes the mappings. Both backends run in parallel on their own worker threads — whichever the router supports wins.

The port is forwarded under EVERY protocol the node is actually listening on (PeerNetwork::transports()), not just TCP. Both transports share one listen port by design, and UDP is the dialer's first choice — a TCP-only mapping leaves an inbound peer able to reach us solely over the TCP fallback. It is also the UDP mapping that a hole punch benefits from: a static, port-preserving mapping is what turns the NAT in front of the shared datagram socket into the endpoint-independent kind HolePunch is willing to fire at (see nat_status.h).

Threading: the backends invoke our result callback from their own worker threads, so all shared state is guarded by mutex_. stop() moves the clients out under the lock and stops them OUTSIDE it — stop() joins those workers, which take the lock from handle_result(), so holding it across stop() would deadlock.

Definition in file port_mapping_service.h.