blob: 4c180361d6a4506714f75220781c469934479a4e [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
Igor Sysoev36860102009-02-24 14:01:40 +0000229ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr)
Igor Sysoev822834e2004-05-25 15:28:46 +0000230{
Igor Sysoev36860102009-02-24 14:01:40 +0000231 u_char *addr, *mask, *last;
232 ngx_int_t shift;
Igor Sysoev822834e2004-05-25 15:28:46 +0000233
Igor Sysoev9c388c02008-08-26 14:19:37 +0000234 addr = text->data;
235 last = addr + text->len;
Igor Sysoev822834e2004-05-25 15:28:46 +0000236
Igor Sysoev9c388c02008-08-26 14:19:37 +0000237 mask = ngx_strlchr(addr, last, '/');
Igor Sysoev822834e2004-05-25 15:28:46 +0000238
Igor Sysoev36860102009-02-24 14:01:40 +0000239 cidr->u.in.addr = ngx_inet_addr(addr, (mask ? mask : last) - addr);
Igor Sysoev822834e2004-05-25 15:28:46 +0000240
Igor Sysoev36860102009-02-24 14:01:40 +0000241 if (cidr->u.in.addr == INADDR_NONE) {
Igor Sysoev822834e2004-05-25 15:28:46 +0000242 return NGX_ERROR;
243 }
244
Igor Sysoev9c388c02008-08-26 14:19:37 +0000245 if (mask == NULL) {
Igor Sysoev36860102009-02-24 14:01:40 +0000246 cidr->family = AF_INET;
247 cidr->u.in.mask = 0xffffffff;
Igor Sysoev9c388c02008-08-26 14:19:37 +0000248 return NGX_OK;
249 }
250
251 mask++;
252
253 shift = ngx_atoi(mask, last - mask);
254 if (shift == NGX_ERROR) {
Igor Sysoev822834e2004-05-25 15:28:46 +0000255 return NGX_ERROR;
256 }
257
Igor Sysoev36860102009-02-24 14:01:40 +0000258 cidr->family = AF_INET;
259
Igor Sysoev9c388c02008-08-26 14:19:37 +0000260 if (shift == 0) {
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000261
262 /* the x86 compilers use the shl instruction that shifts by modulo 32 */
263
Igor Sysoev36860102009-02-24 14:01:40 +0000264 cidr->u.in.mask = 0;
Igor Sysoev9c388c02008-08-26 14:19:37 +0000265
Igor Sysoev36860102009-02-24 14:01:40 +0000266 if (cidr->u.in.addr == 0) {
Igor Sysoev9c388c02008-08-26 14:19:37 +0000267 return NGX_OK;
268 }
269
270 return NGX_DONE;
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000271 }
272
Igor Sysoev36860102009-02-24 14:01:40 +0000273 cidr->u.in.mask = htonl((ngx_uint_t) (0 - (1 << (32 - shift))));
Igor Sysoev59f3aa32004-06-24 16:07:04 +0000274
Igor Sysoev36860102009-02-24 14:01:40 +0000275 if (cidr->u.in.addr == (cidr->u.in.addr & cidr->u.in.mask)) {
Igor Sysoev845f6d52007-08-10 13:13:28 +0000276 return NGX_OK;
277 }
278
Igor Sysoev36860102009-02-24 14:01:40 +0000279 cidr->u.in.addr &= cidr->u.in.mask;
Igor Sysoev845f6d52007-08-10 13:13:28 +0000280
281 return NGX_DONE;
Igor Sysoev822834e2004-05-25 15:28:46 +0000282}
Igor Sysoeve2ff3ea2004-09-14 15:55:24 +0000283
284
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000285ngx_int_t
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000286ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u)
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000287{
Igor Sysoev154013c2008-08-22 13:36:56 +0000288 u_char *p;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000289
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000290 p = u->url.data;
291
Igor Sysoev722231f2007-02-14 18:51:19 +0000292 if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) {
Igor Sysoev154013c2008-08-22 13:36:56 +0000293 return ngx_parse_unix_domain_url(pool, u);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000294 }
295
296 if ((p[0] == ':' || p[0] == '/') && !u->listen) {
297 u->err = "invalid host";
298 return NGX_ERROR;
299 }
300
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000301 if (p[0] == '[') {
302 return ngx_parse_inet6_url(pool, u);
303 }
304
Igor Sysoev154013c2008-08-22 13:36:56 +0000305 return ngx_parse_inet_url(pool, u);
306}
307
308
309static ngx_int_t
310ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
311{
312#if (NGX_HAVE_UNIX_DOMAIN)
Igor Sysoevc9491d12008-08-26 14:24:14 +0000313 u_char *path, *uri, *last;
Igor Sysoev154013c2008-08-22 13:36:56 +0000314 size_t len;
Igor Sysoev154013c2008-08-22 13:36:56 +0000315 struct sockaddr_un *saun;
316
317 len = u->url.len;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000318 path = u->url.data;
Igor Sysoev154013c2008-08-22 13:36:56 +0000319
Igor Sysoevc9491d12008-08-26 14:24:14 +0000320 path += 5;
Igor Sysoev154013c2008-08-22 13:36:56 +0000321 len -= 5;
322
Igor Sysoev154013c2008-08-22 13:36:56 +0000323 if (u->uri_part) {
Igor Sysoev154013c2008-08-22 13:36:56 +0000324
Igor Sysoevc9491d12008-08-26 14:24:14 +0000325 last = path + len;
326 uri = ngx_strlchr(path, last, ':');
Igor Sysoev154013c2008-08-22 13:36:56 +0000327
Igor Sysoevc9491d12008-08-26 14:24:14 +0000328 if (uri) {
329 len = uri - path;
330 uri++;
331 u->uri.len = last - uri;
332 u->uri.data = uri;
Igor Sysoev154013c2008-08-22 13:36:56 +0000333 }
334 }
335
336 if (len == 0) {
337 u->err = "no path in the unix domain socket";
338 return NGX_ERROR;
339 }
340
Igor Sysoevc9491d12008-08-26 14:24:14 +0000341 u->host.len = len++;
342 u->host.data = path;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000343
344 if (len > sizeof(saun->sun_path)) {
Igor Sysoev154013c2008-08-22 13:36:56 +0000345 u->err = "too long path in the unix domain socket";
346 return NGX_ERROR;
347 }
348
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000349 u->socklen = sizeof(struct sockaddr_un);
350 saun = (struct sockaddr_un *) &u->sockaddr;
351 saun->sun_family = AF_UNIX;
352 (void) ngx_cpystrn((u_char *) saun->sun_path, path, len);
353
Igor Sysoev154013c2008-08-22 13:36:56 +0000354 u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t));
355 if (u->addrs == NULL) {
356 return NGX_ERROR;
357 }
358
359 saun = ngx_pcalloc(pool, sizeof(struct sockaddr_un));
360 if (saun == NULL) {
361 return NGX_ERROR;
362 }
363
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000364 u->family = AF_UNIX;
Igor Sysoev154013c2008-08-22 13:36:56 +0000365 u->naddrs = 1;
366
367 saun->sun_family = AF_UNIX;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000368 (void) ngx_cpystrn((u_char *) saun->sun_path, path, len);
Igor Sysoev154013c2008-08-22 13:36:56 +0000369
370 u->addrs[0].sockaddr = (struct sockaddr *) saun;
371 u->addrs[0].socklen = sizeof(struct sockaddr_un);
Igor Sysoevc9491d12008-08-26 14:24:14 +0000372 u->addrs[0].name.len = len + 4;
Igor Sysoev154013c2008-08-22 13:36:56 +0000373 u->addrs[0].name.data = u->url.data;
374
Igor Sysoev154013c2008-08-22 13:36:56 +0000375 return NGX_OK;
376
377#else
378
379 u->err = "the unix domain sockets are not supported on this platform";
380
381 return NGX_ERROR;
382
383#endif
384}
385
386
387static ngx_int_t
388ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)
389{
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000390 u_char *p, *host, *port, *last, *uri, *args;
391 size_t len;
392 ngx_int_t n;
393 struct hostent *h;
394 struct sockaddr_in *sin;
395
396 u->socklen = sizeof(struct sockaddr_in);
397 sin = (struct sockaddr_in *) &u->sockaddr;
398 sin->sin_family = AF_INET;
Igor Sysoev154013c2008-08-22 13:36:56 +0000399
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000400 u->family = AF_INET;
401
Igor Sysoevc9491d12008-08-26 14:24:14 +0000402 host = u->url.data;
Igor Sysoev154013c2008-08-22 13:36:56 +0000403
Igor Sysoevc9491d12008-08-26 14:24:14 +0000404 last = host + u->url.len;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000405
Igor Sysoevc9491d12008-08-26 14:24:14 +0000406 port = ngx_strlchr(host, last, ':');
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000407
Igor Sysoevc239da52008-10-24 15:12:11 +0000408 uri = ngx_strlchr(host, last, '/');
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000409
Igor Sysoev164abfb2008-10-24 19:34:24 +0000410 args = ngx_strlchr(host, last, '?');
411
412 if (args) {
413 if (uri == NULL) {
414 uri = args;
415
416 } else if (args < uri) {
417 uri = args;
418 }
419 }
420
Igor Sysoevc9491d12008-08-26 14:24:14 +0000421 if (uri) {
422 if (u->listen || !u->uri_part) {
423 u->err = "invalid host";
424 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000425 }
426
Igor Sysoevc9491d12008-08-26 14:24:14 +0000427 u->uri.len = last - uri;
428 u->uri.data = uri;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000429
Igor Sysoevc9491d12008-08-26 14:24:14 +0000430 last = uri;
Igor Sysoevc239da52008-10-24 15:12:11 +0000431
432 if (uri < port) {
433 port = NULL;
Igor Sysoevead80912008-11-11 16:04:05 +0000434 }
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000435 }
436
Igor Sysoevc9491d12008-08-26 14:24:14 +0000437 if (port) {
438 port++;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000439
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000440 len = last - port;
441
442 if (len == 0) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000443 u->err = "invalid port";
444 return NGX_ERROR;
445 }
446
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000447 n = ngx_atoi(port, len);
448
449 if (n < 1 || n > 65536) {
450 u->err = "invalid port";
451 return NGX_ERROR;
452 }
453
454 u->port = (in_port_t) n;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000455 sin->sin_port = htons((in_port_t) n);
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000456
457 u->port_text.len = len;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000458 u->port_text.data = port;
459
460 last = port - 1;
Igor Sysoev00e03772007-11-28 19:55:31 +0000461
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000462 } else {
Igor Sysoevc9491d12008-08-26 14:24:14 +0000463 if (uri == NULL) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000464
Igor Sysoevc9491d12008-08-26 14:24:14 +0000465 if (u->listen) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000466
Igor Sysoevc9491d12008-08-26 14:24:14 +0000467 /* test value as port only */
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000468
Igor Sysoevc9491d12008-08-26 14:24:14 +0000469 n = ngx_atoi(host, last - host);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000470
Igor Sysoevc9491d12008-08-26 14:24:14 +0000471 if (n != NGX_ERROR) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000472
Igor Sysoevc9491d12008-08-26 14:24:14 +0000473 if (n < 1 || n > 65536) {
474 u->err = "invalid port";
475 return NGX_ERROR;
476 }
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000477
Igor Sysoevc9491d12008-08-26 14:24:14 +0000478 u->port = (in_port_t) n;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000479 sin->sin_port = htons((in_port_t) n);
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000480
Igor Sysoevc9491d12008-08-26 14:24:14 +0000481 u->port_text.len = last - host;
482 u->port_text.data = host;
Igor Sysoev20bf47b2006-10-24 13:06:55 +0000483
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000484 u->wildcard = 1;
485
Igor Sysoevc9491d12008-08-26 14:24:14 +0000486 return NGX_OK;
Igor Sysoev20bf47b2006-10-24 13:06:55 +0000487 }
Igor Sysoev20bf47b2006-10-24 13:06:55 +0000488 }
Igor Sysoev20bf47b2006-10-24 13:06:55 +0000489 }
490
Igor Sysoevc9491d12008-08-26 14:24:14 +0000491 u->no_port = 1;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000492 }
493
Igor Sysoevc9491d12008-08-26 14:24:14 +0000494 len = last - host;
495
496 if (len == 0) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000497 u->err = "no host";
498 return NGX_ERROR;
499 }
500
Igor Sysoevc9491d12008-08-26 14:24:14 +0000501 if (len == 1 && *host == '*') {
502 len = 0;
503 }
504
505 u->host.len = len;
506 u->host.data = host;
507
Igor Sysoevd3bf7c12008-08-26 16:11:30 +0000508 if (u->no_resolve) {
509 return NGX_OK;
510 }
511
Igor Sysoevc9491d12008-08-26 14:24:14 +0000512 if (len++) {
513
514 p = ngx_alloc(len, pool->log);
515 if (p == NULL) {
516 return NGX_ERROR;
517 }
518
519 (void) ngx_cpystrn(p, host, len);
520
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
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000532 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[0]);
533 }
534
535 if (sin->sin_addr.s_addr == INADDR_ANY) {
536 u->wildcard = 1;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000537 }
538
539 ngx_free(p);
540
541 } else {
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000542 sin->sin_addr.s_addr = INADDR_ANY;
543 u->wildcard = 1;
Igor Sysoevc9491d12008-08-26 14:24:14 +0000544 }
545
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000546 if (u->no_port) {
547 u->port = u->default_port;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000548 sin->sin_port = htons(u->default_port);
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000549 }
550
Igor Sysoevc9491d12008-08-26 14:24:14 +0000551 if (u->listen) {
552 return NGX_OK;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000553 }
554
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000555 if (ngx_inet_resolve_host(pool, u) != NGX_OK) {
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000556 return NGX_ERROR;
557 }
558
559 return NGX_OK;
560}
561
562
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000563static ngx_int_t
564ngx_parse_inet6_url(ngx_pool_t *pool, ngx_url_t *u)
565{
566#if (NGX_HAVE_INET6)
567 int rc;
568 u_char *p, *host, *port, *last, *uri;
569 size_t len;
570 ngx_int_t n;
571 struct sockaddr_in6 *sin6;
572
573 u->socklen = sizeof(struct sockaddr_in6);
574 sin6 = (struct sockaddr_in6 *) &u->sockaddr;
575 sin6->sin6_family = AF_INET6;
576
577 host = u->url.data + 1;
578
579 last = u->url.data + u->url.len;
580
581 p = ngx_strlchr(host, last, ']');
582
583 if (p == NULL) {
584 u->err = "invalid host";
585 return NGX_ERROR;
586 }
587
588 if (last - p) {
589
590 port = p + 1;
591
592 uri = ngx_strlchr(port, last, '/');
593
594 if (uri) {
595 if (u->listen || !u->uri_part) {
596 u->err = "invalid host";
597 return NGX_ERROR;
598 }
599
600 u->uri.len = last - uri;
601 u->uri.data = uri;
602 }
603
604 if (*port == ':') {
605 port++;
606
607 len = last - port;
608
609 if (len == 0) {
610 u->err = "invalid port";
611 return NGX_ERROR;
612 }
613
614 n = ngx_atoi(port, len);
615
616 if (n < 1 || n > 65536) {
617 u->err = "invalid port";
618 return NGX_ERROR;
619 }
620
621 u->port = (in_port_t) n;
622 sin6->sin6_port = htons((in_port_t) n);
623
624 u->port_text.len = len;
625 u->port_text.data = port;
626
627 } else {
628 u->no_port = 1;
629 }
630 }
631
632 len = p - host;
633
634 if (len == 0) {
635 u->err = "no host";
636 return NGX_ERROR;
637 }
638
639 u->host.len = len++;
640 u->host.data = host;
641
642 p = ngx_alloc(len, pool->log);
643 if (p == NULL) {
644 return NGX_ERROR;
645 }
646
647 (void) ngx_cpystrn(p, host, len);
648
Igor Sysoevdbc205a2009-05-07 13:05:04 +0000649#if (NGX_WIN32)
650
651 rc = WSAStringToAddress((char *) p, AF_INET6, NULL,
652 (SOCKADDR *) sin6, &u->socklen);
653 rc = !rc;
654
655 if (u->port) {
656 sin6->sin6_port = htons(u->port);
657 }
658
659#else
660
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000661 rc = inet_pton(AF_INET6, (const char *) p, &sin6->sin6_addr);
662
Igor Sysoevdbc205a2009-05-07 13:05:04 +0000663#endif
664
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000665 ngx_free(p);
666
667 if (rc == 0) {
668 u->err = "invalid IPv6 address";
669 return NGX_ERROR;
670 }
671
672 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
673 u->wildcard = 1;
674 }
675
676 u->family = AF_INET6;
677
678 if (u->no_resolve) {
679 return NGX_OK;
680 }
681
682 if (u->no_port) {
683 u->port = u->default_port;
684 sin6->sin6_port = htons(u->default_port);
685 }
686
687 return NGX_OK;
688
689#else
690
691 u->err = "the INET6 sockets are not supported on this platform";
692
693 return NGX_ERROR;
694
695#endif
696}
697
698
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000699ngx_int_t
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000700ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000701{
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000702 u_char *p, *host;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000703 size_t len;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000704 in_port_t port;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000705 in_addr_t in_addr;
706 ngx_uint_t i;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000707 struct hostent *h;
708 struct sockaddr_in *sin;
709
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000710 host = ngx_alloc(u->host.len + 1, pool->log);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000711 if (host == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000712 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000713 }
714
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000715 (void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000716
717 /* AF_INET only */
718
Igor Sysoev2bc44ea2009-02-21 14:34:32 +0000719 port = htons(u->port);
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000720
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000721 in_addr = inet_addr((char *) host);
722
723 if (in_addr == INADDR_NONE) {
724 h = gethostbyname((char *) host);
725
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000726 ngx_free(host);
727
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000728 if (h == NULL || h->h_addr_list[0] == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000729 u->err = "host not found";
730 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000731 }
732
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000733 if (u->one_addr == 0) {
734 for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ }
735
736 } else {
737 i = 1;
738 }
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000739
740 /* MP: ngx_shared_palloc() */
741
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000742 u->addrs = ngx_pcalloc(pool, i * sizeof(ngx_peer_addr_t));
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000743 if (u->addrs == NULL) {
744 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000745 }
746
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000747 u->naddrs = i;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000748
749 for (i = 0; h->h_addr_list[i] != NULL; i++) {
750
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000751 sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in));
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000752 if (sin == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000753 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000754 }
755
756 sin->sin_family = AF_INET;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000757 sin->sin_port = port;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000758 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
759
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000760 u->addrs[i].sockaddr = (struct sockaddr *) sin;
761 u->addrs[i].socklen = sizeof(struct sockaddr_in);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000762
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000763 len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000764
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +0000765 p = ngx_pnalloc(pool, len);
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000766 if (p == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000767 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000768 }
769
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000770 len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000771
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000772 u->addrs[i].name.len = len;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000773 u->addrs[i].name.data = p;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000774 }
775
776 } else {
777
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000778 ngx_free(host);
779
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000780 /* MP: ngx_shared_palloc() */
781
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000782 u->addrs = ngx_pcalloc(pool, sizeof(ngx_peer_addr_t));
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000783 if (u->addrs == NULL) {
784 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000785 }
786
Igor Sysoev7ed63ee2007-10-08 08:55:12 +0000787 sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in));
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000788 if (sin == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000789 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000790 }
791
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000792 u->naddrs = 1;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000793
794 sin->sin_family = AF_INET;
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000795 sin->sin_port = port;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000796 sin->sin_addr.s_addr = in_addr;
797
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000798 u->addrs[0].sockaddr = (struct sockaddr *) sin;
799 u->addrs[0].socklen = sizeof(struct sockaddr_in);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000800
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000801 p = ngx_pnalloc(pool, u->host.len + sizeof(":65535") - 1);
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000802 if (p == NULL) {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000803 return NGX_ERROR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000804 }
805
Igor Sysoeva35eacc2009-02-21 07:02:02 +0000806 u->addrs[0].name.len = ngx_sprintf(p, "%V:%d",
807 &u->host, ntohs(port)) - p;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +0000808 u->addrs[0].name.data = p;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000809 }
810
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000811 return NGX_OK;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000812}