Introduced ngx_inet_get_port() and ngx_inet_set_port() functions.

This commit is contained in:
Roman Arutyunyan 2016-06-20 11:50:39 +03:00
parent 72d4e5d793
commit 5b201ac31f
15 changed files with 90 additions and 317 deletions

View File

@ -532,10 +532,6 @@ ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
u_char *p, *last;
size_t plen;
ngx_int_t rc, port;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
rc = ngx_parse_addr(pool, addr, text, len);
@ -585,20 +581,7 @@ ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
return rc;
}
switch (addr->sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) addr->sockaddr;
sin6->sin6_port = htons((in_port_t) port);
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) addr->sockaddr;
sin->sin_port = htons((in_port_t) port);
break;
}
ngx_inet_set_port(addr->sockaddr, port);
return NGX_OK;
}
@ -1356,3 +1339,61 @@ ngx_cmp_sockaddr(struct sockaddr *sa1, socklen_t slen1,
return NGX_OK;
}
in_port_t
ngx_inet_get_port(struct sockaddr *sa)
{
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
switch (sa->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) sa;
return ntohs(sin6->sin6_port);
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
return 0;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) sa;
return ntohs(sin->sin_port);
}
}
void
ngx_inet_set_port(struct sockaddr *sa, in_port_t port)
{
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
switch (sa->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) sa;
sin6->sin6_port = htons(port);
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) sa;
sin->sin_port = htons(port);
break;
}
}

View File

@ -121,6 +121,8 @@ ngx_int_t ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u);
ngx_int_t ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u);
ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, socklen_t slen1,
struct sockaddr *sa2, socklen_t slen2, ngx_uint_t cmp_port);
in_port_t ngx_inet_get_port(struct sockaddr *sa);
void ngx_inet_set_port(struct sockaddr *sa, in_port_t port);
#endif /* _NGX_INET_H_INCLUDED_ */

View File

@ -141,19 +141,11 @@ ngx_proxy_protocol_write(ngx_connection_t *c, u_char *buf, u_char *last)
case AF_INET:
buf = ngx_cpymem(buf, "PROXY TCP4 ", sizeof("PROXY TCP4 ") - 1);
port = ntohs(((struct sockaddr_in *) c->sockaddr)->sin_port);
lport = ntohs(((struct sockaddr_in *) c->local_sockaddr)->sin_port);
break;
#if (NGX_HAVE_INET6)
case AF_INET6:
buf = ngx_cpymem(buf, "PROXY TCP6 ", sizeof("PROXY TCP6 ") - 1);
port = ntohs(((struct sockaddr_in6 *) c->sockaddr)->sin6_port);
lport = ntohs(((struct sockaddr_in6 *) c->local_sockaddr)->sin6_port);
break;
#endif
@ -169,5 +161,8 @@ ngx_proxy_protocol_write(ngx_connection_t *c, u_char *buf, u_char *last)
buf += ngx_sock_ntop(c->local_sockaddr, c->local_socklen, buf, last - buf,
0);
port = ngx_inet_get_port(c->sockaddr);
lport = ngx_inet_get_port(c->local_sockaddr);
return ngx_slprintf(buf, last, " %ui %ui" CRLF, port, lport);
}

View File

@ -2996,12 +2996,8 @@ ngx_resolver_srv_names_handler(ngx_resolver_ctx_t *cctx)
ngx_addr_t *addrs;
ngx_resolver_t *r;
ngx_sockaddr_t *sockaddr;
struct sockaddr_in *sin;
ngx_resolver_ctx_t *ctx;
ngx_resolver_srv_name_t *srv;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
r = cctx->resolver;
ctx = cctx->data;
@ -3045,17 +3041,7 @@ ngx_resolver_srv_names_handler(ngx_resolver_ctx_t *cctx)
ngx_memcpy(&sockaddr[i], cctx->addrs[i].sockaddr,
addrs[i].socklen);
switch (addrs[i].sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) addrs[i].sockaddr;
sin6->sin6_port = htons(srv->port);
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) addrs[i].sockaddr;
sin->sin_port = htons(srv->port);
}
ngx_inet_set_port(addrs[i].sockaddr, srv->port);
}
srv->addrs = addrs;

View File

@ -920,7 +920,6 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolver_ctx_t *resolve)
u_char *p;
size_t len;
in_port_t port;
socklen_t socklen;
ngx_uint_t i;
struct sockaddr *sockaddr;
@ -962,8 +961,6 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolver_ctx_t *resolve)
goto failed;
}
port = htons(ctx->port);
for (i = 0; i < resolve->naddrs; i++) {
socklen = resolve->addrs[i].socklen;
@ -974,16 +971,7 @@ ngx_ssl_ocsp_resolve_handler(ngx_resolver_ctx_t *resolve)
}
ngx_memcpy(sockaddr, resolve->addrs[i].sockaddr, socklen);
switch (sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
((struct sockaddr_in6 *) sockaddr)->sin6_port = port;
break;
#endif
default: /* AF_INET */
((struct sockaddr_in *) sockaddr)->sin_port = port;
}
ngx_inet_set_port(sockaddr, ctx->port);
ctx->addrs[i].sockaddr = sockaddr;
ctx->addrs[i].socklen = socklen;

View File

@ -138,10 +138,6 @@ ngx_http_realip_handler(ngx_http_request_t *r)
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_connection_t *c;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
ngx_http_realip_ctx_t *ctx;
ngx_http_realip_loc_conf_t *rlcf;
@ -242,21 +238,7 @@ found:
!= NGX_DECLINED)
{
if (rlcf->type == NGX_HTTP_REALIP_PROXY) {
switch (addr.sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) addr.sockaddr;
sin6->sin6_port = htons(c->proxy_protocol_port);
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) addr.sockaddr;
sin->sin_port = htons(c->proxy_protocol_port);
break;
}
ngx_inet_set_port(addr.sockaddr, c->proxy_protocol_port);
}
return ngx_http_realip_set_addr(r, &addr);
@ -578,24 +560,7 @@ ngx_http_realip_remote_port_variable(ngx_http_request_t *r,
return NGX_ERROR;
}
switch (sa->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
port = ntohs(((struct sockaddr_in6 *) sa)->sin6_port);
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
port = 0;
break;
#endif
default: /* AF_INET */
port = ntohs(((struct sockaddr_in *) sa)->sin_port);
break;
}
port = ngx_inet_get_port(sa);
if (port > 0 && port < 65536) {
v->len = ngx_sprintf(v->data, "%ui", port) - v->data;

View File

@ -1144,12 +1144,8 @@ ngx_http_add_listen(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
in_port_t p;
ngx_uint_t i;
struct sockaddr *sa;
struct sockaddr_in *sin;
ngx_http_conf_port_t *port;
ngx_http_core_main_conf_t *cmcf;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
@ -1162,27 +1158,7 @@ ngx_http_add_listen(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
}
sa = &lsopt->sockaddr.sockaddr;
switch (sa->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = &lsopt->sockaddr.sockaddr_in6;
p = sin6->sin6_port;
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
p = 0;
break;
#endif
default: /* AF_INET */
sin = &lsopt->sockaddr.sockaddr_in;
p = sin->sin_port;
break;
}
p = ngx_inet_get_port(sa);
port = cmcf->ports->elts;
for (i = 0; i < cmcf->ports->nelts; i++) {

View File

@ -161,10 +161,6 @@ ngx_http_header_filter(ngx_http_request_t *r)
ngx_connection_t *c;
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t *cscf;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
u_char addr[NGX_SOCKADDR_STRLEN];
if (r->header_sent) {
@ -333,24 +329,7 @@ ngx_http_header_filter(ngx_http_request_t *r)
}
}
switch (c->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
port = ntohs(sin6->sin6_port);
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
port = 0;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) c->local_sockaddr;
port = ntohs(sin->sin_port);
break;
}
port = ngx_inet_get_port(c->local_sockaddr);
len += sizeof("Location: https://") - 1
+ host.len

View File

@ -354,16 +354,7 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
}
ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
switch (sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
((struct sockaddr_in6 *) sockaddr)->sin6_port = htons(ur->port);
break;
#endif
default: /* AF_INET */
((struct sockaddr_in *) sockaddr)->sin_port = htons(ur->port);
}
ngx_inet_set_port(sockaddr, ur->port);
p = ngx_pnalloc(r->pool, NGX_SOCKADDR_STRLEN);
if (p == NULL) {

View File

@ -1202,10 +1202,6 @@ ngx_http_variable_remote_port(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_uint_t port;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
v->len = 0;
v->valid = 1;
@ -1217,26 +1213,7 @@ ngx_http_variable_remote_port(ngx_http_request_t *r,
return NGX_ERROR;
}
switch (r->connection->sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
port = ntohs(sin6->sin6_port);
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
port = 0;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) r->connection->sockaddr;
port = ntohs(sin->sin_port);
break;
}
port = ngx_inet_get_port(r->connection->sockaddr);
if (port > 0 && port < 65536) {
v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
@ -1322,10 +1299,6 @@ ngx_http_variable_server_port(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_uint_t port;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
v->len = 0;
v->valid = 1;
@ -1341,26 +1314,7 @@ ngx_http_variable_server_port(ngx_http_request_t *r,
return NGX_ERROR;
}
switch (r->connection->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
port = ntohs(sin6->sin6_port);
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
port = 0;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) r->connection->local_sockaddr;
port = ntohs(sin->sin_port);
break;
}
port = ngx_inet_get_port(r->connection->local_sockaddr);
if (port > 0 && port < 65536) {
v->len = ngx_sprintf(v->data, "%ui", port) - v->data;

View File

@ -137,10 +137,6 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
ngx_http_v2_out_frame_t *frame;
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t *cscf;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
u_char addr[NGX_SOCKADDR_STRLEN];
static const u_char nginx[5] = "\x84\xaa\x63\x55\xe7";
@ -280,24 +276,7 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
}
}
switch (fc->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) fc->local_sockaddr;
port = ntohs(sin6->sin6_port);
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
port = 0;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) fc->local_sockaddr;
port = ntohs(sin->sin_port);
break;
}
port = ngx_inet_get_port(fc->local_sockaddr);
location.len = sizeof("https://") - 1 + host.len
+ r->headers_out.location->value.len;

