Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
upnp Namespace Reference

UPnP port mapping. More...

Classes

class  deinit_t
 RAII helper that runs shutdown cleanup when destroyed. More...
struct  igd_t
 A discovered IGD and the data needed to manage its port mappings. More...
struct  mapping_lookup_t
 Verified ownership and lease information for one mapping. More...
struct  mapping_reply_t
 Fields returned by a specific or generic mapping query. More...
struct  mapping_t
 UPnP port mapping description and lease state. More...
struct  owned_mapping_t
 A forwarding rule Hermes owns, including its gateway and destination. More...
class  urls_t
 Owning wrapper for MiniUPnPc URLs. More...

Typedefs

using device_t = util::safe_ptr<UPNPDev, freeUPNPDevlist>
 Owning pointer to miniupnpc device discovery results.

Enumerations

enum class  igd_status_e : int { none = 0 , connected = 1 , disconnected , unknown_device }
 Result codes returned by the installed MiniUPnPc library. More...
enum class  mapping_state_e {
  missing , ours , existing , conflict ,
  unknown
}
 Relationship between a gateway's existing mapping and the requested rule. More...

Functions

igd_status_e UPNP_GetValidIGDStatus (device_t &device, urls_t *urls, IGDdatas *data, std::array< char, INET6_ADDRESS_STRLEN > &lan_addr)
 Get the valid IGD status.
bool is_valid_igd (igd_status_e status)
 Check whether a UPNP_GetValidIGD() status code identifies a usable IGD.
int shared_prefix_length (std::string_view a, std::string_view b)
 Count the leading bits two IPv4 addresses have in common.
std::string url_host (std::string_view url)
 Extract the host from a URL such as http://192.168.1.1:1900/rootDesc.xml.
std::size_t select_inner_igd (const std::vector< std::string > &igd_hosts, std::string_view lan_addr)
 Select the IGD of the network Hermes is attached to.
std::optional< std::size_t > select_outer_igd (const std::vector< std::string > &igd_hosts, std::size_t inner, std::string_view inner_external_addr)
 Select the IGD in front of the inner gateway when Hermes is behind two routers (double NAT).
std::unique_ptr< platf::deinit_t > start ()
 Start UPnP port mapping and return its shutdown guard.
bool lease_needs_refresh (std::string_view lease)
 Detect an owned finite lease that needs replacement on routers that do not renew repeated adds.
mapping_state_e classify_mapping (int result, std::string_view client, std::string_view port, std::string_view description, const owned_mapping_t &wanted)
 Classify a lookup without treating a destination alone as proof of ownership.
bool may_manage_mapping (mapping_state_e state, bool recorded)
 Require a local ownership record as well as a matching router response.
template<class Specific, class Generic>
mapping_lookup_t verify_mapping_lookup (const owned_mapping_t &wanted, bool recorded, Specific specific, Generic generic)
 Verify an owned rule even when GetSpecificPortMappingEntry reports port zero.
bool should_retain_recovery_record (mapping_state_e state, bool permanent)
 Keep permanent recovery records when router ownership is ambiguous.
std::string mapping_journal_event (const owned_mapping_t &mapping, bool added)
 Encode one append-only recovery event before adding or after removing a permanent rule.
std::vector< owned_mapping_t > read_mapping_journal (const std::string &text)
 Recover outstanding permanent rules, ignoring incomplete or invalid journal lines.

Variables

constexpr auto INET6_ADDRESS_STRLEN = 46
 Buffer size for a textual IPv6 address.
constexpr auto IPv4 = 0
 IPv4 discovery.
constexpr auto IPv6 = 1
 IPv6 discovery.
constexpr auto PORT_MAPPING_LIFETIME = 3600s
 Requested finite mapping lifetime.
constexpr auto REFRESH_INTERVAL = 120s
 Interval between mapping refresh passes.

Detailed Description

UPnP port mapping.

Typedef Documentation

◆ device_t

using upnp::device_t = util::safe_ptr<UPNPDev, freeUPNPDevlist>

Owning pointer to miniupnpc device discovery results.

Definition at line 33 of file upnp.h.

Enumeration Type Documentation

◆ igd_status_e

enum class upnp::igd_status_e : int
strong

Result codes returned by the installed MiniUPnPc library.

Project-owned equivalent of the UPNP_GetValidIGD() result codes. MiniUPnPc 2.2.8 (API version 18) introduced the private-address result, renumbering the values that follow it; the library's own UPNP_* symbolic names only exist starting with MiniUPnPc 2.3.0 (API version 19), so this enum centralizes the version-dependent numbering instead of relying on those names.

