Skip to content

Use getsockname() to get port number assigned by the kernel #3005

@dxdxdt

Description

@dxdxdt

Describe the bug
ncat accepts zero for listen port number. This is a reasonable behaviour. According to ip(4):

  IP_PORTRANGE may	be used	to set the port	range used for selecting a lo-
  cal port	number on a socket with	an unspecified (zero) port number.  It
  has the following possible values:

When sin_port is zero, the kernel will pick a port from the ephemeral range. This behaviour is observed on Windows, BSD and Linux. The problem is that ncat does not use getsockname() to get the actual port assigned. Instead, the program reports the local port number being zero.

To Reproduce

$ nc -lv 0 & ss -tlnp                                                                                                           
[1] 214046      
Ncat: Version 7.92 ( https://nmap.org/ncat )
Ncat: Listening on :::0
Ncat: Listening on 0.0.0.0:0
State        Recv-Q       Send-Q             Local Address:Port               Peer Address:Port       Process                               
LISTEN       0            10                       0.0.0.0:42767                   0.0.0.0:*           users:(("nc",pid=214046,fd=4))                                            
LISTEN       0            10                          [::]:44911                      [::]:*           users:(("nc",pid=214046,fd=3))                                            

Expected behavior
ncat should report the actual local port.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions