blob: 497d84a3ced571b390ab4048d500059c87db9ce4 [file] [log] [blame]
Igor Sysoev42feecb2002-12-15 06:25:09 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev42feecb2002-12-15 06:25:09 +00007#include <ngx_config.h>
Igor Sysoev1c104622003-06-03 15:42:58 +00008#include <ngx_core.h>
Igor Sysoev42feecb2002-12-15 06:25:09 +00009
10
Igor Sysoeva35eacc2009-02-21 07:02:02 +000011#if (NGX_HAVE_INET6)
12static size_t ngx_inet6_ntop(u_char *p, u_char *text, size_t len);
13#endif
Igor Sysoev154013c2008-08-22 13:36:56 +000014static ngx_int_t ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u);
15static ngx_int_t ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u);
Igor Sysoeva35eacc2009-02-21 07:02:02 +000016static ngx_int_t ngx_parse_inet6_url(ngx_pool_t *pool, ngx_url_t *u);
Igor Sysoev154013c2008-08-22 13:36:56 +000017
18
Igor Sysoev36b634c2007-11-23 16:59:24 +000019in_addr_t
20ngx_inet_addr(u_char *text, size_t len)
21{
22 u_char *p, c;
23 in_addr_t addr;
24 ngx_uint_t octet, n;
25
26 addr = 0;
27 octet = 0;
28 n = 0;
29
30 for (p = text; p < text + len; p++) {
31
32 c = *p;
33
34 if (c >= '0' && c <= '9') {
35 octet = octet * 10 + (c - '0');
36 continue;
37 }
38
39 if (c == '.' && octet < 256) {
40 addr = (addr << 8) + octet;
41 octet = 0;
42 n++;
43 continue;
44 }
45
46 return INADDR_NONE;
47 }
48
49 if (n != 3) {
50 return INADDR_NONE;
51 }
52
53 if (octet < 256) {
54 addr = (addr << 8) + octet;
55 return htonl(addr);
56 }
57
58 return INADDR_NONE;
59}
60
61
Igor Sysoev899b44e2005-05-12 14:58:06 +000062size_t
Igor Sysoeva35eacc2009-02-21 07:02:02 +000063ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len, ngx_uint_t port)
Igor Sysoev86de4cb2003-01-30 07:28:09 +000064{
Igor Sysoeva35eacc2009-02-21 07:02:02 +000065 u_char *p;
66 struct sockaddr_in *sin;
67#if (NGX_HAVE_INET6)
68 size_t n;
69 struct sockaddr_in6 *sin6;
70#endif
Igor Sysoev86de4cb2003-01-30 07:28:09 +000071
Igor Sysoeva35eacc2009-02-21 07:02:02 +000072 switch (sa->sa_family) {
73
74 case AF_INET:
Igor Sysoev6a750192008-08-21 18:47:23 +000075
76 sin = (struct sockaddr_in *) sa;
77 p = (u_char *) &sin->sin_addr;
78
Igor Sysoeva35eacc2009-02-21 07:02:02 +000079 if (port) {
80 p = ngx_snprintf(text, len, "%ud.%ud.%ud.%ud:%d",
81 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
82 } else {
83 p = ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
84 p[0], p[1], p[2], p[3]);
85 }
Igor Sysoev9c610952004-03-16 13:35:20 +000086
Igor Sysoeva35eacc2009-02-21 07:02:02 +000087 return (p - text);
88
89#if (NGX_HAVE_INET6)
90
91 case AF_INET6:
92
93 sin6 = (struct sockaddr_in6 *) sa;
94
95 n = 0;
96
97 if (port) {
98 text[n++] = '[';
99 }
100
101 n = ngx_inet6_ntop((u_char *) &sin6->sin6_addr, &text[n], len);
102
103 if (port) {
104 n = ngx_sprintf(&text[1 + n], "]:%d",
105 ntohs(sin6->sin6_port)) - text;
106 }
107
108 return n;
109#endif
110
111 default:
112 return 0;
113 }
Igor Sysoev86de4cb2003-01-30 07:28:09 +0000114}
115
Igor Sysoev02729772008-01-04 11:54:55 +0000116
Igor Sysoev899b44e2005-05-12 14:58:06 +0000117size_t
118ngx_inet_ntop(int family, void *addr, u_char *text, size_t len)
Igor Sysoev42feecb2002-12-15 06:25:09 +0000119{
Igor Sysoev6a750192008-08-21 18:47:23 +0000120 u_char *p;
Igor Sysoev9c610952004-03-16 13:35:20 +0000121
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000122 switch (family) {
Igor Sysoev6a750192008-08-21 18:47:23 +0000123
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000124 case AF_INET:
125
126 p = addr;
Igor Sysoev6a750192008-08-21 18:47:23 +0000127
128 return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
129 p[0], p[1], p[2], p[3])
130 - text;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000131
132#if (NGX_HAVE_INET6)
133
134 case AF_INET6:
135 return ngx_inet6_ntop(addr, text, len);
136
137#endif
138
139 default:
140 return 0;
141 }
142}
143
144
145#if (NGX_HAVE_INET6)
146
147static size_t
148ngx_inet6_ntop(u_char *p, u_char *text, size_t len)
149{
150 u_char *dst;
151 size_t max, n;
152 ngx_uint_t i, zero, last;
153
154 if (len < NGX_INET6_ADDRSTRLEN) {
155 return 0;
Igor Sysoev9c610952004-03-16 13:35:20 +0000156 }
157
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000158 zero = (ngx_uint_t) -1;
159 last = (ngx_uint_t) -1;
160 max = 1;
161 n = 0;
162
163 for (i = 0; i < 16; i += 2) {
164
165 if (p[i] || p[i + 1]) {
166
167 if (max < n) {
168 zero = last;
169 max = n;
170 }
171
172 n = 0;
173 continue;
174 }
175
176 if (n++ == 0) {
177 last = i;
178 }
179 }
180
181 if (max < n) {
182 zero = last;
183 max = n;
184 }
185
186 dst = text;
187 n = 16;
188
189 if (zero == 0) {
190
191 if ((max == 5 && p[10] == 0xff && p[11] == 0xff)
192 || (max == 6)
193 || (max == 7 && p[14] != 0 && p[15] != 1))
194 {
195 n = 12;
196 }
197
198 *dst++ = ':';
199 }
200
201 for (i = 0; i < n; i += 2) {
202
203 if (i == zero) {
204 *dst++ = ':';
205 i += (max - 1) * 2;
206 continue;
207 }
208
209 dst = ngx_sprintf(dst, "%uxi", p[i] * 256 + p[i + 1]);
210
211 if (i < 14) {
212 *dst++ = ':';
213 }
214 }
215
216 if (n == 12) {
217 dst = ngx_sprintf(dst, "%ud.%ud.%ud.%ud", p[12], p[13], p[14], p[15]);
218 }
219
220 return dst - text;
Igor Sysoev02729772008-01-04 11:54:55 +0000221}
222
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000223#endif
224
Igor Sysoev02729772008-01-04 11:54:55 +0000225
Igor Sysoev822834e2004-05-25 15:28:46 +0000226/* AF_INET only */
227
Igor Sysoev899b44e2005-05-12 14:58:06 +0000228ngx_int_t
229ngx_ptocidr(ngx_str_t *text, void *cidr)
Igor Sysoev822834e2004-05-25 15:28:46 +0000230{
Igor Sysoev9c388c02008-08-26 14:19:37 +0000231 u_char *addr, *mask, *last;
232 ngx_int_t shift;
Igor Sysoev822834e2004-05-25 15:28:46 +0000233 ngx_inet_cidr_t *in_cidr;
234
235 in_cidr = cidr;
Igor Sysoev9c388c02008-08-26 14:19:37 +0000236 addr = text->data;
237 last = addr + text->len;
Igor Sysoev822834e2004-05-25 15:28:46 +0000238
Igor Sysoev9c388c02008-08-26 14:19:37 +0000239 mask = ngx_strlchr(addr, last, '/');
Igor Sysoev822834e2004-05-25 15:28:46 +0000240
Igor Sysoev9c388c02008-08-26 14:19:37 +0000241 in_cidr->addr = ngx_inet_addr(addr, (mask ? mask : last) - addr);
Igor Sysoev822834e2004-05-25 15:28:46 +0000242
Igor Sysoev822834e2004-05-25 15:28:46 +0000243 if (in_cidr->addr == INADDR_NONE) {
244 return NGX_ERROR;
245 }
246
Igor Sysoev9c388c02008-08-26 14:19:37 +0000247 if (mask == NULL) {
248 in_cidr->mask = 0xffffffff;
249 return NGX_OK;
250 }
251
252 mask++;
253
254 shift = ngx_atoi(mask, last - mask);
255 if (shift == NGX_ERROR) {
Igor Sysoev822834e2004-05-25 15:28:46 +0000256 return NGX_ERROR;
257 }
258
Igor Sysoev9c388c02008-08-26 14:19:37 +0000259 if (shift == 0) {
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000260
261 /* the x86 compilers use the shl instruction that shifts by modulo 32 */
262
263 in_cidr->mask = 0;
Igor Sysoev9c388c02008-08-26 14:19:37 +0000264
265 if (in_cidr->addr == 0) {
266 return NGX_OK;
267 }
268
269 return NGX_DONE;
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000270 }
271
Igor Sysoev9c388c02008-08-26 14:19:37 +0000272 in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - shift))));
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000273
Igor Sysoev845f6d52007-08-10 13:13:28 +0000274 if (in_cidr->addr == (in_cidr->addr & in_cidr->mask)) {
275 return NGX_OK;
276 }
277
278 in_cidr->addr &= in_cidr->mask;
279
280 return NGX_DONE;
Igor Sysoev822834e2004-05-25 15:28:46 +0000281}
Igor Sysoeve2ff3ea2004-09-14 15:55:24 +0000282
283
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000284ngx_int_t
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000285ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u)
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000286{
Igor Sysoev154013c2008-08-22 13:36:56 +0000287 u_char *p;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000288
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000289 p = u->url.data;
290
Igor Sysoev722231f2007-02-14 18:51:19 +0000291 if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) {
Igor Sysoev154013c2008-08-22 13:36:56 +0000292 return ngx_parse_unix_domain_url(pool, u);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000293 }
294
295 if ((p[0] == ':' || p[0] == '/') && !u->listen) {
296 u->err = "invalid host";
297 return NGX_ERROR;
298 }
299
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000300 if (p[0] == '[') {
301 return ngx_parse_inet6_url(pool, u);
302 }
303
Igor Sysoev154013c2008-08-22 13:36:56 +0000304 return ngx_parse_inet_url(pool, u);
305}
306
307
308static ngx_int_t
309ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
310{
311#if (NGX_HAVE_UNIX_DOMAIN)
Igor Sysoevc9491d12008-08-26 14:24:14 +0000312 u_char *path, *uri, *last;
Igor Sysoev154013c2008-08-22 13:36:56 +0000313 size_t len;
Igor Sysoev154013c2008-08-22 13:36:56 +0000314 struct sockaddr_un *saun;
315
316 len = u->url.len;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000317 path = u->url.data;
Igor Sysoev154013c2008-08-22 13:36:56 +0000318
Igor Sysoevc9491d12008-08-26 14:24:14 +0000319 path += 5;
Igor Sysoev154013c2008-08-22 13:36:56 +0000320 len -= 5;
321
Igor Sysoev154013c2008-08-22 13:36:56 +0000322 if (u->uri_part) {
Igor Sysoev154013c2008-08-22 13:36:56 +0000323
Igor Sysoevc9491d12008-08-26 14:24:14 +0000324 last = path + len;
325 uri = ngx_strlchr(path, last, ':');
Igor Sysoev154013c2008-08-22 13:36:56 +0000326
Igor Sysoevc9491d12008-08-26 14:24:14 +0000327 if (uri) {
328 len = uri - path;
329 uri++;
330 u->uri.len = last - uri;
331 u->uri.data = uri;
Igor Sysoev154013c2008-08-22 13:36:56 +0000332 }
333 }
334
335 if (len == 0) {
336 u->err = "no path in the unix domain socket";
337 return NGX_ERROR;
338 }
339
Igor Sysoevc9491d12008-08-26 14:24:14 +0000340 u->host.len = len++;
341 u->host.data = path;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000342
343 if (len > sizeof(saun->sun_path)) {
Igor Sysoev154013c2008-08-22 13:36:56 +0000344 u->err = "too long path in the unix domain socket";
345 return NGX_ERROR;
346 }
347
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000348 u->socklen = sizeof(struct sockaddr_un);
349 saun = (struct sockaddr_un *) &u->sockaddr;
350 saun->sun_family = AF_UNIX;
351 (void) ngx_cpystrn((u_char *) saun->sun_path, path, len);
352
Igor Sysoev154013c2008-08-22 13:36:56 +0000353 u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t));
354 if (u->addrs == NULL) {
355 return NGX_ERROR;
356 }
357
358 saun = ngx_pcalloc(pool, sizeof(struct sockaddr_un));
359 if (saun == NULL) {
360 return NGX_ERROR;
361 }
362
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000363 u->family = AF_UNIX;
Igor Sysoev154013c2008-08-22 13:36:56 +0000364 u->naddrs = 1;
365
366 saun->sun_family = AF_UNIX;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000367 (void) ngx_cpystrn((u_char *) saun->sun_path, path, len);
Igor Sysoev154013c2008-08-22 13:36:56 +0000368
369 u->addrs[0].sockaddr = (struct sockaddr *) saun;
370 u->addrs[0].socklen = sizeof(struct sockaddr_un);
Igor Sysoevc9491d12008-08-26 14:24:14 +0000371 u->addrs[0].name.len = len + 4;
Igor Sysoev154013c2008-08-22 13:36:56 +0000372 u->addrs[0].name.data = u->url.data;
373
Igor Sysoev154013c2008-08-22 13:36:56 +0000374 return NGX_OK;
375
376#else
377
378 u->err = "the unix domain sockets are not supported on this platform";
379
380 return NGX_ERROR;
381
382#endif
383}
384
385
386static ngx_int_t
387ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)
388{
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000389 u_char *p, *host, *port, *last, *uri, *args;
390 size_t len;
391 ngx_int_t n;
392 struct hostent *h;
393 struct sockaddr_in *sin;
394
395 u->socklen = sizeof(struct sockaddr_in);
396 sin = (struct sockaddr_in *) &u->sockaddr;
397 sin->sin_family = AF_INET;
Igor Sysoev154013c2008-08-22 13:36:56 +0000398
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000399 u->family = AF_INET;
400
Igor Sysoevc9491d12008-08-26 14:24:14 +0000401 host = u->url.data;
Igor Sysoev154013c2008-08-22 13:36:56 +0000402
Igor Sysoevc9491d12008-08-26 14:24:14 +0000403 last = host + u->url.len;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000404
Igor Sysoevc9491d12008-08-26 14:24:14 +0000405 port = ngx_strlchr(host, last, ':');
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000406
Igor Sysoevc239da52008-10-24 15:12:11 +0000407 uri = ngx_strlchr(host, last, '/');
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000408
Igor Sysoev164abfb2008-10-24 19:34:24 +0000409 args = ngx_strlchr(host, last, '?');
410
411 if (args) {
412 if (uri == NULL) {
413 uri = args;
414
415 } else if (args < uri) {
416 uri = args;
417 }
418 }
419
Igor Sysoevc9491d12008-08-26 14:24:14 +0000420 if (uri) {
421 if (u->listen || !u->uri_part) {
422 u->err = "invalid host";
423 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000424 }
425
Igor Sysoevc9491d12008-08-26 14:24:14 +0000426 u->uri.len = last - uri;
427 u->uri.data = uri;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000428
Igor Sysoevc9491d12008-08-26 14:24:14 +0000429 last = uri;
Igor Sysoevc239da52008-10-24 15:12:11 +0000430
431 if (uri < port) {
432 port = NULL;
Igor Sysoevead80912008-11-11 16:04:05 +0000433 }
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000434 }
435
Igor Sysoevc9491d12008-08-26 14:24:14 +0000436 if (port) {
437 port++;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000438
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000439 len = last - port;
440
441 if (len == 0) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000442 u->err = "invalid port";
443 return NGX_ERROR;
444 }
445
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000446 n = ngx_atoi(port, len);
447
448 if (n < 1 || n > 65536) {
449 u->err = "invalid port";
450 return NGX_ERROR;
451 }
452
453 u->port = (in_port_t) n;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000454 sin->sin_port = htons((in_port_t) n);
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000455
456 u->port_text.len = len;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000457 u->port_text.data = port;
458
459 last = port - 1;
Igor Sysoev00e03772007-11-28 19:55:31 +0000460
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000461 } else {
Igor Sysoevc9491d12008-08-26 14:24:14 +0000462 if (uri == NULL) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000463
Igor Sysoevc9491d12008-08-26 14:24:14 +0000464 if (u->listen) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000465
Igor Sysoevc9491d12008-08-26 14:24:14 +0000466 /* test value as port only */
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000467
Igor Sysoevc9491d12008-08-26 14:24:14 +0000468 n = ngx_atoi(host, last - host);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000469
Igor Sysoevc9491d12008-08-26 14:24:14 +0000470 if (n != NGX_ERROR) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000471
Igor Sysoevc9491d12008-08-26 14:24:14 +0000472 if (n < 1 || n > 65536) {
473 u->err = "invalid port";
474 return NGX_ERROR;
475 }
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000476
Igor Sysoevc9491d12008-08-26 14:24:14 +0000477 u->port = (in_port_t) n;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000478 sin->sin_port = htons((in_port_t) n);
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000479
Igor Sysoevc9491d12008-08-26 14:24:14 +0000480 u->port_text.len = last - host;
481 u->port_text.data = host;
Igor Sysoev20bf47b2006-10-24 13:06:55 +0000482
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000483 u->wildcard = 1;
484
Igor Sysoevc9491d12008-08-26 14:24:14 +0000485 return NGX_OK;
Igor Sysoev20bf47b2006-10-24 13:06:55 +0000486 }
Igor Sysoev20bf47b2006-10-24 13:06:55 +0000487 }
Igor Sysoev20bf47b2006-10-24 13:06:55 +0000488 }
489
Igor Sysoevc9491d12008-08-26 14:24:14 +0000490 u->no_port = 1;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000491 }
492
Igor Sysoevc9491d12008-08-26 14:24:14 +0000493 len = last - host;
494
495 if (len == 0) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000496 u->err = "no host";
497 return NGX_ERROR;
498 }
499
Igor Sysoevc9491d12008-08-26 14:24:14 +0000500 if (len == 1 && *host == '*') {
501 len = 0;
502 }
503
504 u->host.len = len;
505 u->host.data = host;
506
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000507 if (u->no_resolve) {
508 return NGX_OK;
509 }
510
Igor Sysoevc9491d12008-08-26 14:24:14 +0000511 if (len++) {
512
513 p = ngx_alloc(len, pool->log);
514 if (p == NULL) {
515 return NGX_ERROR;
516 }
517
518 (void) ngx_cpystrn(p, host, len);
519
520 u->addr.in_addr = inet_addr((const char *) p);
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000521 sin->sin_addr.s_addr = inet_addr((const char *) p);
Igor Sysoevc9491d12008-08-26 14:24:14 +0000522
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000523 if (sin->sin_addr.s_addr == INADDR_NONE) {
Igor Sysoevc9491d12008-08-26 14:24:14 +0000524 h = gethostbyname((const char *) p);
525
526 if (h == NULL || h->h_addr_list[0] == NULL) {
527 ngx_free(p);
528 u->err = "host not found";
529 return NGX_ERROR;
530 }
531
532 u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000533 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[0]);
534 }
535
536 if (sin->sin_addr.s_addr == INADDR_ANY) {
537 u->wildcard = 1;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000538 }
539
540 ngx_free(p);
541
542 } else {
543 u->addr.in_addr = INADDR_ANY;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000544 sin->sin_addr.s_addr = INADDR_ANY;
545 u->wildcard = 1;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000546 }
547
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000548 if (u->no_port) {
549 u->port = u->default_port;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000550 sin->sin_port = htons(u->default_port);
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000551 }
552
Igor Sysoevc9491d12008-08-26 14:24:14 +0000553 if (u->listen) {
554 return NGX_OK;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000555 }
556
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000557 if (ngx_inet_resolve_host(pool, u) != NGX_OK) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000558 return NGX_ERROR;
559 }
560
561 return NGX_OK;
562}
563
564
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000565static ngx_int_t
566ngx_parse_inet6_url(ngx_pool_t *pool, ngx_url_t *u)
567{
568#if (NGX_HAVE_INET6)
569 int rc;
570 u_char *p, *host, *port, *last, *uri;
571 size_t len;
572 ngx_int_t n;
573 struct sockaddr_in6 *sin6;
574
575 u->socklen = sizeof(struct sockaddr_in6);
576 sin6 = (struct sockaddr_in6 *) &u->sockaddr;
577 sin6->sin6_family = AF_INET6;
578
579 host = u->url.data + 1;
580
581 last = u->url.data + u->url.len;
582
583 p = ngx_strlchr(host, last, ']');
584
585 if (p == NULL) {
586 u->err = "invalid host";
587 return NGX_ERROR;
588 }
589
590 if (last - p) {
591
592 port = p + 1;
593
594 uri = ngx_strlchr(port, last, '/');
595
596 if (uri) {
597 if (u->listen || !u->uri_part) {
598 u->err = "invalid host";
599 return NGX_ERROR;
600 }
601
602 u->uri.len = last - uri;
603 u->uri.data = uri;
604 }
605
606 if (*port == ':') {
607 port++;
608
609 len = last - port;
610
611 if (len == 0) {
612 u->err = "invalid port";
613 return NGX_ERROR;
614 }
615
616 n = ngx_atoi(port, len);
617
618 if (n < 1 || n > 65536) {
619 u->err = "invalid port";
620 return NGX_ERROR;
621 }
622
623 u->port = (in_port_t) n;
624 sin6->sin6_port = htons((in_port_t) n);
625
626 u->port_text.len = len;
627 u->port_text.data = port;
628
629 } else {
630 u->no_port = 1;
631 }
632 }
633
634 len = p - host;
635
636 if (len == 0) {
637 u->err = "no host";
638 return NGX_ERROR;
639 }
640
641 u->host.len = len++;
642 u->host.data = host;
643
644 p = ngx_alloc(len, pool->log);
645 if (p == NULL) {
646 return NGX_ERROR;
647 }
648
649 (void) ngx_cpystrn(p, host, len);
650
651 rc = inet_pton(AF_INET6, (const char *) p, &sin6->sin6_addr);
652
653 ngx_free(p);
654
655 if (rc == 0) {
656 u->err = "invalid IPv6 address";
657 return NGX_ERROR;
658 }
659
660 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
661 u->wildcard = 1;
662 }
663
664 u->family = AF_INET6;
665
666 if (u->no_resolve) {
667 return NGX_OK;
668 }
669
670 if (u->no_port) {
671 u->port = u->default_port;
672 sin6->sin6_port = htons(u->default_port);
673 }
674
675 return NGX_OK;
676
677#else
678
679 u->err = "the INET6 sockets are not supported on this platform";
680
681 return NGX_ERROR;
682
683#endif
684}
685
686
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000687ngx_int_t
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000688ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000689{
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000690 u_char *p, *host;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000691 size_t len;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000692 in_port_t port;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000693 in_addr_t in_addr;
694 ngx_uint_t i;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000695 struct hostent *h;
696 struct sockaddr_in *sin;
697
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000698 host = ngx_alloc(u->host.len + 1, pool->log);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000699 if (host == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000700 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000701 }
702
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000703 (void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000704
705 /* AF_INET only */
706
Igor Sysoev2bc44ea2009-02-21 14:34:32 +0000707 port = htons(u->port);
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000708
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000709 in_addr = inet_addr((char *) host);
710
711 if (in_addr == INADDR_NONE) {
712 h = gethostbyname((char *) host);
713
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000714 ngx_free(host);
715
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000716 if (h == NULL || h->h_addr_list[0] == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000717 u->err = "host not found";
718 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000719 }
720
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000721 if (u->one_addr == 0) {
722 for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ }
723
724 } else {
725 i = 1;
726 }
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000727
728 /* MP: ngx_shared_palloc() */
729
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000730 u->addrs = ngx_pcalloc(pool, i * sizeof(ngx_peer_addr_t));
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000731 if (u->addrs == NULL) {
732 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000733 }
734
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000735 u->naddrs = i;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000736
737 for (i = 0; h->h_addr_list[i] != NULL; i++) {
738
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000739 sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in));
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000740 if (sin == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000741 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000742 }
743
744 sin->sin_family = AF_INET;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000745 sin->sin_port = port;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000746 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
747
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000748 u->addrs[i].sockaddr = (struct sockaddr *) sin;
749 u->addrs[i].socklen = sizeof(struct sockaddr_in);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000750
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000751 len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000752
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +0000753 p = ngx_pnalloc(pool, len);
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000754 if (p == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000755 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000756 }
757
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000758 len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000759
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000760 u->addrs[i].name.len = len;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000761 u->addrs[i].name.data = p;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000762 }
763
764 } else {
765
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000766 ngx_free(host);
767
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000768 /* MP: ngx_shared_palloc() */
769
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000770 u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t));
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000771 if (u->addrs == NULL) {
772 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000773 }
774
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000775 sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in));
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000776 if (sin == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000777 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000778 }
779
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000780 u->naddrs = 1;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000781
782 sin->sin_family = AF_INET;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000783 sin->sin_port = port;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000784 sin->sin_addr.s_addr = in_addr;
785
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000786 u->addrs[0].sockaddr = (struct sockaddr *) sin;
787 u->addrs[0].socklen = sizeof(struct sockaddr_in);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000788
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000789 p = ngx_pnalloc(pool, u->host.len + sizeof(":65535") - 1);
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000790 if (p == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000791 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000792 }
793
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000794 u->addrs[0].name.len = ngx_sprintf(p, "%V:%d",
795 &u->host, ntohs(port)) - p;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000796 u->addrs[0].name.data = p;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000797 }
798
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000799 return NGX_OK;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000800}