]> git.kaiwu.me - nginx.git/commitdiff
ipv6only
authorIgor Sysoev <igor@sysoev.ru>
Fri, 13 Mar 2009 14:20:34 +0000 (14:20 +0000)
committerIgor Sysoev <igor@sysoev.ru>
Fri, 13 Mar 2009 14:20:34 +0000 (14:20 +0000)
src/core/ngx_connection.c
src/core/ngx_connection.h
src/http/ngx_http.c
src/http/ngx_http_core_module.c
src/http/ngx_http_core_module.h

index 804ab09169d41ce77cf0fe8406eaa92a183d3928..f413ffbe88697f096ab956fb73e378cc37d3bdc4 100644 (file)
@@ -282,6 +282,23 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
                 return NGX_ERROR;
             }
 
+#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
+
+            if (ls[i].sockaddr->sa_family == AF_INET6 && ls[i].ipv6only) {
+                int  ipv6only;
+
+                ipv6only = (ls[i].ipv6only == 1);
+
+                if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
+                               (const void *) &ipv6only, sizeof(int))
+                    == -1)
+                {
+                    ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
+                                  "setsockopt(IPV6_V6ONLY) %V failed, ignored",
+                                  &ls[i].addr_text);
+                }
+            }
+#endif
             /* TODO: close on exit */
 
             if (!(ngx_event_flags & NGX_USE_AIO_EVENT)) {
index 171078b2375c8ea48e5b8dbd29b90cc162b812db..cc6a92989f78156380e7a5e387350785d62ccc0a 100644 (file)
@@ -56,6 +56,10 @@ struct ngx_listening_s {
     unsigned            shared:1;    /* shared between threads or processes */
     unsigned            addr_ntop:1;
 
+#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
+    unsigned            ipv6only:2;
+#endif
+
 #if (NGX_HAVE_DEFERRED_ACCEPT)
     unsigned            deferred_accept:1;
     unsigned            delete_deferred:1;
index 626686ca8246306cbfadb510826672f5f0f58b12..d519bf98173f6758c1fb7d22a43ca39daf92ab94 100644 (file)
@@ -1768,6 +1768,10 @@ ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
     ls->deferred_accept = addr->listen_conf->deferred_accept;
 #endif
 
+#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
+    ls->ipv6only = addr->listen_conf->ipv6only;
+#endif
+
     return ls;
 }
 
index a2d4e86882556a8e4b072c4e76ea91123be412fa..2c3992b56bcbcff563d0fe372108da659f8b01ec 100644 (file)
@@ -3332,6 +3332,45 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
             continue;
         }
 
+        if (ngx_strncmp(value[n].data, "ipv6only=o", 10) == 0) {
+#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
+            struct sockaddr  *sa;
+
+            sa = (struct sockaddr *) ls->sockaddr;
+
+            if (sa->sa_family == AF_INET6) {
+
+                if (ngx_strcmp(&value[n].data[10], "n") == 0) {
+                    ls->conf.ipv6only = 1;
+
+                } else if (ngx_strcmp(&value[n].data[10], "ff") == 0) {
+                    ls->conf.ipv6only = 2;
+
+                } else {
+                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                                       "invalid ipv6only flags \"%s\"",
+                                       &value[n].data[9]);
+                    return NGX_CONF_ERROR;
+                }
+
+                ls->conf.bind = 1;
+
+            } else {
+                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                                   "ipv6only is not supported "
+                                   "on addr \"%s\", ignored",
+                                   ls->conf.addr);
+            }
+
+            continue;
+#else
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                               "bind ipv6only is not supported "
+                               "on this platform");
+            return NGX_CONF_ERROR;
+#endif
+        }
+
         if (ngx_strcmp(value[n].data, "ssl") == 0) {
 #if (NGX_HTTP_SSL)
             ls->conf.ssl = 1;
index 7ad18edd242b62da4de5ebc5f2dcc1b2d202cb2f..705659d0a9b5f3b9dc200e98dade9572dfdbe499 100644 (file)
@@ -44,6 +44,9 @@ typedef struct {
 #if (NGX_HTTP_SSL)
     unsigned                   ssl:1;
 #endif
+#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
+    unsigned                   ipv6only:2;
+#endif
 
     int                        backlog;
     int                        rcvbuf;