<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>[nginx] svn commit: r4624 - trunk/src/http</title>
<description>Author: ru
Date: 2012-05-14 12:27:41 +0000 (Mon, 14 May 2012)
New Revision: 4624
URL: http://trac.nginx.org/nginx/changeset/4624/nginx
Log:
New function ngx_http_get_forwarded_addr() to look up real client address.
On input it takes an original address, string in the X-Forwarded-For format
and its length, list of trusted proxies, and a flag indicating to perform
the recursive search. On output it returns NGX_OK and the &amp;quot;deepest&amp;quot; valid
address in a chain, or NGX_DECLINED. It supports AF_INET and AF_INET6.
Additionally, original address and/or proxy may be specified as AF_UNIX.
Modified:
trunk/src/http/ngx_http_core_module.c
trunk/src/http/ngx_http_core_module.h
Modified: trunk/src/http/ngx_http_core_module.c
===================================================================
--- trunk/src/http/ngx_http_core_module.c 2012-05-14 09:58:07 UTC (rev 4623)
+++ trunk/src/http/ngx_http_core_module.c 2012-05-14 12:27:41 UTC (rev 4624)
@@ -2699,6 +2699,102 @@
}
+ngx_int_t
+ngx_http_get_forwarded_addr(ngx_http_request_t *r, ngx_addr_t *addr,
+ u_char *xff, size_t xfflen, ngx_array_t *proxies, int recursive)
+{
+ u_char *p;
+ in_addr_t *inaddr;
+ ngx_addr_t paddr;
+ ngx_cidr_t *cidr;
+ ngx_uint_t family, i;
+#if (NGX_HAVE_INET6)
+ ngx_uint_t n;
+ struct in6_addr *inaddr6;
+#endif
+
+ family = addr-&amp;gt;sockaddr-&amp;gt;sa_family;
+
+ if (family == AF_INET) {
+ inaddr = &amp;amp;((struct sockaddr_in *) addr-&amp;gt;sockaddr)-&amp;gt;sin_addr.s_addr;
+ }
+
+#if (NGX_HAVE_INET6)
+ else if (family == AF_INET6) {
+ inaddr6 = &amp;amp;((struct sockaddr_in6 *) addr-&amp;gt;sockaddr)-&amp;gt;sin6_addr;
+
+ if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
+ family = AF_INET;
+ inaddr = (in_addr_t *) &amp;amp;inaddr6-&amp;gt;s6_addr[12];
+ }
+ }
+#endif
+
+ for (cidr = proxies-&amp;gt;elts, i = 0; i &amp;lt; proxies-&amp;gt;nelts; i++) {
+ if (cidr[i].family != family) {
+ goto next;
+ }
+
+ switch (family) {
+
+#if (NGX_HAVE_INET6)
+ case AF_INET6:
+ for (n = 0; n &amp;lt; 16; n++) {
+ if ((inaddr6-&amp;gt;s6_addr[n] &amp;amp; cidr[i].u.in6.mask.s6_addr[n])
+ != cidr[i].u.in6.addr.s6_addr[n])
+ {
+ goto next;
+ }
+ }
+ break;
+#endif
+
+#if (NGX_HAVE_UNIX_DOMAIN)
+ case AF_UNIX:
+ break;
+#endif
+
+ default: /* AF_INET */
+ if ((*inaddr &amp;amp; cidr[i].u.in.mask) != cidr[i].u.in.addr) {
+ goto next;
+ }
+ break;
+ }
+
+ for (p = xff + xfflen - 1; p &amp;gt; xff; p--, xfflen--) {
+ if (*p != ' ' &amp;amp;&amp;amp; *p != ',') {
+ break;
+ }
+ }
+
+ for ( /* void */ ; p &amp;gt; xff; p--) {
+ if (*p == ' ' || *p == ',') {
+ p++;
+ break;
+ }
+ }
+
+ if (ngx_parse_addr(r-&amp;gt;pool, &amp;amp;paddr, p, xfflen - (p - xff)) != NGX_OK) {
+ return NGX_DECLINED;
+ }
+
+ *addr = paddr;
+
+ if (recursive &amp;amp;&amp;amp; p &amp;gt; xff) {
+ (void) ngx_http_get_forwarded_addr(r, addr, xff, p - 1 - xff,
+ proxies, 1);
+ }
+
+ return NGX_OK;
+
+ next:
+ continue;
+ }
+
+ return NGX_DECLINED;
+}
+
+
static char *
ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
{
Modified: trunk/src/http/ngx_http_core_module.h
===================================================================
--- trunk/src/http/ngx_http_core_module.h 2012-05-14 09:58:07 UTC (rev 4623)
+++ trunk/src/http/ngx_http_core_module.h 2012-05-14 12:27:41 UTC (rev 4624)
@@ -513,7 +513,10 @@
ngx_int_t ngx_http_set_disable_symlinks(ngx_http_request_t *r,
ngx_http_core_loc_conf_t *clcf, ngx_str_t *path, ngx_open_file_info_t *of);
+ngx_int_t ngx_http_get_forwarded_addr(ngx_http_request_t *r, ngx_addr_t *addr,
+ u_char *xff, size_t xfflen, ngx_array_t *proxies, int recursive);
+
extern ngx_module_t ngx_http_core_module;
extern ngx_uint_t ngx_http_max_module;
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel</description><link>http://forum.nginx.org/read.php?29,226396,226396#msg-226396</link><lastBuildDate>Tue, 21 May 2013 13:37:22 -0400</lastBuildDate>
<generator>Phorum 5.2.16</generator>
<item>
<guid>http://forum.nginx.org/read.php?29,226396,226396#msg-226396</guid>
<title>[nginx] svn commit: r4624 - trunk/src/http</title><link>http://forum.nginx.org/read.php?29,226396,226396#msg-226396</link><description><![CDATA[Author: ru<br />Date: 2012-05-14 12:27:41 +0000 (Mon, 14 May 2012)<br />New Revision: 4624<br />URL: http://trac.nginx.org/nginx/changeset/4624/nginx<br /><br />Log:<br />New function ngx_http_get_forwarded_addr() to look up real client address.<br /><br />On input it takes an original address, string in the X-Forwarded-For format<br />and its length, list of trusted proxies, and a flag indicating to perform<br />the recursive search. On output it returns NGX_OK and the &quot;deepest&quot; valid<br />address in a chain, or NGX_DECLINED. It supports AF_INET and AF_INET6.<br />Additionally, original address and/or proxy may be specified as AF_UNIX.<br /><br /><br />Modified:<br />trunk/src/http/ngx_http_core_module.c<br />trunk/src/http/ngx_http_core_module.h<br /><br />Modified: trunk/src/http/ngx_http_core_module.c<br />===================================================================<br />--- trunk/src/http/ngx_http_core_module.c 2012-05-14 09:58:07 UTC (rev 4623)<br />+++ trunk/src/http/ngx_http_core_module.c 2012-05-14 12:27:41 UTC (rev 4624)<br />@@ -2699,6 +2699,102 @@<br />}<br /><br /><br />+ngx_int_t<br />+ngx_http_get_forwarded_addr(ngx_http_request_t *r, ngx_addr_t *addr,<br />+ u_char *xff, size_t xfflen, ngx_array_t *proxies, int recursive)<br />+{<br />+ u_char *p;<br />+ in_addr_t *inaddr;<br />+ ngx_addr_t paddr;<br />+ ngx_cidr_t *cidr;<br />+ ngx_uint_t family, i;<br />+#if (NGX_HAVE_INET6)<br />+ ngx_uint_t n;<br />+ struct in6_addr *inaddr6;<br />+#endif<br />+<br />+ family = addr-&gt;sockaddr-&gt;sa_family;<br />+<br />+ if (family == AF_INET) {<br />+ inaddr = &amp;((struct sockaddr_in *) addr-&gt;sockaddr)-&gt;sin_addr.s_addr;<br />+ }<br />+<br />+#if (NGX_HAVE_INET6)<br />+ else if (family == AF_INET6) {<br />+ inaddr6 = &amp;((struct sockaddr_in6 *) addr-&gt;sockaddr)-&gt;sin6_addr;<br />+<br />+ if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {<br />+ family = AF_INET;<br />+ inaddr = (in_addr_t *) &amp;inaddr6-&gt;s6_addr[12];<br />+ }<br />+ }<br />+#endif<br />+<br />+ for (cidr = proxies-&gt;elts, i = 0; i &lt; proxies-&gt;nelts; i++) {<br />+ if (cidr[i].family != family) {<br />+ goto next;<br />+ }<br />+<br />+ switch (family) {<br />+<br />+#if (NGX_HAVE_INET6)<br />+ case AF_INET6:<br />+ for (n = 0; n &lt; 16; n++) {<br />+ if ((inaddr6-&gt;s6_addr[n] &amp; cidr[i].u.in6.mask.s6_addr[n])<br />+ != cidr[i].u.in6.addr.s6_addr[n])<br />+ {<br />+ goto next;<br />+ }<br />+ }<br />+ break;<br />+#endif<br />+<br />+#if (NGX_HAVE_UNIX_DOMAIN)<br />+ case AF_UNIX:<br />+ break;<br />+#endif<br />+<br />+ default: /* AF_INET */<br />+ if ((*inaddr &amp; cidr[i].u.in.mask) != cidr[i].u.in.addr) {<br />+ goto next;<br />+ }<br />+ break;<br />+ }<br />+<br />+ for (p = xff + xfflen - 1; p &gt; xff; p--, xfflen--) {<br />+ if (*p != ' ' &amp;&amp; *p != ',') {<br />+ break;<br />+ }<br />+ }<br />+<br />+ for ( /* void */ ; p &gt; xff; p--) {<br />+ if (*p == ' ' || *p == ',') {<br />+ p++;<br />+ break;<br />+ }<br />+ }<br />+<br />+ if (ngx_parse_addr(r-&gt;pool, &amp;paddr, p, xfflen - (p - xff)) != NGX_OK) {<br />+ return NGX_DECLINED;<br />+ }<br />+<br />+ *addr = paddr;<br />+<br />+ if (recursive &amp;&amp; p &gt; xff) {<br />+ (void) ngx_http_get_forwarded_addr(r, addr, xff, p - 1 - xff,<br />+ proxies, 1);<br />+ }<br />+<br />+ return NGX_OK;<br />+<br />+ next:<br />+ continue;<br />+ }<br />+<br />+ return NGX_DECLINED;<br />+}<br />+<br />+<br />static char *<br />ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)<br />{<br /><br />Modified: trunk/src/http/ngx_http_core_module.h<br />===================================================================<br />--- trunk/src/http/ngx_http_core_module.h 2012-05-14 09:58:07 UTC (rev 4623)<br />+++ trunk/src/http/ngx_http_core_module.h 2012-05-14 12:27:41 UTC (rev 4624)<br />@@ -513,7 +513,10 @@<br />ngx_int_t ngx_http_set_disable_symlinks(ngx_http_request_t *r,<br />ngx_http_core_loc_conf_t *clcf, ngx_str_t *path, ngx_open_file_info_t *of);<br /><br />+ngx_int_t ngx_http_get_forwarded_addr(ngx_http_request_t *r, ngx_addr_t *addr,<br />+ u_char *xff, size_t xfflen, ngx_array_t *proxies, int recursive);<br /><br />+<br />extern ngx_module_t ngx_http_core_module;<br /><br />extern ngx_uint_t ngx_http_max_module;<br /><br />_______________________________________________<br />nginx-devel mailing list<br />nginx-devel@nginx.org<br />http://mailman.nginx.org/mailman/listinfo/nginx-devel]]></description>
<dc:creator>Anonymous User</dc:creator>
<category>Nginx Development</category><pubDate>Mon, 14 May 2012 09:46:01 -0400</pubDate></item>
</channel>
</rss>