comparison src/core/ngx_inet.c @ 260:0effe91f6083 NGINX_0_5_0

nginx 0.5.0 *) Change: the parameters in the "%name" form in the "log_format" directive are not supported anymore. *) Change: the "proxy_upstream_max_fails", "proxy_upstream_fail_timeout", "fastcgi_upstream_max_fails", "fastcgi_upstream_fail_timeout", "memcached_upstream_max_fails", and "memcached_upstream_fail_timeout" directives are not supported anymore. *) Feature: the "server" directive in the "upstream" context supports the "max_fails", "fail_timeout", and "down" parameters. *) Feature: the "ip_hash" directive inside the "upstream" block. *) Feature: the WAIT status in the "Auth-Status" header line of the IMAP/POP3 proxy authentication server response. *) Bugfix: nginx could not be built on 64-bit platforms; bug appeared in 0.4.14.
author Igor Sysoev <http://sysoev.ru>
date Mon, 04 Dec 2006 00:00:00 +0300
parents 644510700914
children e0b1d0a6c629
comparison
equal deleted inserted replaced
259:c68f18041059 260:0effe91f6083
4 */ 4 */
5 5
6 6
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 #include <ngx_event.h>
10 #include <ngx_event_connect.h>
11 9
12 10
13 /* 11 /*
14 * ngx_sock_ntop() and ngx_inet_ntop() may be implemented as 12 * ngx_sock_ntop() and ngx_inet_ntop() may be implemented as
15 * "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])", however, 13 * "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])", however,
237 235
238 if (ngx_strncasecmp(p, "unix:", 5) == 0) { 236 if (ngx_strncasecmp(p, "unix:", 5) == 0) {
239 237
240 #if (NGX_HAVE_UNIX_DOMAIN) 238 #if (NGX_HAVE_UNIX_DOMAIN)
241 239
242 u->type = NGX_PARSE_URL_UNIX;
243
244 p += 5; 240 p += 5;
245 len -= 5; 241 len -= 5;
246 242
247 u->uri.len = len; 243 u->uri.len = len;
248 u->uri.data = p; 244 u->uri.data = p;
269 if (len + 1 > sizeof(saun->sun_path)) { 265 if (len + 1 > sizeof(saun->sun_path)) {
270 u->err = "too long path in the unix domain socket"; 266 u->err = "too long path in the unix domain socket";
271 return NGX_ERROR; 267 return NGX_ERROR;
272 } 268 }
273 269
274 u->peers = ngx_pcalloc(cf->pool, sizeof(ngx_peers_t)); 270 u->addrs = ngx_pcalloc(cf->pool, sizeof(ngx_peer_addr_t));
275 if (u->peers == NULL) { 271 if (u->addrs == NULL) {
276 return NGX_ERROR; 272 return NGX_ERROR;
277 } 273 }
278 274
279 saun = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_un)); 275 saun = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_un));
280 if (saun == NULL) { 276 if (saun == NULL) {
281 return NGX_ERROR; 277 return NGX_ERROR;
282 } 278 }
283 279
284 u->peers->number = 1; 280 u->naddrs = 1;
285 281
286 saun->sun_family = AF_UNIX; 282 saun->sun_family = AF_UNIX;
287 (void) ngx_cpystrn((u_char *) saun->sun_path, p, len + 1); 283 (void) ngx_cpystrn((u_char *) saun->sun_path, p, len + 1);
288 284
289 u->peers->peer[0].sockaddr = (struct sockaddr *) saun; 285 u->addrs[0].sockaddr = (struct sockaddr *) saun;
290 u->peers->peer[0].socklen = sizeof(struct sockaddr_un); 286 u->addrs[0].socklen = sizeof(struct sockaddr_un);
291 u->peers->peer[0].name.len = len + 5; 287 u->addrs[0].name.len = len + 5;
292 u->peers->peer[0].name.data = u->url.data; 288 u->addrs[0].name.data = u->url.data;
293 u->peers->peer[0].uri_separator = ":";
294 289
295 u->host_header.len = sizeof("localhost") - 1; 290 u->host_header.len = sizeof("localhost") - 1;
296 u->host_header.data = (u_char *) "localhost"; 291 u->host_header.data = (u_char *) "localhost";
297 292
298 return NGX_OK; 293 return NGX_OK;
307 302
308 if ((p[0] == ':' || p[0] == '/') && !u->listen) { 303 if ((p[0] == ':' || p[0] == '/') && !u->listen) {
309 u->err = "invalid host"; 304 u->err = "invalid host";
310 return NGX_ERROR; 305 return NGX_ERROR;
311 } 306 }
312
313 u->type = NGX_PARSE_URL_INET;
314 307
315 u->host.data = p; 308 u->host.data = p;
316 u->host_header.len = len; 309 u->host_header.len = len;
317 u->host_header.data = p; 310 u->host_header.data = p;
318 311
321 if (p[i] == ':') { 314 if (p[i] == ':') {
322 u->port.data = &p[i + 1]; 315 u->port.data = &p[i + 1];
323 u->host.len = i; 316 u->host.len = i;
324 317
325 if (!u->uri_part) { 318 if (!u->uri_part) {
326 u->port.len = &p[len] - u->port.data; 319 u->port.len = len - (i + 1);
327 break; 320 break;
328 } 321 }
329 } 322 }
330 323
331 if (p[i] == '/') { 324 if (p[i] == '/') {
336 if (u->host.len == 0) { 329 if (u->host.len == 0) {
337 u->host.len = i; 330 u->host.len = i;
338 } 331 }
339 332
340 if (u->port.data == NULL) { 333 if (u->port.data == NULL) {
341 u->default_port = 1; 334 u->no_port = 1;
342 goto port; 335 goto no_port;
343 } 336 }
344 337
345 u->port.len = &p[i] - u->port.data; 338 u->port.len = &p[i] - u->port.data;
346 339
347 if (u->port.len == 0) { 340 if (u->port.len == 0) {
373 366
374 } else { 367 } else {
375 port = ngx_atoi(p, len); 368 port = ngx_atoi(p, len);
376 369
377 if (port == NGX_ERROR) { 370 if (port == NGX_ERROR) {
378 u->default_port = 1;
379 u->host.len = len; 371 u->host.len = len;
380 372 u->no_port = 1;
381 goto port; 373
374 goto no_port;
382 } 375 }
383 376
384 u->port.len = len; 377 u->port.len = len;
385 u->port.data = p; 378 u->port.data = p;
386 u->wildcard = 1; 379 u->wildcard = 1;
387 } 380 }
388 381
389 u->portn = (in_port_t) port; 382 u->portn = (in_port_t) port;
390 383
391 port: 384 no_port:
392 385
393 if (u->listen) { 386 if (u->listen) {
387
394 if (u->portn == 0) { 388 if (u->portn == 0) {
395 if (u->default_portn == 0) { 389 if (u->default_portn == 0) {
396 u->err = "no port"; 390 u->err = "no port";
397 return NGX_ERROR; 391 return NGX_ERROR;
398 } 392 }
433 } 427 }
434 428
435 return NGX_OK; 429 return NGX_OK;
436 } 430 }
437 431
438 if (u->default_port) { 432 if (u->no_port) {
439 433
440 if (u->upstream) { 434 if (u->default_portn == 0 && !u->upstream) {
441 return NGX_OK;
442 }
443
444 if (u->default_portn == 0) {
445 u->err = "no port"; 435 u->err = "no port";
446 return NGX_ERROR; 436 return NGX_ERROR;
447 } 437 }
448 438
449 u->portn = u->default_portn; 439 u->portn = u->default_portn;
453 return NGX_ERROR; 443 return NGX_ERROR;
454 } 444 }
455 445
456 u->port.len = ngx_sprintf(u->port.data, "%d", u->portn) - u->port.data; 446 u->port.len = ngx_sprintf(u->port.data, "%d", u->portn) - u->port.data;
457 447
458 } else if (u->portn) { 448 } else if (u->portn == 0) {
459 if (u->portn == u->default_portn) { 449
460 u->default_port = 1;
461 }
462
463 } else {
464 u->err = "no port"; 450 u->err = "no port";
465 return NGX_ERROR; 451 return NGX_ERROR;
466 } 452 }
467 453
468 if (u->host.len == 0) { 454 if (u->host.len == 0) {
469 u->err = "no host"; 455 u->err = "no host";
470 return NGX_ERROR; 456 return NGX_ERROR;
471 } 457 }
472 458
473 u->peers = ngx_inet_resolve_peer(cf, &u->host, u->portn); 459 if (u->no_resolve) {
474 460 return NGX_OK;
475 if (u->peers == NULL) { 461 }
476 return NGX_ERROR; 462
477 } 463 if (ngx_inet_resolve_host(cf, u) != NGX_OK) {
478
479 if (u->peers == NGX_CONF_ERROR) {
480 u->err = "host not found";
481 return NGX_ERROR; 464 return NGX_ERROR;
482 } 465 }
483 466
484 return NGX_OK; 467 return NGX_OK;
485 } 468 }
486 469
487 470
488 ngx_peers_t * 471 ngx_int_t
489 ngx_inet_resolve_peer(ngx_conf_t *cf, ngx_str_t *name, in_port_t port) 472 ngx_inet_resolve_host(ngx_conf_t *cf, ngx_url_t *u)
490 { 473 {
491 u_char *host; 474 u_char *host;
492 size_t len; 475 size_t len;
493 in_addr_t in_addr; 476 in_addr_t in_addr;
494 ngx_uint_t i; 477 ngx_uint_t i;
495 ngx_peers_t *peers;
496 struct hostent *h; 478 struct hostent *h;
497 struct sockaddr_in *sin; 479 struct sockaddr_in *sin;
498 480
499 host = ngx_palloc(cf->temp_pool, name->len + 1); 481 host = ngx_palloc(cf->temp_pool, u->host.len + 1);
500 if (host == NULL) { 482 if (host == NULL) {
501 return NULL; 483 return NGX_ERROR;
502 } 484 }
503 485
504 (void) ngx_cpystrn(host, name->data, name->len + 1); 486 (void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
505 487
506 /* AF_INET only */ 488 /* AF_INET only */
507 489
508 in_addr = inet_addr((char *) host); 490 in_addr = inet_addr((char *) host);
509 491
510 if (in_addr == INADDR_NONE) { 492 if (in_addr == INADDR_NONE) {
511 h = gethostbyname((char *) host); 493 h = gethostbyname((char *) host);
512 494
513 if (h == NULL || h->h_addr_list[0] == NULL) { 495 if (h == NULL || h->h_addr_list[0] == NULL) {
514 return NGX_CONF_ERROR; 496 u->err = "host not found";
515 } 497 return NGX_ERROR;
516 498 }
517 for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ } 499
500 if (u->one_addr == 0) {
501 for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ }
502
503 } else {
504 i = 1;
505 }
518 506
519 /* MP: ngx_shared_palloc() */ 507 /* MP: ngx_shared_palloc() */
520 508
521 peers = ngx_pcalloc(cf->pool, 509 u->addrs = ngx_pcalloc(cf->pool, i * sizeof(ngx_peer_addr_t));
522 sizeof(ngx_peers_t) + sizeof(ngx_peer_t) * (i - 1)); 510 if (u->addrs == NULL) {
523 if (peers == NULL) { 511 return NGX_ERROR;
524 return NULL; 512 }
525 } 513
526 514 u->naddrs = i;
527 peers->number = i;
528 515
529 for (i = 0; h->h_addr_list[i] != NULL; i++) { 516 for (i = 0; h->h_addr_list[i] != NULL; i++) {
530 517
531 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); 518 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in));
532 if (sin == NULL) { 519 if (sin == NULL) {
533 return NULL; 520 return NGX_ERROR;
534 } 521 }
535 522
536 sin->sin_family = AF_INET; 523 sin->sin_family = AF_INET;
537 sin->sin_port = htons(port); 524 sin->sin_port = htons(u->portn);
538 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]); 525 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
539 526
540 peers->peer[i].sockaddr = (struct sockaddr *) sin; 527 u->addrs[i].sockaddr = (struct sockaddr *) sin;
541 peers->peer[i].socklen = sizeof(struct sockaddr_in); 528 u->addrs[i].socklen = sizeof(struct sockaddr_in);
542 529
543 len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1; 530 len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1;
544 531
545 peers->peer[i].name.data = ngx_palloc(cf->pool, len); 532 u->addrs[i].name.data = ngx_palloc(cf->pool, len);
546 if (peers->peer[i].name.data == NULL) { 533 if (u->addrs[i].name.data == NULL) {
547 return NULL; 534 return NGX_ERROR;
548 } 535 }
549 536
550 len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin, 537 len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin,
551 peers->peer[i].name.data, len); 538 u->addrs[i].name.data, len);
552 539
553 peers->peer[i].name.len = 540 u->addrs[i].name.len = ngx_sprintf(&u->addrs[i].name.data[len],
554 ngx_sprintf(&peers->peer[i].name.data[len], 541 ":%d", u->portn)
555 ":%d", port) 542 - u->addrs[i].name.data;
556 - peers->peer[i].name.data;
557
558 peers->peer[i].uri_separator = "";
559
560 peers->peer[i].weight = NGX_CONF_UNSET_UINT;
561 peers->peer[i].max_fails = NGX_CONF_UNSET_UINT;
562 peers->peer[i].fail_timeout = NGX_CONF_UNSET;
563 } 543 }
564 544
565 } else { 545 } else {
566 546
567 /* MP: ngx_shared_palloc() */ 547 /* MP: ngx_shared_palloc() */
568 548
569 peers = ngx_pcalloc(cf->pool, sizeof(ngx_peers_t)); 549 u->addrs = ngx_pcalloc(cf->pool, sizeof(ngx_peer_addr_t));
570 if (peers == NULL) { 550 if (u->addrs == NULL) {
571 return NULL; 551 return NGX_ERROR;
572 } 552 }
573 553
574 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); 554 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in));
575 if (sin == NULL) { 555 if (sin == NULL) {
576 return NULL; 556 return NGX_ERROR;
577 } 557 }
578 558
579 peers->number = 1; 559 u->naddrs = 1;
580 560
581 sin->sin_family = AF_INET; 561 sin->sin_family = AF_INET;
582 sin->sin_port = htons(port); 562 sin->sin_port = htons(u->portn);
583 sin->sin_addr.s_addr = in_addr; 563 sin->sin_addr.s_addr = in_addr;
584 564
585 peers->peer[0].sockaddr = (struct sockaddr *) sin; 565 u->addrs[0].sockaddr = (struct sockaddr *) sin;
586 peers->peer[0].socklen = sizeof(struct sockaddr_in); 566 u->addrs[0].socklen = sizeof(struct sockaddr_in);
587 567
588 peers->peer[0].name.data = ngx_palloc(cf->pool, 568 u->addrs[0].name.data = ngx_palloc(cf->pool,
589 name->len + sizeof(":65536") - 1); 569 u->host.len + sizeof(":65536") - 1);
590 if (peers->peer[0].name.data == NULL) { 570 if (u->addrs[0].name.data == NULL) {
591 return NULL; 571 return NGX_ERROR;
592 } 572 }
593 573
594 peers->peer[0].name.len = ngx_sprintf(peers->peer[0].name.data, "%V:%d", 574 u->addrs[0].name.len = ngx_sprintf(u->addrs[0].name.data, "%V:%d",
595 name, port) 575 &u->host, u->portn)
596 - peers->peer[0].name.data; 576 - u->addrs[0].name.data;
597 577 }
598 peers->peer[0].uri_separator = ""; 578
599 } 579 return NGX_OK;
600
601 return peers;
602 } 580 }