]> err.no Git - varnish/commitdiff
Fix protocol family selection logic to also work on
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Fri, 11 Aug 2006 07:35:09 +0000 (07:35 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Fri, 11 Aug 2006 07:35:09 +0000 (07:35 +0000)
a FreeBSD machine with now IPv6.

Remember to also free the addrinfo in case of success.

git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@796 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/tcp.c

index 2b644ef4a25b12a8301f71f0d8608e613a0c717b..29fc2c3f4a751339b519ccc141212271a1f5f732 100644 (file)
@@ -69,34 +69,38 @@ accept_filter(int fd)
 }
 #endif
 
-int
-open_tcp(const char *port)
+static int
+try_sock(int family, const char *port, struct addrinfo **resp)
 {
        struct addrinfo hints, *res;
-       int ret, sd, val;
+       int ret, sd;
 
        memset(&hints, 0, sizeof hints);
-       hints.ai_family = AF_INET6;
+       hints.ai_family = family;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_PASSIVE;
-       if ((ret = getaddrinfo(NULL, port, &hints, &res)) != 0) {
-               fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(ret));
+       ret = getaddrinfo(NULL, port, &hints, &res);
+       if (ret != 0)
                return (-1);
-       }
-
        sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
-       if (sd < 0 && errno == EPROTONOSUPPORT) {
+       if (sd < 0)
                freeaddrinfo(res);
-               hints.ai_family = AF_INET;
-               if ((ret = getaddrinfo(NULL, port, &hints, &res)) != 0) {
-                       fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(ret));
-                       return (-1);
-               }
-               sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
-       }
+       else
+               *resp = res;
+       return (sd);
+}
+
+int
+open_tcp(const char *port)
+{
+       int sd, val;
+       struct addrinfo *res;
+
+       sd = try_sock(AF_INET6, port, &res);
+       if (sd < 0)
+               sd = try_sock(AF_INET, port, &res);
        if (sd < 0) {
-               perror("socket()");
-               freeaddrinfo(res);
+               fprintf(stderr, "Failed to get listening socket\n");
                return (-1);
        }
        val = 1;
@@ -129,6 +133,7 @@ open_tcp(const char *port)
 #ifdef HAVE_ACCEPT_FILTERS
        accept_filter(sd);
 #endif
+       freeaddrinfo(res);
        heritage.socket = sd;
        return (0);
 }