Mercurial > hg > nginx-quic
annotate src/core/ngx_inet.c @ 4818:28a64967e891
Removed a stale "AF_INET only" comment.
IPv6 client connections in mail modules have been supported since r2856.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Fri, 17 Aug 2012 05:14:19 +0000 |
parents | c070fa19511e |
children | 2efa05bbbc1e 032f71d771e6 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
4412 | 4 * Copyright (C) Nginx, Inc. |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
5 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
6 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
7 |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_config.h> |
100
7ebc8b7fb816
nginx-0.0.1-2003-06-03-19:42:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
57
diff
changeset
|
9 #include <ngx_core.h> |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
2200 | 12 static ngx_int_t ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u); |
13 static ngx_int_t ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u); | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
14 static ngx_int_t ngx_parse_inet6_url(ngx_pool_t *pool, ngx_url_t *u); |
2200 | 15 |
1647 | 16 |
17 in_addr_t | |
18 ngx_inet_addr(u_char *text, size_t len) | |
19 { | |
20 u_char *p, c; | |
21 in_addr_t addr; | |
22 ngx_uint_t octet, n; | |
23 | |
24 addr = 0; | |
25 octet = 0; | |
26 n = 0; | |
27 | |
28 for (p = text; p < text + len; p++) { | |
29 | |
30 c = *p; | |
31 | |
32 if (c >= '0' && c <= '9') { | |
33 octet = octet * 10 + (c - '0'); | |
34 continue; | |
35 } | |
36 | |
37 if (c == '.' && octet < 256) { | |
38 addr = (addr << 8) + octet; | |
39 octet = 0; | |
40 n++; | |
41 continue; | |
42 } | |
43 | |
44 return INADDR_NONE; | |
45 } | |
46 | |
4584
3d51fa5a110d
Reduced the number of lines of code in ngx_inet_addr().
Ruslan Ermilov <ru@nginx.com>
parents:
4582
diff
changeset
|
47 if (n == 3 && octet < 256) { |
1647 | 48 addr = (addr << 8) + octet; |
49 return htonl(addr); | |
50 } | |
51 | |
52 return INADDR_NONE; | |
53 } | |
54 | |
55 | |
3265 | 56 #if (NGX_HAVE_INET6) |
57 | |
58 ngx_int_t | |
59 ngx_inet6_addr(u_char *p, size_t len, u_char *addr) | |
60 { | |
61 u_char c, *zero, *digit, *s, *d; | |
62 size_t len4; | |
63 ngx_uint_t n, nibbles, word; | |
64 | |
65 if (len == 0) { | |
66 return NGX_ERROR; | |
67 } | |
68 | |
69 zero = NULL; | |
70 digit = NULL; | |
71 len4 = 0; | |
72 nibbles = 0; | |
73 word = 0; | |
74 n = 8; | |
75 | |
76 if (p[0] == ':') { | |
77 p++; | |
78 len--; | |
79 } | |
80 | |
81 for (/* void */; len; len--) { | |
82 c = *p++; | |
83 | |
84 if (c == ':') { | |
85 if (nibbles) { | |
86 digit = p; | |
87 len4 = len; | |
88 *addr++ = (u_char) (word >> 8); | |
89 *addr++ = (u_char) (word & 0xff); | |
90 | |
91 if (--n) { | |
92 nibbles = 0; | |
93 word = 0; | |
94 continue; | |
95 } | |
96 | |
97 } else { | |
98 if (zero == NULL) { | |
99 digit = p; | |
100 len4 = len; | |
101 zero = addr; | |
102 continue; | |
103 } | |
104 } | |
105 | |
106 return NGX_ERROR; | |
107 } | |
108 | |
109 if (c == '.' && nibbles) { | |
3894
4d4b5770616f
fix segfault in IPv6 parsing while processing invalid IPv4 address X.YYYY.Z
Igor Sysoev <igor@sysoev.ru>
parents:
3864
diff
changeset
|
110 if (n < 2 || digit == NULL) { |
3265 | 111 return NGX_ERROR; |
112 } | |
113 | |
114 word = ngx_inet_addr(digit, len4 - 1); | |
115 if (word == INADDR_NONE) { | |
116 return NGX_ERROR; | |
117 } | |
118 | |
119 word = ntohl(word); | |
120 *addr++ = (u_char) ((word >> 24) & 0xff); | |
121 *addr++ = (u_char) ((word >> 16) & 0xff); | |
122 n--; | |
123 break; | |
124 } | |
125 | |
126 if (++nibbles > 4) { | |
127 return NGX_ERROR; | |
128 } | |
129 | |
130 if (c >= '0' && c <= '9') { | |
131 word = word * 16 + (c - '0'); | |
132 continue; | |
133 } | |
134 | |
135 c |= 0x20; | |
136 | |
137 if (c >= 'a' && c <= 'f') { | |
138 word = word * 16 + (c - 'a') + 10; | |
139 continue; | |
140 } | |
141 | |
142 return NGX_ERROR; | |
143 } | |
144 | |
145 if (nibbles == 0 && zero == NULL) { | |
146 return NGX_ERROR; | |
147 } | |
148 | |
149 *addr++ = (u_char) (word >> 8); | |
150 *addr++ = (u_char) (word & 0xff); | |
151 | |
152 if (--n) { | |
153 if (zero) { | |
154 n *= 2; | |
155 s = addr - 1; | |
156 d = s + n; | |
157 while (s >= zero) { | |
158 *d-- = *s--; | |
159 } | |
160 ngx_memzero(zero, n); | |
161 return NGX_OK; | |
162 } | |
163 | |
164 } else { | |
165 if (zero == NULL) { | |
166 return NGX_OK; | |
167 } | |
168 } | |
169 | |
170 return NGX_ERROR; | |
171 } | |
172 | |
173 #endif | |
174 | |
175 | |
509 | 176 size_t |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
177 ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len, ngx_uint_t port) |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
178 { |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
179 u_char *p; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
180 struct sockaddr_in *sin; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
181 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
182 size_t n; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
183 struct sockaddr_in6 *sin6; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
184 #endif |
3230
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
185 #if (NGX_HAVE_UNIX_DOMAIN) |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
186 struct sockaddr_un *saun; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
187 #endif |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
188 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
189 switch (sa->sa_family) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
190 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
191 case AF_INET: |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
192 |
2197
74477ea8074f
*) remove zero termination in ngx_inet_ntop() and ngx_sock_ntop()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
193 sin = (struct sockaddr_in *) sa; |
74477ea8074f
*) remove zero termination in ngx_inet_ntop() and ngx_sock_ntop()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
194 p = (u_char *) &sin->sin_addr; |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
195 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
196 if (port) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
197 p = ngx_snprintf(text, len, "%ud.%ud.%ud.%ud:%d", |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
198 p[0], p[1], p[2], p[3], ntohs(sin->sin_port)); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
199 } else { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
200 p = ngx_snprintf(text, len, "%ud.%ud.%ud.%ud", |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
201 p[0], p[1], p[2], p[3]); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
202 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
203 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
204 return (p - text); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
205 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
206 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
207 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
208 case AF_INET6: |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
209 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
210 sin6 = (struct sockaddr_in6 *) sa; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
211 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
212 n = 0; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
213 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
214 if (port) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
215 text[n++] = '['; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
216 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
217 |
3268
c6a40c815d45
use sin6_addr.s6_addr instead of "(u_char *) & .sin6_addr"
Igor Sysoev <igor@sysoev.ru>
parents:
3267
diff
changeset
|
218 n = ngx_inet6_ntop(sin6->sin6_addr.s6_addr, &text[n], len); |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
219 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
220 if (port) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
221 n = ngx_sprintf(&text[1 + n], "]:%d", |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
222 ntohs(sin6->sin6_port)) - text; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
223 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
224 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
225 return n; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
226 #endif |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
227 |
3230
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
228 #if (NGX_HAVE_UNIX_DOMAIN) |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
229 |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
230 case AF_UNIX: |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
231 saun = (struct sockaddr_un *) sa; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
232 |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
233 /* we do not include trailing zero in address length */ |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
234 |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
235 return ngx_snprintf(text, len, "unix:%s%Z", saun->sun_path) - text - 1; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
236 |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
237 #endif |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
2856
diff
changeset
|
238 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
239 default: |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
240 return 0; |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
241 } |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
242 } |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
243 |
1830 | 244 |
509 | 245 size_t |
246 ngx_inet_ntop(int family, void *addr, u_char *text, size_t len) | |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
247 { |
2197
74477ea8074f
*) remove zero termination in ngx_inet_ntop() and ngx_sock_ntop()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
248 u_char *p; |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
249 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
250 switch (family) { |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
251 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
252 case AF_INET: |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
253 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
254 p = addr; |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
255 |
2197
74477ea8074f
*) remove zero termination in ngx_inet_ntop() and ngx_sock_ntop()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
256 return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud", |
74477ea8074f
*) remove zero termination in ngx_inet_ntop() and ngx_sock_ntop()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
257 p[0], p[1], p[2], p[3]) |
74477ea8074f
*) remove zero termination in ngx_inet_ntop() and ngx_sock_ntop()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
258 - text; |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
259 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
260 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
261 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
262 case AF_INET6: |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
263 return ngx_inet6_ntop(addr, text, len); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
264 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
265 #endif |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
266 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
267 default: |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
268 return 0; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
269 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
270 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
271 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
272 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
273 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
274 |
3276
beaf94f2f265
make ngx_inet6_ntop() non-static
Igor Sysoev <igor@sysoev.ru>
parents:
3272
diff
changeset
|
275 size_t |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
276 ngx_inet6_ntop(u_char *p, u_char *text, size_t len) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
277 { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
278 u_char *dst; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
279 size_t max, n; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
280 ngx_uint_t i, zero, last; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
281 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
282 if (len < NGX_INET6_ADDRSTRLEN) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
283 return 0; |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
284 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
285 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
286 zero = (ngx_uint_t) -1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
287 last = (ngx_uint_t) -1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
288 max = 1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
289 n = 0; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
290 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
291 for (i = 0; i < 16; i += 2) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
292 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
293 if (p[i] || p[i + 1]) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
294 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
295 if (max < n) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
296 zero = last; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
297 max = n; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
298 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
299 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
300 n = 0; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
301 continue; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
302 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
303 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
304 if (n++ == 0) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
305 last = i; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
306 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
307 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
308 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
309 if (max < n) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
310 zero = last; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
311 max = n; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
312 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
313 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
314 dst = text; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
315 n = 16; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
316 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
317 if (zero == 0) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
318 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
319 if ((max == 5 && p[10] == 0xff && p[11] == 0xff) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
320 || (max == 6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
321 || (max == 7 && p[14] != 0 && p[15] != 1)) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
322 { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
323 n = 12; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
324 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
325 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
326 *dst++ = ':'; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
327 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
328 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
329 for (i = 0; i < n; i += 2) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
330 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
331 if (i == zero) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
332 *dst++ = ':'; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
333 i += (max - 1) * 2; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
334 continue; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
335 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
336 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
337 dst = ngx_sprintf(dst, "%uxi", p[i] * 256 + p[i + 1]); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
338 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
339 if (i < 14) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
340 *dst++ = ':'; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
341 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
342 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
343 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
344 if (n == 12) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
345 dst = ngx_sprintf(dst, "%ud.%ud.%ud.%ud", p[12], p[13], p[14], p[15]); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
346 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
347 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
348 return dst - text; |
1830 | 349 } |
350 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
351 #endif |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
352 |
1830 | 353 |
509 | 354 ngx_int_t |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2517
diff
changeset
|
355 ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr) |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
356 { |
3277 | 357 u_char *addr, *mask, *last; |
358 size_t len; | |
359 ngx_int_t shift; | |
360 #if (NGX_HAVE_INET6) | |
361 ngx_int_t rc; | |
362 ngx_uint_t s, i; | |
363 #endif | |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
364 |
2202 | 365 addr = text->data; |
366 last = addr + text->len; | |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
367 |
2202 | 368 mask = ngx_strlchr(addr, last, '/'); |
3277 | 369 len = (mask ? mask : last) - addr; |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
370 |
3277 | 371 cidr->u.in.addr = ngx_inet_addr(addr, len); |
372 | |
373 if (cidr->u.in.addr != INADDR_NONE) { | |
374 cidr->family = AF_INET; | |
375 | |
376 if (mask == NULL) { | |
377 cidr->u.in.mask = 0xffffffff; | |
378 return NGX_OK; | |
379 } | |
2202 | 380 |
3277 | 381 #if (NGX_HAVE_INET6) |
382 } else if (ngx_inet6_addr(addr, len, cidr->u.in6.addr.s6_addr) == NGX_OK) { | |
383 cidr->family = AF_INET6; | |
384 | |
385 if (mask == NULL) { | |
386 ngx_memset(cidr->u.in6.mask.s6_addr, 0xff, 16); | |
387 return NGX_OK; | |
388 } | |
389 | |
390 #endif | |
391 } else { | |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
392 return NGX_ERROR; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
393 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
394 |
2202 | 395 mask++; |
396 | |
397 shift = ngx_atoi(mask, last - mask); | |
398 if (shift == NGX_ERROR) { | |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
399 return NGX_ERROR; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
400 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
401 |
3277 | 402 switch (cidr->family) { |
2537
a472d954c534
prepare ngx_ptocidr() for IPv6
Igor Sysoev <igor@sysoev.ru>
parents:
2517
diff
changeset
|
403 |
3277 | 404 #if (NGX_HAVE_INET6) |
405 case AF_INET6: | |
4573
98a2518a98dc
In ngx_ptocidr(), check that the supplied prefix length is within
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
406 if (shift > 128) { |
98a2518a98dc
In ngx_ptocidr(), check that the supplied prefix length is within
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
407 return NGX_ERROR; |
98a2518a98dc
In ngx_ptocidr(), check that the supplied prefix length is within
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
408 } |
98a2518a98dc
In ngx_ptocidr(), check that the supplied prefix length is within
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
409 |
3277 | 410 addr = cidr->u.in6.addr.s6_addr; |
411 mask = cidr->u.in6.mask.s6_addr; | |
412 rc = NGX_OK; | |
413 | |
414 for (i = 0; i < 16; i++) { | |
415 | |
416 s = (shift > 8) ? 8 : shift; | |
417 shift -= s; | |
418 | |
4582
a8881886a5f7
Improved readability of the code that produces bitmask from prefix.
Ruslan Ermilov <ru@nginx.com>
parents:
4573
diff
changeset
|
419 mask[i] = (u_char) (0xffu << (8 - s)); |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
420 |
3277 | 421 if (addr[i] != (addr[i] & mask[i])) { |
422 rc = NGX_DONE; | |
423 addr[i] &= mask[i]; | |
424 } | |
425 } | |
426 | |
427 return rc; | |
428 #endif | |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
429 |
3277 | 430 default: /* AF_INET */ |
4573
98a2518a98dc
In ngx_ptocidr(), check that the supplied prefix length is within
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
431 if (shift > 32) { |
98a2518a98dc
In ngx_ptocidr(), check that the supplied prefix length is within
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
432 return NGX_ERROR; |
98a2518a98dc
In ngx_ptocidr(), check that the supplied prefix length is within
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
433 } |
3277 | 434 |
435 if (shift) { | |
4582
a8881886a5f7
Improved readability of the code that produces bitmask from prefix.
Ruslan Ermilov <ru@nginx.com>
parents:
4573
diff
changeset
|
436 cidr->u.in.mask = htonl((uint32_t) (0xffffffffu << (32 - shift))); |
2202 | 437 |
3277 | 438 } else { |
439 /* x86 compilers use a shl instruction that shifts by modulo 32 */ | |
440 cidr->u.in.mask = 0; | |
441 } | |
442 | |
443 if (cidr->u.in.addr == (cidr->u.in.addr & cidr->u.in.mask)) { | |
2202 | 444 return NGX_OK; |
445 } | |
446 | |
3277 | 447 cidr->u.in.addr &= cidr->u.in.mask; |
448 | |
2202 | 449 return NGX_DONE; |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
450 } |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
451 } |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
452 |
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
453 |
3272
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
454 ngx_int_t |
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
455 ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text, size_t len) |
3270 | 456 { |
457 in_addr_t inaddr; | |
458 ngx_uint_t family; | |
459 struct sockaddr_in *sin; | |
460 #if (NGX_HAVE_INET6) | |
461 struct in6_addr inaddr6; | |
462 struct sockaddr_in6 *sin6; | |
3277 | 463 |
464 /* | |
4572
67653855682e
Fixed spelling in multiline C comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
465 * prevent MSVC8 warning: |
3277 | 466 * potentially uninitialized local variable 'inaddr6' used |
467 */ | |
468 ngx_memzero(inaddr6.s6_addr, sizeof(struct in6_addr)); | |
3270 | 469 #endif |
470 | |
3272
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
471 inaddr = ngx_inet_addr(text, len); |
3270 | 472 |
473 if (inaddr != INADDR_NONE) { | |
474 family = AF_INET; | |
475 len = sizeof(struct sockaddr_in); | |
476 | |
477 #if (NGX_HAVE_INET6) | |
3272
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
478 } else if (ngx_inet6_addr(text, len, inaddr6.s6_addr) == NGX_OK) { |
3270 | 479 family = AF_INET6; |
480 len = sizeof(struct sockaddr_in6); | |
481 | |
482 #endif | |
483 } else { | |
3272
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
484 return NGX_DECLINED; |
3270 | 485 } |
486 | |
3272
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
487 addr->sockaddr = ngx_pcalloc(pool, len); |
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
488 if (addr->sockaddr == NULL) { |
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
489 return NGX_ERROR; |
3270 | 490 } |
491 | |
3272
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
492 addr->sockaddr->sa_family = (u_char) family; |
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
493 addr->socklen = len; |
3270 | 494 |
495 switch (family) { | |
496 | |
497 #if (NGX_HAVE_INET6) | |
498 case AF_INET6: | |
3272
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
499 sin6 = (struct sockaddr_in6 *) addr->sockaddr; |
3270 | 500 ngx_memcpy(sin6->sin6_addr.s6_addr, inaddr6.s6_addr, 16); |
501 break; | |
502 #endif | |
503 | |
504 default: /* AF_INET */ | |
3272
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
505 sin = (struct sockaddr_in *) addr->sockaddr; |
3270 | 506 sin->sin_addr.s_addr = inaddr; |
507 break; | |
508 } | |
509 | |
3272
98f49b2bcae8
change ngx_parse_addr() interface
Igor Sysoev <igor@sysoev.ru>
parents:
3270
diff
changeset
|
510 return NGX_OK; |
3270 | 511 } |
512 | |
513 | |
651 | 514 ngx_int_t |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
515 ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u) |
651 | 516 { |
2200 | 517 u_char *p; |
651 | 518 |
519 p = u->url.data; | |
520 | |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
521 if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) { |
2200 | 522 return ngx_parse_unix_domain_url(pool, u); |
651 | 523 } |
524 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
525 if (p[0] == '[') { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
526 return ngx_parse_inet6_url(pool, u); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
527 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
528 |
2200 | 529 return ngx_parse_inet_url(pool, u); |
530 } | |
531 | |
532 | |
533 static ngx_int_t | |
534 ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u) | |
535 { | |
536 #if (NGX_HAVE_UNIX_DOMAIN) | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
537 u_char *path, *uri, *last; |
2200 | 538 size_t len; |
539 struct sockaddr_un *saun; | |
540 | |
541 len = u->url.len; | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
542 path = u->url.data; |
2200 | 543 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
544 path += 5; |
2200 | 545 len -= 5; |
546 | |
547 if (u->uri_part) { | |
548 | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
549 last = path + len; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
550 uri = ngx_strlchr(path, last, ':'); |
2200 | 551 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
552 if (uri) { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
553 len = uri - path; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
554 uri++; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
555 u->uri.len = last - uri; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
556 u->uri.data = uri; |
2200 | 557 } |
558 } | |
559 | |
560 if (len == 0) { | |
561 u->err = "no path in the unix domain socket"; | |
562 return NGX_ERROR; | |
563 } | |
564 | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
565 u->host.len = len++; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
566 u->host.data = path; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
567 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
568 if (len > sizeof(saun->sun_path)) { |
2200 | 569 u->err = "too long path in the unix domain socket"; |
570 return NGX_ERROR; | |
571 } | |
572 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
573 u->socklen = sizeof(struct sockaddr_un); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
574 saun = (struct sockaddr_un *) &u->sockaddr; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
575 saun->sun_family = AF_UNIX; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
576 (void) ngx_cpystrn((u_char *) saun->sun_path, path, len); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
577 |
3269
f0d596e84634
rename ngx_peer_addr_t to ngx_addr_t
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
578 u->addrs = ngx_pcalloc(pool, sizeof(ngx_addr_t)); |
2200 | 579 if (u->addrs == NULL) { |
580 return NGX_ERROR; | |
581 } | |
582 | |
583 saun = ngx_pcalloc(pool, sizeof(struct sockaddr_un)); | |
584 if (saun == NULL) { | |
585 return NGX_ERROR; | |
586 } | |
587 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
588 u->family = AF_UNIX; |
2200 | 589 u->naddrs = 1; |
590 | |
591 saun->sun_family = AF_UNIX; | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
592 (void) ngx_cpystrn((u_char *) saun->sun_path, path, len); |
2200 | 593 |
594 u->addrs[0].sockaddr = (struct sockaddr *) saun; | |
595 u->addrs[0].socklen = sizeof(struct sockaddr_un); | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
596 u->addrs[0].name.len = len + 4; |
2200 | 597 u->addrs[0].name.data = u->url.data; |
598 | |
599 return NGX_OK; | |
600 | |
601 #else | |
602 | |
603 u->err = "the unix domain sockets are not supported on this platform"; | |
604 | |
605 return NGX_ERROR; | |
606 | |
607 #endif | |
608 } | |
609 | |
610 | |
611 static ngx_int_t | |
612 ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u) | |
613 { | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
614 u_char *p, *host, *port, *last, *uri, *args; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
615 size_t len; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
616 ngx_int_t n; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
617 struct hostent *h; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
618 struct sockaddr_in *sin; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
619 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
620 u->socklen = sizeof(struct sockaddr_in); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
621 sin = (struct sockaddr_in *) &u->sockaddr; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
622 sin->sin_family = AF_INET; |
2200 | 623 |
2206 | 624 u->family = AF_INET; |
625 | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
626 host = u->url.data; |
2200 | 627 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
628 last = host + u->url.len; |
906 | 629 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
630 port = ngx_strlchr(host, last, ':'); |
651 | 631 |
2283
231b52f8adc0
fix case when URL has no port, but has ":" in URI part,
Igor Sysoev <igor@sysoev.ru>
parents:
2206
diff
changeset
|
632 uri = ngx_strlchr(host, last, '/'); |
651 | 633 |
2284
c3035feb6638
fix case proxy_pass URL is evaluted to http://host?args
Igor Sysoev <igor@sysoev.ru>
parents:
2283
diff
changeset
|
634 args = ngx_strlchr(host, last, '?'); |
c3035feb6638
fix case proxy_pass URL is evaluted to http://host?args
Igor Sysoev <igor@sysoev.ru>
parents:
2283
diff
changeset
|
635 |
c3035feb6638
fix case proxy_pass URL is evaluted to http://host?args
Igor Sysoev <igor@sysoev.ru>
parents:
2283
diff
changeset
|
636 if (args) { |
4652
215857d53cbe
Code reduction (no functional changes).
Ruslan Ermilov <ru@nginx.com>
parents:
4642
diff
changeset
|
637 if (uri == NULL || args < uri) { |
2284
c3035feb6638
fix case proxy_pass URL is evaluted to http://host?args
Igor Sysoev <igor@sysoev.ru>
parents:
2283
diff
changeset
|
638 uri = args; |
c3035feb6638
fix case proxy_pass URL is evaluted to http://host?args
Igor Sysoev <igor@sysoev.ru>
parents:
2283
diff
changeset
|
639 } |
c3035feb6638
fix case proxy_pass URL is evaluted to http://host?args
Igor Sysoev <igor@sysoev.ru>
parents:
2283
diff
changeset
|
640 } |
c3035feb6638
fix case proxy_pass URL is evaluted to http://host?args
Igor Sysoev <igor@sysoev.ru>
parents:
2283
diff
changeset
|
641 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
642 if (uri) { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
643 if (u->listen || !u->uri_part) { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
644 u->err = "invalid host"; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
645 return NGX_ERROR; |
651 | 646 } |
647 | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
648 u->uri.len = last - uri; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
649 u->uri.data = uri; |
651 | 650 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
651 last = uri; |
2283
231b52f8adc0
fix case when URL has no port, but has ":" in URI part,
Igor Sysoev <igor@sysoev.ru>
parents:
2206
diff
changeset
|
652 |
231b52f8adc0
fix case when URL has no port, but has ":" in URI part,
Igor Sysoev <igor@sysoev.ru>
parents:
2206
diff
changeset
|
653 if (uri < port) { |
231b52f8adc0
fix case when URL has no port, but has ":" in URI part,
Igor Sysoev <igor@sysoev.ru>
parents:
2206
diff
changeset
|
654 port = NULL; |
2301
148c2e840c2b
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
2284
diff
changeset
|
655 } |
651 | 656 } |
657 | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
658 if (port) { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
659 port++; |
651 | 660 |
2206 | 661 len = last - port; |
662 | |
663 n = ngx_atoi(port, len); | |
664 | |
4227 | 665 if (n < 1 || n > 65535) { |
2206 | 666 u->err = "invalid port"; |
667 return NGX_ERROR; | |
668 } | |
669 | |
670 u->port = (in_port_t) n; | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
671 sin->sin_port = htons((in_port_t) n); |
2206 | 672 |
673 u->port_text.len = len; | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
674 u->port_text.data = port; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
675 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
676 last = port - 1; |
1671
8075179cae21
ngx_parse_url() saves port text, this allows to use it in proxy host header
Igor Sysoev <igor@sysoev.ru>
parents:
1647
diff
changeset
|
677 |
651 | 678 } else { |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
679 if (uri == NULL) { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
680 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
681 if (u->listen) { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
682 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
683 /* test value as port only */ |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
684 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
685 n = ngx_atoi(host, last - host); |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
686 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
687 if (n != NGX_ERROR) { |
651 | 688 |
4227 | 689 if (n < 1 || n > 65535) { |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
690 u->err = "invalid port"; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
691 return NGX_ERROR; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
692 } |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
693 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
694 u->port = (in_port_t) n; |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
695 sin->sin_port = htons((in_port_t) n); |
651 | 696 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
697 u->port_text.len = last - host; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
698 u->port_text.data = host; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
699 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
700 u->wildcard = 1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
701 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
702 return NGX_OK; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
703 } |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
704 } |
651 | 705 } |
706 | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
707 u->no_port = 1; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
708 } |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
709 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
710 len = last - host; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
711 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
712 if (len == 0) { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
713 u->err = "no host"; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
714 return NGX_ERROR; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
715 } |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
716 |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
717 if (len == 1 && *host == '*') { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
718 len = 0; |
651 | 719 } |
720 | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
721 u->host.len = len; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
722 u->host.data = host; |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
723 |
2206 | 724 if (u->no_resolve) { |
725 return NGX_OK; | |
726 } | |
727 | |
3267
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
728 if (len) { |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
729 sin->sin_addr.s_addr = ngx_inet_addr(host, len); |
884 | 730 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
731 if (sin->sin_addr.s_addr == INADDR_NONE) { |
3267
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
732 p = ngx_alloc(++len, pool->log); |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
733 if (p == NULL) { |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
734 return NGX_ERROR; |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
735 } |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
736 |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
737 (void) ngx_cpystrn(p, host, len); |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
738 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
739 h = gethostbyname((const char *) p); |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
740 |
3267
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
741 ngx_free(p); |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
742 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
743 if (h == NULL || h->h_addr_list[0] == NULL) { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
744 u->err = "host not found"; |
804
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
745 return NGX_ERROR; |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
746 } |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
747 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
748 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[0]); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
749 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
750 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
751 if (sin->sin_addr.s_addr == INADDR_ANY) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
752 u->wildcard = 1; |
804
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
753 } |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
754 |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
755 } else { |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
756 sin->sin_addr.s_addr = INADDR_ANY; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
757 u->wildcard = 1; |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
758 } |
804
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
759 |
906 | 760 if (u->no_port) { |
761 u->port = u->default_port; | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
762 sin->sin_port = htons(u->default_port); |
906 | 763 } |
764 | |
2203
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
765 if (u->listen) { |
8e5bf1bc87e2
*) refactor ngx_parse_inet_url()
Igor Sysoev <igor@sysoev.ru>
parents:
2202
diff
changeset
|
766 return NGX_OK; |
906 | 767 } |
768 | |
4652
215857d53cbe
Code reduction (no functional changes).
Ruslan Ermilov <ru@nginx.com>
parents:
4642
diff
changeset
|
769 return ngx_inet_resolve_host(pool, u); |
651 | 770 } |
771 | |
772 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
773 static ngx_int_t |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
774 ngx_parse_inet6_url(ngx_pool_t *pool, ngx_url_t *u) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
775 { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
776 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
777 u_char *p, *host, *port, *last, *uri; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
778 size_t len; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
779 ngx_int_t n; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
780 struct sockaddr_in6 *sin6; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
781 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
782 u->socklen = sizeof(struct sockaddr_in6); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
783 sin6 = (struct sockaddr_in6 *) &u->sockaddr; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
784 sin6->sin6_family = AF_INET6; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
785 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
786 host = u->url.data + 1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
787 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
788 last = u->url.data + u->url.len; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
789 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
790 p = ngx_strlchr(host, last, ']'); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
791 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
792 if (p == NULL) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
793 u->err = "invalid host"; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
794 return NGX_ERROR; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
795 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
796 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
797 if (last - p) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
798 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
799 port = p + 1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
800 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
801 uri = ngx_strlchr(port, last, '/'); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
802 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
803 if (uri) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
804 if (u->listen || !u->uri_part) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
805 u->err = "invalid host"; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
806 return NGX_ERROR; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
807 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
808 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
809 u->uri.len = last - uri; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
810 u->uri.data = uri; |
4670
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
811 |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
812 last = uri; |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
813 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
814 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
815 if (*port == ':') { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
816 port++; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
817 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
818 len = last - port; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
819 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
820 n = ngx_atoi(port, len); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
821 |
4227 | 822 if (n < 1 || n > 65535) { |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
823 u->err = "invalid port"; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
824 return NGX_ERROR; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
825 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
826 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
827 u->port = (in_port_t) n; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
828 sin6->sin6_port = htons((in_port_t) n); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
829 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
830 u->port_text.len = len; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
831 u->port_text.data = port; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
832 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
833 } else { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
834 u->no_port = 1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
835 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
836 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
837 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
838 len = p - host; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
839 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
840 if (len == 0) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
841 u->err = "no host"; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
842 return NGX_ERROR; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
843 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
844 |
4670
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
845 u->host.len = len + 2; |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
846 u->host.data = host - 1; |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
847 |
3268
c6a40c815d45
use sin6_addr.s6_addr instead of "(u_char *) & .sin6_addr"
Igor Sysoev <igor@sysoev.ru>
parents:
3267
diff
changeset
|
848 if (ngx_inet6_addr(host, len, sin6->sin6_addr.s6_addr) != NGX_OK) { |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
849 u->err = "invalid IPv6 address"; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
850 return NGX_ERROR; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
851 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
852 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
853 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
854 u->wildcard = 1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
855 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
856 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
857 if (u->no_port) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
858 u->port = u->default_port; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
859 sin6->sin6_port = htons(u->default_port); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
860 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
861 |
4670
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
862 u->family = AF_INET6; |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
863 u->naddrs = 1; |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
864 |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
865 u->addrs = ngx_pcalloc(pool, sizeof(ngx_addr_t)); |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
866 if (u->addrs == NULL) { |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
867 return NGX_ERROR; |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
868 } |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
869 |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
870 sin6 = ngx_pcalloc(pool, sizeof(struct sockaddr_in6)); |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
871 if (sin6 == NULL) { |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
872 return NGX_ERROR; |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
873 } |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
874 |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
875 ngx_memcpy(sin6, u->sockaddr, sizeof(struct sockaddr_in6)); |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
876 |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
877 u->addrs[0].sockaddr = (struct sockaddr *) sin6; |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
878 u->addrs[0].socklen = sizeof(struct sockaddr_in6); |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
879 |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
880 p = ngx_pnalloc(pool, u->host.len + sizeof(":65535") - 1); |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
881 if (p == NULL) { |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
882 return NGX_ERROR; |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
883 } |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
884 |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
885 u->addrs[0].name.len = ngx_sprintf(p, "%V:%d", |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
886 &u->host, u->port) - p; |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
887 u->addrs[0].name.data = p; |
c070fa19511e
Support for IPv6 literals in proxy_pass and so on.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4652
diff
changeset
|
888 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
889 return NGX_OK; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
890 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
891 #else |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
892 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
893 u->err = "the INET6 sockets are not supported on this platform"; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
894 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
895 return NGX_ERROR; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
896 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
897 #endif |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
898 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
899 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
900 |
884 | 901 ngx_int_t |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
902 ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u) |
651 | 903 { |
906 | 904 u_char *p, *host; |
651 | 905 size_t len; |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
906 in_port_t port; |
651 | 907 in_addr_t in_addr; |
908 ngx_uint_t i; | |
909 struct hostent *h; | |
910 struct sockaddr_in *sin; | |
911 | |
912 /* AF_INET only */ | |
913 | |
2517
095f28fc0c4a
fix upstream port, introduced in r2513
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
914 port = htons(u->port); |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
915 |
3267
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
916 in_addr = ngx_inet_addr(u->host.data, u->host.len); |
651 | 917 |
918 if (in_addr == INADDR_NONE) { | |
3267
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
919 host = ngx_alloc(u->host.len + 1, pool->log); |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
920 if (host == NULL) { |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
921 return NGX_ERROR; |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
922 } |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
923 |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
924 (void) ngx_cpystrn(host, u->host.data, u->host.len + 1); |
fb4f7605505f
replace inet_addr() with ngx_inet_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
3266
diff
changeset
|
925 |
651 | 926 h = gethostbyname((char *) host); |
927 | |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
928 ngx_free(host); |
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
929 |
651 | 930 if (h == NULL || h->h_addr_list[0] == NULL) { |
884 | 931 u->err = "host not found"; |
932 return NGX_ERROR; | |
651 | 933 } |
934 | |
884 | 935 if (u->one_addr == 0) { |
936 for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ } | |
937 | |
938 } else { | |
939 i = 1; | |
940 } | |
651 | 941 |
942 /* MP: ngx_shared_palloc() */ | |
943 | |
3269
f0d596e84634
rename ngx_peer_addr_t to ngx_addr_t
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
944 u->addrs = ngx_pcalloc(pool, i * sizeof(ngx_addr_t)); |
884 | 945 if (u->addrs == NULL) { |
946 return NGX_ERROR; | |
651 | 947 } |
948 | |
884 | 949 u->naddrs = i; |
651 | 950 |
3864
8e77b5216894
fix u->one_addr handling in ngx_inet_resolve_host()
Igor Sysoev <igor@sysoev.ru>
parents:
3377
diff
changeset
|
951 for (i = 0; i < u->naddrs; i++) { |
651 | 952 |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
953 sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in)); |
651 | 954 if (sin == NULL) { |
884 | 955 return NGX_ERROR; |
651 | 956 } |
957 | |
958 sin->sin_family = AF_INET; | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
959 sin->sin_port = port; |
651 | 960 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]); |
961 | |
884 | 962 u->addrs[i].sockaddr = (struct sockaddr *) sin; |
963 u->addrs[i].socklen = sizeof(struct sockaddr_in); | |
651 | 964 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
965 len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1; |
651 | 966 |
2049 | 967 p = ngx_pnalloc(pool, len); |
906 | 968 if (p == NULL) { |
884 | 969 return NGX_ERROR; |
651 | 970 } |
971 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
972 len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1); |
651 | 973 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
974 u->addrs[i].name.len = len; |
906 | 975 u->addrs[i].name.data = p; |
651 | 976 } |
977 | |
978 } else { | |
979 | |
980 /* MP: ngx_shared_palloc() */ | |
981 | |
3269
f0d596e84634
rename ngx_peer_addr_t to ngx_addr_t
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
982 u->addrs = ngx_pcalloc(pool, sizeof(ngx_addr_t)); |
884 | 983 if (u->addrs == NULL) { |
984 return NGX_ERROR; | |
651 | 985 } |
986 | |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1380
diff
changeset
|
987 sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in)); |
651 | 988 if (sin == NULL) { |
884 | 989 return NGX_ERROR; |
651 | 990 } |
991 | |
884 | 992 u->naddrs = 1; |
651 | 993 |
994 sin->sin_family = AF_INET; | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
995 sin->sin_port = port; |
651 | 996 sin->sin_addr.s_addr = in_addr; |
997 | |
884 | 998 u->addrs[0].sockaddr = (struct sockaddr *) sin; |
999 u->addrs[0].socklen = sizeof(struct sockaddr_in); | |
651 | 1000 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
1001 p = ngx_pnalloc(pool, u->host.len + sizeof(":65535") - 1); |
906 | 1002 if (p == NULL) { |
884 | 1003 return NGX_ERROR; |
651 | 1004 } |
1005 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
1006 u->addrs[0].name.len = ngx_sprintf(p, "%V:%d", |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2301
diff
changeset
|
1007 &u->host, ntohs(port)) - p; |
906 | 1008 u->addrs[0].name.data = p; |
651 | 1009 } |
1010 | |
884 | 1011 return NGX_OK; |
651 | 1012 } |