nat Module¶
The nat module provides the main public API for NAT traversal operations.
Types¶
Discovery Functions¶
discover/0¶
Discover a NAT gateway and store the context in nat_server.
The discovery process tries multiple protocols in parallel:
- UPnP IGD v2
- UPnP IGD v1
- NAT-PMP
- PCP
The first protocol to successfully respond is used.
Returns:
ok- NAT gateway discovered and context storedno_nat- No NAT gateway found (may indicate direct internet connection){error, Reason}- Discovery failed
Example:
case nat:discover() of
ok ->
io:format("NAT discovered~n");
no_nat ->
io:format("No NAT found~n");
{error, timeout} ->
io:format("Discovery timed out~n")
end.
get_context/0¶
Get the stored NAT context for advanced operations.
Returns:
{ok, {Module, Context}}- The NAT context tuple{error, no_context}- No context stored (calldiscover/0first)
Example:
Port Mapping Functions¶
add_port_mapping/3¶
-spec add_port_mapping(Protocol, InternalPort, ExternalPort) -> Result when
Protocol :: nat_protocol(),
InternalPort :: non_neg_integer(),
ExternalPort :: non_neg_integer(),
Result :: {ok, Since, InternalPort, ExternalPort, Lifetime} | {error, term()}.
Add a port mapping with the default lifetime (3600 seconds).
The mapping is automatically renewed before expiry.
Parameters:
Protocol-tcporudpInternalPort- Local port number to mapExternalPort- Requested external port (0 for any available port)
Returns:
{ok, Since, InternalPort, ExternalPort, Lifetime}- Mapping createdSince- NAT epoch timestamp when mapping was createdInternalPort- Actual internal port mappedExternalPort- Actual external port assignedLifetime- Mapping lifetime in seconds
{error, Reason}- Mapping failed
Example:
add_port_mapping/4¶
-spec add_port_mapping(Protocol, InternalPort, ExternalPort, Lifetime) -> Result when
Protocol :: nat_protocol(),
InternalPort :: non_neg_integer(),
ExternalPort :: non_neg_integer(),
Lifetime :: non_neg_integer(),
Result :: {ok, Since, InternalPort, ExternalPort, Lifetime} | {error, term()}.
Add a port mapping with a specific lifetime.
Parameters:
Protocol-tcporudpInternalPort- Local port number to mapExternalPort- Requested external port (0 for any available port)Lifetime- Requested lifetime in seconds
Example:
%% Request 2-hour lifetime
{ok, _, _, _, ActualLifetime} = nat:add_port_mapping(tcp, 8080, 8080, 7200).
Lifetime may differ
The actual lifetime granted may be less than requested, depending on the NAT device's configuration. Always check the returned lifetime.
delete_port_mapping/3¶
Delete a port mapping and cancel its auto-renewal timer.
Parameters:
Protocol-tcporudpInternalPort- Internal port of the mappingExternalPort- External port of the mapping
Example:
list_mappings/0¶
List all port mappings managed by nat_server.
Returns:
A list of tuples containing:
Protocol-tcporudpInternalPort- Internal port numberExternalPort- External port numberExpiresAt- Unix timestamp when the mapping expires
Example:
Mappings = nat:list_mappings().
%% => [{tcp, 8080, 8080, 1706500000}, {udp, 9000, 9000, 1706500000}]
Address Functions¶
get_external_address/0¶
Get the external (public) IP address as reported by the NAT device.
Example:
get_device_address/0¶
Get the NAT gateway's IP address.
Example:
get_internal_address/0¶
Get the local device's IP address (the address used to communicate with the NAT gateway).
Example:
Event Registration Functions¶
reg_pid/1¶
Register a process to receive {nat_event, Event} messages.
The registered process is monitored; if it exits, it is automatically unregistered.
Example:
unreg_pid/1¶
Unregister a process from receiving events.
Example:
reg_fun/1¶
Register a callback function for NAT events.
The function is called asynchronously (spawned) for each event.
Returns:
{ok, Ref}- Reference to use for unregistration
Example:
unreg_fun/1¶
Unregister a callback function by its reference.
Example:
Error Handling¶
Common error reasons:
| Error | Description |
|---|---|
no_context |
No NAT context stored (call discover/0 first) |
timeout |
Operation timed out |
not_authorized |
NAT device rejected the request |
no_resources |
NAT device has no available ports |
{http_error, Code, Body} |
UPnP SOAP error |
Example error handling:
case nat:add_port_mapping(tcp, 80, 80) of
{ok, _, _, ExtPort, _} ->
{ok, ExtPort};
{error, no_context} ->
%% Need to discover first
nat:discover(),
nat:add_port_mapping(tcp, 80, 80);
{error, not_authorized} ->
%% Port 80 may be restricted
nat:add_port_mapping(tcp, 80, 8080);
{error, Reason} ->
{error, Reason}
end.