From: Amos Jeffries Date: Wed, 16 Sep 2009 07:34:24 +0000 (+1200) Subject: Bug 2483: bind() called before connect() X-Git-Tag: SQUID_3_2_0_1~713 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ac760b5e58f647b00c1bdd0cc4c86c313a1e5f1e;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 cd6972c157..1319eccdda 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -597,8 +597,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()); @@ -628,6 +630,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); @@ -790,10 +795,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) @@ -805,7 +808,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 c54093d3be..a6ac3eeaec 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 #define safe_free(x) if (x) { xxfree(x); x = NULL; }