macula_port_predictor (macula v0.20.5)
View SourcePort Prediction for NAT Traversal.
Provides intelligent port prediction based on NAT allocation policies and historical port allocation data. Improves hole punch success rates by predicting the external ports a peer will use.
Prediction Strategies by Allocation Policy:
- PP (Port Preservation): External port = internal port Prediction: Use last known port (high confidence)
- PC (Port Contiguity): Sequential port allocation Prediction: Calculate delta from history, predict next ports Delta tracking with exponential moving average
- RD (Random): Random port allocation Prediction: Use historical range + common NAT port ranges Statistical approach with lower confidence
Port History Storage: - Tracks last N port allocations per peer (default: 10) - Calculates port deltas for PC allocation - Maintains statistics for RD allocation (mean, stddev, range)
Summary
Functions
Clear port history for a peer.
Get port history for a peer.
Get port statistics for a peer.
Predict external ports for a peer. Uses NAT profile if available, otherwise uses historical data.
Predict external ports with options. Options: base_port - Known/expected base port for prediction count - Number of ports to predict (default: 5)
Record an observed external port for a peer. Used to build history for better predictions.
Start the port predictor server.
Types
-type allocation_policy() :: pp | pc | rd | unknown.
-type port_history() :: #{node_id := binary(), ports := [inet:port_number()], deltas := [integer()], updated_at := integer()}.
-type port_prediction() :: #{ports := [inet:port_number()], confidence := float(), strategy := pp | pc | rd | fallback, delta => integer(), stats => port_stats()}.
-type port_stats() :: #{mean := float(), stddev := float(), min := inet:port_number(), max := inet:port_number(), count := non_neg_integer()}.
Functions
-spec clear(binary()) -> ok.
Clear port history for a peer.
-spec get_history(binary()) -> {ok, port_history()} | not_found.
Get port history for a peer.
-spec get_stats(binary()) -> {ok, port_stats()} | not_found.
Get port statistics for a peer.
-spec predict(binary(), allocation_policy()) -> port_prediction().
Predict external ports for a peer. Uses NAT profile if available, otherwise uses historical data.
-spec predict(binary(), allocation_policy(), map()) -> port_prediction().
Predict external ports with options. Options: base_port - Known/expected base port for prediction count - Number of ports to predict (default: 5)
-spec record_port(binary(), inet:port_number(), allocation_policy()) -> ok.
Record an observed external port for a peer. Used to build history for better predictions.
Start the port predictor server.