Enumerator
none 

No IGD was found.

connected 

A connected IGD was found.

disconnected 

A disconnected IGD was found.

unknown_device 

The device was not recognized as an IGD.

Definition at line 46 of file upnp.h.

◆ mapping_state_e

enum class upnp::mapping_state_e
strong

Relationship between a gateway's existing mapping and the requested rule.

Enumerator
missing 

Gateway explicitly reports no such mapping.

ours 

Destination, port and description match Hermes's record.

existing 

Someone else's rule already forwards to the requested destination.

conflict 

Rule forwards elsewhere and must be left untouched.

unknown 

Lookup failed; ownership cannot be established.

Definition at line 61 of file upnp_mapping.h.

Function Documentation

◆ classify_mapping()

mapping_state_e upnp::classify_mapping ( int result,
std::string_view client,
std::string_view port,
std::string_view description,
const owned_mapping_t & wanted )
inline

Classify a lookup without treating a destination alone as proof of ownership.

Parameters
resultMiniUPnPc lookup result.
clientReported destination address.
portReported destination port.
descriptionReported description.
wantedRequested or previously recorded rule.
Returns
Ownership classification.

Definition at line 78 of file upnp_mapping.h.

◆ is_valid_igd()

bool upnp::is_valid_igd ( igd_status_e status)
nodiscard

Check whether a UPNP_GetValidIGD() status code identifies a usable IGD.

Accepts every status code that identifies a valid IGD (connected, private-IP, or disconnected). The private_ip member only exists with MiniUPnPc >= 2.2.8 (API version 18); disconnected is 2 before API 18 and 3 starting with API 18, and unknown_device is 3 or 4 respectively. Callers don't need to handle the version difference themselves.

Parameters
statusThe status returned by UPNP_GetValidIGDStatus(), converted to igd_status_e.
Returns
true if the status identifies a usable IGD, false otherwise.

Definition at line 133 of file upnp.cpp.

◆ lease_needs_refresh()

bool upnp::lease_needs_refresh ( std::string_view lease)
inline

Detect an owned finite lease that needs replacement on routers that do not renew repeated adds.

Parameters
leaseRemaining lifetime reported by the router, in seconds.
Returns
Whether a valid nonzero lease has at most ten minutes remaining.

Definition at line 27 of file upnp_mapping.h.

◆ mapping_journal_event()

std::string upnp::mapping_journal_event ( const owned_mapping_t & mapping,
bool added )
inline

Encode one append-only recovery event before adding or after removing a permanent rule.

Parameters
mappingRule being recorded.
addedWhether this records creation intent rather than completed removal.
Returns
A newline-delimited event, or an empty string for an invalid identity.

Definition at line 222 of file upnp_mapping.h.

◆ may_manage_mapping()

bool upnp::may_manage_mapping ( mapping_state_e state,
bool recorded )
inline

Require a local ownership record as well as a matching router response.

A pre-existing manual mapping may use the same Hermes description.

Definition at line 95 of file upnp_mapping.h.

◆ read_mapping_journal()

std::vector< owned_mapping_t > upnp::read_mapping_journal ( const std::string & text)
inline

Recover outstanding permanent rules, ignoring incomplete or invalid journal lines.

Parameters
textPreviously written recovery events.
Returns
Rules whose creation intent has no matching completed removal.

Definition at line 241 of file upnp_mapping.h.

◆ select_inner_igd()

std::size_t upnp::select_inner_igd ( const std::vector< std::string > & igd_hosts,
std::string_view lan_addr )
nodiscard

Select the IGD of the network Hermes is attached to.

Behind two routers, the outer router often answers discovery too, and UPNP_GetValidIGD() prefers it because it has a public address. That router can't reach Hermes's address, so a mapping there opens nothing. The gateway Hermes is attached to is the one whose address shares the most leading bits with Hermes's own address. On a tie, the earlier entry wins, so the first entry (the IGD chosen by UPNP_GetValidIGD()) is kept unless another IGD is strictly closer.

Parameters
igd_hostsAddresses of the discovered IGDs.
lan_addrHermes's local address.
Returns
Index of the selected IGD in igd_hosts, or 0 if the list is empty.

Definition at line 185 of file upnp.cpp.

◆ select_outer_igd()

std::optional< std::size_t > upnp::select_outer_igd ( const std::vector< std::string > & igd_hosts,
std::size_t inner,
std::string_view inner_external_addr )
nodiscard

Select the IGD in front of the inner gateway when Hermes is behind two routers (double NAT).

