public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Grant Edwards <grant.b.edwards@gmail.com>
To: ecos-discuss@ecos.sourceware.org
Subject: [ECOS] BSD TCP connect() ignores SO_REUSEADDR option
Date: Tue, 30 Apr 2013 19:11:00 -0000	[thread overview]
Message-ID: <klp4vo$r2b$1@ger.gmane.org> (raw)

I've recently noticed that when using the FreeBSD TCP stack, the
connect() system call ignores the SO_REUSEADDRESS setting and returns
EADDRINUSE when attempting to connect a TCP socket if a connection
with the same (SrcIP,SrcPort,DstIP,DstPort) tuple is still hanging
around in the TIME_WAIT state.

Here's an example sequence of events:

   * create a TCP socket

   * set SO_REUSEADDRESS and SO_REUSEPORT options
   
   * bind to port 12346

   * connect to 1.2.3.4:54321

   * send a little data   
   
   * close connection

   * wait a few seconds

   * create a TCP socket

   * set SO_REUSEADDRESS and SO_REUSEPORT options
   
   * bind() to port 12346

   * connect() to 1.2.3.4:54321

The bind() will succeed due to the SO_REUSEADDR, SO_REUSEPORT option.

The connect() will fail with EADDRINUSE.

If you wait a minute or two between connections it works fine.


There appear to be a certain set of conditions when an existing
TIME_WAIT connection will be discarded and reused -- see line 747
below:

From  tcp_usrreq.c:

733        /*
734         * Cannot simply call in_pcbconnect, because there might be an
735         * earlier incarnation of this same connection still in
736         * TIME_WAIT state, creating an ADDRINUSE error.
737         */
738        error = in_pcbladdr(inp, nam, &ifaddr);
739        if (error)
740                return error;
741        oinp = in_pcblookup_hash(inp->inp_pcbinfo,
742            sin->sin_addr, sin->sin_port,
743            inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
744                                                : ifaddr->sin_addr,
745            inp->inp_lport,  0, NULL);
746        if (oinp) {
747                if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
748                otp->t_state == TCPS_TIME_WAIT &&
749                    (ticks - otp->t_starttime) < tcp_msl &&
750                    (otp->t_flags & TF_RCVD_CC))
751                        otp = tcp_close(otp);
752                else
753                        return EADDRINUSE;
754        }

But setting SO_REUSEADDR doesn't affect the behavior of connect().

Should it?

-- 
Grant Edwards               grant.b.edwards        Yow! VICARIOUSLY experience
                                  at               some reason to LIVE!!
                              gmail.com            


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

             reply	other threads:[~2013-04-30 19:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-30 19:11 Grant Edwards [this message]
2013-04-30 19:40 ` [ECOS] " Grant Edwards

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='klp4vo$r2b$1@ger.gmane.org' \
    --to=grant.b.edwards@gmail.com \
    --cc=ecos-discuss@ecos.sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).