From: Amos Jeffries Date: Thu, 24 Sep 2009 09:16:35 +0000 (+1200) Subject: Bug 2483: bind() called before connect() X-Git-Tag: SQUID_3_1_0_14~16 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=23f90a4d81350a427d685d484c94d2ee4c41190d;p=thirdparty%2Fsquid.git Bug 2483: bind() called before connect() Many of the occasions Squid was calling bind() are not required. This reduces the bind() calls to only those which are actually needed. Further optimization can be done in a future version to drop the paranoid and slightly performance degrading safety checks for instances of Squid binding ANYADDR without listener status, and attempting to bind NOADDR. --- diff --git a/src/comm.cc b/src/comm.cc index 7d103a75a4..b1fc8786cc 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -590,8 +590,10 @@ commBind(int s, struct addrinfo &inaddr) { statCounter.syscalls.sock.binds++; - if (bind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) + if (bind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) { + debugs(50, 6, "commBind: bind socket FD " << s << " to " << fd_table[s].local_addr); return COMM_OK; + } debugs(50, 0, "commBind: Cannot bind socket FD " << s << " to " << fd_table[s].local_addr << ": " << xstrerror()); @@ -621,6 +623,9 @@ comm_open_listener(int sock_type, { int sock = -1; + /* all listener sockets require bind() */ + flags |= COMM_DOBIND; + /* attempt native enabled port. */ sock = comm_openex(sock_type, proto, addr, flags, 0, note); @@ -783,10 +788,8 @@ comm_openex(int sock_type, if (addr.GetPort() > (u_short) 0) { #ifdef _SQUID_MSWIN_ - if (sock_type != SOCK_DGRAM) #endif - commSetNoLinger(new_socket); if (opt_reuseaddr) @@ -798,7 +801,12 @@ comm_openex(int sock_type, comm_set_transparent(new_socket); } - if (!addr.IsNoAddr()) { + if ( (flags & COMM_DOBIND) || addr.GetPort() > 0 || !addr.IsAnyAddr() ) { + if ( !(flags & COMM_DOBIND) && addr.IsAnyAddr() ) + debugs(5,1,"WARNING: Squid is attempting to bind() port " << addr << " without being a listener."); + if ( addr.IsNoAddr() ) + debugs(5,0,"CRITICAL: Squid is attempting to bind() port " << addr << "!!"); + if (commBind(new_socket, *AI) != COMM_OK) { comm_close(new_socket); addr.FreeAddrInfo(AI); diff --git a/src/defines.h b/src/defines.h index ac6ace1e58..dc4e7ea5e4 100644 --- a/src/defines.h +++ b/src/defines.h @@ -66,6 +66,7 @@ #define COMM_NOCLOEXEC 0x02 #define COMM_REUSEADDR 0x04 #define COMM_TRANSPARENT 0x08 +#define COMM_DOBIND 0x10 #include "Debug.h" #define do_debug(SECTION, LEVEL) ((Debug::level = (LEVEL)) > Debug::Levels[SECTION])