a prelimiary IPv6 support, HTTP listen
Igor Sysoev
13 years ago
41 | 41 | EVENT_AIO=NO |
42 | 42 | |
43 | 43 | USE_THREADS=NO |
44 | ||
45 | NGX_IPV6=NO | |
44 | 46 | |
45 | 47 | HTTP=YES |
46 | 48 | |
159 | 161 | #--with-threads=*) USE_THREADS="$value" ;; |
160 | 162 | #--with-threads) USE_THREADS="pthreads" ;; |
161 | 163 | |
164 | --with-ipv6) NGX_IPV6=YES ;; | |
165 | ||
162 | 166 | --without-http) HTTP=NO ;; |
163 | 167 | --http-log-path=*) NGX_HTTP_LOG_PATH="$value" ;; |
164 | 168 | --http-client-body-temp-path=*) NGX_HTTP_CLIENT_TEMP_PATH="$value" ;; |
284 | 288 | --with-poll_module enable poll module |
285 | 289 | --without-poll_module disable poll module |
286 | 290 | |
291 | --with-ipv6 enable ipv6 support | |
292 | ||
287 | 293 | --with-http_ssl_module enable ngx_http_ssl_module |
288 | 294 | --with-http_realip_module enable ngx_http_realip_module |
289 | 295 | --with-http_addition_module enable ngx_http_addition_module |
63 | 63 | # syscalls, libc calls and some features |
64 | 64 | |
65 | 65 | |
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 | ||
66 | 81 | ngx_feature="setproctitle()" |
67 | 82 | ngx_feature_name="NGX_HAVE_SETPROCTITLE" |
68 | 83 | ngx_feature_run=no |
51 | 51 | ls->type = SOCK_STREAM; |
52 | 52 | ls->sockaddr = (struct sockaddr *) sin; |
53 | 53 | ls->socklen = sizeof(struct sockaddr_in); |
54 | ls->addr = offsetof(struct sockaddr_in, sin_addr); | |
55 | 54 | ls->addr_text_max_len = NGX_INET_ADDRSTRLEN; |
56 | 55 | |
57 | 56 | return ls; |
64 | 63 | size_t len; |
65 | 64 | ngx_uint_t i; |
66 | 65 | ngx_listening_t *ls; |
67 | struct sockaddr_in *sin; | |
68 | 66 | socklen_t olen; |
69 | 67 | #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) |
70 | 68 | ngx_err_t err; |
93 | 91 | continue; |
94 | 92 | } |
95 | 93 | |
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: | |
99 | 107 | ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno, |
100 | 108 | "the inherited socket #%d has " |
101 | "unsupported family", ls[i].fd); | |
109 | "an unsupported protocol family", ls[i].fd); | |
102 | 110 | ls[i].ignore = 1; |
103 | 111 | continue; |
104 | 112 | } |
105 | 113 | |
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); | |
110 | 117 | if (ls[i].addr_text.data == NULL) { |
111 | 118 | return NGX_ERROR; |
112 | 119 | } |
113 | 120 | |
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); | |
116 | 122 | if (len == 0) { |
117 | 123 | return NGX_ERROR; |
118 | 124 | } |
119 | 125 | |
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; | |
123 | 127 | |
124 | 128 | ls[i].backlog = NGX_LISTEN_BACKLOG; |
125 | 129 |
18 | 18 | |
19 | 19 | struct sockaddr *sockaddr; |
20 | 20 | socklen_t socklen; /* size of sockaddr */ |
21 | size_t addr; /* offset to address in sockaddr */ | |
22 | 21 | size_t addr_text_max_len; |
23 | 22 | ngx_str_t addr_text; |
24 | 23 | |
122 | 121 | ngx_ssl_connection_t *ssl; |
123 | 122 | #endif |
124 | 123 | |
125 | #if (NGX_HAVE_IOCP) | |
126 | 124 | struct sockaddr *local_sockaddr; |
127 | 125 | socklen_t local_socklen; |
128 | #endif | |
129 | 126 | |
130 | 127 | ngx_buf_t *buffer; |
131 | 128 |
875 | 875 | static ngx_int_t |
876 | 876 | ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2) |
877 | 877 | { |
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) { | |
883 | 884 | return NGX_DECLINED; |
884 | 885 | } |
885 | 886 | |
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; | |
895 | 919 | } |
896 | 920 | |
897 | 921 | return NGX_OK; |
7 | 7 | #include <ngx_core.h> |
8 | 8 | |
9 | 9 | |
10 | #if (NGX_HAVE_INET6) | |
11 | static size_t ngx_inet6_ntop(u_char *p, u_char *text, size_t len); | |
12 | #endif | |
10 | 13 | static ngx_int_t ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u); |
11 | 14 | 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 | ||
15 | 17 | |
16 | 18 | in_addr_t |
17 | 19 | ngx_inet_addr(u_char *text, size_t len) |
56 | 58 | } |
57 | 59 | |
58 | 60 | |
59 | /* AF_INET only */ | |
60 | ||
61 | 61 | 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: | |
68 | 74 | |
69 | 75 | sin = (struct sockaddr_in *) sa; |
70 | 76 | 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; | |
71 | 126 | |
72 | 127 | return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud", |
73 | 128 | p[0], p[1], p[2], p[3]) |
74 | 129 | - 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 | |
97 | 223 | |
98 | 224 | |
99 | 225 | /* AF_INET only */ |
168 | 294 | if ((p[0] == ':' || p[0] == '/') && !u->listen) { |
169 | 295 | u->err = "invalid host"; |
170 | 296 | return NGX_ERROR; |
297 | } | |
298 | ||
299 | if (p[0] == '[') { | |
300 | return ngx_parse_inet6_url(pool, u); | |
171 | 301 | } |
172 | 302 | |
173 | 303 | return ngx_parse_inet_url(pool, u); |
208 | 338 | |
209 | 339 | u->host.len = len++; |
210 | 340 | u->host.data = path; |
211 | u->family = AF_UNIX; | |
212 | 341 | |
213 | 342 | if (len > sizeof(saun->sun_path)) { |
214 | 343 | u->err = "too long path in the unix domain socket"; |
215 | 344 | return NGX_ERROR; |
216 | 345 | } |
217 | 346 | |
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 | ||
218 | 352 | u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t)); |
219 | 353 | if (u->addrs == NULL) { |
220 | 354 | return NGX_ERROR; |
225 | 359 | return NGX_ERROR; |
226 | 360 | } |
227 | 361 | |
362 | u->family = AF_UNIX; | |
228 | 363 | u->naddrs = 1; |
229 | 364 | |
230 | 365 | saun->sun_family = AF_UNIX; |
250 | 385 | static ngx_int_t |
251 | 386 | ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u) |
252 | 387 | { |
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; | |
257 | 397 | |
258 | 398 | u->family = AF_INET; |
259 | 399 | |
310 | 450 | } |
311 | 451 | |
312 | 452 | u->port = (in_port_t) n; |
453 | sin->sin_port = htons((in_port_t) n); | |
313 | 454 | |
314 | 455 | u->port_text.len = len; |
315 | 456 | u->port_text.data = port; |
333 | 474 | } |
334 | 475 | |
335 | 476 | u->port = (in_port_t) n; |
477 | sin->sin_port = htons((in_port_t) n); | |
336 | 478 | |
337 | 479 | u->port_text.len = last - host; |
338 | 480 | u->port_text.data = host; |
481 | ||
482 | u->wildcard = 1; | |
339 | 483 | |
340 | 484 | return NGX_OK; |
341 | 485 | } |
373 | 517 | (void) ngx_cpystrn(p, host, len); |
374 | 518 | |
375 | 519 | 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) { | |
378 | 523 | h = gethostbyname((const char *) p); |
379 | 524 | |
380 | 525 | if (h == NULL || h->h_addr_list[0] == NULL) { |
384 | 529 | } |
385 | 530 | |
386 | 531 | 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; | |
387 | 537 | } |
388 | 538 | |
389 | 539 | ngx_free(p); |
390 | 540 | |
391 | 541 | } else { |
392 | 542 | u->addr.in_addr = INADDR_ANY; |
543 | sin->sin_addr.s_addr = INADDR_ANY; | |
544 | u->wildcard = 1; | |
393 | 545 | } |
394 | 546 | |
395 | 547 | if (u->no_port) { |
396 | 548 | u->port = u->default_port; |
549 | sin->sin_port = htons(u->default_port); | |
397 | 550 | } |
398 | 551 | |
399 | 552 | if (u->listen) { |
408 | 561 | } |
409 | 562 | |
410 | 563 | |
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 | ||
411 | 686 | ngx_int_t |
412 | 687 | ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u) |
413 | 688 | { |
414 | 689 | u_char *p, *host; |
415 | 690 | size_t len; |
691 | in_port_t port; | |
416 | 692 | in_addr_t in_addr; |
417 | 693 | ngx_uint_t i; |
418 | 694 | struct hostent *h; |
427 | 703 | |
428 | 704 | /* AF_INET only */ |
429 | 705 | |
706 | sin = (struct sockaddr_in *) &u->sockaddr; | |
707 | port = sin->sin_port; | |
708 | ||
430 | 709 | in_addr = inet_addr((char *) host); |
431 | 710 | |
432 | 711 | if (in_addr == INADDR_NONE) { |
463 | 742 | } |
464 | 743 | |
465 | 744 | sin->sin_family = AF_INET; |
466 | sin->sin_port = htons(u->port); | |
745 | sin->sin_port = port; | |
467 | 746 | sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]); |
468 | 747 | |
469 | 748 | u->addrs[i].sockaddr = (struct sockaddr *) sin; |
470 | 749 | u->addrs[i].socklen = sizeof(struct sockaddr_in); |
471 | 750 | |
472 | len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1; | |
751 | len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1; | |
473 | 752 | |
474 | 753 | p = ngx_pnalloc(pool, len); |
475 | 754 | if (p == NULL) { |
476 | 755 | return NGX_ERROR; |
477 | 756 | } |
478 | 757 | |
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; | |
482 | 761 | u->addrs[i].name.data = p; |
483 | 762 | } |
484 | 763 | |
501 | 780 | u->naddrs = 1; |
502 | 781 | |
503 | 782 | sin->sin_family = AF_INET; |
504 | sin->sin_port = htons(u->port); | |
783 | sin->sin_port = port; | |
505 | 784 | sin->sin_addr.s_addr = in_addr; |
506 | 785 | |
507 | 786 | u->addrs[0].sockaddr = (struct sockaddr *) sin; |
508 | 787 | u->addrs[0].socklen = sizeof(struct sockaddr_in); |
509 | 788 | |
510 | p = ngx_pnalloc(pool, u->host.len + sizeof(":65536") - 1); | |
789 | p = ngx_pnalloc(pool, u->host.len + sizeof(":65535") - 1); | |
511 | 790 | if (p == NULL) { |
512 | 791 | return NGX_ERROR; |
513 | 792 | } |
514 | 793 | |
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; | |
516 | 796 | u->addrs[0].name.data = p; |
517 | 797 | } |
518 | 798 |
11 | 11 | #include <ngx_core.h> |
12 | 12 | |
13 | 13 | |
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 | |
15 | 33 | |
16 | 34 | |
17 | 35 | typedef struct { |
48 | 66 | unsigned one_addr:1; |
49 | 67 | |
50 | 68 | unsigned no_port:1; |
69 | unsigned wildcard:1; | |
51 | 70 | |
52 | 71 | ngx_url_addr_t addr; |
72 | ||
73 | socklen_t socklen; | |
74 | u_char sockaddr[NGX_SOCKADDRLEN]; | |
53 | 75 | |
54 | 76 | ngx_peer_addr_t *addrs; |
55 | 77 | ngx_uint_t naddrs; |
59 | 81 | |
60 | 82 | |
61 | 83 | 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); | |
63 | 86 | size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len); |
64 | 87 | ngx_int_t ngx_ptocidr(ngx_str_t *text, void *cidr); |
65 | 88 | ngx_int_t ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u); |
6 | 6 | #include <ngx_config.h> |
7 | 7 | #include <ngx_core.h> |
8 | 8 | #include <ngx_event.h> |
9 | ||
10 | ||
11 | /* the buffer size is enough to hold "struct sockaddr_un" */ | |
12 | #define NGX_SOCKLEN 512 | |
13 | 9 | |
14 | 10 | |
15 | 11 | static ngx_int_t ngx_enable_accept_events(ngx_cycle_t *cycle); |
28 | 24 | ngx_listening_t *ls; |
29 | 25 | ngx_connection_t *c, *lc; |
30 | 26 | ngx_event_conf_t *ecf; |
31 | char sa[NGX_SOCKLEN]; | |
27 | u_char sa[NGX_SOCKADDRLEN]; | |
32 | 28 | |
33 | 29 | ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module); |
34 | 30 | |
47 | 43 | "accept on %V, ready: %d", &ls->addr_text, ev->available); |
48 | 44 | |
49 | 45 | do { |
50 | socklen = NGX_SOCKLEN; | |
46 | socklen = NGX_SOCKADDRLEN; | |
51 | 47 | |
52 | 48 | s = accept(lc->fd, (struct sockaddr *) sa, &socklen); |
53 | 49 | |
152 | 148 | c->log = log; |
153 | 149 | c->pool->log = log; |
154 | 150 | |
151 | c->socklen = socklen; | |
155 | 152 | c->listening = ls; |
156 | c->socklen = socklen; | |
153 | c->local_sockaddr = ls->sockaddr; | |
154 | c->local_socklen = ls->socklen; | |
157 | 155 | |
158 | 156 | c->unexpected_eof = 1; |
159 | 157 | |
207 | 205 | } |
208 | 206 | |
209 | 207 | 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); | |
211 | 209 | if (c->addr_text.len == 0) { |
212 | 210 | ngx_close_accepted_connection(c); |
213 | 211 | return; |
65 | 65 | } |
66 | 66 | |
67 | 67 | 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); | |
69 | 69 | if (c->addr_text.len == 0) { |
70 | 70 | /* TODO: close socket */ |
71 | 71 | return; |
7 | 7 | #include <ngx_core.h> |
8 | 8 | #include <ngx_http.h> |
9 | 9 | |
10 | ||
11 | /* AF_INET only */ | |
12 | 10 | |
13 | 11 | typedef struct { |
14 | 12 | in_addr_t mask; |
102 | 100 | |
103 | 101 | /* AF_INET only */ |
104 | 102 | |
103 | if (r->connection->sockaddr->sa_family != AF_INET) { | |
104 | return NGX_DECLINED; | |
105 | } | |
106 | ||
105 | 107 | sin = (struct sockaddr_in *) r->connection->sockaddr; |
106 | 108 | |
107 | 109 | rule = alcf->rules->elts; |
174 | 174 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
175 | 175 | "http geo started: %V", &r->connection->addr_text); |
176 | 176 | |
177 | if (r->connection->sockaddr->sa_family != AF_INET) { | |
178 | return 0; | |
179 | } | |
180 | ||
177 | 181 | sin = (struct sockaddr_in *) r->connection->sockaddr; |
178 | 182 | return ntohl(sin->sin_addr.s_addr); |
179 | 183 | } |
12 | 12 | #define NGX_HTTP_REALIP_XFWD 1 |
13 | 13 | #define NGX_HTTP_REALIP_HEADER 2 |
14 | 14 | |
15 | ||
16 | /* AF_INET only */ | |
17 | 15 | |
18 | 16 | typedef struct { |
19 | 17 | in_addr_t mask; |
208 | 206 | |
209 | 207 | /* AF_INET only */ |
210 | 208 | |
209 | if (r->connection->sockaddr->sa_family != AF_INET) { | |
210 | return NGX_DECLINED; | |
211 | } | |
212 | ||
211 | 213 | sin = (struct sockaddr_in *) c->sockaddr; |
212 | 214 | |
213 | 215 | from = rlcf->from->elts; |
14 | 14 | |
15 | 15 | ngx_uint_t hash; |
16 | 16 | |
17 | /* AF_INET only */ | |
18 | 17 | u_char addr[3]; |
19 | 18 | |
20 | 19 | u_char tries; |
110 | 109 | r->upstream->peer.get = ngx_http_upstream_get_ip_hash_peer; |
111 | 110 | |
112 | 111 | /* 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 | } | |
118 | 126 | |
119 | 127 | iphp->hash = 89; |
120 | 128 | iphp->tries = 0; |
19 | 19 | static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf, |
20 | 20 | ngx_array_t *servers, ngx_array_t *in_ports); |
21 | 21 | 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, | |
23 | 23 | ngx_http_listen_t *listen); |
24 | 24 | 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, | |
26 | 26 | ngx_http_listen_t *listen); |
27 | 27 | 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, | |
29 | 29 | ngx_http_listen_t *listen); |
30 | 30 | 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); | |
32 | 32 | |
33 | 33 | static char *ngx_http_merge_locations(ngx_conf_t *cf, |
34 | 34 | ngx_queue_t *locations, void **loc_conf, ngx_http_module_t *module, |
48 | 48 | size_t prefix); |
49 | 49 | |
50 | 50 | 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); | |
52 | 52 | 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); | |
55 | 55 | static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one, |
56 | 56 | const void *two); |
57 | 57 | |
58 | 58 | 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 | |
60 | 68 | |
61 | 69 | ngx_uint_t ngx_http_max_module; |
62 | 70 | |
358 | 366 | * to find quickly the server core module configuration at run-time |
359 | 367 | */ |
360 | 368 | |
361 | /* AF_INET only */ | |
362 | ||
363 | 369 | if (ngx_http_init_server_lists(cf, &cmcf->servers, &in_ports) != NGX_OK) { |
364 | 370 | return NGX_CONF_ERROR; |
365 | 371 | } |
366 | 372 | |
367 | 373 | |
368 | 374 | /* optimize the lists of ports, addresses and server names */ |
369 | ||
370 | /* AF_INET only */ | |
371 | 375 | |
372 | 376 | if (ngx_http_optimize_servers(cf, cmcf, &in_ports) != NGX_OK) { |
373 | 377 | return NGX_CONF_ERROR; |
1106 | 1110 | |
1107 | 1111 | static ngx_int_t |
1108 | 1112 | ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers, |
1109 | ngx_array_t *in_ports) | |
1113 | ngx_array_t *ports) | |
1110 | 1114 | { |
1111 | 1115 | ngx_uint_t s, i; |
1112 | 1116 | ngx_http_listen_t *listen; |
1113 | 1117 | ngx_http_core_srv_conf_t **cscfp; |
1114 | 1118 | |
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)) | |
1117 | 1120 | != NGX_OK) |
1118 | 1121 | { |
1119 | 1122 | return NGX_ERROR; |
1129 | 1132 | listen = cscfp[s]->listen.elts; |
1130 | 1133 | for (i = 0; i < cscfp[s]->listen.nelts; i++) { |
1131 | 1134 | |
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) { | |
1137 | 1136 | return NGX_ERROR; |
1138 | 1137 | } |
1139 | 1138 | } |
1142 | 1141 | return NGX_OK; |
1143 | 1142 | } |
1144 | 1143 | |
1145 | ||
1146 | /* AF_INET only */ | |
1147 | 1144 | |
1148 | 1145 | static ngx_int_t |
1149 | 1146 | 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; | |
1152 | 1150 | 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) { | |
1159 | 1179 | continue; |
1160 | 1180 | } |
1161 | 1181 | |
1162 | 1182 | /* a port is already in the in_port list */ |
1163 | 1183 | |
1164 | return ngx_http_add_addresses(cf, cscf, &in_port[i], listen); | |
1184 | return ngx_http_add_addresses(cf, cscf, &port[i], listen); | |
1165 | 1185 | } |
1166 | 1186 | |
1167 | 1187 | /* add a port to the in_port list */ |
1168 | 1188 | |
1169 | in_port = ngx_array_push(in_ports); | |
1170 | if (in_port == NULL) { | |
1189 | port = ngx_array_push(ports); | |
1190 | if (port == NULL) { | |
1171 | 1191 | return NGX_ERROR; |
1172 | 1192 | } |
1173 | 1193 | |
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); | |
1178 | 1199 | } |
1179 | 1200 | |
1180 | 1201 | |
1181 | 1202 | static ngx_int_t |
1182 | 1203 | 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) { | |
1193 | 1241 | continue; |
1194 | 1242 | } |
1195 | 1243 | |
1196 | 1244 | /* the address is already in the address list */ |
1197 | 1245 | |
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) { | |
1199 | 1247 | return NGX_ERROR; |
1200 | 1248 | } |
1201 | 1249 | |
1202 | /* | |
1203 | * check the duplicate "default" server | |
1204 | * for this address:port | |
1205 | */ | |
1250 | /* check the duplicate "default" server for this address:port */ | |
1206 | 1251 | |
1207 | 1252 | if (listen->conf.default_server) { |
1208 | 1253 | |
1209 | if (in_addr[i].default_server) { | |
1254 | if (addr[i].default_server) { | |
1210 | 1255 | ngx_log_error(NGX_LOG_ERR, cf->log, 0, |
1211 | 1256 | "the duplicate default server in %s:%ui", |
1212 | 1257 | listen->file_name, listen->line); |
1214 | 1259 | return NGX_ERROR; |
1215 | 1260 | } |
1216 | 1261 | |
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; | |
1219 | 1264 | #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; | |
1223 | 1268 | } |
1224 | 1269 | |
1225 | 1270 | return NGX_OK; |
1226 | 1271 | } |
1227 | 1272 | |
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); | |
1234 | 1276 | } |
1235 | 1277 | |
1236 | 1278 | |
1241 | 1283 | |
1242 | 1284 | static ngx_int_t |
1243 | 1285 | 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)) | |
1251 | 1293 | != NGX_OK) |
1252 | 1294 | { |
1253 | 1295 | return NGX_ERROR; |
1254 | 1296 | } |
1255 | 1297 | } |
1256 | 1298 | |
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) { | |
1259 | 1301 | return NGX_ERROR; |
1260 | 1302 | } |
1261 | 1303 | |
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; | |
1268 | 1311 | #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; | |
1275 | 1319 | #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); | |
1281 | 1325 | } |
1282 | 1326 | |
1283 | 1327 | |
1284 | 1328 | /* |
1285 | 1329 | * add the server names and the server core module |
1286 | * configurations to the address:port (in_addr) | |
1330 | * configurations to the address:port | |
1287 | 1331 | */ |
1288 | 1332 | |
1289 | 1333 | static ngx_int_t |
1290 | 1334 | 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) | |
1292 | 1336 | { |
1293 | 1337 | ngx_uint_t i; |
1294 | 1338 | ngx_http_server_name_t *server_names, *name; |
1295 | 1339 | |
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, | |
1298 | 1342 | sizeof(ngx_http_server_name_t)) |
1299 | 1343 | != NGX_OK) |
1300 | 1344 | { |
1312 | 1356 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0, |
1313 | 1357 | "name: %V", &server_names[i].name); |
1314 | 1358 | |
1315 | name = ngx_array_push(&in_addr->names); | |
1359 | name = ngx_array_push(&addr->names); | |
1316 | 1360 | if (name == NULL) { |
1317 | 1361 | return NGX_ERROR; |
1318 | 1362 | } |
1326 | 1370 | |
1327 | 1371 | static ngx_int_t |
1328 | 1372 | 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) | |
1330 | 1374 | { |
1331 | 1375 | ngx_uint_t s, p, a; |
1376 | ngx_http_conf_port_t *port; | |
1377 | ngx_http_conf_addr_t *addr; | |
1332 | 1378 | 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); | |
1341 | 1385 | |
1342 | 1386 | /* |
1343 | 1387 | * check whether all name-based servers have the same |
1344 | 1388 | * configuraiton as a default server for given address:port |
1345 | 1389 | */ |
1346 | 1390 | |
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) { | |
1354 | 1398 | continue; |
1355 | 1399 | } |
1356 | 1400 | |
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) { | |
1358 | 1402 | return NGX_ERROR; |
1359 | 1403 | } |
1360 | 1404 | |
1362 | 1406 | } |
1363 | 1407 | } |
1364 | 1408 | |
1365 | if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) { | |
1409 | if (ngx_http_init_listening(cf, &port[p]) != NGX_OK) { | |
1366 | 1410 | return NGX_ERROR; |
1367 | 1411 | } |
1368 | 1412 | } |
1373 | 1417 | |
1374 | 1418 | static ngx_int_t |
1375 | 1419 | 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) | |
1377 | 1421 | { |
1378 | 1422 | ngx_int_t rc; |
1379 | 1423 | ngx_uint_t s; |
1399 | 1443 | goto failed; |
1400 | 1444 | } |
1401 | 1445 | |
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++) { | |
1405 | 1449 | |
1406 | 1450 | #if (NGX_PCRE) |
1407 | 1451 | if (name[s].regex) { |
1420 | 1464 | if (rc == NGX_DECLINED) { |
1421 | 1465 | ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
1422 | 1466 | "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); | |
1424 | 1468 | return NGX_ERROR; |
1425 | 1469 | } |
1426 | 1470 | |
1427 | 1471 | if (rc == NGX_BUSY) { |
1428 | 1472 | ngx_log_error(NGX_LOG_WARN, cf->log, 0, |
1429 | 1473 | "conflicting server name \"%V\" on %s, ignored", |
1430 | &name[s].name, in_addr->listen_conf->addr); | |
1474 | &name[s].name, addr->listen_conf->addr); | |
1431 | 1475 | } |
1432 | 1476 | } |
1433 | 1477 | |
1438 | 1482 | hash.pool = cf->pool; |
1439 | 1483 | |
1440 | 1484 | if (ha.keys.nelts) { |
1441 | hash.hash = &in_addr->hash; | |
1485 | hash.hash = &addr->hash; | |
1442 | 1486 | hash.temp_pool = NULL; |
1443 | 1487 | |
1444 | 1488 | if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) { |
1461 | 1505 | goto failed; |
1462 | 1506 | } |
1463 | 1507 | |
1464 | in_addr->wc_head = (ngx_hash_wildcard_t *) hash.hash; | |
1508 | addr->wc_head = (ngx_hash_wildcard_t *) hash.hash; | |
1465 | 1509 | } |
1466 | 1510 | |
1467 | 1511 | if (ha.dns_wc_tail.nelts) { |
1479 | 1523 | goto failed; |
1480 | 1524 | } |
1481 | 1525 | |
1482 | in_addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash; | |
1526 | addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash; | |
1483 | 1527 | } |
1484 | 1528 | |
1485 | 1529 | ngx_destroy_pool(ha.temp_pool); |
1490 | 1534 | return NGX_OK; |
1491 | 1535 | } |
1492 | 1536 | |
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) { | |
1497 | 1540 | return NGX_ERROR; |
1498 | 1541 | } |
1499 | 1542 | |
1500 | for (i = 0, s = 0; s < in_addr->names.nelts; s++) { | |
1543 | for (i = 0, s = 0; s < addr->names.nelts; s++) { | |
1501 | 1544 | if (name[s].regex) { |
1502 | in_addr->regex[i++] = name[s]; | |
1545 | addr->regex[i++] = name[s]; | |
1503 | 1546 | } |
1504 | 1547 | } |
1505 | 1548 | |
1516 | 1559 | |
1517 | 1560 | |
1518 | 1561 | 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 */ | |
1528 | 1571 | return 1; |
1529 | 1572 | } |
1530 | 1573 | |
1557 | 1600 | |
1558 | 1601 | |
1559 | 1602 | 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; | |
1563 | 1606 | 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; | |
1567 | 1697 | ngx_http_core_loc_conf_t *clcf; |
1568 | 1698 | 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 | { | |
1592 | 1817 | continue; |
1593 | 1818 | } |
1594 | 1819 | |
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) { | |
1598 | 1822 | return NGX_ERROR; |
1599 | 1823 | } |
1600 | 1824 | |
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)) | |
1616 | 1873 | { |
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) { | |
1640 | 1879 | return NGX_ERROR; |
1641 | 1880 | } |
1642 | 1881 | |
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; | |
1704 | 1887 | #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 | |
1719 | 1891 | } |
1720 | 1892 | |
1721 | 1893 | return NGX_OK; |
1722 | 1894 | } |
1895 | ||
1896 | #endif | |
1723 | 1897 | |
1724 | 1898 | |
1725 | 1899 | char * |
1775 | 1775 | { |
1776 | 1776 | socklen_t len; |
1777 | 1777 | 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]; | |
1781 | 1780 | |
1782 | 1781 | c = r->connection; |
1783 | 1782 | |
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) { | |
1787 | 1788 | ngx_connection_error(c, ngx_socket_errno, "getsockname() failed"); |
1788 | 1789 | return NGX_ERROR; |
1789 | 1790 | } |
1790 | 1791 | |
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; | |
1797 | 1803 | |
1798 | 1804 | if (s == NULL) { |
1799 | 1805 | return NGX_OK; |
1800 | 1806 | } |
1801 | 1807 | |
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); | |
1804 | 1809 | |
1805 | 1810 | return NGX_OK; |
1806 | 1811 | } |
2728 | 2733 | * conf->client_large_buffers.num = 0; |
2729 | 2734 | */ |
2730 | 2735 | |
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)) | |
2732 | 2738 | == NGX_ERROR) |
2733 | 2739 | { |
2734 | 2740 | return NGX_CONF_ERROR; |
2760 | 2766 | ngx_http_core_srv_conf_t *conf = child; |
2761 | 2767 | |
2762 | 2768 | ngx_http_listen_t *ls; |
2769 | struct sockaddr_in *sin; | |
2763 | 2770 | ngx_http_server_name_t *sn; |
2764 | 2771 | |
2765 | 2772 | /* TODO: it does not merge, it inits only */ |
2772 | 2779 | |
2773 | 2780 | ngx_memzero(ls, sizeof(ngx_http_listen_t)); |
2774 | 2781 | |
2775 | ls->addr = INADDR_ANY; | |
2782 | sin = (struct sockaddr_in *) &ls->sockaddr; | |
2783 | ||
2784 | sin->sin_family = AF_INET; | |
2776 | 2785 | #if (NGX_WIN32) |
2777 | ls->port = 80; | |
2786 | sin->sin_port = htons(80); | |
2778 | 2787 | #else |
2779 | /* STUB: getuid() should be cached */ | |
2780 | ls->port = (getuid() == 0) ? 80 : 8000; | |
2788 | sin->sin_port = htons((getuid() == 0) ? 80 : 8000); | |
2781 | 2789 | #endif |
2782 | ls->family = AF_INET; | |
2790 | sin->sin_addr.s_addr = INADDR_ANY; | |
2783 | 2791 | |
2784 | 2792 | ls->conf.backlog = NGX_LISTEN_BACKLOG; |
2785 | 2793 | ls->conf.rcvbuf = -1; |
3158 | 3166 | } |
3159 | 3167 | |
3160 | 3168 | |
3161 | /* AF_INET only */ | |
3162 | ||
3163 | 3169 | static char * |
3164 | 3170 | ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
3165 | 3171 | { |
3200 | 3206 | |
3201 | 3207 | ngx_memzero(ls, sizeof(ngx_http_listen_t)); |
3202 | 3208 | |
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; | |
3206 | 3212 | ls->file_name = cf->conf_file->file.name.data; |
3207 | 3213 | ls->line = cf->conf_file->line; |
3208 | 3214 | ls->conf.backlog = NGX_LISTEN_BACKLOG; |
3209 | 3215 | ls->conf.rcvbuf = -1; |
3210 | 3216 | 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); | |
3214 | 3221 | |
3215 | 3222 | if (cf->args->nelts == 2) { |
3216 | 3223 | return NGX_CONF_OK; |
39 | 39 | typedef struct { |
40 | 40 | unsigned default_server:1; |
41 | 41 | unsigned bind:1; |
42 | unsigned wildcard:1; | |
42 | 43 | #if (NGX_HTTP_SSL) |
43 | 44 | unsigned ssl:1; |
44 | 45 | #endif |
54 | 55 | ngx_uint_t deferred_accept; |
55 | 56 | #endif |
56 | 57 | |
57 | u_char addr[NGX_INET_ADDRSTRLEN + sizeof(":65535")]; | |
58 | ||
58 | u_char addr[NGX_SOCKADDR_STRLEN + 1]; | |
59 | 59 | } ngx_http_listen_conf_t; |
60 | 60 | |
61 | 61 | |
62 | 62 | 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; | |
66 | 65 | |
67 | 66 | u_char *file_name; |
68 | 67 | ngx_uint_t line; |
172 | 171 | |
173 | 172 | |
174 | 173 | typedef struct { |
175 | in_addr_t addr; | |
176 | ||
177 | 174 | /* the default server configuration for this address:port */ |
178 | 175 | ngx_http_core_srv_conf_t *core_srv_conf; |
179 | 176 | |
182 | 179 | #if (NGX_HTTP_SSL) |
183 | 180 | ngx_uint_t ssl; /* unsigned ssl:1; */ |
184 | 181 | #endif |
182 | } ngx_http_addr_conf_t; | |
183 | ||
184 | ||
185 | typedef struct { | |
186 | in_addr_t addr; | |
187 | ngx_http_addr_conf_t conf; | |
185 | 188 | } 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 | |
186 | 199 | |
187 | 200 | |
188 | 201 | typedef struct { |
189 | 202 | in_port_t port; |
190 | 203 | 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; | |
192 | 207 | 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; | |
197 | 213 | 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; | |
204 | 221 | |
205 | 222 | ngx_hash_t hash; |
206 | 223 | ngx_hash_wildcard_t *wc_head; |
218 | 235 | |
219 | 236 | unsigned default_server:1; |
220 | 237 | unsigned bind:1; |
238 | unsigned wildcard:1; | |
221 | 239 | #if (NGX_HTTP_SSL) |
222 | 240 | unsigned ssl:1; |
223 | 241 | #endif |
224 | 242 | |
225 | 243 | ngx_http_listen_conf_t *listen_conf; |
226 | } ngx_http_conf_in_addr_t; | |
244 | } ngx_http_conf_addr_t; | |
227 | 245 | |
228 | 246 | |
229 | 247 | struct ngx_http_server_name_s { |
160 | 160 | ngx_table_elt_t *header; |
161 | 161 | ngx_http_core_loc_conf_t *clcf; |
162 | 162 | 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]; | |
165 | 164 | |
166 | 165 | r->header_sent = 1; |
167 | 166 | |
289 | 288 | host = r->headers_in.server; |
290 | 289 | |
291 | 290 | } else { |
291 | host.len = NGX_SOCKADDR_STRLEN; | |
292 | 292 | host.data = addr; |
293 | 293 | |
294 | 294 | if (ngx_http_server_addr(r, &host) != NGX_OK) { |
231 | 231 | ngx_uint_t i; |
232 | 232 | ngx_connection_t *c; |
233 | 233 | 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; | |
236 | 237 | ngx_http_log_ctx_t *ctx; |
238 | ngx_http_addr_conf_t *addr_conf; | |
237 | 239 | ngx_http_connection_t *hc; |
238 | 240 | ngx_http_core_srv_conf_t *cscf; |
239 | 241 | ngx_http_core_loc_conf_t *clcf; |
240 | 242 | 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 | |
241 | 247 | |
242 | 248 | #if (NGX_STAT_STUB) |
243 | 249 | ngx_atomic_fetch_add(ngx_stat_reading, -1); |
291 | 297 | |
292 | 298 | /* find the server configuration for the address:port */ |
293 | 299 | |
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; | |
303 | 304 | |
304 | 305 | r->connection = c; |
305 | 306 | |
306 | if (hip->naddrs > 1) { | |
307 | if (port->naddrs > 1) { | |
307 | 308 | |
308 | 309 | /* |
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 | |
314 | 313 | */ |
315 | 314 | |
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; | |
336 | 359 | } |
337 | 360 | |
338 | 361 | } 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; | |
343 | 381 | |
344 | 382 | /* the default server configuration for the address:port */ |
345 | cscf = hia[i].core_srv_conf; | |
383 | cscf = addr_conf->core_srv_conf; | |
346 | 384 | |
347 | 385 | r->main_conf = cscf->ctx->main_conf; |
348 | 386 | r->srv_conf = cscf->ctx->srv_conf; |
356 | 394 | ngx_http_ssl_srv_conf_t *sscf; |
357 | 395 | |
358 | 396 | 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) { | |
360 | 398 | |
361 | 399 | if (c->ssl == NULL) { |
362 | 400 | |
363 | 401 | c->log->action = "SSL handshaking"; |
364 | 402 | |
365 | if (hia[i].ssl && sscf->ssl.ctx == NULL) { | |
403 | if (addr_conf->ssl && sscf->ssl.ctx == NULL) { | |
366 | 404 | ngx_log_error(NGX_LOG_ERR, c->log, 0, |
367 | 405 | "no \"ssl_certificate\" is defined " |
368 | 406 | "in server listening on SSL port"); |
827 | 827 | ngx_http_variable_binary_remote_addr(ngx_http_request_t *r, |
828 | 828 | ngx_http_variable_value_t *v, uintptr_t data) |
829 | 829 | { |
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 | } | |
841 | 861 | |
842 | 862 | return NGX_OK; |
843 | 863 | } |
861 | 881 | ngx_http_variable_remote_port(ngx_http_request_t *r, |
862 | 882 | ngx_http_variable_value_t *v, uintptr_t data) |
863 | 883 | { |
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 | |
866 | 889 | |
867 | 890 | v->len = 0; |
868 | 891 | v->valid = 1; |
874 | 897 | return NGX_ERROR; |
875 | 898 | } |
876 | 899 | |
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 */ | |
880 | 910 | sin = (struct sockaddr_in *) r->connection->sockaddr; |
881 | ||
882 | 911 | 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; | |
887 | 917 | } |
888 | 918 | |
889 | 919 | return NGX_OK; |
895 | 925 | ngx_http_variable_value_t *v, uintptr_t data) |
896 | 926 | { |
897 | 927 | 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); | |
900 | 938 | if (s.data == NULL) { |
901 | 939 | return NGX_ERROR; |
902 | 940 | } |
903 | 941 | |
904 | if (ngx_http_server_addr(r, &s) != NGX_OK) { | |
905 | return NGX_ERROR; | |
906 | } | |
942 | ngx_memcpy(s.data, addr, s.len); | |
907 | 943 | |
908 | 944 | v->len = s.len; |
909 | 945 | v->valid = 1; |
304 | 304 | return NGX_CONF_ERROR; |
305 | 305 | } |
306 | 306 | |
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 | ||
307 | 312 | cmcf = ngx_mail_conf_get_module_main_conf(cf, ngx_mail_core_module); |
308 | 313 | |
309 | 314 | imls = cmcf->listen.elts; |