From 5369605f4f7a8275f47e695856d42dc39bcbd3e3 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Sun, 29 Jan 2006 12:23:44 +0000 Subject: [PATCH] * 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. --- winsup/cygwin/ChangeLog | 11 +++++++++ winsup/cygwin/fhandler.h | 2 ++ winsup/cygwin/fhandler_socket.cc | 29 +++++++++++++++++++++--- winsup/cygwin/net.cc | 2 ++ winsup/cygwin/wincap.cc | 39 +++++++++++++++++++++----------- winsup/cygwin/wincap.h | 2 ++ 6 files changed, 69 insertions(+), 16 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 82dcaebb7..1886c3084 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,14 @@ +2006-01-29 Corinna Vinschen + + * 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 * fhandler_disk_file.cc (fhandler_disk_file::mkdir): In case or error, diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index 82152019b..37b7699c8 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -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) diff --git a/winsup/cygwin/fhandler_socket.cc b/winsup/cygwin/fhandler_socket.cc index 2405471fe..b885b97db 100644 --- a/winsup/cygwin/fhandler_socket.cc +++ b/winsup/cygwin/fhandler_socket.cc @@ -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; diff --git a/winsup/cygwin/net.cc b/winsup/cygwin/net.cc index d56bb974c..91d18fd65 100644 --- a/winsup/cygwin/net.cc +++ b/winsup/cygwin/net.cc @@ -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)", diff --git a/winsup/cygwin/wincap.cc b/winsup/cygwin/wincap.cc index 9ff3c92f1..6b6b0e11d 100644 --- a/winsup/cygwin/wincap.cc +++ b/winsup/cygwin/wincap.cc @@ -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)); diff --git a/winsup/cygwin/wincap.h b/winsup/cygwin/wincap.h index 5738850e0..d4edfd3da 100644 --- a/winsup/cygwin/wincap.h +++ b/winsup/cygwin/wincap.h @@ -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 }; -- 2.43.5