sthttpd icon indicating copy to clipboard operation
sthttpd copied to clipboard

Use real IP if available

Open vldzhk opened this issue 5 years ago • 0 comments

The header X-Forwarded-For may contain an ipv4 address for an ipv6 connection, and vice versa

more details https://blog.steve.fi/ipv6_and_thttpd.html

--- src/libhttpd.c
+++ src/libhttpd.c.new
@@ -2207,7 +2204,10 @@
 		{ // Use real IP if available 
 		cp = &buf[16];
 		cp += strspn( cp, " \t" );
-		inet_aton( cp, &(hc->client_addr.sa_in.sin_addr) );
+		if (inet_pton(AF_INET, cp, &(hc->client_addr.sa_in.sin_addr)))
+			hc->client_addr.sa.sa_family = AF_INET;
+		else if (inet_pton(AF_INET6, cp, &(hc->client_addr.sa_in6.sin6_addr)))
+			hc->client_addr.sa.sa_family = AF_INET6;
 		}

vldzhk avatar Mar 10 '20 16:03 vldzhk