]> git.kaiwu.me - nginx.git/commitdiff
Set IP_BIND_ADDRESS_NO_PORT socket option for upstream sockets.
authorAndrei Belov <defan@nginx.com>
Mon, 20 Jun 2016 07:41:17 +0000 (10:41 +0300)
committerAndrei Belov <defan@nginx.com>
Mon, 20 Jun 2016 07:41:17 +0000 (10:41 +0300)
auto/unix
src/event/ngx_event_connect.c

index e6396ef2a25b050695bb9b71a436d8b08d9b6e55..f83173b8b274809b42014aa40f22978e74ad8600 100755 (executable)
--- a/auto/unix
+++ b/auto/unix
@@ -341,6 +341,19 @@ ngx_feature_test="setsockopt(0, SOL_SOCKET, SO_BINDANY, NULL, 0)"
 . auto/feature
 
 
+# Linux IP_BIND_ADDRESS_NO_PORT
+
+ngx_feature="IP_BIND_ADDRESS_NO_PORT"
+ngx_feature_name="NGX_HAVE_IP_BIND_ADDRESS_NO_PORT"
+ngx_feature_run=no
+ngx_feature_incs="#include <sys/socket.h>
+                  #include <netinet/in.h>"
+ngx_feature_path=
+ngx_feature_libs=
+ngx_feature_test="setsockopt(0, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, NULL, 0)"
+. auto/feature
+
+
 # Linux transparent proxying
 
 ngx_feature="IP_TRANSPARENT"
index 5de991e07c1db689e785e8e7e2f331be32aeaf4e..9a8f4d93142003f899a35c46b2f8b1878764c01c 100644 (file)
@@ -87,6 +87,32 @@ ngx_event_connect_peer(ngx_peer_connection_t *pc)
         }
 #endif
 
+#if (NGX_HAVE_IP_BIND_ADDRESS_NO_PORT)
+
+        if (pc->sockaddr->sa_family != AF_UNIX) {
+            static int  bind_address_no_port = 1;
+
+            if (bind_address_no_port) {
+                if (setsockopt(s, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT,
+                               (const void *) &bind_address_no_port,
+                               sizeof(int)) == -1)
+                {
+                    err = ngx_socket_errno;
+
+                    if (err != NGX_EOPNOTSUPP && err != NGX_ENOPROTOOPT) {
+                        ngx_log_error(NGX_LOG_ALERT, pc->log, err,
+                                      "setsockopt(IP_BIND_ADDRESS_NO_PORT) "
+                                      "failed, ignored");
+
+                    } else {
+                        bind_address_no_port = 0;
+                    }
+                }
+            }
+        }
+
+#endif
+
         if (bind(s, pc->local->sockaddr, pc->local->socklen) == -1) {
             ngx_log_error(NGX_LOG_CRIT, pc->log, ngx_socket_errno,
                           "bind(%V) failed", &pc->local->name);