Skip to content

Commit

Permalink
Realip: port support in X-Real-IP and X-Forwarded-For.
Browse files Browse the repository at this point in the history
Now, the module extracts optional port which may accompany an
IP address.  This custom extension is introduced, among other
things, in order to facilitate logging of original client ports.
Addresses with ports are expected to be in the RFC 3986 format,
that is, with IPv6 addresses in square brackets.  E.g.,
"X-Real-IP: [2001:0db8::1]:12345" sets client port ($remote_port)
to 12345.
  • Loading branch information
xeioex committed May 23, 2016
1 parent 19140c8 commit 06176bc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
79 changes: 79 additions & 0 deletions src/core/ngx_inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,85 @@ ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text, size_t len)
}


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

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

if (rc != NGX_DECLINED) {
return rc;
}

last = text + len;

#if (NGX_HAVE_INET6)
if (len && text[0] == '[') {

p = ngx_strlchr(text, last, ']');

if (p == NULL || p == last - 1 || *++p != ':') {
return NGX_DECLINED;
}

text++;
len -= 2;

} else
#endif

{
p = ngx_strlchr(text, last, ':');

if (p == NULL) {
return NGX_DECLINED;
}
}

p++;
plen = last - p;

port = ngx_atoi(p, plen);

if (port < 1 || port > 65535) {
return NGX_DECLINED;
}

len -= plen + 1;

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

if (rc != NGX_OK) {
return rc;
}

switch (addr->sockaddr->sa_family) {

#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) addr->sockaddr;
sin6->sin6_port = htons(port);
break;
#endif

default: /* AF_INET */
sin = (struct sockaddr_in *) addr->sockaddr;
sin->sin_port = htons(port);
break;
}

return NGX_OK;
}


ngx_int_t
ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u)
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/ngx_inet.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len);
ngx_int_t ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr);
ngx_int_t ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
size_t len);
ngx_int_t ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr,
u_char *text, size_t len);
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,
Expand Down
4 changes: 3 additions & 1 deletion src/http/ngx_http_core_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -2910,7 +2910,9 @@ ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r, ngx_addr_t *addr,
}
}

if (ngx_parse_addr(r->pool, &paddr, p, xfflen - (p - xff)) != NGX_OK) {
if (ngx_parse_addr_port(r->pool, &paddr, p, xfflen - (p - xff))
!= NGX_OK)
{
return NGX_DECLINED;
}

Expand Down

0 comments on commit 06176bc

Please sign in to comment.