diff options
Diffstat (limited to 'src/http')
| -rw-r--r-- | src/http/modules/ngx_http_access_module.c | 6 | ||||
| -rw-r--r-- | src/http/modules/ngx_http_geo_module.c | 4 | ||||
| -rw-r--r-- | src/http/modules/ngx_http_realip_module.c | 6 | ||||
| -rw-r--r-- | src/http/modules/ngx_http_upstream_ip_hash_module.c | 20 | ||||
| -rw-r--r-- | src/http/ngx_http.c | 592 | ||||
| -rw-r--r-- | src/http/ngx_http_core_module.c | 57 | ||||
| -rw-r--r-- | src/http/ngx_http_core_module.h | 44 | ||||
| -rw-r--r-- | src/http/ngx_http_header_filter_module.c | 4 | ||||
| -rw-r--r-- | src/http/ngx_http_request.c | 110 | ||||
| -rw-r--r-- | src/http/ngx_http_variables.c | 74 |
10 files changed, 603 insertions, 314 deletions
diff --git a/src/http/modules/ngx_http_access_module.c b/src/http/modules/ngx_http_access_module.c index 12f0b9922..8cd2e0e13 100644 --- a/src/http/modules/ngx_http_access_module.c +++ b/src/http/modules/ngx_http_access_module.c @@ -9,8 +9,6 @@ #include <ngx_http.h> -/* AF_INET only */ - typedef struct { in_addr_t mask; in_addr_t addr; @@ -103,6 +101,10 @@ ngx_http_access_handler(ngx_http_request_t *r) /* AF_INET only */ + if (r->connection->sockaddr->sa_family != AF_INET) { + return NGX_DECLINED; + } + sin = (struct sockaddr_in *) r->connection->sockaddr; rule = alcf->rules->elts; diff --git a/src/http/modules/ngx_http_geo_module.c b/src/http/modules/ngx_http_geo_module.c index bd6499ec7..a8f26854b 100644 --- a/src/http/modules/ngx_http_geo_module.c +++ b/src/http/modules/ngx_http_geo_module.c @@ -175,6 +175,10 @@ ngx_http_geo_addr(ngx_http_request_t *r, ngx_http_geo_ctx_t *ctx) ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http geo started: %V", &r->connection->addr_text); + if (r->connection->sockaddr->sa_family != AF_INET) { + return 0; + } + sin = (struct sockaddr_in *) r->connection->sockaddr; return ntohl(sin->sin_addr.s_addr); } diff --git a/src/http/modules/ngx_http_realip_module.c b/src/http/modules/ngx_http_realip_module.c index 44c74a182..58bdeeb37 100644 --- a/src/http/modules/ngx_http_realip_module.c +++ b/src/http/modules/ngx_http_realip_module.c @@ -14,8 +14,6 @@ #define NGX_HTTP_REALIP_HEADER 2 -/* AF_INET only */ - typedef struct { in_addr_t mask; in_addr_t addr; @@ -209,6 +207,10 @@ found: /* AF_INET only */ + if (r->connection->sockaddr->sa_family != AF_INET) { + return NGX_DECLINED; + } + sin = (struct sockaddr_in *) c->sockaddr; from = rlcf->from->elts; diff --git a/src/http/modules/ngx_http_upstream_ip_hash_module.c b/src/http/modules/ngx_http_upstream_ip_hash_module.c index 1d13481a9..dffbf22b2 100644 --- a/src/http/modules/ngx_http_upstream_ip_hash_module.c +++ b/src/http/modules/ngx_http_upstream_ip_hash_module.c @@ -15,7 +15,6 @@ typedef struct { ngx_uint_t hash; - /* AF_INET only */ u_char addr[3]; u_char tries; @@ -111,11 +110,20 @@ ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r, r->upstream->peer.get = ngx_http_upstream_get_ip_hash_peer; /* AF_INET only */ - sin = (struct sockaddr_in *) r->connection->sockaddr; - p = (u_char *) &sin->sin_addr.s_addr; - iphp->addr[0] = p[0]; - iphp->addr[1] = p[1]; - iphp->addr[2] = p[2]; + + if (r->connection->sockaddr->sa_family == AF_INET) { + + sin = (struct sockaddr_in *) r->connection->sockaddr; + p = (u_char *) &sin->sin_addr.s_addr; + iphp->addr[0] = p[0]; + iphp->addr[1] = p[1]; + iphp->addr[2] = p[2]; + + } else { + iphp->addr[0] = 0; + iphp->addr[1] = 0; + iphp->addr[2] = 0; + } iphp->hash = 89; iphp->tries = 0; diff --git a/src/http/ngx_http.c b/src/http/ngx_http.c index 6134f495e..ecd9c97d7 100644 --- a/src/http/ngx_http.c +++ b/src/http/ngx_http.c @@ -20,16 +20,16 @@ static ngx_int_t ngx_http_init_phase_handlers(ngx_conf_t *cf, static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers, ngx_array_t *in_ports); static ngx_int_t ngx_http_add_ports(ngx_conf_t *cf, - ngx_http_core_srv_conf_t *cscf, ngx_array_t *in_ports, + ngx_http_core_srv_conf_t *cscf, ngx_array_t *ports, ngx_http_listen_t *listen); static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf, - ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port, + ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port, ngx_http_listen_t *listen); static ngx_int_t ngx_http_add_address(ngx_conf_t *cf, - ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port, + ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port, ngx_http_listen_t *listen); static ngx_int_t ngx_http_add_names(ngx_conf_t *cf, - ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_addr_t *in_addr); + ngx_http_core_srv_conf_t *cscf, ngx_http_conf_addr_t *addr); static char *ngx_http_merge_locations(ngx_conf_t *cf, ngx_queue_t *locations, void **loc_conf, ngx_http_module_t *module, @@ -49,15 +49,23 @@ static ngx_http_location_tree_node_t * size_t prefix); static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf, - ngx_http_core_main_conf_t *cmcf, ngx_array_t *in_ports); + ngx_http_core_main_conf_t *cmcf, ngx_array_t *ports); static ngx_int_t ngx_http_server_names(ngx_conf_t *cf, - ngx_http_core_main_conf_t *cmcf, ngx_http_conf_in_addr_t *in_addr); -static ngx_int_t ngx_http_cmp_conf_in_addrs(const void *one, const void *two); + ngx_http_core_main_conf_t *cmcf, ngx_http_conf_addr_t *addr); +static ngx_int_t ngx_http_cmp_conf_addrs(const void *one, const void *two); static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one, const void *two); static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf, - ngx_http_conf_in_port_t *in_port); + ngx_http_conf_port_t *port); +static ngx_listening_t *ngx_http_add_listening(ngx_conf_t *cf, + ngx_http_conf_addr_t *addr); +static ngx_int_t ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport, + ngx_http_conf_addr_t *addr); +#if (NGX_HAVE_INET6) +static ngx_int_t ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport, + ngx_http_conf_addr_t *addr); +#endif ngx_uint_t ngx_http_max_module; @@ -359,8 +367,6 @@ ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) * to find quickly the server core module configuration at run-time */ - /* AF_INET only */ - if (ngx_http_init_server_lists(cf, &cmcf->servers, &in_ports) != NGX_OK) { return NGX_CONF_ERROR; } @@ -368,8 +374,6 @@ ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) /* optimize the lists of ports, addresses and server names */ - /* AF_INET only */ - if (ngx_http_optimize_servers(cf, cmcf, &in_ports) != NGX_OK) { return NGX_CONF_ERROR; } @@ -1107,14 +1111,13 @@ inclusive: static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers, - ngx_array_t *in_ports) + ngx_array_t *ports) { ngx_uint_t s, i; ngx_http_listen_t *listen; ngx_http_core_srv_conf_t **cscfp; - if (ngx_array_init(in_ports, cf->temp_pool, 2, - sizeof(ngx_http_conf_in_port_t)) + if (ngx_array_init(ports, cf->temp_pool, 2, sizeof(ngx_http_conf_port_t)) != NGX_OK) { return NGX_ERROR; @@ -1130,11 +1133,7 @@ ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers, listen = cscfp[s]->listen.elts; for (i = 0; i < cscfp[s]->listen.nelts; i++) { - /* AF_INET only */ - - if (ngx_http_add_ports(cf, cscfp[s], in_ports, &listen[i]) - != NGX_OK) - { + if (ngx_http_add_ports(cf, cscfp[s], ports, &listen[i]) != NGX_OK) { return NGX_ERROR; } } @@ -1144,70 +1143,116 @@ ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers, } -/* AF_INET only */ - static ngx_int_t ngx_http_add_ports(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, - ngx_array_t *in_ports, ngx_http_listen_t *listen) + ngx_array_t *ports, ngx_http_listen_t *listen) { + in_port_t p; ngx_uint_t i; - ngx_http_conf_in_port_t *in_port; + struct sockaddr *sa; + struct sockaddr_in *sin; + ngx_http_conf_port_t *port; +#if (NGX_HAVE_INET6) + struct sockaddr_in6 *sin6; +#endif + + sa = (struct sockaddr *) &listen->sockaddr; - in_port = in_ports->elts; - for (i = 0; i < in_ports->nelts; i++) { + switch (sa->sa_family) { - if (listen->port != in_port[i].port) { +#if (NGX_HAVE_INET6) + case AF_INET6: + sin6 = (struct sockaddr_in6 *) sa; + p = sin6->sin6_port; + break; +#endif + + default: /* AF_INET */ + sin = (struct sockaddr_in *) sa; + p = sin->sin_port; + break; + } + + port = ports->elts; + for (i = 0; i < ports->nelts; i++) { + + if (p != port[i].port || sa->sa_family != port[i].family) { continue; } /* a port is already in the in_port list */ - return ngx_http_add_addresses(cf, cscf, &in_port[i], listen); + return ngx_http_add_addresses(cf, cscf, &port[i], listen); } /* add a port to the in_port list */ - in_port = ngx_array_push(in_ports); - if (in_port == NULL) { + port = ngx_array_push(ports); + if (port == NULL) { return NGX_ERROR; } - in_port->port = listen->port; - in_port->addrs.elts = NULL; + port->family = sa->sa_family; + port->port = p; + port->addrs.elts = NULL; - return ngx_http_add_address(cf, cscf, in_port, listen); + return ngx_http_add_address(cf, cscf, port, listen); } static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, - ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen) + ngx_http_conf_port_t *port, ngx_http_listen_t *listen) { - ngx_uint_t i; - ngx_http_conf_in_addr_t *in_addr; + u_char *p; + size_t len, off; + ngx_uint_t i; + struct sockaddr *sa; + ngx_http_conf_addr_t *addr; + + /* + * we can not compare whole sockaddr struct's as kernel + * may fill some fields in inherited sockaddr struct's + */ + + sa = (struct sockaddr *) &listen->sockaddr; - in_addr = in_port->addrs.elts; + switch (sa->sa_family) { - for (i = 0; i < in_port->addrs.nelts; i++) { +#if (NGX_HAVE_INET6) + case AF_INET6: + off = offsetof(struct sockaddr_in6, sin6_addr); + len = 16; + break; +#endif + + default: /* AF_INET */ + off = offsetof(struct sockaddr_in, sin_addr); + len = 4; + break; + } - if (listen->addr != in_addr[i].addr) { + p = listen->sockaddr + off; + + addr = port->addrs.elts; + + for (i = 0; i < port->addrs.nelts; i++) { + + if (ngx_memcmp(p, (u_char *) addr[i].sockaddr + off, len) != 0) { continue; } /* the address is already in the address list */ - if (ngx_http_add_names(cf, cscf, &in_addr[i]) != NGX_OK) { + if (ngx_http_add_names(cf, cscf, &addr[i]) != NGX_OK) { return NGX_ERROR; } - /* - * check the duplicate "default" server - * for this address:port - */ + /* check the duplicate "default" server for this address:port */ if (listen->conf.default_server) { - if (in_addr[i].default_server) { + if (addr[i].default_server) { ngx_log_error(NGX_LOG_ERR, cf->log, 0, "the duplicate default server in %s:%ui", listen->file_name, listen->line); @@ -1215,23 +1260,20 @@ ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, return NGX_ERROR; } - in_addr[i].core_srv_conf = cscf; - in_addr[i].default_server = 1; + addr[i].core_srv_conf = cscf; + addr[i].default_server = 1; #if (NGX_HTTP_SSL) - in_addr[i].ssl = listen->conf.ssl; + addr[i].ssl = listen->conf.ssl; #endif - in_addr[i].listen_conf = &listen->conf; + addr[i].listen_conf = &listen->conf; } return NGX_OK; } - /* - * add the address to the addresses list that - * bound to this port - */ + /* add the address to the addresses list that bound to this port */ - return ngx_http_add_address(cf, cscf, in_port, listen); + return ngx_http_add_address(cf, cscf, port, listen); } @@ -1242,60 +1284,62 @@ ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, static ngx_int_t ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, - ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen) + ngx_http_conf_port_t *port, ngx_http_listen_t *listen) { - ngx_http_conf_in_addr_t *in_addr; + ngx_http_conf_addr_t *addr; - if (in_port->addrs.elts == NULL) { - if (ngx_array_init(&in_port->addrs, cf->temp_pool, 4, - sizeof(ngx_http_conf_in_addr_t)) + if (port->addrs.elts == NULL) { + if (ngx_array_init(&port->addrs, cf->temp_pool, 4, + sizeof(ngx_http_conf_addr_t)) != NGX_OK) { return NGX_ERROR; } } - in_addr = ngx_array_push(&in_port->addrs); - if (in_addr == NULL) { + addr = ngx_array_push(&port->addrs); + if (addr == NULL) { return NGX_ERROR; } - in_addr->addr = listen->addr; - in_addr->hash.buckets = NULL; - in_addr->hash.size = 0; - in_addr->wc_head = NULL; - in_addr->wc_tail = NULL; - in_addr->names.elts = NULL; + addr->sockaddr = (struct sockaddr *) &listen->sockaddr; + addr->socklen = listen->socklen; + addr->hash.buckets = NULL; + addr->hash.size = 0; + addr->wc_head = NULL; + addr->wc_tail = NULL; + addr->names.elts = NULL; #if (NGX_PCRE) - in_addr->nregex = 0; - in_addr->regex = NULL; + addr->nregex = 0; + addr->regex = NULL; #endif - in_addr->core_srv_conf = cscf; - in_addr->default_server = listen->conf.default_server; - in_addr->bind = listen->conf.bind; + addr->core_srv_conf = cscf; + addr->default_server = listen->conf.default_server; + addr->bind = listen->conf.bind; + addr->wildcard = listen->conf.wildcard; #if (NGX_HTTP_SSL) - in_addr->ssl = listen->conf.ssl; + addr->ssl = listen->conf.ssl; #endif - in_addr->listen_conf = &listen->conf; + addr->listen_conf = &listen->conf; - return ngx_http_add_names(cf, cscf, in_addr); + return ngx_http_add_names(cf, cscf, addr); } /* * add the server names and the server core module - * configurations to the address:port (in_addr) + * configurations to the address:port */ static ngx_int_t ngx_http_add_names(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, - ngx_http_conf_in_addr_t *in_addr) + ngx_http_conf_addr_t *addr) { ngx_uint_t i; ngx_http_server_name_t *server_names, *name; - if (in_addr->names.elts == NULL) { - if (ngx_array_init(&in_addr->names, cf->temp_pool, 4, + if (addr->names.elts == NULL) { + if (ngx_array_init(&addr->names, cf->temp_pool, 4, sizeof(ngx_http_server_name_t)) != NGX_OK) { @@ -1313,7 +1357,7 @@ ngx_http_add_names(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0, "name: %V", &server_names[i].name); - name = ngx_array_push(&in_addr->names); + name = ngx_array_push(&addr->names); if (name == NULL) { return NGX_ERROR; } @@ -1327,35 +1371,35 @@ ngx_http_add_names(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf, static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, - ngx_array_t *in_ports) + ngx_array_t *ports) { ngx_uint_t s, p, a; + ngx_http_conf_port_t *port; + ngx_http_conf_addr_t *addr; ngx_http_server_name_t *name; - ngx_http_conf_in_port_t *in_port; - ngx_http_conf_in_addr_t *in_addr; - in_port = in_ports->elts; - for (p = 0; p < in_ports->nelts; p++) { + port = ports->elts; + for (p = 0; p < ports->nelts; p++) { - ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts, - sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs); + ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts, + sizeof(ngx_http_conf_addr_t), ngx_http_cmp_conf_addrs); /* * check whether all name-based servers have the same * configuraiton as a default server for given address:port */ - in_addr = in_port[p].addrs.elts; - for (a = 0; a < in_port[p].addrs.nelts; a++) { + addr = port[p].addrs.elts; + for (a = 0; a < port[p].addrs.nelts; a++) { - name = in_addr[a].names.elts; - for (s = 0; s < in_addr[a].names.nelts; s++) { + name = addr[a].names.elts; + for (s = 0; s < addr[a].names.nelts; s++) { - if (in_addr[a].core_srv_conf == name[s].core_srv_conf) { + if (addr[a].core_srv_conf == name[s].core_srv_conf) { continue; } - if (ngx_http_server_names(cf, cmcf, &in_addr[a]) != NGX_OK) { + if (ngx_http_server_names(cf, cmcf, &addr[a]) != NGX_OK) { return NGX_ERROR; } @@ -1363,7 +1407,7 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, } } - if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) { + if (ngx_http_init_listening(cf, &port[p]) != NGX_OK) { return NGX_ERROR; } } @@ -1374,7 +1418,7 @@ ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, static ngx_int_t ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, - ngx_http_conf_in_addr_t *in_addr) + ngx_http_conf_addr_t *addr) { ngx_int_t rc; ngx_uint_t s; @@ -1400,9 +1444,9 @@ ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, goto failed; } - name = in_addr->names.elts; + name = addr->names.elts; - for (s = 0; s < in_addr->names.nelts; s++) { + for (s = 0; s < addr->names.nelts; s++) { #if (NGX_PCRE) if (name[s].regex) { @@ -1421,14 +1465,14 @@ ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, if (rc == NGX_DECLINED) { ngx_log_error(NGX_LOG_EMERG, cf->log, 0, "invalid server name or wildcard \"%V\" on %s", - &name[s].name, in_addr->listen_conf->addr); + &name[s].name, addr->listen_conf->addr); return NGX_ERROR; } if (rc == NGX_BUSY) { ngx_log_error(NGX_LOG_WARN, cf->log, 0, "conflicting server name \"%V\" on %s, ignored", - &name[s].name, in_addr->listen_conf->addr); + &name[s].name, addr->listen_conf->addr); } } @@ -1439,7 +1483,7 @@ ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, hash.pool = cf->pool; if (ha.keys.nelts) { - hash.hash = &in_addr->hash; + hash.hash = &addr->hash; hash.temp_pool = NULL; if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) { @@ -1462,7 +1506,7 @@ ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, goto failed; } - in_addr->wc_head = (ngx_hash_wildcard_t *) hash.hash; + addr->wc_head = (ngx_hash_wildcard_t *) hash.hash; } if (ha.dns_wc_tail.nelts) { @@ -1480,7 +1524,7 @@ ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, goto failed; } - in_addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash; + addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash; } ngx_destroy_pool(ha.temp_pool); @@ -1491,16 +1535,15 @@ ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, return NGX_OK; } - in_addr->nregex = regex; - in_addr->regex = ngx_palloc(cf->pool, - regex * sizeof(ngx_http_server_name_t)); - if (in_addr->regex == NULL) { + addr->nregex = regex; + addr->regex = ngx_palloc(cf->pool, regex * sizeof(ngx_http_server_name_t)); + if (addr->regex == NULL) { return NGX_ERROR; } - for (i = 0, s = 0; s < in_addr->names.nelts; s++) { + for (i = 0, s = 0; s < addr->names.nelts; s++) { if (name[s].regex) { - in_addr->regex[i++] = name[s]; + addr->regex[i++] = name[s]; } } @@ -1517,15 +1560,15 @@ failed: static ngx_int_t -ngx_http_cmp_conf_in_addrs(const void *one, const void *two) +ngx_http_cmp_conf_addrs(const void *one, const void *two) { - ngx_http_conf_in_addr_t *first, *second; + ngx_http_conf_addr_t *first, *second; - first = (ngx_http_conf_in_addr_t *) one; - second = (ngx_http_conf_in_addr_t *) two; + first = (ngx_http_conf_addr_t *) one; + second = (ngx_http_conf_addr_t *) two; - if (first->addr == INADDR_ANY) { - /* the INADDR_ANY must be the last resort, shift it to the end */ + if (first->wildcard) { + /* a wildcard address must be the last resort, shift it to the end */ return 1; } @@ -1558,170 +1601,301 @@ ngx_http_cmp_dns_wildcards(const void *one, const void *two) static ngx_int_t -ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_in_port_t *in_port) +ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port) { - ngx_uint_t i, a, last, bind_all, done; + ngx_uint_t i, a, last, bind_wildcard; ngx_listening_t *ls; - ngx_http_in_port_t *hip; - ngx_http_conf_in_addr_t *in_addr; - ngx_http_virtual_names_t *vn; - ngx_http_core_loc_conf_t *clcf; - ngx_http_core_srv_conf_t *cscf; + ngx_http_port_t *hport; + ngx_http_conf_addr_t *addr; - in_addr = in_port->addrs.elts; - last = in_port->addrs.nelts; + addr = port->addrs.elts; + last = port->addrs.nelts; /* - * if there is a binding to a "*:port" then we need to bind() - * to the "*:port" only and ignore other bindings + * If there is a binding to an "*:port" then we need to bind() to + * the "*:port" only and ignore other implicit bindings. The bindings + * have been already sorted: explicit bindings are on the start, then + * implicit bindings go, and wildcard binding is in the end. */ - if (in_addr[last - 1].addr == INADDR_ANY) { - in_addr[last - 1].bind = 1; - bind_all = 0; + if (addr[last - 1].wildcard) { + addr[last - 1].bind = 1; + bind_wildcard = 1; } else { - bind_all = 1; + bind_wildcard = 0; } a = 0; while (a < last) { - if (!bind_all && !in_addr[a].bind) { + if (bind_wildcard && !addr[a].bind) { a++; continue; } - ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr, - in_port->port); + ls = ngx_http_add_listening(cf, &addr[a]); if (ls == NULL) { return NGX_ERROR; } - ls->addr_ntop = 1; + hport = ngx_pcalloc(cf->pool, sizeof(ngx_http_port_t)); + if (hport == NULL) { + return NGX_ERROR; + } - ls->handler = ngx_http_init_connection; + ls->servers = hport; - cscf = in_addr[a].core_srv_conf; - ls->pool_size = cscf->connection_pool_size; - ls->post_accept_timeout = cscf->client_header_timeout; + hport->port = ntohs(port->port); - clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index]; + for (i = ls->addr_text.len - 1; i; i--) { - ls->log = *clcf->err_log; - ls->log.data = &ls->addr_text; - ls->log.handler = ngx_accept_log_error; + if (ls->addr_text.data[i] == ':') { + hport->port_text.len = ls->addr_text.len - i; + hport->port_text.data = &ls->addr_text.data[i]; + break; + } + } -#if (NGX_WIN32) - { - ngx_iocp_conf_t *iocpcf; + if (a == last - 1) { + hport->naddrs = last; - iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module); - if (iocpcf->acceptex_read) { - ls->post_accept_buffer_size = cscf->client_header_buffer_size; + } else { + hport->naddrs = 1; + a = 0; } + + switch (ls->sockaddr->sa_family) { + +#if (NGX_HAVE_INET6) + case AF_INET6: + if (ngx_http_add_addrs6(cf, hport, addr) != NGX_OK) { + return NGX_ERROR; + } + break; +#endif + default: /* AF_INET */ + if (ngx_http_add_addrs(cf, hport, addr) != NGX_OK) { + return NGX_ERROR; + } + break; } + + addr++; + last--; + } + + return NGX_OK; +} + + +static ngx_listening_t * +ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr) +{ + ngx_listening_t *ls; + struct sockaddr *sa; + ngx_http_core_loc_conf_t *clcf; + ngx_http_core_srv_conf_t *cscf; + u_char text[NGX_SOCKADDR_STRLEN]; + + ls = ngx_array_push(&cf->cycle->listening); + if (ls == NULL) { + return NULL; + } + + ngx_memzero(ls, sizeof(ngx_listening_t)); + + sa = ngx_palloc(cf->pool, addr->socklen); + if (sa == NULL) { + return NULL; + } + + ngx_memcpy(sa, addr->sockaddr, addr->socklen); + + ls->sockaddr = sa; + ls->socklen = addr->socklen; + + ls->addr_text.len = ngx_sock_ntop(sa, text, NGX_SOCKADDR_STRLEN, 1); + + ls->addr_text.data = ngx_pnalloc(cf->pool, ls->addr_text.len); + if (ls->addr_text.data == NULL) { + return NULL; + } + + ngx_memcpy(ls->addr_text.data, text, ls->addr_text.len); + + ls->fd = (ngx_socket_t) -1; + ls->type = SOCK_STREAM; + + switch (ls->sockaddr->sa_family) { +#if (NGX_HAVE_INET6) + case AF_INET6: + ls->addr_text_max_len = NGX_INET6_ADDRSTRLEN; + break; +#endif + case AF_INET: + ls->addr_text_max_len = NGX_INET_ADDRSTRLEN; + break; + default: + ls->addr_text_max_len = NGX_SOCKADDR_STRLEN; + break; + } + + ls->addr_ntop = 1; + + ls->handler = ngx_http_init_connection; + + cscf = addr->core_srv_conf; + ls->pool_size = cscf->connection_pool_size; + ls->post_accept_timeout = cscf->client_header_timeout; + + clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index]; + + ls->log = *clcf->err_log; + ls->log.data = &ls->addr_text; + ls->log.handler = ngx_accept_log_error; + +#if (NGX_WIN32) + { + ngx_iocp_conf_t *iocpcf; + + iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module); + if (iocpcf->acceptex_read) { + ls->post_accept_buffer_size = cscf->client_header_buffer_size; + } + } #endif - ls->backlog = in_addr[a].listen_conf->backlog; - ls->rcvbuf = in_addr[a].listen_conf->rcvbuf; - ls->sndbuf = in_addr[a].listen_conf->sndbuf; + ls->backlog = addr->listen_conf->backlog; + ls->rcvbuf = addr->listen_conf->rcvbuf; + ls->sndbuf = addr->listen_conf->sndbuf; #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) - ls->accept_filter = in_addr[a].listen_conf->accept_filter; + ls->accept_filter = addr->listen_conf->accept_filter; #endif #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) - ls->deferred_accept = in_addr[a].listen_conf->deferred_accept; + ls->deferred_accept = addr->listen_conf->deferred_accept; #endif - hip = ngx_palloc(cf->pool, sizeof(ngx_http_in_port_t)); - if (hip == NULL) { - return NGX_ERROR; - } + return ls; +} - hip->port = in_port->port; - hip->port_text.data = ngx_pnalloc(cf->pool, 7); - if (hip->port_text.data == NULL) { - return NGX_ERROR; - } +static ngx_int_t +ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport, + ngx_http_conf_addr_t *addr) +{ + ngx_uint_t i; + ngx_http_in_addr_t *addrs; + struct sockaddr_in *sin; + ngx_http_virtual_names_t *vn; - ls->servers = hip; + hport->addrs = ngx_pcalloc(cf->pool, + hport->naddrs * sizeof(ngx_http_in_addr_t)); + if (hport->addrs == NULL) { + return NGX_ERROR; + } - hip->port_text.len = ngx_sprintf(hip->port_text.data, ":%d", hip->port) - - hip->port_text.data; + addrs = hport->addrs; - in_addr = in_port->addrs.elts; + for (i = 0; i < hport->naddrs; i++) { - if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) { - hip->naddrs = 1; - done = 0; + sin = (struct sockaddr_in *) addr[i].sockaddr; + addrs[i].addr = sin->sin_addr.s_addr; + addrs[i].conf.core_srv_conf = addr[i].core_srv_conf; +#if (NGX_HTTP_SSL) + addrs[i].conf.ssl = addr[i].ssl; +#endif - } else if (in_port->addrs.nelts > 1 - && in_addr[last - 1].addr == INADDR_ANY) + if (addr[i].hash.buckets == NULL + && (addr[i].wc_head == NULL + || addr[i].wc_head->hash.buckets == NULL) + && (addr[i].wc_head == NULL + || addr[i].wc_head->hash.buckets == NULL)) { - hip->naddrs = last; - done = 1; - - } else { - hip->naddrs = 1; - done = 0; + continue; } - hip->addrs = ngx_pcalloc(cf->pool, - hip->naddrs * sizeof(ngx_http_in_addr_t)); - if (hip->addrs == NULL) { + vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t)); + if (vn == NULL) { return NGX_ERROR; } - for (i = 0; i < hip->naddrs; i++) { - hip->addrs[i].addr = in_addr[i].addr; - hip->addrs[i].core_srv_conf = in_addr[i].core_srv_conf; + addrs[i].conf.virtual_names = vn; -#if (NGX_HTTP_SSL) - hip->addrs[i].ssl = in_addr[i].ssl; + vn->names.hash = addr[i].hash; + vn->names.wc_head = addr[i].wc_head; + vn->names.wc_tail = addr[i].wc_tail; +#if (NGX_PCRE) + vn->nregex = addr[i].nregex; + vn->regex = addr[i].regex; #endif + } - if (in_addr[i].hash.buckets == NULL - && (in_addr[i].wc_head == NULL - || in_addr[i].wc_head->hash.buckets == NULL) - && (in_addr[i].wc_head == NULL - || in_addr[i].wc_head->hash.buckets == NULL)) - { - continue; - } + return NGX_OK; +} - vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t)); - if (vn == NULL) { - return NGX_ERROR; - } - hip->addrs[i].virtual_names = vn; - vn->names.hash = in_addr[i].hash; - vn->names.wc_head = in_addr[i].wc_head; - vn->names.wc_tail = in_addr[i].wc_tail; -#if (NGX_PCRE) - vn->nregex = in_addr[i].nregex; - vn->regex = in_addr[i].regex; +#if (NGX_HAVE_INET6) + +static ngx_int_t +ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport, + ngx_http_conf_addr_t *addr) +{ + ngx_uint_t i; + ngx_http_in6_addr_t *addrs6; + struct sockaddr_in6 *sin6; + ngx_http_virtual_names_t *vn; + + hport->addrs = ngx_pcalloc(cf->pool, + hport->naddrs * sizeof(ngx_http_in6_addr_t)); + if (hport->addrs == NULL) { + return NGX_ERROR; + } + + addrs6 = hport->addrs; + + for (i = 0; i < hport->naddrs; i++) { + + sin6 = (struct sockaddr_in6 *) addr[i].sockaddr; + addrs6[i].addr6 = sin6->sin6_addr; + addrs6[i].conf.core_srv_conf = addr[i].core_srv_conf; +#if (NGX_HTTP_SSL) + addrs6[i].conf.ssl = addr[i].ssl; #endif + + if (addr[i].hash.buckets == NULL + && (addr[i].wc_head == NULL + || addr[i].wc_head->hash.buckets == NULL) + && (addr[i].wc_head == NULL + || addr[i].wc_head->hash.buckets == NULL)) + { + continue; } - if (done) { - return NGX_OK; + vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t)); + if (vn == NULL) { + return NGX_ERROR; } - in_addr++; - in_port->addrs.elts = in_addr; - last--; + addrs6[i].conf.virtual_names = vn; - a = 0; + vn->names.hash = addr[i].hash; + vn->names.wc_head = addr[i].wc_head; + vn->names.wc_tail = addr[i].wc_tail; +#if (NGX_PCRE) + vn->nregex = addr[i].nregex; + vn->regex = addr[i].regex; +#endif } return NGX_OK; } +#endif + char * ngx_http_types_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c index 3f5cb29dc..a54a1217e 100644 --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -1776,32 +1776,37 @@ ngx_http_server_addr(ngx_http_request_t *r, ngx_str_t *s) { socklen_t len; ngx_connection_t *c; - struct sockaddr_in sin; - - /* AF_INET only */ + struct sockaddr_in *sin; + u_char sa[NGX_SOCKADDRLEN]; c = r->connection; - if (r->in_addr == 0) { - len = sizeof(struct sockaddr_in); - if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) { + if (c->local_sockaddr == NULL) { + + len = NGX_SOCKADDRLEN; + + if (getsockname(c->fd, (struct sockaddr *) &sa, &len) == -1) { ngx_connection_error(c, ngx_socket_errno, "getsockname() failed"); return NGX_ERROR; } - r->in_addr = sin.sin_addr.s_addr; + c->local_sockaddr = ngx_palloc(r->connection->pool, len); + if (c->local_sockaddr == NULL) { + return NGX_ERROR; + } - } else { - sin.sin_family = c->sockaddr->sa_family; - sin.sin_addr.s_addr = r->in_addr; + c->local_socklen = len; + ngx_memcpy(c->local_sockaddr, &sa, len); } + sin = (struct sockaddr_in *) c->local_sockaddr; + r->in_addr = sin->sin_addr.s_addr; + if (s == NULL) { return NGX_OK; } - s->len = ngx_sock_ntop((struct sockaddr *) &sin, s->data, - NGX_INET_ADDRSTRLEN); + s->len = ngx_sock_ntop(c->local_sockaddr, s->data, s->len, 0); return NGX_OK; } @@ -2729,7 +2734,8 @@ ngx_http_core_create_srv_conf(ngx_conf_t *cf) * conf->client_large_buffers.num = 0; */ - if (ngx_array_init(&cscf->listen, cf->pool, 4, sizeof(ngx_http_listen_t)) + if (ngx_array_init(&cscf->listen, cf->temp_pool, 4, + sizeof(ngx_http_listen_t)) == NGX_ERROR) { return NGX_CONF_ERROR; @@ -2761,6 +2767,7 @@ ngx_http_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) ngx_http_core_srv_conf_t *conf = child; ngx_http_listen_t *ls; + struct sockaddr_in *sin; ngx_http_server_name_t *sn; /* TODO: it does not merge, it inits only */ @@ -2773,14 +2780,15 @@ ngx_http_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) ngx_memzero(ls, sizeof(ngx_http_listen_t)); - ls->addr = INADDR_ANY; + sin = (struct sockaddr_in *) &ls->sockaddr; + + sin->sin_family = AF_INET; #if (NGX_WIN32) - ls->port = 80; + sin->sin_port = htons(80); #else - /* STUB: getuid() should be cached */ - ls->port = (getuid() == 0) ? 80 : 8000; + sin->sin_port = htons((getuid() == 0) ? 80 : 8000); #endif - ls->family = AF_INET; + sin->sin_addr.s_addr = INADDR_ANY; ls->conf.backlog = NGX_LISTEN_BACKLOG; ls->conf.rcvbuf = -1; @@ -3159,8 +3167,6 @@ ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) } -/* AF_INET only */ - static char * ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { @@ -3201,17 +3207,18 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_memzero(ls, sizeof(ngx_http_listen_t)); - ls->family = u.family; - ls->addr = u.addr.in_addr; - ls->port = u.port; + ngx_memcpy(ls->sockaddr, u.sockaddr, u.socklen); + + ls->socklen = u.socklen; ls->file_name = cf->conf_file->file.name.data; ls->line = cf->conf_file->line; ls->conf.backlog = NGX_LISTEN_BACKLOG; ls->conf.rcvbuf = -1; ls->conf.sndbuf = -1; + ls->conf.wildcard = u.wildcard; - n = ngx_inet_ntop(AF_INET, &ls->addr, ls->conf.addr, NGX_INET_ADDRSTRLEN); - ngx_sprintf(&ls->conf.addr[n], ":%ui", ls->port); + (void) ngx_sock_ntop((struct sockaddr *) &ls->sockaddr, ls->conf.addr, + NGX_SOCKADDR_STRLEN, 1); if (cf->args->nelts == 2) { return NGX_CONF_OK; diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h index 21a53552e..2fdc1020b 100644 --- a/src/http/ngx_http_core_module.h +++ b/src/http/ngx_http_core_module.h @@ -40,6 +40,7 @@ typedef struct ngx_http_core_loc_conf_s ngx_http_core_loc_conf_t; typedef struct { unsigned default_server:1; unsigned bind:1; + unsigned wildcard:1; #if (NGX_HTTP_SSL) unsigned ssl:1; #endif @@ -55,15 +56,13 @@ typedef struct { ngx_uint_t deferred_accept; #endif - u_char addr[NGX_INET_ADDRSTRLEN + sizeof(":65535")]; - + u_char addr[NGX_SOCKADDR_STRLEN + 1]; } ngx_http_listen_conf_t; typedef struct { - in_addr_t addr; - in_port_t port; - int family; + u_char sockaddr[NGX_SOCKADDRLEN]; + socklen_t socklen; u_char *file_name; ngx_uint_t line; @@ -173,8 +172,6 @@ typedef struct { typedef struct { - in_addr_t addr; - /* the default server configuration for this address:port */ ngx_http_core_srv_conf_t *core_srv_conf; @@ -183,25 +180,45 @@ typedef struct { #if (NGX_HTTP_SSL) ngx_uint_t ssl; /* unsigned ssl:1; */ #endif +} ngx_http_addr_conf_t; + + +typedef struct { + in_addr_t addr; + ngx_http_addr_conf_t conf; } ngx_http_in_addr_t; +#if (NGX_HAVE_INET6) + +typedef struct { + struct in6_addr addr6; + ngx_http_addr_conf_t conf; +} ngx_http_in6_addr_t; + +#endif + + typedef struct { in_port_t port; ngx_str_t port_text; - ngx_http_in_addr_t *addrs; + + /* ngx_http_in_addr_t or ngx_http_in6_addr_t */ + void *addrs; ngx_uint_t naddrs; -} ngx_http_in_port_t; +} ngx_http_port_t; typedef struct { + ngx_int_t family; in_port_t port; - ngx_array_t addrs; /* array of ngx_http_conf_in_addr_t */ -} ngx_http_conf_in_port_t; + ngx_array_t addrs; /* array of ngx_http_conf_addr_t */ +} ngx_http_conf_port_t; typedef struct { - in_addr_t addr; + struct sockaddr *sockaddr; + socklen_t socklen; ngx_hash_t hash; ngx_hash_wildcard_t *wc_head; @@ -219,12 +236,13 @@ typedef struct { unsigned default_server:1; unsigned bind:1; + unsigned wildcard:1; #if (NGX_HTTP_SSL) unsigned ssl:1; #endif ngx_http_listen_conf_t *listen_conf; -} ngx_http_conf_in_addr_t; +} ngx_http_conf_addr_t; struct ngx_http_server_name_s { diff --git a/src/http/ngx_http_header_filter_module.c b/src/http/ngx_http_header_filter_module.c index de9533391..f9476c695 100644 --- a/src/http/ngx_http_header_filter_module.c +++ b/src/http/ngx_http_header_filter_module.c @@ -161,8 +161,7 @@ ngx_http_header_filter(ngx_http_request_t *r) ngx_table_elt_t *header; ngx_http_core_loc_conf_t *clcf; ngx_http_core_srv_conf_t *cscf; - /* AF_INET only */ - u_char addr[NGX_INET_ADDRSTRLEN]; + u_char addr[NGX_SOCKADDR_STRLEN]; r->header_sent = 1; @@ -290,6 +289,7 @@ ngx_http_header_filter(ngx_http_request_t *r) host = r->headers_in.server; } else { + host.len = NGX_SOCKADDR_STRLEN; host.data = addr; if (ngx_http_server_addr(r, &host) != NGX_OK) { diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c index 3a1c5f700..f03b574f9 100644 --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c @@ -232,13 +232,19 @@ ngx_http_init_request(ngx_event_t *rev) ngx_uint_t i; ngx_connection_t *c; ngx_http_request_t *r; - ngx_http_in_port_t *hip; - ngx_http_in_addr_t *hia; + struct sockaddr_in *sin; + ngx_http_port_t *port; + ngx_http_in_addr_t *addr; ngx_http_log_ctx_t *ctx; + ngx_http_addr_conf_t *addr_conf; ngx_http_connection_t *hc; ngx_http_core_srv_conf_t *cscf; ngx_http_core_loc_conf_t *clcf; ngx_http_core_main_conf_t *cmcf; +#if (NGX_HAVE_INET6) + struct sockaddr_in6 *sin6; + ngx_http_in6_addr_t *addr6; +#endif #if (NGX_STAT_STUB) ngx_atomic_fetch_add(ngx_stat_reading, -1); @@ -292,58 +298,90 @@ ngx_http_init_request(ngx_event_t *rev) /* find the server configuration for the address:port */ - /* AF_INET only */ - - hip = c->listening->servers; - hia = hip->addrs; - - r->port = hip->port; - r->port_text = &hip->port_text; + port = c->listening->servers; - i = 0; + r->port = port->port; + r->port_text = &port->port_text; r->connection = c; - if (hip->naddrs > 1) { + if (port->naddrs > 1) { /* - * There are several addresses on this port and one of them - * is the "*:port" wildcard so getsockname() is needed to determine - * the server address. - * - * AcceptEx() already has given this address. + * there are several addresses on this port and one of them + * is an "*:port" wildcard so getsockname() in ngx_http_server_addr() + * is required to determine a server address */ -#if (NGX_WIN32) - if (c->local_sockaddr) { - r->in_addr = - ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr; + c->local_sockaddr = NULL; - } else -#endif - { - if (ngx_http_server_addr(r, NULL) != NGX_OK) { - ngx_http_close_connection(c); - return; - } + if (ngx_http_server_addr(r, NULL) != NGX_OK) { + ngx_http_close_connection(c); + return; } - /* the last address is "*" */ + switch (c->local_sockaddr->sa_family) { - for ( /* void */ ; i < hip->naddrs - 1; i++) { - if (hia[i].addr == r->in_addr) { - break; +#if (NGX_HAVE_INET6) + case AF_INET6: + sin6 = (struct sockaddr_in6 *) c->local_sockaddr; + + addr6 = (ngx_http_in6_addr_t *) port->addrs; + + /* the last address is "*" */ + + for (i = 0; i < port->naddrs - 1; i++) { + if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) { + break; + } } + + addr_conf = &addr6[i].conf; + + break; +#endif + + default: /* AF_INET */ + sin = (struct sockaddr_in *) c->local_sockaddr; + + addr = port->addrs; + + /* the last address is "*" */ + + for (i = 0; i < port->naddrs - 1; i++) { + if (addr[i].addr == sin->sin_addr.s_addr) { + break; + } + } + + addr_conf = &addr[i].conf; + + break; } } else { - r->in_addr = hia[0].addr; + + switch (c->local_sockaddr->sa_family) { + +#if (NGX_HAVE_INET6) + case AF_INET6: + addr6 = (ngx_http_in6_addr_t *) port->addrs; + addr_conf = &addr6[0].conf; + break; +#endif + + default: /* AF_INET */ + addr = port->addrs; + addr_conf = &addr[0].conf; + r->in_addr = addr[0].addr; + break; + } } - r->virtual_names = hia[i].virtual_names; + r->virtual_names = addr_conf->virtual_names; /* the default server configuration for the address:port */ - cscf = hia[i].core_srv_conf; + cscf = addr_conf->core_srv_conf; r->main_conf = cscf->ctx->main_conf; r->srv_conf = cscf->ctx->srv_conf; @@ -357,13 +395,13 @@ ngx_http_init_request(ngx_event_t *rev) ngx_http_ssl_srv_conf_t *sscf; sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module); - if (sscf->enable || hia[i].ssl) { + if (sscf->enable || addr_conf->ssl) { if (c->ssl == NULL) { c->log->action = "SSL handshaking"; - if (hia[i].ssl && sscf->ssl.ctx == NULL) { + if (addr_conf->ssl && sscf->ssl.ctx == NULL) { ngx_log_error(NGX_LOG_ERR, c->log, 0, "no \"ssl_certificate\" is defined " "in server listening on SSL port"); diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c index 98fef371b..81c8528fe 100644 --- a/src/http/ngx_http_variables.c +++ b/src/http/ngx_http_variables.c @@ -828,17 +828,37 @@ static ngx_int_t ngx_http_variable_binary_remote_addr(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { - struct sockaddr_in *sin; + struct sockaddr_in *sin; +#if (NGX_HAVE_INET6) + struct sockaddr_in6 *sin6; +#endif - /* AF_INET only */ + switch (r->connection->sockaddr->sa_family) { - sin = (struct sockaddr_in *) r->connection->sockaddr; +#if (NGX_HAVE_INET6) + case AF_INET6: + sin6 = (struct sockaddr_in6 *) r->connection->sockaddr; - v->len = sizeof(in_addr_t); - v->valid = 1; - v->no_cacheable = 0; - v->not_found = 0; - v->data = (u_char *) &sin->sin_addr.s_addr; + v->len = sizeof(struct in6_addr); + v->valid = 1; + v->no_cacheable = 0; + v->not_found = 0; + v->data = (u_char *) &sin6->sin6_addr; + + break; +#endif + + default: /* AF_INET */ + sin = (struct sockaddr_in *) r->connection->sockaddr; + + v->len = sizeof(in_addr_t); + v->valid = 1; + v->no_cacheable = 0; + v->not_found = 0; + v->data = (u_char *) &sin->sin_addr; + + break; + } return NGX_OK; } @@ -862,8 +882,11 @@ static ngx_int_t ngx_http_variable_remote_port(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { - ngx_uint_t port; - struct sockaddr_in *sin; + ngx_uint_t port; + struct sockaddr_in *sin; +#if (NGX_HAVE_INET6) + struct sockaddr_in6 *sin6; +#endif v->len = 0; v->valid = 1; @@ -875,16 +898,23 @@ ngx_http_variable_remote_port(ngx_http_request_t *r, return NGX_ERROR; } - /* AF_INET only */ + switch (r->connection->sockaddr->sa_family) { - if (r->connection->sockaddr->sa_family == AF_INET) { - sin = (struct sockaddr_in *) r->connection->sockaddr; +#if (NGX_HAVE_INET6) + case AF_INET6: + sin6 = (struct sockaddr_in6 *) r->connection->sockaddr; + port = ntohs(sin6->sin6_port); + break; +#endif + default: /* AF_INET */ + sin = (struct sockaddr_in *) r->connection->sockaddr; port = ntohs(sin->sin_port); + break; + } - if (port > 0 && port < 65536) { - v->len = ngx_sprintf(v->data, "%ui", port) - v->data; - } + if (port > 0 && port < 65536) { + v->len = ngx_sprintf(v->data, "%ui", port) - v->data; } return NGX_OK; @@ -896,16 +926,22 @@ ngx_http_variable_server_addr(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { ngx_str_t s; + u_char addr[NGX_SOCKADDR_STRLEN]; - s.data = ngx_pnalloc(r->pool, NGX_INET_ADDRSTRLEN); - if (s.data == NULL) { + s.len = NGX_SOCKADDR_STRLEN; + s.data = addr; + + if (ngx_http_server_addr(r, &s) != NGX_OK) { return NGX_ERROR; } - if (ngx_http_server_addr(r, &s) != NGX_OK) { + s.data = ngx_pnalloc(r->pool, s.len); + if (s.data == NULL) { return NGX_ERROR; } + ngx_memcpy(s.data, addr, s.len); + v->len = s.len; v->valid = 1; v->no_cacheable = 0; |
