6

Our software (Nmap port scanner) needs to quickly determine the status of a non-blocking TCP socket connect(). We use select() to monitor a lot of sockets, and Windows is good at notifying us when one succeeds. But if the port is closed and the target sends a TCP RST, Windows will keep trying a few times before notifying the exceptfds, and the socket error is WSAECONNREFUSED as expected. Our application has its own timeout, though, and will usually mark the connection as timed-out before Windows gives up. We want to get as close as possible to the behavior of Linux, which is to notify with ECONNREFUSED immediately upon receipt of the first RST.

We have tried using the TCP_MAXRT socket option, and this works to get select() to signal us right away, but the result (for closed ports) is always WSAETIMEDOUT, which makes it impossible to distinguish closed (RST) from filtered/firewalled (network timeout), which puts us back at the original problem. Determining this distinction is a core feature of our application.

So what is the best way on Windows to find out if a non-blocking socket connect() has received a connection reset?

EDITED TO ADD: A core problem here is this line from Microsoft's documentation on the SO_ERROR socket option: "This per-socket error code is not always immediately set." If it were immediately set, we could check for it prior to the connect timeout.

bonsaiviking
  • 5,825
  • 1
  • 20
  • 35
  • 1
    You may be out of luck here. I believe the underlying problem to be this. BSD-derived TCPs drop incoming SYNs if the backlog queue is full, which is indistinguishable at the client from host or network down, so `connect()` retries, which is plausible: if `connect()` gets an RST it means no listening socket, so it returns immediately. Windows on the other hand issues an RST if the backlog queue is full, so the client can't distinguish that condition from no listening socket, so it retries on both, which is pontless on no listening socket. I don't know whether you can fix this. – user207421 Sep 09 '20 at 00:28
  • @MarquisofLorne That's really helpful information, though, thanks! – bonsaiviking Sep 09 '20 at 16:30

0 Answers0