Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 8 | #include <ngx_core.h> |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 9 | |
| 10 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 11 | /* |
| 12 | * ngx_sock_ntop() and ngx_inet_ntop() may be implemented as |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 13 | * "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])", however, |
| 14 | * they had been implemented long before the ngx_sprintf() had appeared |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 15 | * and they are faster by 1.5-2.5 times, so it is worth to keep them. |
| 16 | * |
| 17 | * By the way, the implementation using ngx_sprintf() is faster by 2.5-3 times |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 18 | * than using FreeBSD libc's snprintf(). |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 22 | static ngx_inline size_t |
| 23 | ngx_sprint_uchar(u_char *text, u_char c, size_t len) |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 24 | { |
| 25 | size_t n; |
| 26 | ngx_uint_t c1, c2; |
| 27 | |
| 28 | n = 0; |
| 29 | |
| 30 | if (len == n) { |
| 31 | return n; |
| 32 | } |
| 33 | |
| 34 | c1 = c / 100; |
| 35 | |
| 36 | if (c1) { |
| 37 | *text++ = (u_char) (c1 + '0'); |
| 38 | n++; |
| 39 | |
| 40 | if (len == n) { |
| 41 | return n; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | c2 = (c % 100) / 10; |
| 46 | |
| 47 | if (c1 || c2) { |
| 48 | *text++ = (u_char) (c2 + '0'); |
| 49 | n++; |
| 50 | |
| 51 | if (len == n) { |
| 52 | return n; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | c2 = c % 10; |
| 57 | |
| 58 | *text++ = (u_char) (c2 + '0'); |
| 59 | n++; |
| 60 | |
| 61 | return n; |
| 62 | } |
| 63 | |
| 64 | |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 65 | /* AF_INET only */ |
| 66 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 67 | size_t |
| 68 | ngx_sock_ntop(int family, struct sockaddr *sa, u_char *text, size_t len) |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 69 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 70 | u_char *p; |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 71 | size_t n; |
| 72 | ngx_uint_t i; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 73 | struct sockaddr_in *sin; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 74 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 75 | if (len == 0) { |
| 76 | return 0; |
| 77 | } |
| 78 | |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 79 | if (family != AF_INET) { |
| 80 | return 0; |
| 81 | } |
| 82 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 83 | sin = (struct sockaddr_in *) sa; |
| 84 | p = (u_char *) &sin->sin_addr; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 85 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 86 | if (len > INET_ADDRSTRLEN) { |
| 87 | len = INET_ADDRSTRLEN; |
| 88 | } |
| 89 | |
| 90 | n = ngx_sprint_uchar(text, p[0], len); |
| 91 | |
| 92 | i = 1; |
| 93 | |
| 94 | do { |
| 95 | if (len == n) { |
| 96 | text[n - 1] = '\0'; |
| 97 | return n; |
| 98 | } |
| 99 | |
| 100 | text[n++] = '.'; |
| 101 | |
| 102 | if (len == n) { |
| 103 | text[n - 1] = '\0'; |
| 104 | return n; |
| 105 | } |
| 106 | |
| 107 | n += ngx_sprint_uchar(&text[n], p[i++], len - n); |
| 108 | |
| 109 | } while (i < 4); |
| 110 | |
| 111 | if (len == n) { |
| 112 | text[n] = '\0'; |
| 113 | return n; |
| 114 | } |
| 115 | |
| 116 | text[n] = '\0'; |
| 117 | |
| 118 | return n; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 121 | size_t |
| 122 | ngx_inet_ntop(int family, void *addr, u_char *text, size_t len) |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 123 | { |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 124 | u_char *p; |
| 125 | size_t n; |
| 126 | ngx_uint_t i; |
| 127 | |
| 128 | if (len == 0) { |
| 129 | return 0; |
| 130 | } |
| 131 | |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 132 | if (family != AF_INET) { |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 133 | return 0; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 134 | } |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 135 | |
Igor Sysoev | 9c61095 | 2004-03-16 13:35:20 +0000 | [diff] [blame] | 136 | p = (u_char *) addr; |
| 137 | |
| 138 | if (len > INET_ADDRSTRLEN) { |
| 139 | len = INET_ADDRSTRLEN; |
| 140 | } |
| 141 | |
| 142 | n = ngx_sprint_uchar(text, p[0], len); |
| 143 | |
| 144 | i = 1; |
| 145 | |
| 146 | do { |
| 147 | if (len == n) { |
| 148 | text[n - 1] = '\0'; |
| 149 | return n; |
| 150 | } |
| 151 | |
| 152 | text[n++] = '.'; |
| 153 | |
| 154 | if (len == n) { |
| 155 | text[n - 1] = '\0'; |
| 156 | return n; |
| 157 | } |
| 158 | |
| 159 | n += ngx_sprint_uchar(&text[n], p[i++], len - n); |
| 160 | |
| 161 | } while (i < 4); |
| 162 | |
| 163 | if (len == n) { |
| 164 | text[n] = '\0'; |
| 165 | return n; |
| 166 | } |
| 167 | |
| 168 | text[n] = '\0'; |
| 169 | |
| 170 | return n; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 171 | } |
Igor Sysoev | 822834e | 2004-05-25 15:28:46 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | /* AF_INET only */ |
| 175 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 176 | ngx_int_t |
| 177 | ngx_ptocidr(ngx_str_t *text, void *cidr) |
Igor Sysoev | 822834e | 2004-05-25 15:28:46 +0000 | [diff] [blame] | 178 | { |
Igor Sysoev | 59f3aa3 | 2004-06-24 16:07:04 +0000 | [diff] [blame] | 179 | ngx_int_t m; |
Igor Sysoev | 822834e | 2004-05-25 15:28:46 +0000 | [diff] [blame] | 180 | ngx_uint_t i; |
| 181 | ngx_inet_cidr_t *in_cidr; |
| 182 | |
| 183 | in_cidr = cidr; |
| 184 | |
| 185 | for (i = 0; i < text->len; i++) { |
| 186 | if (text->data[i] == '/') { |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (i == text->len) { |
| 192 | return NGX_ERROR; |
| 193 | } |
| 194 | |
| 195 | text->data[i] = '\0'; |
| 196 | in_cidr->addr = inet_addr((char *) text->data); |
| 197 | text->data[i] = '/'; |
| 198 | if (in_cidr->addr == INADDR_NONE) { |
| 199 | return NGX_ERROR; |
| 200 | } |
| 201 | |
Igor Sysoev | 59f3aa3 | 2004-06-24 16:07:04 +0000 | [diff] [blame] | 202 | m = ngx_atoi(&text->data[i + 1], text->len - (i + 1)); |
| 203 | if (m == NGX_ERROR) { |
Igor Sysoev | 822834e | 2004-05-25 15:28:46 +0000 | [diff] [blame] | 204 | return NGX_ERROR; |
| 205 | } |
| 206 | |
Igor Sysoev | 59f3aa3 | 2004-06-24 16:07:04 +0000 | [diff] [blame] | 207 | if (m == 0) { |
| 208 | |
| 209 | /* the x86 compilers use the shl instruction that shifts by modulo 32 */ |
| 210 | |
| 211 | in_cidr->mask = 0; |
| 212 | return NGX_OK; |
| 213 | } |
| 214 | |
Igor Sysoev | 6deb041 | 2004-07-30 17:05:14 +0000 | [diff] [blame] | 215 | in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - m)))); |
Igor Sysoev | 59f3aa3 | 2004-06-24 16:07:04 +0000 | [diff] [blame] | 216 | |
Igor Sysoev | 822834e | 2004-05-25 15:28:46 +0000 | [diff] [blame] | 217 | return NGX_OK; |
| 218 | } |
Igor Sysoev | e2ff3ea | 2004-09-14 15:55:24 +0000 | [diff] [blame] | 219 | |
| 220 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 221 | ngx_int_t |
| 222 | ngx_parse_url(ngx_conf_t *cf, ngx_url_t *u) |
| 223 | { |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 224 | u_char *p, *host, *port_start; |
| 225 | size_t len, port_len; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 226 | ngx_int_t port; |
| 227 | ngx_uint_t i; |
Igor Sysoev | 20bf47b | 2006-10-24 13:06:55 +0000 | [diff] [blame] | 228 | struct hostent *h; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 229 | #if (NGX_HAVE_UNIX_DOMAIN) |
| 230 | struct sockaddr_un *saun; |
| 231 | #endif |
| 232 | |
| 233 | len = u->url.len; |
| 234 | p = u->url.data; |
| 235 | |
Igor Sysoev | 722231f | 2007-02-14 18:51:19 +0000 | [diff] [blame] | 236 | if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 237 | |
| 238 | #if (NGX_HAVE_UNIX_DOMAIN) |
| 239 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 240 | p += 5; |
| 241 | len -= 5; |
| 242 | |
Igor Sysoev | 7c20ed8 | 2006-09-24 14:45:37 +0000 | [diff] [blame] | 243 | u->uri.len = len; |
| 244 | u->uri.data = p; |
| 245 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 246 | if (u->uri_part) { |
| 247 | for (i = 0; i < len; i++) { |
| 248 | |
| 249 | if (p[i] == ':') { |
| 250 | len = i; |
| 251 | |
| 252 | u->uri.len -= len + 1; |
| 253 | u->uri.data += len + 1; |
| 254 | |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | if (len == 0) { |
| 261 | u->err = "no path in the unix domain socket"; |
| 262 | return NGX_ERROR; |
| 263 | } |
| 264 | |
| 265 | if (len + 1 > sizeof(saun->sun_path)) { |
| 266 | u->err = "too long path in the unix domain socket"; |
| 267 | return NGX_ERROR; |
| 268 | } |
| 269 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 270 | u->addrs = ngx_pcalloc(cf->pool, sizeof(ngx_peer_addr_t)); |
| 271 | if (u->addrs == NULL) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 272 | return NGX_ERROR; |
| 273 | } |
| 274 | |
| 275 | saun = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_un)); |
| 276 | if (saun == NULL) { |
| 277 | return NGX_ERROR; |
| 278 | } |
| 279 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 280 | u->naddrs = 1; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 281 | |
| 282 | saun->sun_family = AF_UNIX; |
| 283 | (void) ngx_cpystrn((u_char *) saun->sun_path, p, len + 1); |
| 284 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 285 | u->addrs[0].sockaddr = (struct sockaddr *) saun; |
| 286 | u->addrs[0].socklen = sizeof(struct sockaddr_un); |
| 287 | u->addrs[0].name.len = len + 5; |
| 288 | u->addrs[0].name.data = u->url.data; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 289 | |
Igor Sysoev | adf9c7f | 2006-12-06 15:39:08 +0000 | [diff] [blame] | 290 | u->host.len = len; |
| 291 | u->host.data = p; |
| 292 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 293 | u->unix_socket = 1; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 294 | |
| 295 | return NGX_OK; |
| 296 | |
| 297 | #else |
| 298 | u->err = "the unix domain sockets are not supported on this platform"; |
| 299 | |
| 300 | return NGX_ERROR; |
| 301 | |
| 302 | #endif |
| 303 | } |
| 304 | |
| 305 | if ((p[0] == ':' || p[0] == '/') && !u->listen) { |
| 306 | u->err = "invalid host"; |
| 307 | return NGX_ERROR; |
| 308 | } |
| 309 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 310 | u->host.data = p; |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 311 | |
| 312 | port_start = NULL; |
| 313 | port_len = 0; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 314 | |
| 315 | for (i = 0; i < len; i++) { |
| 316 | |
| 317 | if (p[i] == ':') { |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 318 | port_start = &p[i + 1]; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 319 | u->host.len = i; |
| 320 | |
| 321 | if (!u->uri_part) { |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 322 | port_len = len - (i + 1); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (p[i] == '/') { |
| 328 | u->uri.len = len - i; |
| 329 | u->uri.data = &p[i]; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 330 | |
| 331 | if (u->host.len == 0) { |
| 332 | u->host.len = i; |
| 333 | } |
| 334 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 335 | if (port_start == NULL) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 336 | u->no_port = 1; |
| 337 | goto no_port; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 340 | port_len = &p[i] - port_start; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 341 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 342 | if (port_len == 0) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 343 | u->err = "invalid port"; |
| 344 | return NGX_ERROR; |
| 345 | } |
| 346 | |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 351 | if (port_start) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 352 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 353 | if (port_len == 0) { |
| 354 | port_len = &p[i] - port_start; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 355 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 356 | if (port_len == 0) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 357 | u->err = "invalid port"; |
| 358 | return NGX_ERROR; |
| 359 | } |
| 360 | } |
| 361 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 362 | port = ngx_atoi(port_start, port_len); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 363 | |
| 364 | if (port == NGX_ERROR || port < 1 || port > 65536) { |
| 365 | u->err = "invalid port"; |
| 366 | return NGX_ERROR; |
| 367 | } |
| 368 | |
| 369 | } else { |
| 370 | port = ngx_atoi(p, len); |
| 371 | |
| 372 | if (port == NGX_ERROR) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 373 | u->host.len = len; |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 374 | u->no_port = 1; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 375 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 376 | goto no_port; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 379 | u->wildcard = 1; |
| 380 | } |
| 381 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 382 | u->port = (in_port_t) port; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 383 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 384 | no_port: |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 385 | |
| 386 | if (u->listen) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 387 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 388 | if (u->port == 0) { |
| 389 | if (u->default_port == 0) { |
Igor Sysoev | 20bf47b | 2006-10-24 13:06:55 +0000 | [diff] [blame] | 390 | u->err = "no port"; |
| 391 | return NGX_ERROR; |
| 392 | } |
| 393 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 394 | u->port = u->default_port; |
Igor Sysoev | 20bf47b | 2006-10-24 13:06:55 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (u->host.len == 1 && u->host.data[0] == '*') { |
| 398 | u->host.len = 0; |
| 399 | } |
| 400 | |
| 401 | /* AF_INET only */ |
| 402 | |
| 403 | if (u->host.len) { |
| 404 | |
| 405 | host = ngx_palloc(cf->temp_pool, u->host.len + 1); |
| 406 | if (host == NULL) { |
| 407 | return NGX_ERROR; |
| 408 | } |
| 409 | |
| 410 | (void) ngx_cpystrn(host, u->host.data, u->host.len + 1); |
| 411 | |
| 412 | u->addr.in_addr = inet_addr((const char *) host); |
| 413 | |
| 414 | if (u->addr.in_addr == INADDR_NONE) { |
| 415 | h = gethostbyname((const char *) host); |
| 416 | |
| 417 | if (h == NULL || h->h_addr_list[0] == NULL) { |
| 418 | u->err = "host not found"; |
| 419 | return NGX_ERROR; |
| 420 | } |
| 421 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 422 | u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]); |
Igor Sysoev | 20bf47b | 2006-10-24 13:06:55 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | } else { |
| 426 | u->addr.in_addr = INADDR_ANY; |
| 427 | } |
| 428 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 429 | return NGX_OK; |
| 430 | } |
| 431 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 432 | if (u->host.len == 0) { |
| 433 | u->err = "no host"; |
| 434 | return NGX_ERROR; |
| 435 | } |
| 436 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 437 | if (u->no_resolve) { |
| 438 | return NGX_OK; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 441 | if (u->no_port) { |
| 442 | u->port = u->default_port; |
| 443 | } |
| 444 | |
| 445 | if (u->port == 0) { |
| 446 | u->err = "no port"; |
| 447 | return NGX_ERROR; |
| 448 | } |
| 449 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 450 | if (ngx_inet_resolve_host(cf, u) != NGX_OK) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 451 | return NGX_ERROR; |
| 452 | } |
| 453 | |
| 454 | return NGX_OK; |
| 455 | } |
| 456 | |
| 457 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 458 | ngx_int_t |
| 459 | ngx_inet_resolve_host(ngx_conf_t *cf, ngx_url_t *u) |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 460 | { |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 461 | u_char *p, *host; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 462 | size_t len; |
| 463 | in_addr_t in_addr; |
| 464 | ngx_uint_t i; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 465 | struct hostent *h; |
| 466 | struct sockaddr_in *sin; |
| 467 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 468 | host = ngx_palloc(cf->temp_pool, u->host.len + 1); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 469 | if (host == NULL) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 470 | return NGX_ERROR; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 473 | (void) ngx_cpystrn(host, u->host.data, u->host.len + 1); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 474 | |
| 475 | /* AF_INET only */ |
| 476 | |
| 477 | in_addr = inet_addr((char *) host); |
| 478 | |
| 479 | if (in_addr == INADDR_NONE) { |
| 480 | h = gethostbyname((char *) host); |
| 481 | |
| 482 | if (h == NULL || h->h_addr_list[0] == NULL) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 483 | u->err = "host not found"; |
| 484 | return NGX_ERROR; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 487 | if (u->one_addr == 0) { |
| 488 | for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ } |
| 489 | |
| 490 | } else { |
| 491 | i = 1; |
| 492 | } |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 493 | |
| 494 | /* MP: ngx_shared_palloc() */ |
| 495 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 496 | u->addrs = ngx_pcalloc(cf->pool, i * sizeof(ngx_peer_addr_t)); |
| 497 | if (u->addrs == NULL) { |
| 498 | return NGX_ERROR; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 501 | u->naddrs = i; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 502 | |
| 503 | for (i = 0; h->h_addr_list[i] != NULL; i++) { |
| 504 | |
| 505 | sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); |
| 506 | if (sin == NULL) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 507 | return NGX_ERROR; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | sin->sin_family = AF_INET; |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 511 | sin->sin_port = htons(u->port); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 512 | sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]); |
| 513 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 514 | u->addrs[i].sockaddr = (struct sockaddr *) sin; |
| 515 | u->addrs[i].socklen = sizeof(struct sockaddr_in); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 516 | |
| 517 | len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1; |
| 518 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 519 | p = ngx_palloc(cf->pool, len); |
| 520 | if (p == NULL) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 521 | return NGX_ERROR; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 524 | len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin, p, len); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 525 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 526 | u->addrs[i].name.len = ngx_sprintf(&p[len], ":%d", u->port) - p; |
| 527 | u->addrs[i].name.data = p; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | } else { |
| 531 | |
| 532 | /* MP: ngx_shared_palloc() */ |
| 533 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 534 | u->addrs = ngx_pcalloc(cf->pool, sizeof(ngx_peer_addr_t)); |
| 535 | if (u->addrs == NULL) { |
| 536 | return NGX_ERROR; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); |
| 540 | if (sin == NULL) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 541 | return NGX_ERROR; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 544 | u->naddrs = 1; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 545 | |
| 546 | sin->sin_family = AF_INET; |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 547 | sin->sin_port = htons(u->port); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 548 | sin->sin_addr.s_addr = in_addr; |
| 549 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 550 | u->addrs[0].sockaddr = (struct sockaddr *) sin; |
| 551 | u->addrs[0].socklen = sizeof(struct sockaddr_in); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 552 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 553 | p = ngx_palloc(cf->pool, u->host.len + sizeof(":65536") - 1); |
| 554 | if (p == NULL) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 555 | return NGX_ERROR; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 558 | u->addrs[0].name.len = ngx_sprintf(p, "%V:%d", &u->host, u->port) - p; |
| 559 | u->addrs[0].name.data = p; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 562 | return NGX_OK; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 563 | } |