Hi,

I've been working on IPv6 support, and one of the strangest things
I find is BIO_get_accept_socket().

If bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED, and bind() fails
with EADDRINUSE it creates a new socket and tries to connect
to the port it tried to bind() to, and if that fails tries to
bind to it again but now with setting SO_REUSEADDR.

Does anybody have an idea why it's trying to do that, and why we
shouldn't just do SO_REUSEADDR the first time?  Was there some
OS that maybe did strange things when trying to use SO_REUSEADDR
and it was already in use?

The documentation also says:
| BIO_set_bind_mode() and BIO_get_bind_mode() set and retrieve
| the current bind mode. If BIO_BIND_NORMAL (the default) is set
| then another socket cannot be bound to the same port. If
| BIO_BIND_REUSEADDR is set then other sockets can bind to the
| same port. If BIO_BIND_REUSEADDR_IF_UNUSED is set then and
| attempt is first made to use BIO_BIN_NORMAL, if this fails
| and the port is not in use then a second attempt is made
| using BIO_BIND_REUSEADDR.

The documentation is at least confusing, since it's about the
address and port combination and not just the port.  The main
reason for using SO_REUSEADDR is that you can bind to an
address / port combination that is in TIME_WAIT state, and it's
recommended for server applications to always set this, and it's
what I ended up doing in my new function.  There are also some
other cases you might want to use SO_REUSEADDR, and I suggest you
read Stevens for that.

In no case should it be possible to bind to the same TCP address /
port from different applications, SO_REUSEADDR does not allow you
to do that.

But then I found some MSDN documentation that says that Windows
allows others to hijack your socket when you've set SO_REUSEADDR
and the results are non-deterministic.  They also created an
SO_EXCLUSIVEADDRUSE and I'm getting confused what it really does,
but they say that server applications should set it.

Anyway, is that whole logic with the connect() needed, or can I
just always use SO_REUSEADDR (on non-windows hosts)?


Kurt

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       openssl-dev@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to