Search code examples
nanomsg

Clarification on the ephemeral port range in NNG


At https://nng.nanomsg.org/man/tip/nng_sockaddr_zt.5.html, the explanation of the sa_port argument reads:

This field holds the port number used by the zt transport to distinguish different sockets. This value in native byte order. A zero value here indicates that a port number should be chosen randomly from the ephemeral ports. Only the lower 24 bits of the port number are used.

What exactly are the specific lower and upper bounds for the port range, both here and all other places NNG/nanomsg chooses an ephemeral port? I have heard ephemeral ports defined different ways depending on who I ask. Apparently RFC 6056 says 1024 and above, other times it's about 36000 and above, and this Wikipedia page says 49152 and above.


Solution

  • Author of NNG here:

    So for TCP the port numbers used for ephemeral ports are chosen by the operating system, and the range will very by OS, and sometimes by kernel tuning.

    For sockaddr_zt, these are separate, and we use a 24-bit range, divided in half:

    static const uint32_t     zt_ephemeral = 0x800000u; // start of ephemeral ports
    static const uint32_t     zt_max_port  = 0xffffffu; // largest port
    static const uint32_t     zt_port_mask = 0xffffffu; // mask of valid ports
    

    So there are lot more port numbers available for this protocol than for TCP or UDP. (About 8 million.) You should not run out of port numbers unless you do something foolish like using this as a bit field instead of just a sequence of 8 million numbers. :-)