Commit 62ee7028 authored by Max Kellermann's avatar Max Kellermann

listen: pass protocol family to establishListen()

The caller already knows the protocol family, and we can eliminate the complicated switch statement in establishListen() if we just pass this information. This seems more robust.
parent ba594cfe
...@@ -69,32 +69,12 @@ static void redirect_stdin(void) ...@@ -69,32 +69,12 @@ static void redirect_stdin(void)
FATAL("dup2 stdin: %s\n", strerror(errno)); FATAL("dup2 stdin: %s\n", strerror(errno));
} }
static int establishListen(const struct sockaddr *addrp, socklen_t addrlen) static int establishListen(int pf, const struct sockaddr *addrp,
socklen_t addrlen)
{ {
int pf;
int sock; int sock;
int allowReuse = ALLOW_REUSE; int allowReuse = ALLOW_REUSE;
switch (addrp->sa_family) {
#ifdef HAVE_TCP
case AF_INET:
pf = PF_INET;
break;
#ifdef HAVE_IPV6
case AF_INET6:
pf = PF_INET6;
break;
#endif
#endif /* HAVE_TCP */
#ifdef HAVE_UN
case AF_UNIX:
pf = PF_UNIX;
break;
#endif /* HAVE_UN */
default:
FATAL("unknown address family: %i\n", addrp->sa_family);
}
if ((sock = socket(pf, SOCK_STREAM, 0)) < 0) if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
FATAL("socket < 0\n"); FATAL("socket < 0\n");
...@@ -152,7 +132,7 @@ static void parseListenConfigParam(unsigned int port, ConfigParam * param) ...@@ -152,7 +132,7 @@ static void parseListenConfigParam(unsigned int port, ConfigParam * param)
sin6.sin6_addr = in6addr_any; sin6.sin6_addr = in6addr_any;
addrp = (const struct sockaddr *)&sin6; addrp = (const struct sockaddr *)&sin6;
addrlen = sizeof(struct sockaddr_in6); addrlen = sizeof(struct sockaddr_in6);
if (establishListen(addrp, addrlen) < 0) if (establishListen(PF_INET6, addrp, addrlen) < 0)
BINDERROR(); BINDERROR();
} }
#endif #endif
...@@ -160,9 +140,9 @@ static void parseListenConfigParam(unsigned int port, ConfigParam * param) ...@@ -160,9 +140,9 @@ static void parseListenConfigParam(unsigned int port, ConfigParam * param)
addrp = (const struct sockaddr *)&sin4; addrp = (const struct sockaddr *)&sin4;
addrlen = sizeof(struct sockaddr_in); addrlen = sizeof(struct sockaddr_in);
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
if ((establishListen(addrp, addrlen) < 0) && !useIpv6) { if ((establishListen(PF_INET, addrp, addrlen) < 0) && !useIpv6) {
#else #else
if (establishListen(addrp, addrlen) < 0) { if (establishListen(PF_INET, addrp, addrlen) < 0) {
#endif #endif
BINDERROR(); BINDERROR();
} }
...@@ -186,7 +166,7 @@ static void parseListenConfigParam(unsigned int port, ConfigParam * param) ...@@ -186,7 +166,7 @@ static void parseListenConfigParam(unsigned int port, ConfigParam * param)
addrp = (const struct sockaddr *)&sun; addrp = (const struct sockaddr *)&sun;
addrlen = sizeof(sun); addrlen = sizeof(sun);
if (establishListen(addrp, addrlen) < 0) if (establishListen(PF_UNIX, addrp, addrlen) < 0)
FATAL("unable to bind to %s: %s\n", FATAL("unable to bind to %s: %s\n",
param->value, strerror(errno)); param->value, strerror(errno));
#endif /* HAVE_UN */ #endif /* HAVE_UN */
...@@ -212,7 +192,8 @@ static void parseListenConfigParam(unsigned int port, ConfigParam * param) ...@@ -212,7 +192,8 @@ static void parseListenConfigParam(unsigned int port, ConfigParam * param)
param->value, param->line, gai_strerror(ret)); param->value, param->line, gai_strerror(ret));
for (i = ai; i != NULL; i = i->ai_next) for (i = ai; i != NULL; i = i->ai_next)
if (establishListen(i->ai_addr, i->ai_addrlen) < 0) if (establishListen(i->ai_family, i->ai_addr,
i->ai_addrlen) < 0)
BINDERROR(); BINDERROR();
freeaddrinfo(ai); freeaddrinfo(ai);
......
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