Skip to content

Commit

Permalink
Introduced ngx_inet_get_port() and ngx_inet_set_port() functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
arut committed Jun 20, 2016
1 parent 72d4e5d commit 5b201ac
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 317 deletions.
83 changes: 62 additions & 21 deletions src/core/ngx_inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,9 @@ ngx_int_t
ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
size_t len)
{
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
u_char *p, *last;
size_t plen;
ngx_int_t rc, port;

rc = ngx_parse_addr(pool, addr, text, len);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/core/ngx_inet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
11 changes: 3 additions & 8 deletions src/core/ngx_proxy_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);
}
16 changes: 1 addition & 15 deletions src/core/ngx_resolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 1 addition & 13 deletions src/event/ngx_event_openssl_stapling.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
39 changes: 2 additions & 37 deletions src/http/modules/ngx_http_realip_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -242,21 +238,7 @@ ngx_http_realip_handler(ngx_http_request_t *r)
!= 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);
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 1 addition & 25 deletions src/http/ngx_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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++) {
Expand Down
23 changes: 1 addition & 22 deletions src/http/ngx_http_header_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions src/http/ngx_http_upstream_round_robin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 5b201ac

Please sign in to comment.