]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Bug 2483: bind() called before connect()
authorAmos Jeffries <squid3@treenet.co.nz>
Wed, 16 Sep 2009 07:34:24 +0000 (19:34 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Wed, 16 Sep 2009 07:34:24 +0000 (19:34 +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 cd6972c157d3eadd5dda3455fb23e9cd7060b9f6..1319eccdda7f4a83a17df0d04da81ef8af022716 100644 (file)
@@ -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);
index c54093d3be6860d238eb838e32d8001eede844ef..a6ac3eeaecdb0f213ebe7b497392e5caa702cc69 100644 (file)
@@ -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; }