Subject: Re: setsockopt SO_REUSEADDR
From: Brian Dessent <[email-masked]>
To: Cygwin-L <[email-masked]>
Date: Tue, 05 Oct 2004 08:33:00 -0700

Warren Young wrote:

> As I see it, the purpose of SO_REUSEADDR is to allow a program to bind
> to a port that still has WAIT states on it.  (FIN_WAIT_x, TIME_WAIT,

That, and the part about the uniqueness of local IP+port.  Think
multihomed boxes that want to do ip-based virtual hosting.

It seems that this flag has come to mean many things to many people.

Here is what Stevens has to say on the matter from UNPv1:

<quote type="fair use">
The SO_REUSEADDR socket option serves four different purposes:

1. 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. This condition is typically 
   encountered as follows:

   a. A listening server is started.

   b. A connection request arrives and a child process is spawned to 
      handle that client.

   c. The listening server terminates, but the child continues to 
      service the client on the existing connection.

   d. The listening server is restarted.

   By default, when the listening server is restarted in (d) by calling 
   socket, bind, and listen, the call to bind fails because the 
   listening server is trying to bind a port that is part of an existing 
   connection (the one being handled by the previously spawned child). 
   But if the server sets the SO_REUSEADDR socket option between the 
   calls to socket and bind, the latter function will succeed. All TCP 
   servers should specify this socket option to allow the server to be 
   restarted in this situation.

   This scenario is one of the most frequently asked questions on
USENET.

2. SO_REUSEADDR allows a new server to be started on the same port as an 
   existing server that is bound to the wildcard address, as long as 
   each instance binds a different local IP address. This is common for 
   a site hosting multiple HTTP servers using the IP alias technique 
   (Section A.4). [...]

   With TCP, we are never able to start multiple servers that bind the 
   same IP address and the same port [...]

   [...]

3. SO_REUSEADDR allows a single process to bind the same port to 
   multiple sockets, as long as each bind specifies a different local IP 
   address. This is common for UDP servers that need to know the 
   destination IP address of client requests on systems that do not 
   provide the IP_RECVDSTADDR socket option. This technique is normally 
   not used [...]

4. SO_REUSEADDR allows completely duplicate bindings: a bind of an IP 
   address and port, when that same IP address and port are already 
   bound to another socket, if the transport protocol supports it. 
   Normally this feature is supported only for UDP sockets.

   This feature is used with multicasting to allow the same application 
   to be run multiple times on the same host. [...]
</quote>

Brian

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/