Commit ba1c6a69 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Fix protocol family selection logic to also work on

a FreeBSD machine with now IPv6.

Remember to also free the addrinfo in case of success.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@796 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent a1b8a5bf
...@@ -69,34 +69,38 @@ accept_filter(int fd) ...@@ -69,34 +69,38 @@ accept_filter(int fd)
} }
#endif #endif
int static int
open_tcp(const char *port) try_sock(int family, const char *port, struct addrinfo **resp)
{ {
struct addrinfo hints, *res; struct addrinfo hints, *res;
int ret, sd, val; int ret, sd;
memset(&hints, 0, sizeof hints); memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET6; hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
if ((ret = getaddrinfo(NULL, port, &hints, &res)) != 0) { ret = getaddrinfo(NULL, port, &hints, &res);
fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(ret)); if (ret != 0)
return (-1); return (-1);
}
sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sd < 0 && errno == EPROTONOSUPPORT) { if (sd < 0)
freeaddrinfo(res); freeaddrinfo(res);
hints.ai_family = AF_INET; else
if ((ret = getaddrinfo(NULL, port, &hints, &res)) != 0) { *resp = res;
fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(ret)); return (sd);
return (-1); }
}
sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 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) { if (sd < 0) {
perror("socket()"); fprintf(stderr, "Failed to get listening socket\n");
freeaddrinfo(res);
return (-1); return (-1);
} }
val = 1; val = 1;
...@@ -129,6 +133,7 @@ open_tcp(const char *port) ...@@ -129,6 +133,7 @@ open_tcp(const char *port)
#ifdef HAVE_ACCEPT_FILTERS #ifdef HAVE_ACCEPT_FILTERS
accept_filter(sd); accept_filter(sd);
#endif #endif
freeaddrinfo(res);
heritage.socket = sd; heritage.socket = sd;
return (0); return (0);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment