* fhandler.h (class fhandler_socket): Add saw_reuseaddr status flag.

* fhandler_socket.cc (fhandler_socket::bind): Set socket to
	SO_EXCLUSIVEADDRUSE if application didn't explicitely set SO_REUSEADDR
	socket option, on systems supporting SO_EXCLUSIVEADDRUSE.
	* net.cc (cygwin_setsockopt): Set fhandler's saw_reuseaddr status flag
	if SO_REUSEADDR socket option has been successsfully set.
	* wincap.h (wincaps::has_exclusiveaddruse): New element.
	* wincap.cc: Implement above element throughout.
This commit is contained in:
Corinna Vinschen 2006-01-29 12:23:44 +00:00
parent 74d3f96faa
commit 5369605f4f
6 changed files with 69 additions and 16 deletions

View File

@ -1,3 +1,14 @@
2006-01-29 Corinna Vinschen <corinna@vinschen.de>
* fhandler.h (class fhandler_socket): Add saw_reuseaddr status flag.
* fhandler_socket.cc (fhandler_socket::bind): Set socket to
SO_EXCLUSIVEADDRUSE if application didn't explicitely set SO_REUSEADDR
socket option, on systems supporting SO_EXCLUSIVEADDRUSE.
* net.cc (cygwin_setsockopt): Set fhandler's saw_reuseaddr status flag
if SO_REUSEADDR socket option has been successsfully set.
* wincap.h (wincaps::has_exclusiveaddruse): New element.
* wincap.cc: Implement above element throughout.
2006-01-28 Corinna Vinschen <corinna@vinschen.de>
* fhandler_disk_file.cc (fhandler_disk_file::mkdir): In case or error,

View File

@ -417,6 +417,7 @@ class fhandler_socket: public fhandler_base
unsigned async_io : 1; /* async I/O */
unsigned saw_shutdown_read : 1; /* Socket saw a SHUT_RD */
unsigned saw_shutdown_write : 1; /* Socket saw a SHUT_WR */
unsigned saw_reuseaddr : 1; /* Socket saw SO_REUSEADDR call */
unsigned closed : 1;
unsigned owner : 1;
unsigned connect_state : 2;
@ -440,6 +441,7 @@ class fhandler_socket: public fhandler_base
IMPLEMENT_STATUS_FLAG (bool, async_io)
IMPLEMENT_STATUS_FLAG (bool, saw_shutdown_read)
IMPLEMENT_STATUS_FLAG (bool, saw_shutdown_write)
IMPLEMENT_STATUS_FLAG (bool, saw_reuseaddr)
IMPLEMENT_STATUS_FLAG (bool, closed)
IMPLEMENT_STATUS_FLAG (bool, owner)
IMPLEMENT_STATUS_FLAG (conn_state, connect_state)

View File

@ -661,10 +661,33 @@ fhandler_socket::bind (const struct sockaddr *name, int namelen)
}
#undef un_addr
}
else if (::bind (get_socket (), name, namelen))
set_winsock_errno ();
else
res = 0;
{
/* If the application didn't explicitely call setsockopt (SO_REUSEADDR),
enforce exclusive local address use using the SO_EXCLUSIVEADDRUSE
socket option, to emulate POSIX socket behaviour more closely.
KB 870562: Note that this option is only available since NT4 SP4.
Also note that a bug in Win2K SP1-3 and XP up to SP1 only enables
this option for users in the local administrators group. */
if (wincap.has_exclusiveaddruse ())
{
if (!saw_reuseaddr ())
{
int on = 1;
int ret = ::setsockopt (get_socket (), SOL_SOCKET,
~(SO_REUSEADDR),
(const char *) &on, sizeof on);
debug_printf ("%d = setsockopt (SO_EXCLUSIVEADDRUSE), %E", ret);
}
else
debug_printf ("SO_REUSEADDR set");
}
if (::bind (get_socket (), name, namelen))
set_winsock_errno ();
else
res = 0;
}
out:
return res;

View File

