|
|
@ -1,4 +1,4 @@ |
|
|
|
/* $OpenBSD: server.c,v 1.2 2004/06/18 04:51:31 henning Exp $ */ |
|
|
|
/* $OpenBSD: server.c,v 1.3 2004/06/29 18:34:00 alexander Exp $ */ |
|
|
|
|
|
|
|
/* |
|
|
|
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> |
|
|
@ -16,8 +16,10 @@ |
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
|
|
*/ |
|
|
|
|
|
|
|
#include <sys/param.h> |
|
|
|
#include <sys/types.h> |
|
|
|
#include <sys/socket.h> |
|
|
|
#include <errno.h> |
|
|
|
#include <ifaddrs.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <string.h> |
|
|
|
#include <unistd.h> |
|
|
@ -28,46 +30,67 @@ |
|
|
|
int |
|
|
|
setup_listeners(struct servent *se, struct ntpd_conf *conf, u_int *cnt) |
|
|
|
{ |
|
|
|
char ntopbuf[INET6_ADDRSTRLEN]; |
|
|
|
struct listen_addr *la; |
|
|
|
struct ifaddrs *ifap; |
|
|
|
struct sockaddr *sap; |
|
|
|
u_int new_cnt = 0; |
|
|
|
|
|
|
|
if (TAILQ_EMPTY(&conf->listen_addrs)) { |
|
|
|
if ((la = calloc(1, sizeof(struct listen_addr))) == NULL) |
|
|
|
fatal("setup_listeners calloc"); |
|
|
|
la->sa.ss_len = sizeof(struct sockaddr_in); |
|
|
|
((struct sockaddr_in *)&la->sa)->sin_family = AF_INET; |
|
|
|
((struct sockaddr_in *)&la->sa)->sin_addr.s_addr = |
|
|
|
htonl(INADDR_ANY); |
|
|
|
((struct sockaddr_in *)&la->sa)->sin_port = se->s_port; |
|
|
|
TAILQ_INSERT_TAIL(&conf->listen_addrs, la, entry); |
|
|
|
|
|
|
|
if ((la = calloc(1, sizeof(struct listen_addr))) == NULL) |
|
|
|
fatal("setup_listeners calloc"); |
|
|
|
la->sa.ss_len = sizeof(struct sockaddr_in6); |
|
|
|
((struct sockaddr_in6 *)&la->sa)->sin6_family = AF_INET6; |
|
|
|
((struct sockaddr_in6 *)&la->sa)->sin6_port = se->s_port; |
|
|
|
TAILQ_INSERT_TAIL(&conf->listen_addrs, la, entry); |
|
|
|
if (getifaddrs(&ifap) == -1) |
|
|
|
fatal("getifaddrs"); |
|
|
|
|
|
|
|
for (; ifap != NULL; ifap = ifap->ifa_next) { |
|
|
|
sap = ifap->ifa_addr; |
|
|
|
|
|
|
|
if (sap->sa_family != AF_INET && |
|
|
|
sap->sa_family != AF_INET6) |
|
|
|
continue; |
|
|
|
|
|
|
|
if ((la = calloc(1, sizeof(struct listen_addr))) == |
|
|
|
NULL) |
|
|
|
fatal("setup_listeners calloc"); |
|
|
|
|
|
|
|
memcpy(&la->sa, sap, SA_LEN(sap)); |
|
|
|
TAILQ_INSERT_TAIL(&conf->listen_addrs, la, entry); |
|
|
|
} |
|
|
|
|
|
|
|
freeifaddrs(ifap); |
|
|
|
} |
|
|
|
|
|
|
|
TAILQ_FOREACH(la, &conf->listen_addrs, entry) { |
|
|
|
sap = (struct sockaddr *)&la->sa; |
|
|
|
new_cnt++; |
|
|
|
|
|
|
|
switch (la->sa.ss_family) { |
|
|
|
case AF_INET: |
|
|
|
if (((struct sockaddr_in *)&la->sa)->sin_port == 0) |
|
|
|
((struct sockaddr_in *)&la->sa)->sin_port = |
|
|
|
|
|
|
|
if (((struct sockaddr_in *)sap)->sin_port == 0) |
|
|
|
((struct sockaddr_in *)sap)->sin_port = |
|
|
|
se->s_port; |
|
|
|
|
|
|
|
inet_ntop(AF_INET, |
|
|
|
&((struct sockaddr_in *)sap)->sin_addr, |
|
|
|
ntopbuf, sizeof(ntopbuf)); |
|
|
|
|
|
|
|
break; |
|
|
|
case AF_INET6: |
|
|
|
if (((struct sockaddr_in6 *)&la->sa)->sin6_port == 0) |
|
|
|
((struct sockaddr_in6 *)&la->sa)->sin6_port = |
|
|
|
|
|
|
|
if (((struct sockaddr_in6 *)sap)->sin6_port == 0) |
|
|
|
((struct sockaddr_in6 *)sap)->sin6_port = |
|
|
|
se->s_port; |
|
|
|
|
|
|
|
inet_ntop(AF_INET6, |
|
|
|
&((struct sockaddr_in6 *)sap)->sin6_addr, |
|
|
|
ntopbuf, sizeof(ntopbuf)); |
|
|
|
break; |
|
|
|
default: |
|
|
|
fatalx("king bula sez: af borked"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
log_debug("adding listener on %s", ntopbuf); |
|
|
|
|
|
|
|
if ((la->fd = socket(la->sa.ss_family, SOCK_DGRAM, 0)) == -1) |
|
|
|
fatal("socket"); |
|
|
|
|
|
|
|