Skip to main content

Module nat_mapping

Module nat_mapping 

Source
Expand description

NAT-assisted candidates (RFC 6544 section 5.3, RFC 8445 section 5.1.1.2): an opt-in client of the Port Control Protocol (PCP, RFC 6887) with a fallback to NAT-PMP (RFC 6886), asking the gateway for a port mapping. Nothing here runs unless the application calls it, and it adds no dependency: one UDP socket of the driver’s Runtime.

The engine needs nothing new: the mapped address is a candidate the application adds itself, a server-reflexive one whose related address is the base (RFC 6544 section 5.3 calls it NAT-assisted; candidate_line writes it with the type preference 105 RFC 6544 section 4.2 recommends). Finding the gateway is the application’s: this module does not read routing tables (that would need a platform dependency); the default gateway’s address is passed in.

map_port sends a PCP MAP request (RFC 6887 section 11) to gateway:5351, retransmitted as section 8.1.1 says (3 s first, doubling, here up to MappingConfig::attempts times); a gateway that answers with UNSUPP_VERSION, or with a NAT-PMP response, is asked again with NAT-PMP (RFC 6886 section 3.3, retransmitted from 250 ms doubling, at most nine times, section 3.1). Mappings expire: renew them with map_port again before half their lifetime has passed, and delete one with a lifetime of 0.

Structs§

Mapping
A mapping the gateway granted.
MappingConfig
How map_port asks.

Enums§

MappingProtocol
Which protocol answered.
NatPmpAnswer
A NAT-PMP answer.
PcpAnswer
What a PCP response says.
Protocol
The transport a mapping is for.

Constants§

SERVER_PORT
The port PCP and NAT-PMP servers listen on (RFC 6887 section 19.1, RFC 6886 section 3).

Functions§

candidate_line
The NAT-assisted candidate of mapping for the local base (RFC 6544 sections 4.2 and 5.3): a server-reflexive candidate with the type preference 105, for the application to add with add_local_candidate. Over TCP tcp_type names its direction (RFC 6544 section 4.2: for NAT-assisted candidates simultaneous-open 6, active 4, passive 2; an active one is signalled on port 9).
map_port
Asks the gateway at gateway for a mapping of internal_port on local_ip (the interface the gateway is reached from): PCP first, NAT-PMP when the gateway does not speak PCP. Ok(None) when no gateway answered; an error when it refused.
nat_pmp_address_request
A NAT-PMP external address request (RFC 6886 section 3.2).
nat_pmp_map_request
A NAT-PMP mapping request (RFC 6886 section 3.3).
parse_nat_pmp_response
Parses a NAT-PMP response to opcode (RFC 6886 sections 3.2, 3.3, 3.5).
parse_pcp_map_response
Parses a PCP MAP response to the request with nonce (RFC 6887 sections 7.2 and 11.1). None for anything else (not PCP, not a MAP response, another nonce).
pcp_map_request
A PCP MAP request (RFC 6887 sections 7.1 and 11.1): 24-byte header, 36-byte MAP opcode.