Eduardo
Eduardo

Reputation: 2341

Why not using SO_REUSEADDR on Unix TCP/IP servers?

I have not seen any important TCP/IP server not use SO_REUSEADDR :

Is there any use case for not using SO_REUSEADDR on TCP/IP servers ?

I mean, would making the OS always use SO_REUSEADDR break any server that does not use it?

Do you know a TCP/IP server that not uses SO_REUSEADDR for a reason?

(of course you may not want to use it on MSWindows as it allows to run two servers on the same port)

Upvotes: 8

Views: 3664

Answers (3)

user202729
user202729

Reputation: 4013

I think the most likely reason is the same reason why TIME_WAIT state exists.

Refer to https://stackoverflow.com/a/3233022/5267751 .

The risk in setting SO_REUSEADDR is that it creates an ambiguity: the metadata in a TCP packet's headers isn't sufficiently unique that the stack can reliably tell whether the packet is stale and so should be dropped rather than be delivered to the new listener's socket because it was clearly intended for a now-dead listener.

Also https://stackoverflow.com/a/337137/5267751 :

The reason why there is a TIME_WAIT state following session shutdown is because there may still be live packets out in the network on their way to you (or from you which may solicit a response of some sort). If you were to re-create that same tuple and one of those packets showed up, it would be treated as a valid packet for your connection (and probably cause an error due to sequencing).

So the TIME_WAIT time is generally set to double the packets maximum age. This value is the maximum age your packets will be allowed to get to before the network discards them.

Upvotes: 1

Walt Howard
Walt Howard

Reputation: 8218

Of course there is a very valid reason for not using SO_REUSEADDR by default.

It would allow ANY process to bind to the same listening socket as a sensitive Internet service and accept connections on its behalf! That permits unlimited eavesdropping and man-in-the-middling.

Upvotes: -1

cnicutar
cnicutar

Reputation: 182794

Well, UNP (Stevens 2004) says:

SO_REUSEADDR allows a listening server to start and bind its well-known port, even if previously established connections exist that use this port as their local port.

All TCP servers should specify this socket option to allow the server to be restarted

Upvotes: 6

Related Questions