comparison src/stream/ngx_stream.c @ 7478:4f9b72a229c1

Multiple addresses in "listen". Previously only one address was used by the listen directive handler even if host name resolved to multiple addresses. Now a separate listening socket is created for each address.
author Roman Arutyunyan <arut@nginx.com>
date Fri, 15 Mar 2019 15:45:56 +0300
parents e7b2b907c0f8
children 2ab7b55ae4a0 893b3313f53c
comparison
equal deleted inserted replaced
7477:c74904a17021 7478:4f9b72a229c1
385 ngx_uint_t i; 385 ngx_uint_t i;
386 struct sockaddr *sa; 386 struct sockaddr *sa;
387 ngx_stream_conf_port_t *port; 387 ngx_stream_conf_port_t *port;
388 ngx_stream_conf_addr_t *addr; 388 ngx_stream_conf_addr_t *addr;
389 389
390 sa = &listen->sockaddr.sockaddr; 390 sa = listen->sockaddr;
391 p = ngx_inet_get_port(sa); 391 p = ngx_inet_get_port(sa);
392 392
393 port = ports->elts; 393 port = ports->elts;
394 for (i = 0; i < ports->nelts; i++) { 394 for (i = 0; i < ports->nelts; i++) {
395 395
474 if (bind_wildcard && !addr[i].opt.bind) { 474 if (bind_wildcard && !addr[i].opt.bind) {
475 i++; 475 i++;
476 continue; 476 continue;
477 } 477 }
478 478
479 ls = ngx_create_listening(cf, &addr[i].opt.sockaddr.sockaddr, 479 ls = ngx_create_listening(cf, addr[i].opt.sockaddr,
480 addr[i].opt.socklen); 480 addr[i].opt.socklen);
481 if (ls == NULL) { 481 if (ls == NULL) {
482 return NGX_CONF_ERROR; 482 return NGX_CONF_ERROR;
483 } 483 }
484 484
549 549
550 static ngx_int_t 550 static ngx_int_t
551 ngx_stream_add_addrs(ngx_conf_t *cf, ngx_stream_port_t *stport, 551 ngx_stream_add_addrs(ngx_conf_t *cf, ngx_stream_port_t *stport,
552 ngx_stream_conf_addr_t *addr) 552 ngx_stream_conf_addr_t *addr)
553 { 553 {
554 u_char *p;
555 size_t len;
556 ngx_uint_t i; 554 ngx_uint_t i;
557 struct sockaddr_in *sin; 555 struct sockaddr_in *sin;
558 ngx_stream_in_addr_t *addrs; 556 ngx_stream_in_addr_t *addrs;
559 u_char buf[NGX_SOCKADDR_STRLEN];
560 557
561 stport->addrs = ngx_pcalloc(cf->pool, 558 stport->addrs = ngx_pcalloc(cf->pool,
562 stport->naddrs * sizeof(ngx_stream_in_addr_t)); 559 stport->naddrs * sizeof(ngx_stream_in_addr_t));
563 if (stport->addrs == NULL) { 560 if (stport->addrs == NULL) {
564 return NGX_ERROR; 561 return NGX_ERROR;
566 563
567 addrs = stport->addrs; 564 addrs = stport->addrs;
568 565
569 for (i = 0; i < stport->naddrs; i++) { 566 for (i = 0; i < stport->naddrs; i++) {
570 567
571 sin = &addr[i].opt.sockaddr.sockaddr_in; 568 sin = (struct sockaddr_in *) addr[i].opt.sockaddr;
572 addrs[i].addr = sin->sin_addr.s_addr; 569 addrs[i].addr = sin->sin_addr.s_addr;
573 570
574 addrs[i].conf.ctx = addr[i].opt.ctx; 571 addrs[i].conf.ctx = addr[i].opt.ctx;
575 #if (NGX_STREAM_SSL) 572 #if (NGX_STREAM_SSL)
576 addrs[i].conf.ssl = addr[i].opt.ssl; 573 addrs[i].conf.ssl = addr[i].opt.ssl;
577 #endif 574 #endif
578 addrs[i].conf.proxy_protocol = addr[i].opt.proxy_protocol; 575 addrs[i].conf.proxy_protocol = addr[i].opt.proxy_protocol;
579 576 addrs[i].conf.addr_text = addr[i].opt.addr_text;
580 len = ngx_sock_ntop(&addr[i].opt.sockaddr.sockaddr, addr[i].opt.socklen,
581 buf, NGX_SOCKADDR_STRLEN, 1);
582
583 p = ngx_pnalloc(cf->pool, len);
584 if (p == NULL) {
585 return NGX_ERROR;
586 }
587
588 ngx_memcpy(p, buf, len);
589
590 addrs[i].conf.addr_text.len = len;
591 addrs[i].conf.addr_text.data = p;
592 } 577 }
593 578
594 return NGX_OK; 579 return NGX_OK;
595 } 580 }
596 581
599 584
600 static ngx_int_t 585 static ngx_int_t
601 ngx_stream_add_addrs6(ngx_conf_t *cf, ngx_stream_port_t *stport, 586 ngx_stream_add_addrs6(ngx_conf_t *cf, ngx_stream_port_t *stport,
602 ngx_stream_conf_addr_t *addr) 587 ngx_stream_conf_addr_t *addr)
603 { 588 {
604 u_char *p;
605 size_t len;
606 ngx_uint_t i; 589 ngx_uint_t i;
607 struct sockaddr_in6 *sin6; 590 struct sockaddr_in6 *sin6;
608 ngx_stream_in6_addr_t *addrs6; 591 ngx_stream_in6_addr_t *addrs6;
609 u_char buf[NGX_SOCKADDR_STRLEN];
610 592
611 stport->addrs = ngx_pcalloc(cf->pool, 593 stport->addrs = ngx_pcalloc(cf->pool,
612 stport->naddrs * sizeof(ngx_stream_in6_addr_t)); 594 stport->naddrs * sizeof(ngx_stream_in6_addr_t));
613 if (stport->addrs == NULL) { 595 if (stport->addrs == NULL) {
614 return NGX_ERROR; 596 return NGX_ERROR;
616 598
617 addrs6 = stport->addrs; 599 addrs6 = stport->addrs;
618 600
619 for (i = 0; i < stport->naddrs; i++) { 601 for (i = 0; i < stport->naddrs; i++) {
620 602
621 sin6 = &addr[i].opt.sockaddr.sockaddr_in6; 603 sin6 = (struct sockaddr_in6 *) addr[i].opt.sockaddr;
622 addrs6[i].addr6 = sin6->sin6_addr; 604 addrs6[i].addr6 = sin6->sin6_addr;
623 605
624 addrs6[i].conf.ctx = addr[i].opt.ctx; 606 addrs6[i].conf.ctx = addr[i].opt.ctx;
625 #if (NGX_STREAM_SSL) 607 #if (NGX_STREAM_SSL)
626 addrs6[i].conf.ssl = addr[i].opt.ssl; 608 addrs6[i].conf.ssl = addr[i].opt.ssl;
627 #endif 609 #endif
628 addrs6[i].conf.proxy_protocol = addr[i].opt.proxy_protocol; 610 addrs6[i].conf.proxy_protocol = addr[i].opt.proxy_protocol;
629 611 addrs6[i].conf.addr_text = addr[i].opt.addr_text;
630 len = ngx_sock_ntop(&addr[i].opt.sockaddr.sockaddr, addr[i].opt.socklen,
631 buf, NGX_SOCKADDR_STRLEN, 1);
632
633 p = ngx_pnalloc(cf->pool, len);
634 if (p == NULL) {
635 return NGX_ERROR;
636 }
637
638 ngx_memcpy(p, buf, len);
639
640 addrs6[i].conf.addr_text.len = len;
641 addrs6[i].conf.addr_text.data = p;
642 } 612 }
643 613
644 return NGX_OK; 614 return NGX_OK;
645 } 615 }
646 616