The outer router is only looked for when the inner gateway's external address is private. It is the other IGD whose address shares the most leading bits (at least 16) with that external address.

Parameters
igd_hostsAddresses of the discovered IGDs.
innerIndex of the inner gateway in igd_hosts.
inner_external_addrExternal address reported by the inner gateway.
Returns
Index of the outer IGD in igd_hosts, or std::nullopt if there is none.

Definition at line 198 of file upnp.cpp.

◆ shared_prefix_length()

int upnp::shared_prefix_length ( std::string_view a,
std::string_view b )
nodiscard

Count the leading bits two IPv4 addresses have in common.

Parameters
aFirst IPv4 address in dotted notation.
bSecond IPv4 address in dotted notation.
Returns
Number of shared leading bits (0 to 32), or -1 if either value is not an IPv4 address.

Definition at line 161 of file upnp.cpp.

◆ should_retain_recovery_record()

bool upnp::should_retain_recovery_record ( mapping_state_e state,
bool permanent )
inline

Keep permanent recovery records when router ownership is ambiguous.

A gateway may rewrite descriptions without removing the forwarding rule.

Definition at line 212 of file upnp_mapping.h.

◆ start()

std::unique_ptr< platf::deinit_t > upnp::start ( )
nodiscard

Start UPnP port mapping and return its shutdown guard.

Returns
Start status.

Definition at line 743 of file upnp.cpp.

◆ UPNP_GetValidIGDStatus()

igd_status_e upnp::UPNP_GetValidIGDStatus ( device_t & device,
urls_t * urls,
IGDdatas * data,
std::array< char, INET6_ADDRESS_STRLEN > & lan_addr )

Get the valid IGD status.

This function is a wrapper around UPNP_GetValidIGD() that returns the status code. There is a pre-processor check to determine which version of the function to call based on the version of the MiniUPnPc library.

Parameters
deviceThe device.
urlsThe URLs.
dataThe IGD data.
lan_addrThe LAN address.
Returns
The IGD status as an igd_status_e value, converted from the raw UPNP_GetValidIGD() result.
Return values
igd_status_e::noneNo IGD found.
igd_status_e::connectedA valid connected IGD has been found.
igd_status_e::private_ipA valid connected IGD with a private IP address (MiniUPnPc >= 2.2.8 only).
igd_status_e::disconnectedA valid IGD has been found but it reported as not connected.
igd_status_e::unknown_deviceAn UPnP device has been found but was not recognized as an IGD.

Definition at line 124 of file upnp.cpp.

◆ url_host()

std::string upnp::url_host ( std::string_view url)
nodiscard

Extract the host from a URL such as http://192.168.1.1:1900/rootDesc.xml.

Parameters
urlThe URL to parse.
Returns
The host part of the URL, without port or brackets.

Definition at line 173 of file upnp.cpp.

◆ verify_mapping_lookup()

template<class Specific, class Generic>
mapping_lookup_t upnp::verify_mapping_lookup ( const owned_mapping_t & wanted,
bool recorded,
Specific specific,
Generic generic )

Verify an owned rule even when GetSpecificPortMappingEntry reports port zero.

Some IGDs report the correct port only through GetGenericPortMappingEntry, but append garbage to its description. Never use that description as ownership evidence: require the exact description in specific queries both before and after a complete, bounded enumeration. An unrecorded rule cannot use this fallback.

Parameters
wantedRule on this exact gateway/service and wildcard remote host.
recordedWhether Hermes has a local ownership record for this identity.
specificQuery the wildcard mapping by external port and protocol.
genericQuery a mapping by table index, returning 713 at the end.

Definition at line 131 of file upnp_mapping.h.

Variable Documentation

◆ INET6_ADDRESS_STRLEN

auto upnp::INET6_ADDRESS_STRLEN = 46
constexpr

Buffer size for a textual IPv6 address.

Definition at line 24 of file upnp.h.

◆ IPv4

auto upnp::IPv4 = 0
constexpr

IPv4 discovery.

Definition at line 25 of file upnp.h.

◆ IPv6

auto upnp::IPv6 = 1
constexpr

IPv6 discovery.

Definition at line 26 of file upnp.h.

◆ PORT_MAPPING_LIFETIME

auto upnp::PORT_MAPPING_LIFETIME = 3600s
constexpr

Requested finite mapping lifetime.

Definition at line 27 of file upnp.h.

◆ REFRESH_INTERVAL

auto upnp::REFRESH_INTERVAL = 120s
constexpr

Interval between mapping refresh passes.

Definition at line 28 of file upnp.h.