]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Bug 2483: bind() called before connect()
authorAmos Jeffries <squid3@treenet.co.nz>
Thu, 24 Sep 2009 09:16:35 +0000 (21:16 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Thu, 24 Sep 2009 09:16:35 +0000 (21:16 +1200)
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.

src/comm.cc
src/defines.h

index 7d103a75a4acf9c4cdc961503e39b3deb39e2342..b1fc8786ccbf7c7289ea9d472ca013198612ff0e 100644 (file)
@@ -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);
index ac6ace1e5842539ce08ac744524c6668e759c547..dc4e7ea5e44448662567409ca48e9be31b4641f6 100644 (file)
@@ -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])