Skip to content

feat(portmapper): allow suggesting an external port - #251

Open
HeikoBornholdt wants to merge 1 commit into
n0-computer:mainfrom
HeikoBornholdt:portmapper-suggest-external-port
Open

HeikoBornholdt wants to merge 1 commit into
n0-computer:mainfrom
HeikoBornholdt:portmapper-suggest-external-port

Conversation

@HeikoBornholdt

Copy link
Copy Markdown

Description

Closes #232.

Adds Config::suggested_external_port so clients can request a specific external port, e.g. to stay reachable at a known endpoint across restarts. The internals already supported this; it just wasn't exposed.

  • Used only when creating a new mapping; an existing mapping's address keeps precedence on renewal and local port changes.
  • Gateways may refuse the suggestion. UPnP falls back to any free port, NAT-PMP answers with an assigned one, but PCP answers CANNOT_PROVIDE_EXTERNAL, so the PCP client retries once without a suggestion. The same retry covers renewal when the gateway can no longer provide the held address.

API Changes

New public field portmapper::Config::suggested_external_port: Option<NonZeroU16>, defaults to None.

Since Config is not non_exhaustive, struct-literal constructions must add the field (suggested_external_port: None). Code using ..Default::default() is unaffected.

Notes & open questions

Tested against a FRITZ!Box gateway: suggested port granted when free; when another mapping holds it, PCP refuses and the retry obtains a gateway-assigned port instead of failing.

Change checklist

  • Self-review.
  • Documentation updates following the style guide, if relevant.
  • Tests if relevant.
  • All API changes documented.
  • This PR was created by a human that thought critically about the
    proposed change and wrote an as clear and concise description as
    they could.
  • This PR isn't slop, and is carefully crafted to do have the
    intented effect.
  • cargo make passes locally.

PCP, NAT-PMP and UPnP all allow clients to suggest the external port
they want. Expose this via Config::suggested_external_port so clients
can request a static external port and stay connectable through known
endpoints across program restarts.

The suggestion is only used when creating a new mapping; an established
mapping's external address keeps taking precedence on renewal and local
port changes. Gateways may ignore the suggestion and assign a different
port: UPnP and NAT-PMP already handle that, while PCP answers
CANNOT_PROVIDE_EXTERNAL, so the PCP client retries the request once
without a suggestion. The retry also covers renewal of a held mapping
the gateway can no longer provide.

Verified against a real PCP gateway (FRITZ!Box): the suggested port is
granted when free, and when another mapping holds it the retry obtains
a gateway-assigned port instead of failing.

Closes n0-computer#232

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🚑 Needs Triage

Development

Successfully merging this pull request may close these issues.

portmapper: allow suggesting external port

1 participant