Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
net.ListenTCP succeeds twice on Windows #2307
Comments
gopherbot
commented
Sep 27, 2011
All our windows sockets are marked as SO_REUSEADDR. This seems to allow to call listen http://goo.gl/a8hJu multiple times. If we want opposite behavior, we should not set SO_REUSEADDR on correspondent socket. Should we stop setting SO_REUSEADDR on all sockets? Or should we do it selectively on listening sockets only? I am surprised it works on linux, listen with SO_REUSEADDR set ... Alex Labels changed: added packagebug. Owner changed to @alexbrainman. |
SO_REUSEADDR doesn't permit multiple sockets to listen on the same IP address and port number. By default, after you close a socket, it enters a TIME_WAIT state when no socket can use the same pair of address & port for two minutes. This is designed to permit any trailing packets on the connection to get dropped. SO_REUSEADDR disables that wait, permitting the connection to be reestablished immediately. It is normal to use SO_REUSEADDR on a server. So this has nothing to do with SO_REUSEADDR. It does sound Windows-specific. I can't imagine what it would mean to open multiple sockets listening on the same IP address and port. Perhaps Windows defines that in some way. |
It appears that Microsoft decided to make SO_REUSEADDR mean something different. It looks like the fix is to not use SO_REUSEADDR, but only on Windows. On other systems it works just fine and does what it is supposed to do. http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621(v=vs.85).aspx "The SO_REUSEADDR socket option allows a socket to forcibly bind to a port in use by another socket. The second socket calls setsockopt with the optname parameter set to SO_REUSEADDR and the optval parameter set to a boolean value of TRUE before calling bind on the same port as the original socket. Once the second socket has successfully bound, the behavior for all sockets bound to that port is indeterminate. For example, if all of the sockets on the same port provide TCP service, any incoming TCP connection requests over the port cannot be guaranteed to be handled by the correct socket — the behavior is non-deterministic. A malicious program can use SO_REUSEADDR to forcibly bind sockets already in use for standard network protocol services in order to deny access to those service. No special privileges are required to use this option." WTF. |
|
This issue was closed by revision c1d0f0e. Status changed to Fixed. |
gopherbot
added
fixed
labels
Oct 23, 2011
gopherbot
locked and limited conversation to collaborators
Jun 24, 2016
gopherbot
added
the
FrozenDueToAge
label
Jun 24, 2016
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
gopherbot commentedSep 26, 2011
by yeswah:
Attachments: