mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-25 01:02:24 +00:00
winnet: use SO_EXCLUSIVEADDRUSE for listening sockets.
Thanks to Patrick Stekovic for pointing out that, unlike sensible IP stacks, Windows requires a non-default socket option to prevent a second application from binding to a port you were already listening on, causing some of your incoming connections to be diverted. This replaces the previous setsockopt that enabled SO_REUSEADDR, which I put there a long time ago in order to fix an annoying behaviour if you used the same listening socket twice in rapid succession (e.g. for successive PuTTYs forwarding the same port) and the second one failed to bind the listening port because a left-over connection from the first one was still in TIME_WAIT and causing the port number to be marked as used. As far as I can see, SO_EXCLUSIVEADDRUSE and SO_REUSEADDR are mutually exclusive - if I try to set both, either way round, then setsockopt returns failure on the second one - so if I have to set the former then I _can't_ set the latter. And fortunately, re-testing on Windows 10, the TIME_WAIT problem that SO_REUSEADDR was supposed to solve doesn't seem to exist any more: I deliberately tried listening on a port that had a TIME_WAIT connection sitting on it, and it worked for me even without SO_REUSEADDR. (I can't remember now whether I definitely confirmed the TIME_WAIT problem on a previous version of Windows, or whether I just assumed it would happen on Windows in the same way as Linux, where I definitely do remember observing it.) While I'm changing that setsockopt call, I've also fixed its 'on' parameter so that it's a BOOL rather than an int, in accordance with the docs for WinSock setsockopt.
This commit is contained in:
parent
2c279283cc
commit
15653f67e8
@ -1140,7 +1140,6 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
||||
char *errstr;
|
||||
NetSocket *ret;
|
||||
int retcode;
|
||||
int on = 1;
|
||||
|
||||
int address_family;
|
||||
|
||||
@ -1197,7 +1196,11 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
||||
|
||||
ret->oobinline = false;
|
||||
|
||||
p_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
|
||||
{
|
||||
BOOL on = true;
|
||||
p_setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
|
||||
(const char *)&on, sizeof(on));
|
||||
}
|
||||
|
||||
#ifndef NO_IPV6
|
||||
if (address_family == AF_INET6) {
|
||||
|
Loading…
Reference in New Issue
Block a user