View File

@ -228,35 +228,11 @@ ngx_mail_add_ports(ngx_conf_t *cf, ngx_array_t *ports,
in_port_t p;
ngx_uint_t i;
struct sockaddr *sa;
struct sockaddr_in *sin;
ngx_mail_conf_port_t *port;
ngx_mail_conf_addr_t *addr;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
sa = &listen->sockaddr.sockaddr;
switch (sa->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = &listen->sockaddr.sockaddr_in6;
p = sin6->sin6_port;
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
p = 0;
break;
#endif
default: /* AF_INET */
sin = &listen->sockaddr.sockaddr_in;
p = sin->sin_port;
break;
}
p = ngx_inet_get_port(sa);
port = ports->elts;
for (i = 0; i < ports->nelts; i++) {

View File

@ -467,10 +467,6 @@ ngx_mail_auth_http_process_headers(ngx_mail_session_t *s,
size_t len, size;
ngx_int_t rc, port, n;
ngx_addr_t *peer;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
"mail auth http process headers");
@ -813,20 +809,7 @@ ngx_mail_auth_http_process_headers(ngx_mail_session_t *s,
return;
}
switch (peer->sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) peer->sockaddr;
sin6->sin6_port = htons((in_port_t) port);
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) peer->sockaddr;
sin->sin_port = htons((in_port_t) port);
break;
}
ngx_inet_set_port(peer->sockaddr, port);
len = ctx->addr.len + 1 + ctx->port.len;

View File

@ -243,35 +243,11 @@ ngx_stream_add_ports(ngx_conf_t *cf, ngx_array_t *ports,
in_port_t p;
ngx_uint_t i;
struct sockaddr *sa;
struct sockaddr_in *sin;
ngx_stream_conf_port_t *port;
ngx_stream_conf_addr_t *addr;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
sa = &listen->sockaddr.sockaddr;
switch (sa->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = &listen->sockaddr.sockaddr_in6;
p = sin6->sin6_port;
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
p = 0;
break;
#endif
default: /* AF_INET */
sin = &listen->sockaddr.sockaddr_in;
p = sin->sin_port;
break;
}
p = ngx_inet_get_port(sa);
port = ports->elts;
for (i = 0; i < ports->nelts; i++) {

View File

@ -448,10 +448,6 @@ ngx_stream_proxy_set_local(ngx_stream_session_t *s, ngx_stream_upstream_t *u,
{
ngx_addr_t *addr;
ngx_connection_t *c;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
if (local == NULL) {
u->peer.local = NULL;
@ -484,21 +480,7 @@ ngx_stream_proxy_set_local(ngx_stream_session_t *s, ngx_stream_upstream_t *u,
}
ngx_memcpy(addr->sockaddr, c->sockaddr, c->socklen);
switch (addr->sockaddr->sa_family) {
case AF_INET:
sin = (struct sockaddr_in *) addr->sockaddr;
sin->sin_port = 0;
break;
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) addr->sockaddr;
sin6->sin6_port = 0;
break;
#endif
}
ngx_inet_set_port(addr->sockaddr, 0);
addr->name = c->addr_text;
u->peer.local = addr;