diff options
author | James Brown <jbrown@easypost.com> | 2016-03-11 15:36:47 -0800 |
---|---|---|
committer | James Brown <jbrown@easypost.com> | 2017-01-31 10:18:02 -0800 |
commit | cecfe60b1a8b099cea76e05082adbd933341bf14 (patch) | |
tree | 32eb804535e2004384173af875698d43b29e8953 /proto | |
parent | 2d76dfe9fe2015183b6fb838d223a1fb4d9815f5 (diff) | |
download | uwsgi-cecfe60b1a8b099cea76e05082adbd933341bf14.tar.gz |
correctly handle IPv6 sources in proto/http
Diffstat (limited to 'proto')
-rw-r--r-- | proto/http.c | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/proto/http.c b/proto/http.c index fa1e0ebc..c797ee02 100644 --- a/proto/http.c +++ b/proto/http.c @@ -207,8 +207,8 @@ static int http_parse(struct wsgi_request *wsgi_req, char *watermark) { char *ptr = wsgi_req->proto_parser_buf; char *base = ptr; char *query_string = NULL; - char ip[INET_ADDRSTRLEN+1]; - struct sockaddr_in *http_sin = (struct sockaddr_in *) &wsgi_req->c_addr; + char ip[INET6_ADDRSTRLEN+1]; + struct sockaddr *http_sa = (struct sockaddr *) &wsgi_req->c_addr; char *proxy_src = NULL; char *proxy_dst = NULL; char *proxy_src_port = NULL; @@ -328,14 +328,35 @@ static int http_parse(struct wsgi_request *wsgi_req, char *watermark) { } } else { - // TODO add ipv6 support - memset(ip, 0, INET_ADDRSTRLEN+1); - if (inet_ntop(AF_INET, (void *) &http_sin->sin_addr.s_addr, ip, INET_ADDRSTRLEN)) { - wsgi_req->len += proto_base_add_uwsgi_var(wsgi_req, "REMOTE_ADDR", 11, ip, strlen(ip)); - } - else { - uwsgi_error("inet_ntop()"); - return -1; + // TODO log something useful for AF_UNIX sockets + switch (http_sa->sa_family) { + case AF_INET6: + { + memset(ip, 0, INET6_ADDRSTRLEN+1); + struct sockaddr_in6* http_sin = (struct sockaddr_in6*)http_sa; + if (inet_ntop(AF_INET6, (void *) &http_sin->sin6_addr, ip, INET6_ADDRSTRLEN)) { + wsgi_req->len += proto_base_add_uwsgi_var(wsgi_req, "REMOTE_ADDR", 11, ip, strlen(ip)); + } + else { + uwsgi_error("inet_ntop()"); + return -1; + } + } + break; + case AF_INET: + default: + { + struct sockaddr_in* http_sin = (struct sockaddr_in*)http_sa; + memset(ip, 0, INET_ADDRSTRLEN+1); + if (inet_ntop(AF_INET, (void *) &http_sin->sin_addr, ip, INET_ADDRSTRLEN)) { + wsgi_req->len += proto_base_add_uwsgi_var(wsgi_req, "REMOTE_ADDR", 11, ip, strlen(ip)); + } + else { + uwsgi_error("inet_ntop()"); + return -1; + } + } + break; } } |