Klaus Demo nginx / a35eacc
a prelimiary IPv6 support, HTTP listen Igor Sysoev 13 years ago
21 changed file(s) with 1144 addition(s) and 502 deletion(s). Raw diff Collapse all Expand all
4141 EVENT_AIO=NO
4242
4343 USE_THREADS=NO
44
45 NGX_IPV6=NO
4446
4547 HTTP=YES
4648
159161 #--with-threads=*) USE_THREADS="$value" ;;
160162 #--with-threads) USE_THREADS="pthreads" ;;
161163
164 --with-ipv6) NGX_IPV6=YES ;;
165
162166 --without-http) HTTP=NO ;;
163167 --http-log-path=*) NGX_HTTP_LOG_PATH="$value" ;;
164168 --http-client-body-temp-path=*) NGX_HTTP_CLIENT_TEMP_PATH="$value" ;;
284288 --with-poll_module enable poll module
285289 --without-poll_module disable poll module
286290
291 --with-ipv6 enable ipv6 support
292
287293 --with-http_ssl_module enable ngx_http_ssl_module
288294 --with-http_realip_module enable ngx_http_realip_module
289295 --with-http_addition_module enable ngx_http_addition_module
6363 # syscalls, libc calls and some features
6464
6565
66 if [ $NGX_IPV6 = YES ]; then
67 ngx_feature="AF_INET6"
68 ngx_feature_name="NGX_HAVE_INET6"
69 ngx_feature_run=no
70 ngx_feature_incs="#include <sys/socket.h>
71 #include <netinet/in.h>
72 #include <arpa/inet.h>"
73 ngx_feature_path=
74 ngx_feature_libs=
75 ngx_feature_test="struct sockaddr_in6 sin6;
76 sin6.sin6_family = AF_INET6;"
77 . auto/feature
78 fi
79
80
6681 ngx_feature="setproctitle()"
6782 ngx_feature_name="NGX_HAVE_SETPROCTITLE"
6883 ngx_feature_run=no
5151 ls->type = SOCK_STREAM;
5252 ls->sockaddr = (struct sockaddr *) sin;
5353 ls->socklen = sizeof(struct sockaddr_in);
54 ls->addr = offsetof(struct sockaddr_in, sin_addr);
5554 ls->addr_text_max_len = NGX_INET_ADDRSTRLEN;
5655
5756 return ls;
6463 size_t len;
6564 ngx_uint_t i;
6665 ngx_listening_t *ls;
67 struct sockaddr_in *sin;
6866 socklen_t olen;
6967 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
7068 ngx_err_t err;
9391 continue;
9492 }
9593
96 sin = (struct sockaddr_in *) ls[i].sockaddr;
97
98 if (sin->sin_family != AF_INET) {
94 switch (ls[i].sockaddr->sa_family) {
95
96 #if (NGX_HAVE_INET6)
97 case AF_INET6:
98 ls[i].addr_text_max_len = NGX_INET6_ADDRSTRLEN;
99 break;
100 #endif
101
102 case AF_INET:
103 ls[i].addr_text_max_len = NGX_INET_ADDRSTRLEN;
104 break;
105
106 default:
99107 ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
100108 "the inherited socket #%d has "
101 "unsupported family", ls[i].fd);
109 "an unsupported protocol family", ls[i].fd);
102110 ls[i].ignore = 1;
103111 continue;
104112 }
105113
106 ls[i].addr_text_max_len = NGX_INET_ADDRSTRLEN;
107
108 ls[i].addr_text.data = ngx_pnalloc(cycle->pool,
109 NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1);
114 len = ls[i].addr_text_max_len + sizeof(":65535") - 1;
115
116 ls[i].addr_text.data = ngx_pnalloc(cycle->pool, len);
110117 if (ls[i].addr_text.data == NULL) {
111118 return NGX_ERROR;
112119 }
113120
114 len = ngx_sock_ntop(ls[i].sockaddr, ls[i].addr_text.data,
115 NGX_INET_ADDRSTRLEN);
121 len = ngx_sock_ntop(ls[i].sockaddr, ls[i].addr_text.data, len, 1);
116122 if (len == 0) {
117123 return NGX_ERROR;
118124 }
119125
120 ls[i].addr_text.len = ngx_sprintf(ls[i].addr_text.data + len, ":%d",
121 ntohs(sin->sin_port))
122 - ls[i].addr_text.data;
126 ls[i].addr_text.len = len;
123127
124128 ls[i].backlog = NGX_LISTEN_BACKLOG;
125129
1818
1919 struct sockaddr *sockaddr;
2020 socklen_t socklen; /* size of sockaddr */
21 size_t addr; /* offset to address in sockaddr */
2221 size_t addr_text_max_len;
2322 ngx_str_t addr_text;
2423
122121 ngx_ssl_connection_t *ssl;
123122 #endif
124123
125 #if (NGX_HAVE_IOCP)
126124 struct sockaddr *local_sockaddr;
127125 socklen_t local_socklen;
128 #endif
129126
130127 ngx_buf_t *buffer;
131128
875875 static ngx_int_t
876876 ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2)
877877 {
878 struct sockaddr_in *sin1, *sin2;
879
880 /* AF_INET only */
881
882 if (sa1->sa_family != AF_INET || sa2->sa_family != AF_INET) {
878 struct sockaddr_in *sin1, *sin2;
879 #if (NGX_HAVE_INET6)
880 struct sockaddr_in6 *sin61, *sin62;
881 #endif
882
883 if (sa1->sa_family != sa2->sa_family) {
883884 return NGX_DECLINED;
884885 }
885886
886 sin1 = (struct sockaddr_in *) sa1;
887 sin2 = (struct sockaddr_in *) sa2;
888
889 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
890 return NGX_DECLINED;
891 }
892
893 if (sin1->sin_port != sin2->sin_port) {
894 return NGX_DECLINED;
887 switch (sa1->sa_family) {
888
889 #if (NGX_HAVE_INET6)
890 case AF_INET6:
891 sin61 = (struct sockaddr_in6 *) sa1;
892 sin62 = (struct sockaddr_in6 *) sa2;
893
894 if (sin61->sin6_port != sin61->sin6_port) {
895 return NGX_DECLINED;
896 }
897
898 if (ngx_memcmp(&sin61->sin6_addr, &sin62->sin6_addr, 16) != 0) {
899 return NGX_DECLINED;
900 }
901
902 break;
903 #endif
904
905 default: /* AF_INET */
906
907 sin1 = (struct sockaddr_in *) sa1;
908 sin2 = (struct sockaddr_in *) sa2;
909
910 if (sin1->sin_port != sin2->sin_port) {
911 return NGX_DECLINED;
912 }
913
914 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
915 return NGX_DECLINED;
916 }
917
918 break;
895919 }
896920
897921 return NGX_OK;
77 #include <ngx_core.h>
88
99
10 #if (NGX_HAVE_INET6)
11 static size_t ngx_inet6_ntop(u_char *p, u_char *text, size_t len);
12 #endif
1013 static ngx_int_t ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u);
1114 static ngx_int_t ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u);
12
13
14 /* AF_INET only */
15 static ngx_int_t ngx_parse_inet6_url(ngx_pool_t *pool, ngx_url_t *u);
16
1517
1618 in_addr_t
1719 ngx_inet_addr(u_char *text, size_t len)
5658 }
5759
5860
59 /* AF_INET only */
60
6161 size_t
62 ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len)
63 {
64 u_char *p;
65 struct sockaddr_in *sin;
66
67 if (sa->sa_family == AF_INET) {
62 ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len, ngx_uint_t port)
63 {
64 u_char *p;
65 struct sockaddr_in *sin;
66 #if (NGX_HAVE_INET6)
67 size_t n;
68 struct sockaddr_in6 *sin6;
69 #endif
70
71 switch (sa->sa_family) {
72
73 case AF_INET:
6874
6975 sin = (struct sockaddr_in *) sa;
7076 p = (u_char *) &sin->sin_addr;
77
78 if (port) {
79 p = ngx_snprintf(text, len, "%ud.%ud.%ud.%ud:%d",
80 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
81 } else {
82 p = ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
83 p[0], p[1], p[2], p[3]);
84 }
85
86 return (p - text);
87
88 #if (NGX_HAVE_INET6)
89
90 case AF_INET6:
91
92 sin6 = (struct sockaddr_in6 *) sa;
93
94 n = 0;
95
96 if (port) {
97 text[n++] = '[';
98 }
99
100 n = ngx_inet6_ntop((u_char *) &sin6->sin6_addr, &text[n], len);
101
102 if (port) {
103 n = ngx_sprintf(&text[1 + n], "]:%d",
104 ntohs(sin6->sin6_port)) - text;
105 }
106
107 return n;
108 #endif
109
110 default:
111 return 0;
112 }
113 }
114
115
116 size_t
117 ngx_inet_ntop(int family, void *addr, u_char *text, size_t len)
118 {
119 u_char *p;
120
121 switch (family) {
122
123 case AF_INET:
124
125 p = addr;
71126
72127 return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
73128 p[0], p[1], p[2], p[3])
74129 - text;
75 }
76
77 return 0;
78 }
79
80
81 size_t
82 ngx_inet_ntop(int family, void *addr, u_char *text, size_t len)
83 {
84 u_char *p;
85
86 if (family == AF_INET) {
87
88 p = (u_char *) addr;
89
90 return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
91 p[0], p[1], p[2], p[3])
92 - text;
93 }
94
95 return 0;
96 }
130
131 #if (NGX_HAVE_INET6)
132
133 case AF_INET6:
134 return ngx_inet6_ntop(addr, text, len);
135
136 #endif
137
138 default:
139 return 0;
140 }
141 }
142
143
144 #if (NGX_HAVE_INET6)
145
146 static size_t
147 ngx_inet6_ntop(u_char *p, u_char *text, size_t len)
148 {
149 u_char *dst;
150 size_t max, n;
151 ngx_uint_t i, zero, last;
152
153 if (len < NGX_INET6_ADDRSTRLEN) {
154 return 0;
155 }
156
157 zero = (ngx_uint_t) -1;
158 last = (ngx_uint_t) -1;
159 max = 1;
160 n = 0;
161
162 for (i = 0; i < 16; i += 2) {
163
164 if (p[i] || p[i + 1]) {
165
166 if (max < n) {
167 zero = last;
168 max = n;
169 }
170
171 n = 0;
172 continue;
173 }
174
175 if (n++ == 0) {
176 last = i;
177 }
178 }
179
180 if (max < n) {
181 zero = last;
182 max = n;
183 }
184
185 dst = text;
186 n = 16;
187
188 if (zero == 0) {
189
190 if ((max == 5 && p[10] == 0xff && p[11] == 0xff)
191 || (max == 6)
192 || (max == 7 && p[14] != 0 && p[15] != 1))
193 {
194 n = 12;
195 }
196
197 *dst++ = ':';
198 }
199
200 for (i = 0; i < n; i += 2) {
201
202 if (i == zero) {
203 *dst++ = ':';
204 i += (max - 1) * 2;
205 continue;
206 }
207
208 dst = ngx_sprintf(dst, "%uxi", p[i] * 256 + p[i + 1]);
209
210 if (i < 14) {
211 *dst++ = ':';
212 }
213 }
214
215 if (n == 12) {
216 dst = ngx_sprintf(dst, "%ud.%ud.%ud.%ud", p[12], p[13], p[14], p[15]);
217 }
218
219 return dst - text;
220 }
221
222 #endif
97223
98224
99225 /* AF_INET only */
168294 if ((p[0] == ':' || p[0] == '/') && !u->listen) {
169295 u->err = "invalid host";
170296 return NGX_ERROR;
297 }
298
299 if (p[0] == '[') {
300 return ngx_parse_inet6_url(pool, u);
171301 }
172302
173303 return ngx_parse_inet_url(pool, u);
208338
209339 u->host.len = len++;
210340 u->host.data = path;
211 u->family = AF_UNIX;
212341
213342 if (len > sizeof(saun->sun_path)) {
214343 u->err = "too long path in the unix domain socket";
215344 return NGX_ERROR;
216345 }
217346
347 u->socklen = sizeof(struct sockaddr_un);
348 saun = (struct sockaddr_un *) &u->sockaddr;
349 saun->sun_family = AF_UNIX;
350 (void) ngx_cpystrn((u_char *) saun->sun_path, path, len);
351
218352 u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t));
219353 if (u->addrs == NULL) {
220354 return NGX_ERROR;
225359 return NGX_ERROR;
226360 }
227361
362 u->family = AF_UNIX;
228363 u->naddrs = 1;
229364
230365 saun->sun_family = AF_UNIX;
250385 static ngx_int_t
251386 ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)
252387 {
253 u_char *p, *host, *port, *last, *uri, *args;
254 size_t len;
255 ngx_int_t n;
256 struct hostent *h;
388 u_char *p, *host, *port, *last, *uri, *args;
389 size_t len;
390 ngx_int_t n;
391 struct hostent *h;
392 struct sockaddr_in *sin;
393
394 u->socklen = sizeof(struct sockaddr_in);
395 sin = (struct sockaddr_in *) &u->sockaddr;
396 sin->sin_family = AF_INET;
257397
258398 u->family = AF_INET;
259399
310450 }
311451
312452 u->port = (in_port_t) n;
453 sin->sin_port = htons((in_port_t) n);
313454
314455 u->port_text.len = len;
315456 u->port_text.data = port;
333474 }
334475
335476 u->port = (in_port_t) n;
477 sin->sin_port = htons((in_port_t) n);
336478
337479 u->port_text.len = last - host;
338480 u->port_text.data = host;
481
482 u->wildcard = 1;
339483
340484 return NGX_OK;
341485 }
373517 (void) ngx_cpystrn(p, host, len);
374518
375519 u->addr.in_addr = inet_addr((const char *) p);
376
377 if (u->addr.in_addr == INADDR_NONE) {
520 sin->sin_addr.s_addr = inet_addr((const char *) p);
521
522 if (sin->sin_addr.s_addr == INADDR_NONE) {
378523 h = gethostbyname((const char *) p);
379524
380525 if (h == NULL || h->h_addr_list[0] == NULL) {
384529 }
385530
386531 u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
532 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[0]);
533 }
534
535 if (sin->sin_addr.s_addr == INADDR_ANY) {
536 u->wildcard = 1;
387537 }
388538
389539 ngx_free(p);
390540
391541 } else {
392542 u->addr.in_addr = INADDR_ANY;
543 sin->sin_addr.s_addr = INADDR_ANY;
544 u->wildcard = 1;
393545 }
394546
395547 if (u->no_port) {
396548 u->port = u->default_port;
549 sin->sin_port = htons(u->default_port);
397550 }
398551
399552 if (u->listen) {
408561 }
409562
410563
564 static ngx_int_t
565 ngx_parse_inet6_url(ngx_pool_t *pool, ngx_url_t *u)
566 {
567 #if (NGX_HAVE_INET6)
568 int rc;
569 u_char *p, *host, *port, *last, *uri;
570 size_t len;
571 ngx_int_t n;
572 struct sockaddr_in6 *sin6;
573
574 u->socklen = sizeof(struct sockaddr_in6);
575 sin6 = (struct sockaddr_in6 *) &u->sockaddr;
576 sin6->sin6_family = AF_INET6;
577
578 host = u->url.data + 1;
579
580 last = u->url.data + u->url.len;
581
582 p = ngx_strlchr(host, last, ']');
583
584 if (p == NULL) {
585 u->err = "invalid host";
586 return NGX_ERROR;
587 }
588
589 if (last - p) {
590
591 port = p + 1;
592
593 uri = ngx_strlchr(port, last, '/');
594
595 if (uri) {
596 if (u->listen || !u->uri_part) {
597 u->err = "invalid host";
598 return NGX_ERROR;
599 }
600
601 u->uri.len = last - uri;
602 u->uri.data = uri;
603 }
604
605 if (*port == ':') {
606 port++;
607
608 len = last - port;
609
610 if (len == 0) {
611 u->err = "invalid port";
612 return NGX_ERROR;
613 }
614
615 n = ngx_atoi(port, len);
616
617 if (n < 1 || n > 65536) {
618 u->err = "invalid port";
619 return NGX_ERROR;
620 }
621
622 u->port = (in_port_t) n;
623 sin6->sin6_port = htons((in_port_t) n);
624
625 u->port_text.len = len;
626 u->port_text.data = port;
627
628 } else {
629 u->no_port = 1;
630 }
631 }
632
633 len = p - host;
634
635 if (len == 0) {
636 u->err = "no host";
637 return NGX_ERROR;
638 }
639
640 u->host.len = len++;
641 u->host.data = host;
642
643 p = ngx_alloc(len, pool->log);
644 if (p == NULL) {
645 return NGX_ERROR;
646 }
647
648 (void) ngx_cpystrn(p, host, len);
649
650 rc = inet_pton(AF_INET6, (const char *) p, &sin6->sin6_addr);
651
652 ngx_free(p);
653
654 if (rc == 0) {
655 u->err = "invalid IPv6 address";
656 return NGX_ERROR;
657 }
658
659 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
660 u->wildcard = 1;
661 }
662
663 u->family = AF_INET6;
664
665 if (u->no_resolve) {
666 return NGX_OK;
667 }
668
669 if (u->no_port) {
670 u->port = u->default_port;
671 sin6->sin6_port = htons(u->default_port);
672 }
673
674 return NGX_OK;
675
676 #else
677
678 u->err = "the INET6 sockets are not supported on this platform";
679
680 return NGX_ERROR;
681
682 #endif
683 }
684
685
411686 ngx_int_t
412687 ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
413688 {
414689 u_char *p, *host;
415690 size_t len;
691 in_port_t port;
416692 in_addr_t in_addr;
417693 ngx_uint_t i;
418694 struct hostent *h;
427703
428704 /* AF_INET only */
429705
706 sin = (struct sockaddr_in *) &u->sockaddr;
707 port = sin->sin_port;
708
430709 in_addr = inet_addr((char *) host);
431710
432711 if (in_addr == INADDR_NONE) {
463742 }
464743
465744 sin->sin_family = AF_INET;
466 sin->sin_port = htons(u->port);
745 sin->sin_port = port;
467746 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
468747
469748 u->addrs[i].sockaddr = (struct sockaddr *) sin;
470749 u->addrs[i].socklen = sizeof(struct sockaddr_in);
471750
472 len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
751 len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
473752
474753 p = ngx_pnalloc(pool, len);
475754 if (p == NULL) {
476755 return NGX_ERROR;
477756 }
478757
479 len = ngx_sock_ntop((struct sockaddr *) sin, p, len);
480
481 u->addrs[i].name.len = ngx_sprintf(&p[len], ":%d", u->port) - p;
758 len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1);
759
760 u->addrs[i].name.len = len;
482761 u->addrs[i].name.data = p;
483762 }
484763
501780 u->naddrs = 1;
502781
503782 sin->sin_family = AF_INET;
504 sin->sin_port = htons(u->port);
783 sin->sin_port = port;
505784 sin->sin_addr.s_addr = in_addr;
506785
507786 u->addrs[0].sockaddr = (struct sockaddr *) sin;
508787 u->addrs[0].socklen = sizeof(struct sockaddr_in);
509788
510 p = ngx_pnalloc(pool, u->host.len + sizeof(":65536") - 1);
789 p = ngx_pnalloc(pool, u->host.len + sizeof(":65535") - 1);
511790 if (p == NULL) {
512791 return NGX_ERROR;
513792 }
514793
515 u->addrs[0].name.len = ngx_sprintf(p, "%V:%d", &u->host, u->port) - p;
794 u->addrs[0].name.len = ngx_sprintf(p, "%V:%d",
795 &u->host, ntohs(port)) - p;
516796 u->addrs[0].name.data = p;
517797 }
518798
1111 #include <ngx_core.h>
1212
1313
14 #define NGX_INET_ADDRSTRLEN (sizeof("255.255.255.255") - 1)
14 #define NGX_INET_ADDRSTRLEN (sizeof("255.255.255.255") - 1)
15 #define NGX_INET6_ADDRSTRLEN \
16 (sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - 1)
17
18 #define NGX_SOCKADDR_STRLEN (NGX_INET6_ADDRSTRLEN + sizeof(":65535") - 1)
19
20
21 /*
22 * TODO: autoconfigure NGX_SOCKADDRLEN as
23 * sizeof(struct sockaddr_storage)
24 * sizeof(struct sockaddr_in6)
25 * sizeof(struct sockaddr_in)
26 */
27
28 #if (NGX_HAVE_INET6)
29 #define NGX_SOCKADDRLEN sizeof(struct sockaddr_in6)
30 #else
31 #define NGX_SOCKADDRLEN sizeof(struct sockaddr_in)
32 #endif
1533
1634
1735 typedef struct {
4866 unsigned one_addr:1;
4967
5068 unsigned no_port:1;
69 unsigned wildcard:1;
5170
5271 ngx_url_addr_t addr;
72
73 socklen_t socklen;
74 u_char sockaddr[NGX_SOCKADDRLEN];
5375
5476 ngx_peer_addr_t *addrs;
5577 ngx_uint_t naddrs;
5981
6082
6183 in_addr_t ngx_inet_addr(u_char *text, size_t len);
62 size_t ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len);
84 size_t ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len,
85 ngx_uint_t port);
6386 size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len);
6487 ngx_int_t ngx_ptocidr(ngx_str_t *text, void *cidr);
6588 ngx_int_t ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u);
66 #include <ngx_config.h>
77 #include <ngx_core.h>
88 #include <ngx_event.h>
9
10
11 /* the buffer size is enough to hold "struct sockaddr_un" */
12 #define NGX_SOCKLEN 512
139
1410
1511 static ngx_int_t ngx_enable_accept_events(ngx_cycle_t *cycle);
2824 ngx_listening_t *ls;
2925 ngx_connection_t *c, *lc;
3026 ngx_event_conf_t *ecf;
31 char sa[NGX_SOCKLEN];
27 u_char sa[NGX_SOCKADDRLEN];
3228
3329 ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module);
3430
4743 "accept on %V, ready: %d", &ls->addr_text, ev->available);
4844
4945 do {
50 socklen = NGX_SOCKLEN;
46 socklen = NGX_SOCKADDRLEN;
5147
5248 s = accept(lc->fd, (struct sockaddr *) sa, &socklen);
5349
152148 c->log = log;
153149 c->pool->log = log;
154150
151 c->socklen = socklen;
155152 c->listening = ls;
156 c->socklen = socklen;
153 c->local_sockaddr = ls->sockaddr;
154 c->local_socklen = ls->socklen;
157155
158156 c->unexpected_eof = 1;
159157
207205 }
208206
209207 c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->addr_text.data,
210 ls->addr_text_max_len);
208 ls->addr_text_max_len, 0);
211209 if (c->addr_text.len == 0) {
212210 ngx_close_accepted_connection(c);
213211 return;
6565 }
6666
6767 c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->addr_text.data,
68 c->listening->addr_text_max_len);
68 c->listening->addr_text_max_len, 0);
6969 if (c->addr_text.len == 0) {
7070 /* TODO: close socket */
7171 return;
77 #include <ngx_core.h>
88 #include <ngx_http.h>
99
10
11 /* AF_INET only */
1210
1311 typedef struct {
1412 in_addr_t mask;
102100
103101 /* AF_INET only */
104102
103 if (r->connection->sockaddr->sa_family != AF_INET) {
104 return NGX_DECLINED;
105 }
106
105107 sin = (struct sockaddr_in *) r->connection->sockaddr;
106108
107109 rule = alcf->rules->elts;
174174 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
175175 "http geo started: %V", &r->connection->addr_text);
176176
177 if (r->connection->sockaddr->sa_family != AF_INET) {
178 return 0;
179 }
180
177181 sin = (struct sockaddr_in *) r->connection->sockaddr;
178182 return ntohl(sin->sin_addr.s_addr);
179183 }
1212 #define NGX_HTTP_REALIP_XFWD 1
1313 #define NGX_HTTP_REALIP_HEADER 2
1414
15
16 /* AF_INET only */
1715
1816 typedef struct {
1917 in_addr_t mask;
208206
209207 /* AF_INET only */
210208
209 if (r->connection->sockaddr->sa_family != AF_INET) {
210 return NGX_DECLINED;
211 }
212
211213 sin = (struct sockaddr_in *) c->sockaddr;
212214
213215 from = rlcf->from->elts;
1414
1515 ngx_uint_t hash;
1616
17 /* AF_INET only */
1817 u_char addr[3];
1918
2019 u_char tries;
110109 r->upstream->peer.get = ngx_http_upstream_get_ip_hash_peer;
111110
112111 /* AF_INET only */
113 sin = (struct sockaddr_in *) r->connection->sockaddr;
114 p = (u_char *) &sin->sin_addr.s_addr;
115 iphp->addr[0] = p[0];
116 iphp->addr[1] = p[1];
117 iphp->addr[2] = p[2];
112
113 if (r->connection->sockaddr->sa_family == AF_INET) {
114
115 sin = (struct sockaddr_in *) r->connection->sockaddr;
116 p = (u_char *) &sin->sin_addr.s_addr;
117 iphp->addr[0] = p[0];
118 iphp->addr[1] = p[1];
119 iphp->addr[2] = p[2];
120
121 } else {
122 iphp->addr[0] = 0;
123 iphp->addr[1] = 0;
124 iphp->addr[2] = 0;
125 }
118126
119127 iphp->hash = 89;
120128 iphp->tries = 0;
1919 static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf,
2020 ngx_array_t *servers, ngx_array_t *in_ports);
2121 static ngx_int_t ngx_http_add_ports(ngx_conf_t *cf,
22 ngx_http_core_srv_conf_t *cscf, ngx_array_t *in_ports,
22 ngx_http_core_srv_conf_t *cscf, ngx_array_t *ports,
2323 ngx_http_listen_t *listen);
2424 static ngx_int_t ngx_http_add_addresses(ngx_conf_t *cf,
25 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port,
25 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port,
2626 ngx_http_listen_t *listen);
2727 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
28 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port,
28 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_port_t *port,
2929 ngx_http_listen_t *listen);
3030 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
31 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_addr_t *in_addr);
31 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_addr_t *addr);
3232
3333 static char *ngx_http_merge_locations(ngx_conf_t *cf,
3434 ngx_queue_t *locations, void **loc_conf, ngx_http_module_t *module,
4848 size_t prefix);
4949
5050 static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf,
51 ngx_http_core_main_conf_t *cmcf, ngx_array_t *in_ports);
51 ngx_http_core_main_conf_t *cmcf, ngx_array_t *ports);
5252 static ngx_int_t ngx_http_server_names(ngx_conf_t *cf,
53 ngx_http_core_main_conf_t *cmcf, ngx_http_conf_in_addr_t *in_addr);
54 static ngx_int_t ngx_http_cmp_conf_in_addrs(const void *one, const void *two);
53 ngx_http_core_main_conf_t *cmcf, ngx_http_conf_addr_t *addr);
54 static ngx_int_t ngx_http_cmp_conf_addrs(const void *one, const void *two);
5555 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
5656 const void *two);
5757
5858 static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf,
59 ngx_http_conf_in_port_t *in_port);
59 ngx_http_conf_port_t *port);
60 static ngx_listening_t *ngx_http_add_listening(ngx_conf_t *cf,
61 ngx_http_conf_addr_t *addr);
62 static ngx_int_t ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
63 ngx_http_conf_addr_t *addr);
64 #if (NGX_HAVE_INET6)
65 static ngx_int_t ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
66 ngx_http_conf_addr_t *addr);
67 #endif
6068
6169 ngx_uint_t ngx_http_max_module;
6270
358366 * to find quickly the server core module configuration at run-time
359367 */
360368
361 /* AF_INET only */
362
363369 if (ngx_http_init_server_lists(cf, &cmcf->servers, &in_ports) != NGX_OK) {
364370 return NGX_CONF_ERROR;
365371 }
366372
367373
368374 /* optimize the lists of ports, addresses and server names */
369
370 /* AF_INET only */
371375
372376 if (ngx_http_optimize_servers(cf, cmcf, &in_ports) != NGX_OK) {
373377 return NGX_CONF_ERROR;
11061110
11071111 static ngx_int_t
11081112 ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
1109 ngx_array_t *in_ports)
1113 ngx_array_t *ports)
11101114 {
11111115 ngx_uint_t s, i;
11121116 ngx_http_listen_t *listen;
11131117 ngx_http_core_srv_conf_t **cscfp;
11141118
1115 if (ngx_array_init(in_ports, cf->temp_pool, 2,
1116 sizeof(ngx_http_conf_in_port_t))
1119 if (ngx_array_init(ports, cf->temp_pool, 2, sizeof(ngx_http_conf_port_t))
11171120 != NGX_OK)
11181121 {
11191122 return NGX_ERROR;
11291132 listen = cscfp[s]->listen.elts;
11301133 for (i = 0; i < cscfp[s]->listen.nelts; i++) {
11311134
1132 /* AF_INET only */
1133
1134 if (ngx_http_add_ports(cf, cscfp[s], in_ports, &listen[i])
1135 != NGX_OK)
1136 {
1135 if (ngx_http_add_ports(cf, cscfp[s], ports, &listen[i]) != NGX_OK) {
11371136 return NGX_ERROR;
11381137 }
11391138 }
11421141 return NGX_OK;
11431142 }
11441143
1145
1146 /* AF_INET only */
11471144
11481145 static ngx_int_t
11491146 ngx_http_add_ports(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1150 ngx_array_t *in_ports, ngx_http_listen_t *listen)
1151 {
1147 ngx_array_t *ports, ngx_http_listen_t *listen)
1148 {
1149 in_port_t p;
11521150 ngx_uint_t i;
1153 ngx_http_conf_in_port_t *in_port;
1154
1155 in_port = in_ports->elts;
1156 for (i = 0; i < in_ports->nelts; i++) {
1157
1158 if (listen->port != in_port[i].port) {
1151 struct sockaddr *sa;
1152 struct sockaddr_in *sin;
1153 ngx_http_conf_port_t *port;
1154 #if (NGX_HAVE_INET6)
1155 struct sockaddr_in6 *sin6;
1156 #endif
1157
1158 sa = (struct sockaddr *) &listen->sockaddr;
1159
1160 switch (sa->sa_family) {
1161
1162 #if (NGX_HAVE_INET6)
1163 case AF_INET6:
1164 sin6 = (struct sockaddr_in6 *) sa;
1165 p = sin6->sin6_port;
1166 break;
1167 #endif
1168
1169 default: /* AF_INET */
1170 sin = (struct sockaddr_in *) sa;
1171 p = sin->sin_port;
1172 break;
1173 }
1174
1175 port = ports->elts;
1176 for (i = 0; i < ports->nelts; i++) {
1177
1178 if (p != port[i].port || sa->sa_family != port[i].family) {
11591179 continue;
11601180 }
11611181
11621182 /* a port is already in the in_port list */
11631183
1164 return ngx_http_add_addresses(cf, cscf, &in_port[i], listen);
1184 return ngx_http_add_addresses(cf, cscf, &port[i], listen);
11651185 }
11661186
11671187 /* add a port to the in_port list */
11681188
1169 in_port = ngx_array_push(in_ports);
1170 if (in_port == NULL) {
1189 port = ngx_array_push(ports);
1190 if (port == NULL) {
11711191 return NGX_ERROR;
11721192 }
11731193
1174 in_port->port = listen->port;
1175 in_port->addrs.elts = NULL;
1176
1177 return ngx_http_add_address(cf, cscf, in_port, listen);
1194 port->family = sa->sa_family;
1195 port->port = p;
1196 port->addrs.elts = NULL;
1197
1198 return ngx_http_add_address(cf, cscf, port, listen);
11781199 }
11791200
11801201
11811202 static ngx_int_t
11821203 ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1183 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen)
1184 {
1185 ngx_uint_t i;
1186 ngx_http_conf_in_addr_t *in_addr;
1187
1188 in_addr = in_port->addrs.elts;
1189
1190 for (i = 0; i < in_port->addrs.nelts; i++) {
1191
1192 if (listen->addr != in_addr[i].addr) {
1204 ngx_http_conf_port_t *port, ngx_http_listen_t *listen)
1205 {
1206 u_char *p;
1207 size_t len, off;
1208 ngx_uint_t i;
1209 struct sockaddr *sa;
1210 ngx_http_conf_addr_t *addr;
1211
1212 /*
1213 * we can not compare whole sockaddr struct's as kernel
1214 * may fill some fields in inherited sockaddr struct's
1215 */
1216
1217 sa = (struct sockaddr *) &listen->sockaddr;
1218
1219 switch (sa->sa_family) {
1220
1221 #if (NGX_HAVE_INET6)
1222 case AF_INET6:
1223 off = offsetof(struct sockaddr_in6, sin6_addr);
1224 len = 16;
1225 break;
1226 #endif
1227
1228 default: /* AF_INET */
1229 off = offsetof(struct sockaddr_in, sin_addr);
1230 len = 4;
1231 break;
1232 }
1233
1234 p = listen->sockaddr + off;
1235
1236 addr = port->addrs.elts;
1237
1238 for (i = 0; i < port->addrs.nelts; i++) {
1239
1240 if (ngx_memcmp(p, (u_char *) addr[i].sockaddr + off, len) != 0) {
11931241 continue;
11941242 }
11951243
11961244 /* the address is already in the address list */
11971245
1198 if (ngx_http_add_names(cf, cscf, &in_addr[i]) != NGX_OK) {
1246 if (ngx_http_add_names(cf, cscf, &addr[i]) != NGX_OK) {
11991247 return NGX_ERROR;
12001248 }
12011249
1202 /*
1203 * check the duplicate "default" server
1204 * for this address:port
1205 */
1250 /* check the duplicate "default" server for this address:port */
12061251
12071252 if (listen->conf.default_server) {
12081253
1209 if (in_addr[i].default_server) {
1254 if (addr[i].default_server) {
12101255 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
12111256 "the duplicate default server in %s:%ui",
12121257 listen->file_name, listen->line);
12141259 return NGX_ERROR;
12151260 }
12161261
1217 in_addr[i].core_srv_conf = cscf;
1218 in_addr[i].default_server = 1;
1262 addr[i].core_srv_conf = cscf;
1263 addr[i].default_server = 1;
12191264 #if (NGX_HTTP_SSL)
1220 in_addr[i].ssl = listen->conf.ssl;
1221 #endif
1222 in_addr[i].listen_conf = &listen->conf;
1265 addr[i].ssl = listen->conf.ssl;
1266 #endif
1267 addr[i].listen_conf = &listen->conf;
12231268 }
12241269
12251270 return NGX_OK;
12261271 }
12271272
1228 /*
1229 * add the address to the addresses list that
1230 * bound to this port
1231 */
1232
1233 return ngx_http_add_address(cf, cscf, in_port, listen);
1273 /* add the address to the addresses list that bound to this port */
1274
1275 return ngx_http_add_address(cf, cscf, port, listen);
12341276 }
12351277
12361278
12411283
12421284 static ngx_int_t
12431285 ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1244 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen)
1245 {
1246 ngx_http_conf_in_addr_t *in_addr;
1247
1248 if (in_port->addrs.elts == NULL) {
1249 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 4,
1250 sizeof(ngx_http_conf_in_addr_t))
1286 ngx_http_conf_port_t *port, ngx_http_listen_t *listen)
1287 {
1288 ngx_http_conf_addr_t *addr;
1289
1290 if (port->addrs.elts == NULL) {
1291 if (ngx_array_init(&port->addrs, cf->temp_pool, 4,
1292 sizeof(ngx_http_conf_addr_t))
12511293 != NGX_OK)
12521294 {
12531295 return NGX_ERROR;
12541296 }
12551297 }
12561298
1257 in_addr = ngx_array_push(&in_port->addrs);
1258 if (in_addr == NULL) {
1299 addr = ngx_array_push(&port->addrs);
1300 if (addr == NULL) {
12591301 return NGX_ERROR;
12601302 }
12611303
1262 in_addr->addr = listen->addr;
1263 in_addr->hash.buckets = NULL;
1264 in_addr->hash.size = 0;
1265 in_addr->wc_head = NULL;
1266 in_addr->wc_tail = NULL;
1267 in_addr->names.elts = NULL;
1304 addr->sockaddr = (struct sockaddr *) &listen->sockaddr;
1305 addr->socklen = listen->socklen;
1306 addr->hash.buckets = NULL;
1307 addr->hash.size = 0;
1308 addr->wc_head = NULL;
1309 addr->wc_tail = NULL;
1310 addr->names.elts = NULL;
12681311 #if (NGX_PCRE)
1269 in_addr->nregex = 0;
1270 in_addr->regex = NULL;
1271 #endif
1272 in_addr->core_srv_conf = cscf;
1273 in_addr->default_server = listen->conf.default_server;
1274 in_addr->bind = listen->conf.bind;
1312 addr->nregex = 0;
1313 addr->regex = NULL;
1314 #endif
1315 addr->core_srv_conf = cscf;
1316 addr->default_server = listen->conf.default_server;
1317 addr->bind = listen->conf.bind;
1318 addr->wildcard = listen->conf.wildcard;
12751319 #if (NGX_HTTP_SSL)
1276 in_addr->ssl = listen->conf.ssl;
1277 #endif
1278 in_addr->listen_conf = &listen->conf;
1279
1280 return ngx_http_add_names(cf, cscf, in_addr);
1320 addr->ssl = listen->conf.ssl;
1321 #endif
1322 addr->listen_conf = &listen->conf;
1323
1324 return ngx_http_add_names(cf, cscf, addr);
12811325 }
12821326
12831327
12841328 /*
12851329 * add the server names and the server core module
1286 * configurations to the address:port (in_addr)
1330 * configurations to the address:port
12871331 */
12881332
12891333 static ngx_int_t
12901334 ngx_http_add_names(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
1291 ngx_http_conf_in_addr_t *in_addr)
1335 ngx_http_conf_addr_t *addr)
12921336 {
12931337 ngx_uint_t i;
12941338 ngx_http_server_name_t *server_names, *name;
12951339
1296 if (in_addr->names.elts == NULL) {
1297 if (ngx_array_init(&in_addr->names, cf->temp_pool, 4,
1340 if (addr->names.elts == NULL) {
1341 if (ngx_array_init(&addr->names, cf->temp_pool, 4,
12981342 sizeof(ngx_http_server_name_t))
12991343 != NGX_OK)
13001344 {
13121356 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
13131357 "name: %V", &server_names[i].name);
13141358
1315 name = ngx_array_push(&in_addr->names);
1359 name = ngx_array_push(&addr->names);
13161360 if (name == NULL) {
13171361 return NGX_ERROR;
13181362 }
13261370
13271371 static ngx_int_t
13281372 ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1329 ngx_array_t *in_ports)
1373 ngx_array_t *ports)
13301374 {
13311375 ngx_uint_t s, p, a;
1376 ngx_http_conf_port_t *port;
1377 ngx_http_conf_addr_t *addr;
13321378 ngx_http_server_name_t *name;
1333 ngx_http_conf_in_port_t *in_port;
1334 ngx_http_conf_in_addr_t *in_addr;
1335
1336 in_port = in_ports->elts;
1337 for (p = 0; p < in_ports->nelts; p++) {
1338
1339 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts,
1340 sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs);
1379
1380 port = ports->elts;
1381 for (p = 0; p < ports->nelts; p++) {
1382
1383 ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts,
1384 sizeof(ngx_http_conf_addr_t), ngx_http_cmp_conf_addrs);
13411385
13421386 /*
13431387 * check whether all name-based servers have the same
13441388 * configuraiton as a default server for given address:port
13451389 */
13461390
1347 in_addr = in_port[p].addrs.elts;
1348 for (a = 0; a < in_port[p].addrs.nelts; a++) {
1349
1350 name = in_addr[a].names.elts;
1351 for (s = 0; s < in_addr[a].names.nelts; s++) {
1352
1353 if (in_addr[a].core_srv_conf == name[s].core_srv_conf) {
1391 addr = port[p].addrs.elts;
1392 for (a = 0; a < port[p].addrs.nelts; a++) {
1393
1394 name = addr[a].names.elts;
1395 for (s = 0; s < addr[a].names.nelts; s++) {
1396
1397 if (addr[a].core_srv_conf == name[s].core_srv_conf) {
13541398 continue;
13551399 }
13561400
1357 if (ngx_http_server_names(cf, cmcf, &in_addr[a]) != NGX_OK) {
1401 if (ngx_http_server_names(cf, cmcf, &addr[a]) != NGX_OK) {
13581402 return NGX_ERROR;
13591403 }
13601404
13621406 }
13631407 }
13641408
1365 if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) {
1409 if (ngx_http_init_listening(cf, &port[p]) != NGX_OK) {
13661410 return NGX_ERROR;
13671411 }
13681412 }
13731417
13741418 static ngx_int_t
13751419 ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
1376 ngx_http_conf_in_addr_t *in_addr)
1420 ngx_http_conf_addr_t *addr)
13771421 {
13781422 ngx_int_t rc;
13791423 ngx_uint_t s;
13991443 goto failed;
14001444 }
14011445
1402 name = in_addr->names.elts;
1403
1404 for (s = 0; s < in_addr->names.nelts; s++) {
1446 name = addr->names.elts;
1447
1448 for (s = 0; s < addr->names.nelts; s++) {
14051449
14061450 #if (NGX_PCRE)
14071451 if (name[s].regex) {
14201464 if (rc == NGX_DECLINED) {
14211465 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
14221466 "invalid server name or wildcard \"%V\" on %s",
1423 &name[s].name, in_addr->listen_conf->addr);
1467 &name[s].name, addr->listen_conf->addr);
14241468 return NGX_ERROR;
14251469 }
14261470
14271471 if (rc == NGX_BUSY) {
14281472 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
14291473 "conflicting server name \"%V\" on %s, ignored",
1430 &name[s].name, in_addr->listen_conf->addr);
1474 &name[s].name, addr->listen_conf->addr);
14311475 }
14321476 }
14331477
14381482 hash.pool = cf->pool;
14391483
14401484 if (ha.keys.nelts) {
1441 hash.hash = &in_addr->hash;
1485 hash.hash = &addr->hash;
14421486 hash.temp_pool = NULL;
14431487
14441488 if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) {
14611505 goto failed;
14621506 }
14631507
1464 in_addr->wc_head = (ngx_hash_wildcard_t *) hash.hash;
1508 addr->wc_head = (ngx_hash_wildcard_t *) hash.hash;
14651509 }
14661510
14671511 if (ha.dns_wc_tail.nelts) {
14791523 goto failed;
14801524 }
14811525
1482 in_addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash;
1526 addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash;
14831527 }
14841528
14851529 ngx_destroy_pool(ha.temp_pool);
14901534 return NGX_OK;
14911535 }
14921536
1493 in_addr->nregex = regex;
1494 in_addr->regex = ngx_palloc(cf->pool,
1495 regex * sizeof(ngx_http_server_name_t));
1496 if (in_addr->regex == NULL) {
1537 addr->nregex = regex;
1538 addr->regex = ngx_palloc(cf->pool, regex * sizeof(ngx_http_server_name_t));
1539 if (addr->regex == NULL) {
14971540 return NGX_ERROR;
14981541 }
14991542
1500 for (i = 0, s = 0; s < in_addr->names.nelts; s++) {
1543 for (i = 0, s = 0; s < addr->names.nelts; s++) {
15011544 if (name[s].regex) {
1502 in_addr->regex[i++] = name[s];
1545 addr->regex[i++] = name[s];
15031546 }
15041547 }
15051548
15161559
15171560
15181561 static ngx_int_t
1519 ngx_http_cmp_conf_in_addrs(const void *one, const void *two)
1520 {
1521 ngx_http_conf_in_addr_t *first, *second;
1522
1523 first = (ngx_http_conf_in_addr_t *) one;
1524 second = (ngx_http_conf_in_addr_t *) two;
1525
1526 if (first->addr == INADDR_ANY) {
1527 /* the INADDR_ANY must be the last resort, shift it to the end */
1562 ngx_http_cmp_conf_addrs(const void *one, const void *two)
1563 {
1564 ngx_http_conf_addr_t *first, *second;
1565
1566 first = (ngx_http_conf_addr_t *) one;
1567 second = (ngx_http_conf_addr_t *) two;
1568
1569 if (first->wildcard) {
1570 /* a wildcard address must be the last resort, shift it to the end */
15281571 return 1;
15291572 }
15301573
15571600
15581601
15591602 static ngx_int_t
1560 ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_in_port_t *in_port)
1561 {
1562 ngx_uint_t i, a, last, bind_all, done;
1603 ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_port_t *port)
1604 {
1605 ngx_uint_t i, a, last, bind_wildcard;
15631606 ngx_listening_t *ls;
1564 ngx_http_in_port_t *hip;
1565 ngx_http_conf_in_addr_t *in_addr;
1566 ngx_http_virtual_names_t *vn;
1607 ngx_http_port_t *hport;
1608 ngx_http_conf_addr_t *addr;
1609
1610 addr = port->addrs.elts;
1611 last = port->addrs.nelts;
1612
1613 /*
1614 * If there is a binding to an "*:port" then we need to bind() to
1615 * the "*:port" only and ignore other implicit bindings. The bindings
1616 * have been already sorted: explicit bindings are on the start, then
1617 * implicit bindings go, and wildcard binding is in the end.
1618 */
1619
1620 if (addr[last - 1].wildcard) {
1621 addr[last - 1].bind = 1;
1622 bind_wildcard = 1;
1623
1624 } else {
1625 bind_wildcard = 0;
1626 }
1627
1628 a = 0;
1629
1630 while (a < last) {
1631
1632 if (bind_wildcard && !addr[a].bind) {
1633 a++;
1634 continue;
1635 }
1636
1637 ls = ngx_http_add_listening(cf, &addr[a]);
1638 if (ls == NULL) {
1639 return NGX_ERROR;
1640 }
1641
1642 hport = ngx_pcalloc(cf->pool, sizeof(ngx_http_port_t));
1643 if (hport == NULL) {
1644 return NGX_ERROR;
1645 }
1646
1647 ls->servers = hport;
1648
1649 hport->port = ntohs(port->port);
1650
1651 for (i = ls->addr_text.len - 1; i; i--) {
1652
1653 if (ls->addr_text.data[i] == ':') {
1654 hport->port_text.len = ls->addr_text.len - i;
1655 hport->port_text.data = &ls->addr_text.data[i];
1656 break;
1657 }
1658 }
1659
1660 if (a == last - 1) {
1661 hport->naddrs = last;
1662
1663 } else {
1664 hport->naddrs = 1;
1665 a = 0;
1666 }
1667
1668 switch (ls->sockaddr->sa_family) {
1669
1670 #if (NGX_HAVE_INET6)
1671 case AF_INET6:
1672 if (ngx_http_add_addrs6(cf, hport, addr) != NGX_OK) {
1673 return NGX_ERROR;
1674 }
1675 break;
1676 #endif
1677 default: /* AF_INET */
1678 if (ngx_http_add_addrs(cf, hport, addr) != NGX_OK) {
1679 return NGX_ERROR;
1680 }
1681 break;
1682 }
1683
1684 addr++;
1685 last--;
1686 }
1687
1688 return NGX_OK;
1689 }
1690
1691
1692 static ngx_listening_t *
1693 ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
1694 {
1695 ngx_listening_t *ls;
1696 struct sockaddr *sa;
15671697 ngx_http_core_loc_conf_t *clcf;
15681698 ngx_http_core_srv_conf_t *cscf;
1569
1570 in_addr = in_port->addrs.elts;
1571 last = in_port->addrs.nelts;
1572
1573 /*
1574 * if there is a binding to a "*:port" then we need to bind()
1575 * to the "*:port" only and ignore other bindings
1576 */
1577
1578 if (in_addr[last - 1].addr == INADDR_ANY) {
1579 in_addr[last - 1].bind = 1;
1580 bind_all = 0;
1581
1582 } else {
1583 bind_all = 1;
1584 }
1585
1586 a = 0;
1587
1588 while (a < last) {
1589
1590 if (!bind_all && !in_addr[a].bind) {
1591 a++;
1699 u_char text[NGX_SOCKADDR_STRLEN];
1700
1701 ls = ngx_array_push(&cf->cycle->listening);
1702 if (ls == NULL) {
1703 return NULL;
1704 }
1705
1706 ngx_memzero(ls, sizeof(ngx_listening_t));
1707
1708 sa = ngx_palloc(cf->pool, addr->socklen);
1709 if (sa == NULL) {
1710 return NULL;
1711 }
1712
1713 ngx_memcpy(sa, addr->sockaddr, addr->socklen);
1714
1715 ls->sockaddr = sa;
1716 ls->socklen = addr->socklen;
1717
1718 ls->addr_text.len = ngx_sock_ntop(sa, text, NGX_SOCKADDR_STRLEN, 1);
1719
1720 ls->addr_text.data = ngx_pnalloc(cf->pool, ls->addr_text.len);
1721 if (ls->addr_text.data == NULL) {
1722 return NULL;
1723 }
1724
1725 ngx_memcpy(ls->addr_text.data, text, ls->addr_text.len);
1726
1727 ls->fd = (ngx_socket_t) -1;
1728 ls->type = SOCK_STREAM;
1729
1730 switch (ls->sockaddr->sa_family) {
1731 #if (NGX_HAVE_INET6)
1732 case AF_INET6:
1733 ls->addr_text_max_len = NGX_INET6_ADDRSTRLEN;
1734 break;
1735 #endif
1736 case AF_INET:
1737 ls->addr_text_max_len = NGX_INET_ADDRSTRLEN;
1738 break;
1739 default:
1740 ls->addr_text_max_len = NGX_SOCKADDR_STRLEN;
1741 break;
1742 }
1743
1744 ls->addr_ntop = 1;
1745
1746 ls->handler = ngx_http_init_connection;
1747
1748 cscf = addr->core_srv_conf;
1749 ls->pool_size = cscf->connection_pool_size;
1750 ls->post_accept_timeout = cscf->client_header_timeout;
1751
1752 clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
1753
1754 ls->log = *clcf->err_log;
1755 ls->log.data = &ls->addr_text;
1756 ls->log.handler = ngx_accept_log_error;
1757
1758 #if (NGX_WIN32)
1759 {
1760 ngx_iocp_conf_t *iocpcf;
1761
1762 iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
1763 if (iocpcf->acceptex_read) {
1764 ls->post_accept_buffer_size = cscf->client_header_buffer_size;
1765 }
1766 }
1767 #endif
1768
1769 ls->backlog = addr->listen_conf->backlog;
1770 ls->rcvbuf = addr->listen_conf->rcvbuf;
1771 ls->sndbuf = addr->listen_conf->sndbuf;
1772
1773 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
1774 ls->accept_filter = addr->listen_conf->accept_filter;
1775 #endif
1776
1777 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
1778 ls->deferred_accept = addr->listen_conf->deferred_accept;
1779 #endif
1780
1781 return ls;
1782 }
1783
1784
1785 static ngx_int_t
1786 ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
1787 ngx_http_conf_addr_t *addr)
1788 {
1789 ngx_uint_t i;
1790 ngx_http_in_addr_t *addrs;
1791 struct sockaddr_in *sin;
1792 ngx_http_virtual_names_t *vn;
1793
1794 hport->addrs = ngx_pcalloc(cf->pool,
1795 hport->naddrs * sizeof(ngx_http_in_addr_t));
1796 if (hport->addrs == NULL) {
1797 return NGX_ERROR;
1798 }
1799
1800 addrs = hport->addrs;
1801
1802 for (i = 0; i < hport->naddrs; i++) {
1803
1804 sin = (struct sockaddr_in *) addr[i].sockaddr;
1805 addrs[i].addr = sin->sin_addr.s_addr;
1806 addrs[i].conf.core_srv_conf = addr[i].core_srv_conf;
1807 #if (NGX_HTTP_SSL)
1808 addrs[i].conf.ssl = addr[i].ssl;
1809 #endif
1810
1811 if (addr[i].hash.buckets == NULL
1812 && (addr[i].wc_head == NULL
1813 || addr[i].wc_head->hash.buckets == NULL)
1814 && (addr[i].wc_head == NULL
1815 || addr[i].wc_head->hash.buckets == NULL))
1816 {
15921817 continue;
15931818 }
15941819
1595 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr,
1596 in_port->port);
1597 if (ls == NULL) {
1820 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1821 if (vn == NULL) {
15981822 return NGX_ERROR;
15991823 }
16001824
1601 ls->addr_ntop = 1;
1602
1603 ls->handler = ngx_http_init_connection;
1604
1605 cscf = in_addr[a].core_srv_conf;
1606 ls->pool_size = cscf->connection_pool_size;
1607 ls->post_accept_timeout = cscf->client_header_timeout;
1608
1609 clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
1610
1611 ls->log = *clcf->err_log;
1612 ls->log.data = &ls->addr_text;
1613 ls->log.handler = ngx_accept_log_error;
1614
1615 #if (NGX_WIN32)
1825 addrs[i].conf.virtual_names = vn;
1826
1827 vn->names.hash = addr[i].hash;
1828 vn->names.wc_head = addr[i].wc_head;
1829 vn->names.wc_tail = addr[i].wc_tail;
1830 #if (NGX_PCRE)
1831 vn->nregex = addr[i].nregex;
1832 vn->regex = addr[i].regex;
1833 #endif
1834 }
1835
1836 return NGX_OK;
1837 }
1838
1839
1840 #if (NGX_HAVE_INET6)
1841
1842 static ngx_int_t
1843 ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
1844 ngx_http_conf_addr_t *addr)
1845 {
1846 ngx_uint_t i;
1847 ngx_http_in6_addr_t *addrs6;
1848 struct sockaddr_in6 *sin6;
1849 ngx_http_virtual_names_t *vn;
1850
1851 hport->addrs = ngx_pcalloc(cf->pool,
1852 hport->naddrs * sizeof(ngx_http_in6_addr_t));
1853 if (hport->addrs == NULL) {
1854 return NGX_ERROR;
1855 }
1856
1857 addrs6 = hport->addrs;
1858
1859 for (i = 0; i < hport->naddrs; i++) {
1860
1861 sin6 = (struct sockaddr_in6 *) addr[i].sockaddr;
1862 addrs6[i].addr6 = sin6->sin6_addr;
1863 addrs6[i].conf.core_srv_conf = addr[i].core_srv_conf;
1864 #if (NGX_HTTP_SSL)
1865 addrs6[i].conf.ssl = addr[i].ssl;
1866 #endif
1867
1868 if (addr[i].hash.buckets == NULL
1869 && (addr[i].wc_head == NULL
1870 || addr[i].wc_head->hash.buckets == NULL)
1871 && (addr[i].wc_head == NULL
1872 || addr[i].wc_head->hash.buckets == NULL))
16161873 {
1617 ngx_iocp_conf_t *iocpcf;
1618
1619 iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
1620 if (iocpcf->acceptex_read) {
1621 ls->post_accept_buffer_size = cscf->client_header_buffer_size;
1622 }
1623 }
1624 #endif
1625
1626 ls->backlog = in_addr[a].listen_conf->backlog;
1627 ls->rcvbuf = in_addr[a].listen_conf->rcvbuf;
1628 ls->sndbuf = in_addr[a].listen_conf->sndbuf;
1629
1630 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
1631 ls->accept_filter = in_addr[a].listen_conf->accept_filter;
1632 #endif
1633
1634 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
1635 ls->deferred_accept = in_addr[a].listen_conf->deferred_accept;
1636 #endif
1637
1638 hip = ngx_palloc(cf->pool, sizeof(ngx_http_in_port_t));
1639 if (hip == NULL) {
1874 continue;
1875 }
1876
1877 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1878 if (vn == NULL) {
16401879 return NGX_ERROR;
16411880 }
16421881
1643 hip->port = in_port->port;
1644
1645 hip->port_text.data = ngx_pnalloc(cf->pool, 7);
1646 if (hip->port_text.data == NULL) {
1647 return NGX_ERROR;
1648 }
1649
1650 ls->servers = hip;
1651
1652 hip->port_text.len = ngx_sprintf(hip->port_text.data, ":%d", hip->port)
1653 - hip->port_text.data;
1654
1655 in_addr = in_port->addrs.elts;
1656
1657 if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) {
1658 hip->naddrs = 1;
1659 done = 0;
1660
1661 } else if (in_port->addrs.nelts > 1
1662 && in_addr[last - 1].addr == INADDR_ANY)
1663 {
1664 hip->naddrs = last;
1665 done = 1;
1666
1667 } else {
1668 hip->naddrs = 1;
1669 done = 0;
1670 }
1671
1672 hip->addrs = ngx_pcalloc(cf->pool,
1673 hip->naddrs * sizeof(ngx_http_in_addr_t));
1674 if (hip->addrs == NULL) {
1675 return NGX_ERROR;
1676 }
1677
1678 for (i = 0; i < hip->naddrs; i++) {
1679 hip->addrs[i].addr = in_addr[i].addr;
1680 hip->addrs[i].core_srv_conf = in_addr[i].core_srv_conf;
1681
1682 #if (NGX_HTTP_SSL)
1683 hip->addrs[i].ssl = in_addr[i].ssl;
1684 #endif
1685
1686 if (in_addr[i].hash.buckets == NULL
1687 && (in_addr[i].wc_head == NULL
1688 || in_addr[i].wc_head->hash.buckets == NULL)
1689 && (in_addr[i].wc_head == NULL
1690 || in_addr[i].wc_head->hash.buckets == NULL))
1691 {
1692 continue;
1693 }
1694
1695 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
1696 if (vn == NULL) {
1697 return NGX_ERROR;
1698 }
1699 hip->addrs[i].virtual_names = vn;
1700
1701 vn->names.hash = in_addr[i].hash;
1702 vn->names.wc_head = in_addr[i].wc_head;
1703 vn->names.wc_tail = in_addr[i].wc_tail;
1882 addrs6[i].conf.virtual_names = vn;
1883
1884 vn->names.hash = addr[i].hash;
1885 vn->names.wc_head = addr[i].wc_head;
1886 vn->names.wc_tail = addr[i].wc_tail;
17041887 #if (NGX_PCRE)
1705 vn->nregex = in_addr[i].nregex;
1706 vn->regex = in_addr[i].regex;
1707 #endif
1708 }
1709
1710 if (done) {
1711 return NGX_OK;
1712 }
1713
1714 in_addr++;
1715 in_port->addrs.elts = in_addr;
1716 last--;
1717
1718 a = 0;
1888 vn->nregex = addr[i].nregex;
1889 vn->regex = addr[i].regex;
1890 #endif
17191891 }
17201892
17211893 return NGX_OK;
17221894 }
1895
1896 #endif
17231897
17241898
17251899 char *
17751775 {
17761776 socklen_t len;
17771777 ngx_connection_t *c;
1778 struct sockaddr_in sin;
1779
1780 /* AF_INET only */
1778 struct sockaddr_in *sin;
1779 u_char sa[NGX_SOCKADDRLEN];
17811780
17821781 c = r->connection;
17831782
1784 if (r->in_addr == 0) {
1785 len = sizeof(struct sockaddr_in);
1786 if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) {
1783 if (c->local_sockaddr == NULL) {
1784
1785 len = NGX_SOCKADDRLEN;
1786
1787 if (getsockname(c->fd, (struct sockaddr *) &sa, &len) == -1) {
17871788 ngx_connection_error(c, ngx_socket_errno, "getsockname() failed");
17881789 return NGX_ERROR;
17891790 }
17901791
1791 r->in_addr = sin.sin_addr.s_addr;
1792
1793 } else {
1794 sin.sin_family = c->sockaddr->sa_family;
1795 sin.sin_addr.s_addr = r->in_addr;
1796 }
1792 c->local_sockaddr = ngx_palloc(r->connection->pool, len);
1793 if (c->local_sockaddr == NULL) {
1794 return NGX_ERROR;
1795 }
1796
1797 c->local_socklen = len;
1798 ngx_memcpy(c->local_sockaddr, &sa, len);
1799 }
1800
1801 sin = (struct sockaddr_in *) c->local_sockaddr;
1802 r->in_addr = sin->sin_addr.s_addr;
17971803
17981804 if (s == NULL) {
17991805 return NGX_OK;
18001806 }
18011807
1802 s->len = ngx_sock_ntop((struct sockaddr *) &sin, s->data,
1803 NGX_INET_ADDRSTRLEN);
1808 s->len = ngx_sock_ntop(c->local_sockaddr, s->data, s->len, 0);
18041809
18051810 return NGX_OK;
18061811 }
27282733 * conf->client_large_buffers.num = 0;
27292734 */
27302735
2731 if (ngx_array_init(&cscf->listen, cf->pool, 4, sizeof(ngx_http_listen_t))
2736 if (ngx_array_init(&cscf->listen, cf->temp_pool, 4,
2737 sizeof(ngx_http_listen_t))
27322738 == NGX_ERROR)
27332739 {
27342740 return NGX_CONF_ERROR;
27602766 ngx_http_core_srv_conf_t *conf = child;
27612767
27622768 ngx_http_listen_t *ls;
2769 struct sockaddr_in *sin;
27632770 ngx_http_server_name_t *sn;
27642771
27652772 /* TODO: it does not merge, it inits only */
27722779
27732780 ngx_memzero(ls, sizeof(ngx_http_listen_t));
27742781
2775 ls->addr = INADDR_ANY;
2782 sin = (struct sockaddr_in *) &ls->sockaddr;
2783
2784 sin->sin_family = AF_INET;
27762785 #if (NGX_WIN32)
2777 ls->port = 80;
2786 sin->sin_port = htons(80);
27782787 #else
2779 /* STUB: getuid() should be cached */
2780 ls->port = (getuid() == 0) ? 80 : 8000;
2788 sin->sin_port = htons((getuid() == 0) ? 80 : 8000);
27812789 #endif
2782 ls->family = AF_INET;
2790 sin->sin_addr.s_addr = INADDR_ANY;
27832791
27842792 ls->conf.backlog = NGX_LISTEN_BACKLOG;
27852793 ls->conf.rcvbuf = -1;
31583166 }
31593167
31603168
3161 /* AF_INET only */
3162
31633169 static char *
31643170 ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
31653171 {
32003206
32013207 ngx_memzero(ls, sizeof(ngx_http_listen_t));
32023208
3203 ls->family = u.family;
3204 ls->addr = u.addr.in_addr;
3205 ls->port = u.port;
3209 ngx_memcpy(ls->sockaddr, u.sockaddr, u.socklen);
3210
3211 ls->socklen = u.socklen;
32063212 ls->file_name = cf->conf_file->file.name.data;
32073213 ls->line = cf->conf_file->line;
32083214 ls->conf.backlog = NGX_LISTEN_BACKLOG;
32093215 ls->conf.rcvbuf = -1;
32103216 ls->conf.sndbuf = -1;
3211
3212 n = ngx_inet_ntop(AF_INET, &ls->addr, ls->conf.addr, NGX_INET_ADDRSTRLEN);
3213 ngx_sprintf(&ls->conf.addr[n], ":%ui", ls->port);
3217 ls->conf.wildcard = u.wildcard;
3218
3219 (void) ngx_sock_ntop((struct sockaddr *) &ls->sockaddr, ls->conf.addr,
3220 NGX_SOCKADDR_STRLEN, 1);
32143221
32153222 if (cf->args->nelts == 2) {
32163223 return NGX_CONF_OK;
3939 typedef struct {
4040 unsigned default_server:1;
4141 unsigned bind:1;
42 unsigned wildcard:1;
4243 #if (NGX_HTTP_SSL)
4344 unsigned ssl:1;
4445 #endif
5455 ngx_uint_t deferred_accept;
5556 #endif
5657
57 u_char addr[NGX_INET_ADDRSTRLEN + sizeof(":65535")];
58
58 u_char addr[NGX_SOCKADDR_STRLEN + 1];
5959 } ngx_http_listen_conf_t;
6060
6161
6262 typedef struct {
63 in_addr_t addr;
64 in_port_t port;
65 int family;
63 u_char sockaddr[NGX_SOCKADDRLEN];
64 socklen_t socklen;
6665
6766 u_char *file_name;
6867 ngx_uint_t line;
172171
173172
174173 typedef struct {
175 in_addr_t addr;
176
177174 /* the default server configuration for this address:port */
178175 ngx_http_core_srv_conf_t *core_srv_conf;
179176
182179 #if (NGX_HTTP_SSL)
183180 ngx_uint_t ssl; /* unsigned ssl:1; */
184181 #endif
182 } ngx_http_addr_conf_t;
183
184
185 typedef struct {
186 in_addr_t addr;
187 ngx_http_addr_conf_t conf;
185188 } ngx_http_in_addr_t;
189
190
191 #if (NGX_HAVE_INET6)
192
193 typedef struct {
194 struct in6_addr addr6;
195 ngx_http_addr_conf_t conf;
196 } ngx_http_in6_addr_t;
197
198 #endif
186199
187200
188201 typedef struct {
189202 in_port_t port;
190203 ngx_str_t port_text;
191 ngx_http_in_addr_t *addrs;
204
205 /* ngx_http_in_addr_t or ngx_http_in6_addr_t */
206 void *addrs;
192207 ngx_uint_t naddrs;
193 } ngx_http_in_port_t;
194
195
196 typedef struct {
208 } ngx_http_port_t;
209
210
211 typedef struct {
212 ngx_int_t family;
197213 in_port_t port;
198 ngx_array_t addrs; /* array of ngx_http_conf_in_addr_t */
199 } ngx_http_conf_in_port_t;
200
201
202 typedef struct {
203 in_addr_t addr;
214 ngx_array_t addrs; /* array of ngx_http_conf_addr_t */
215 } ngx_http_conf_port_t;
216
217
218 typedef struct {
219 struct sockaddr *sockaddr;
220 socklen_t socklen;
204221
205222 ngx_hash_t hash;
206223 ngx_hash_wildcard_t *wc_head;
218235
219236 unsigned default_server:1;
220237 unsigned bind:1;
238 unsigned wildcard:1;
221239 #if (NGX_HTTP_SSL)
222240 unsigned ssl:1;
223241 #endif
224242
225243 ngx_http_listen_conf_t *listen_conf;
226 } ngx_http_conf_in_addr_t;
244 } ngx_http_conf_addr_t;
227245
228246
229247 struct ngx_http_server_name_s {
160160 ngx_table_elt_t *header;
161161 ngx_http_core_loc_conf_t *clcf;
162162 ngx_http_core_srv_conf_t *cscf;
163 /* AF_INET only */
164 u_char addr[NGX_INET_ADDRSTRLEN];
163 u_char addr[NGX_SOCKADDR_STRLEN];
165164
166165 r->header_sent = 1;
167166
289288 host = r->headers_in.server;
290289
291290 } else {
291 host.len = NGX_SOCKADDR_STRLEN;
292292 host.data = addr;
293293
294294 if (ngx_http_server_addr(r, &host) != NGX_OK) {
231231 ngx_uint_t i;
232232 ngx_connection_t *c;
233233 ngx_http_request_t *r;
234 ngx_http_in_port_t *hip;
235 ngx_http_in_addr_t *hia;
234 struct sockaddr_in *sin;
235 ngx_http_port_t *port;
236 ngx_http_in_addr_t *addr;
236237 ngx_http_log_ctx_t *ctx;
238 ngx_http_addr_conf_t *addr_conf;
237239 ngx_http_connection_t *hc;
238240 ngx_http_core_srv_conf_t *cscf;
239241 ngx_http_core_loc_conf_t *clcf;
240242 ngx_http_core_main_conf_t *cmcf;
243 #if (NGX_HAVE_INET6)
244 struct sockaddr_in6 *sin6;
245 ngx_http_in6_addr_t *addr6;
246 #endif
241247
242248 #if (NGX_STAT_STUB)
243249 ngx_atomic_fetch_add(ngx_stat_reading, -1);
291297
292298 /* find the server configuration for the address:port */
293299
294 /* AF_INET only */
295
296 hip = c->listening->servers;
297 hia = hip->addrs;
298
299 r->port = hip->port;
300 r->port_text = &hip->port_text;
301
302 i = 0;
300 port = c->listening->servers;
301
302 r->port = port->port;
303 r->port_text = &port->port_text;
303304
304305 r->connection = c;
305306
306 if (hip->naddrs > 1) {
307 if (port->naddrs > 1) {
307308
308309 /*
309 * There are several addresses on this port and one of them
310 * is the "*:port" wildcard so getsockname() is needed to determine
311 * the server address.
312 *
313 * AcceptEx() already has given this address.
310 * there are several addresses on this port and one of them
311 * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
312 * is required to determine a server address
314313 */
315314
316 #if (NGX_WIN32)
317 if (c->local_sockaddr) {
318 r->in_addr =
319 ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
320
321 } else
322 #endif
323 {
324 if (ngx_http_server_addr(r, NULL) != NGX_OK) {
325 ngx_http_close_connection(c);
326 return;
327 }
328 }
329
330 /* the last address is "*" */
331
332 for ( /* void */ ; i < hip->naddrs - 1; i++) {
333 if (hia[i].addr == r->in_addr) {
334 break;
335 }
315 c->local_sockaddr = NULL;
316
317 if (ngx_http_server_addr(r, NULL) != NGX_OK) {
318 ngx_http_close_connection(c);
319 return;
320 }
321
322 switch (c->local_sockaddr->sa_family) {
323
324 #if (NGX_HAVE_INET6)
325 case AF_INET6:
326 sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
327
328 addr6 = (ngx_http_in6_addr_t *) port->addrs;
329
330 /* the last address is "*" */
331
332 for (i = 0; i < port->naddrs - 1; i++) {
333 if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
334 break;
335 }
336 }
337
338 addr_conf = &addr6[i].conf;
339
340 break;
341 #endif
342
343 default: /* AF_INET */
344 sin = (struct sockaddr_in *) c->local_sockaddr;
345
346 addr = port->addrs;
347
348 /* the last address is "*" */
349
350 for (i = 0; i < port->naddrs - 1; i++) {
351 if (addr[i].addr == sin->sin_addr.s_addr) {
352 break;
353 }
354 }
355
356 addr_conf = &addr[i].conf;
357
358 break;
336359 }
337360
338361 } else {
339 r->in_addr = hia[0].addr;
340 }
341
342 r->virtual_names = hia[i].virtual_names;
362
363 switch (c->local_sockaddr->sa_family) {
364
365 #if (NGX_HAVE_INET6)
366 case AF_INET6:
367 addr6 = (ngx_http_in6_addr_t *) port->addrs;
368 addr_conf = &addr6[0].conf;
369 break;
370 #endif
371
372 default: /* AF_INET */
373 addr = port->addrs;
374 addr_conf = &addr[0].conf;
375 r->in_addr = addr[0].addr;
376 break;
377 }
378 }
379
380 r->virtual_names = addr_conf->virtual_names;
343381
344382 /* the default server configuration for the address:port */
345 cscf = hia[i].core_srv_conf;
383 cscf = addr_conf->core_srv_conf;
346384
347385 r->main_conf = cscf->ctx->main_conf;
348386 r->srv_conf = cscf->ctx->srv_conf;
356394 ngx_http_ssl_srv_conf_t *sscf;
357395
358396 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
359 if (sscf->enable || hia[i].ssl) {
397 if (sscf->enable || addr_conf->ssl) {
360398
361399 if (c->ssl == NULL) {
362400
363401 c->log->action = "SSL handshaking";
364402
365 if (hia[i].ssl && sscf->ssl.ctx == NULL) {
403 if (addr_conf->ssl && sscf->ssl.ctx == NULL) {
366404 ngx_log_error(NGX_LOG_ERR, c->log, 0,
367405 "no \"ssl_certificate\" is defined "
368406 "in server listening on SSL port");
827827 ngx_http_variable_binary_remote_addr(ngx_http_request_t *r,
828828 ngx_http_variable_value_t *v, uintptr_t data)
829829 {
830 struct sockaddr_in *sin;
831
832 /* AF_INET only */
833
834 sin = (struct sockaddr_in *) r->connection->sockaddr;
835
836 v->len = sizeof(in_addr_t);
837 v->valid = 1;
838 v->no_cacheable = 0;
839 v->not_found = 0;
840 v->data = (u_char *) &sin->sin_addr.s_addr;
830 struct sockaddr_in *sin;
831 #if (NGX_HAVE_INET6)
832 struct sockaddr_in6 *sin6;
833 #endif
834
835 switch (r->connection->sockaddr->sa_family) {
836
837 #if (NGX_HAVE_INET6)
838 case AF_INET6:
839 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
840
841 v->len = sizeof(struct in6_addr);
842 v->valid = 1;
843 v->no_cacheable = 0;
844 v->not_found = 0;
845 v->data = (u_char *) &sin6->sin6_addr;
846
847 break;
848 #endif
849
850 default: /* AF_INET */
851 sin = (struct sockaddr_in *) r->connection->sockaddr;
852
853 v->len = sizeof(in_addr_t);
854 v->valid = 1;
855 v->no_cacheable = 0;
856 v->not_found = 0;
857 v->data = (u_char *) &sin->sin_addr;
858
859 break;
860 }
841861
842862 return NGX_OK;
843863 }
861881 ngx_http_variable_remote_port(ngx_http_request_t *r,
862882 ngx_http_variable_value_t *v, uintptr_t data)
863883 {
864 ngx_uint_t port;
865 struct sockaddr_in *sin;
884 ngx_uint_t port;
885 struct sockaddr_in *sin;
886 #if (NGX_HAVE_INET6)
887 struct sockaddr_in6 *sin6;
888 #endif
866889
867890 v->len = 0;
868891 v->valid = 1;
874897 return NGX_ERROR;
875898 }
876899
877 /* AF_INET only */
878
879 if (r->connection->sockaddr->sa_family == AF_INET) {
900 switch (r->connection->sockaddr->sa_family) {
901
902 #if (NGX_HAVE_INET6)
903 case AF_INET6:
904 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
905 port = ntohs(sin6->sin6_port);
906 break;
907 #endif
908
909 default: /* AF_INET */
880910 sin = (struct sockaddr_in *) r->connection->sockaddr;
881
882911 port = ntohs(sin->sin_port);
883
884 if (port > 0 && port < 65536) {
885 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
886 }
912 break;
913 }
914
915 if (port > 0 && port < 65536) {
916 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
887917 }
888918
889919 return NGX_OK;
895925 ngx_http_variable_value_t *v, uintptr_t data)
896926 {
897927 ngx_str_t s;
898
899 s.data = ngx_pnalloc(r->pool, NGX_INET_ADDRSTRLEN);
928 u_char addr[NGX_SOCKADDR_STRLEN];
929
930 s.len = NGX_SOCKADDR_STRLEN;
931 s.data = addr;
932
933 if (ngx_http_server_addr(r, &s) != NGX_OK) {
934 return NGX_ERROR;
935 }
936
937 s.data = ngx_pnalloc(r->pool, s.len);
900938 if (s.data == NULL) {
901939 return NGX_ERROR;
902940 }
903941
904 if (ngx_http_server_addr(r, &s) != NGX_OK) {
905 return NGX_ERROR;
906 }
942 ngx_memcpy(s.data, addr, s.len);
907943
908944 v->len = s.len;
909945 v->valid = 1;
304304 return NGX_CONF_ERROR;
305305 }
306306
307 if (u.family != AF_INET) {
308 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "listen supports IPv4 only");
309 return NGX_CONF_ERROR;
310 }
311
307312 cmcf = ngx_mail_conf_get_module_main_conf(cf, ngx_mail_core_module);
308313
309314 imls = cmcf->listen.elts;
2828 #endif
2929
3030 #include <winsock2.h>
31 #include <ws2tcpip.h> /* ipv6 */
3132 #include <mswsock.h>
3233 #include <shellapi.h>
3334 #include <stddef.h> /* offsetof() */