@ -722,6 +722,8 @@ cygwin_setsockopt (int fd, int level, int optname, const void *optval,
else
set_winsock_errno ();
}
else if (level == SOL_SOCKET && optname == SO_REUSEADDR)
fh->saw_reuseaddr (*(int *) optval);
}
syscall_printf ("%d = setsockopt (%d, %d, %x, %p, %d)",

View File

@ -64,7 +64,8 @@ static NO_COPY wincaps wincap_unknown = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:false,
has_disabled_user_tos_setting:false,
has_fileid_dirinfo:false
has_fileid_dirinfo:false,
has_exclusiveaddruse:false
};
static NO_COPY wincaps wincap_95 = {
@ -120,7 +121,8 @@ static NO_COPY wincaps wincap_95 = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:false,
has_disabled_user_tos_setting:false,
has_fileid_dirinfo:false
has_fileid_dirinfo:false,
has_exclusiveaddruse:false
};
static NO_COPY wincaps wincap_95osr2 = {
@ -176,7 +178,8 @@ static NO_COPY wincaps wincap_95osr2 = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:false,
has_disabled_user_tos_setting:false,
has_fileid_dirinfo:false
has_fileid_dirinfo:false,
has_exclusiveaddruse:false
};
static NO_COPY wincaps wincap_98 = {
@ -232,7 +235,8 @@ static NO_COPY wincaps wincap_98 = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:false,
has_disabled_user_tos_setting:false,
has_fileid_dirinfo:false
has_fileid_dirinfo:false,
has_exclusiveaddruse:false
};
static NO_COPY wincaps wincap_98se = {
@ -288,7 +292,8 @@ static NO_COPY wincaps wincap_98se = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:false,
has_disabled_user_tos_setting:false,
has_fileid_dirinfo:false
has_fileid_dirinfo:false,
has_exclusiveaddruse:false
};
static NO_COPY wincaps wincap_me = {
@ -344,7 +349,8 @@ static NO_COPY wincaps wincap_me = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:false,
has_disabled_user_tos_setting:false,
has_fileid_dirinfo:false
has_fileid_dirinfo:false,
has_exclusiveaddruse:false
};
static NO_COPY wincaps wincap_nt3 = {
@ -400,7 +406,8 @@ static NO_COPY wincaps wincap_nt3 = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:true,
has_disabled_user_tos_setting:false,
has_fileid_dirinfo:false
has_fileid_dirinfo:false,
has_exclusiveaddruse:false
};
static NO_COPY wincaps wincap_nt4 = {
@ -456,7 +463,8 @@ static NO_COPY wincaps wincap_nt4 = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:true,
has_disabled_user_tos_setting:false,
has_fileid_dirinfo:false
has_fileid_dirinfo:false,
has_exclusiveaddruse:false
};
static NO_COPY wincaps wincap_nt4sp4 = {
@ -512,7 +520,8 @@ static NO_COPY wincaps wincap_nt4sp4 = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:true,
has_disabled_user_tos_setting:false,
has_fileid_dirinfo:false
has_fileid_dirinfo:false,
has_exclusiveaddruse:true
};
static NO_COPY wincaps wincap_2000 = {
@ -568,7 +577,8 @@ static NO_COPY wincaps wincap_2000 = {
has_disk_ex_ioctls:false,
has_working_virtual_lock:true,
has_disabled_user_tos_setting:true,
has_fileid_dirinfo:true
has_fileid_dirinfo:true,
has_exclusiveaddruse:true
};
static NO_COPY wincaps wincap_xp = {
@ -624,7 +634,8 @@ static NO_COPY wincaps wincap_xp = {
has_disk_ex_ioctls:true,
has_working_virtual_lock:true,
has_disabled_user_tos_setting:true,
has_fileid_dirinfo:true
has_fileid_dirinfo:true,
has_exclusiveaddruse:true
};
static NO_COPY wincaps wincap_2003 = {
@ -680,7 +691,8 @@ static NO_COPY wincaps wincap_2003 = {
has_disk_ex_ioctls:true,
has_working_virtual_lock:true,
has_disabled_user_tos_setting:true,
has_fileid_dirinfo:true
has_fileid_dirinfo:true,
has_exclusiveaddruse:true
};
static NO_COPY wincaps wincap_vista = {
@ -736,7 +748,8 @@ static NO_COPY wincaps wincap_vista = {
has_disk_ex_ioctls:true,
has_working_virtual_lock:true,
has_disabled_user_tos_setting:true,
has_fileid_dirinfo:true
has_fileid_dirinfo:true,
has_exclusiveaddruse:true
};
wincapc wincap __attribute__((section (".cygwin_dll_common"), shared));

View File

@ -66,6 +66,7 @@ struct wincaps
unsigned has_working_virtual_lock : 1;
unsigned has_disabled_user_tos_setting : 1;
unsigned has_fileid_dirinfo : 1;
unsigned has_exclusiveaddruse : 1;
};
class wincapc
@ -138,6 +139,7 @@ public:
bool IMPLEMENT (has_working_virtual_lock)
bool IMPLEMENT (has_disabled_user_tos_setting)
bool IMPLEMENT (has_fileid_dirinfo)
bool IMPLEMENT (has_exclusiveaddruse)
#undef IMPLEMENT
};