httpd: fix address family for reverse proxy client socket
authorLaurent Bercot <ska-dietlibc@skarnet.org>
Fri, 6 Jan 2017 21:03:08 +0000 (22:03 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 8 Jan 2017 14:14:38 +0000 (15:14 +0100)
When httpd proxies a request to another server, it first creates
an AF_INET socket, then resolves the server name to a sockaddr,
then connects to it. This fails if the server name resolves to
an IPv6 address.

This patch ensures that the socket is created with the correct
address family (AF_INET6 if the server resolves to an IPv6 address
and AF_INET otherwise).

Signed-off-by: Laurent Bercot <ska-dietlibc@skarnet.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
networking/httpd.c

index d301d598dcfbe9ab878377c780191301373dcba1..cfcd2a06ec4c771c6706ceb291fd8c82f27cd80e 100644 (file)
@@ -2396,12 +2396,12 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
                int proxy_fd;
                len_and_sockaddr *lsa;
 
-               proxy_fd = socket(AF_INET, SOCK_STREAM, 0);
-               if (proxy_fd < 0)
-                       send_headers_and_exit(HTTP_INTERNAL_SERVER_ERROR);
                lsa = host2sockaddr(proxy_entry->host_port, 80);
                if (lsa == NULL)
                        send_headers_and_exit(HTTP_INTERNAL_SERVER_ERROR);
+               proxy_fd = socket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
+               if (proxy_fd < 0)
+                       send_headers_and_exit(HTTP_INTERNAL_SERVER_ERROR);
                if (connect(proxy_fd, &lsa->u.sa, lsa->len) < 0)
                        send_headers_and_exit(HTTP_INTERNAL_SERVER_ERROR);
                fdprintf(proxy_fd, "%s %s%s%s%s HTTP/%c.%c\r\n",