netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* SO_REUSEADDR behavior different from BSD
@ 2004-08-04 14:25 Michael T Kerrisk
  2004-08-05 14:14 ` bert hubert
  0 siblings, 1 reply; 8+ messages in thread
From: Michael T Kerrisk @ 2004-08-04 14:25 UTC (permalink / raw)
  To: netdev

Gidday,

In the classical BSD sockets implementation, the 
SO_REUSEADDR socket option serves two purposes:

(a) to permit a socket to be bound to a port that currently 
    has a bound endpoint in the TIME_WAIT state, and

(b) to allow a terminated server to be restarted (and once
    again be bound to it's well-known port), even if there 
    is a child of the previous server still serving a 
    connection.

On Linux SO_REUSEADDR does serve this purpose.  However, 
other second scenario is mot permitted by SO_REUSEADDR.  
To make it clear what I mean, I’ll detail the scenario.  
Suppose we have the following:

                      Server (host Y)
                    
                      Create listening socket -- fd 4
                      bind() to INADDR_ANY:9999
                      listen()
                      accept()

Client (host X)

Create socket
connect() to Y:9999   <accept unblocks, fd 5 returned>

                      server forks --------+
                                            \ <child>
                      server terminates      closes fd 4
                                             <keeps running, with
                                             fd 5 holding the
                                             connection to client>

At this point, netstat on host Y shows the tuple 

    [local=Y-IP-addr:9999, remote=X-IP-addr:ephem-port] ESTABLISHED

                      New Server started
                      Create listening socket -- fd 4
                      set SO_REUSEADDR to 1 for fd 4

                      bind() fd 4 to port INADDR_ANY:9999
                      WHAT HAPPENS?

On FreeBSD, the bind() succeeds, and then using "netstat –an" 
on host Y shows:

    [local=Y-IP-addr:9999, remote=X-IP-addr:ephem-port] ESTABLISHED
and
    [local=*:9999, remote=*.*] CLOSED

But on Linux (2.6), the bind() fails with EADDRINUSE.

Why does Linux behave differently in this scenario?

Cheers,

Michael

-- 
Michael Kerrisk
mtk-lists@gmx.net

NEU: WLAN-Router für 0,- EUR* - auch für DSL-Wechsler!
GMX DSL = supergünstig & kabellos http://www.gmx.net/de/go/dsl

^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: SO_REUSEADDR behavior different from BSD
@ 2004-08-05 22:25 Michael T Kerrisk
  0 siblings, 0 replies; 8+ messages in thread
From: Michael T Kerrisk @ 2004-08-05 22:25 UTC (permalink / raw)
  To: YOSHIFUJI.Hideaki/; +Cc: ahu, netdev, David Stevens

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="us-ascii", Size: 911 bytes --]

> In article <19686.1091723777@www48.gmx.net> (at Thu, 5 Aug 2004 18:36:17
> +0200 (MEST)), "Michael T Kerrisk" <mtk-lists@gmx.net> says:
> 
> > Now, on Linux, at this point, the second instance of the 
> > server fails with EADDRINUSE, even though it did use 
> > SO_REUSEADDR.  On FreeBSD 5.1, the second server instance 
> > does successfully bind.
> 
> This behavior is intended.
> First socket is REQUIRED to set SO_REUSEADDR
> I hate BSD's behavior because it is asynmetry.
> (Both sockets are required to agree on "REUSEADDR.")

Hideaki, David,

Thank you for the information / confirmation that this is intended
behavior.  I'll try to see that something on this point makes it 
into the man pages.

Cheers,

Michael

-- 
Michael Kerrisk
mtk-lists@gmx.net

NEU: WLAN-Router für 0,- EUR* - auch für DSL-Wechsler!
GMX DSL = supergünstig & kabellos http://www.gmx.net/de/go/dsl

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2004-08-09 13:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-08-04 14:25 SO_REUSEADDR behavior different from BSD Michael T Kerrisk
2004-08-05 14:14 ` bert hubert
2004-08-05 16:36   ` Michael T Kerrisk
2004-08-05 18:20     ` David Stevens
2004-08-05 18:34     ` YOSHIFUJI Hideaki / 吉藤英明
2004-08-08  2:25       ` Fernando Gont
2004-08-09 13:29         ` Michael T Kerrisk
  -- strict thread matches above, loose matches on Subject: below --
2004-08-05 22:25 Michael T Kerrisk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).