Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Windows: '--local-port range' is unable to detect ports in TIME_WAIT, and so does not reliably try further ports in the range #8112

@gclinch

Description

@gclinch

I did this

On a Windows client:

The first run succeeds:

> curl -v --local-port 600-605 http://github.com/
*   Trying 140.82.121.4:80...
* Local port: 600
* Connected to github.com (140.82.121.4) port 80 (#0)
[...]

The same command a second time fails:

> curl -v --local-port 600-605 http://github.com/
*   Trying 140.82.121.4:80...
* Local port: 600
* connect to 140.82.121.4 port 80 failed: Address already in use
* Failed to connect to github.com port 80 after 13 ms: Address already in use
* Closing connection 0
curl: (7) Failed to connect to github.com port 80 after 13 ms: Address already in use
[...]

I expected the following

On a Linux client, both runs succeed:

First, port 600 is used:

# curl -v --local-port 600-605 http://github.com/
*   Trying 140.82.121.4...
* TCP_NODELAY set
* Local port: 600
* Connected to github.com (140.82.121.4) port 80 (#0)
[...]

And on the second execution, port 601 is used:

# curl -v --local-port 600-605 http://github.com/
*   Trying 140.82.121.4...
* TCP_NODELAY set
* Bind to local port 600 failed, trying next
* Local port: 601
* Connected to github.com (140.82.121.4) port 80 (#0)
[...]

It appears that given an already in-use port number (as seen through netstat / ss), Windows bind will succeed (then failing later on the connect call), whilst Linux bind will not succeed. Curl's localport "try next" logic considers only whether the bind call succeeds.

curl/libcurl version

curl 7.80.0 (x86_64-pc-win32) libcurl/7.80.0 OpenSSL/3.0.0 (Schannel) zlib/1.2.11 brotli/1.0.9 zstd/1.5.0 libidn2/2.3.2 libssh2/1.10.0 nghttp2/1.46.0 libgsasl/1.10.0
Release-Date: 2021-11-10
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: alt-svc AsynchDNS brotli gsasl HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz MultiSSL NTLM SPNEGO SSL SSPI TLS-SRP UnixSockets zstd

operating system

Windows 10 Version 20H2 (OS Build 19042.1348)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions