summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorAlexander Guy <alexander@cvs.openbsd.org>2004-06-29 18:34:01 +0000
committerAlexander Guy <alexander@cvs.openbsd.org>2004-06-29 18:34:01 +0000
commit9e5b66fbd884908f741c62f2591ed2eb2725d3f2 (patch)
tree8875b3de6f66ee87bd8ea1b2dc0dd528f89a01ba /usr.sbin
parent744fae1bc742aef3083f4a9599be4ef2e241964e (diff)
When no listener is specified, create a listener on every IP. Problem
reported by Peter Hessler. ok henning@
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/ntpd/server.c65
1 files changed, 44 insertions, 21 deletions
diff --git a/usr.sbin/ntpd/server.c b/usr.sbin/ntpd/server.c
index f7faba55de1..2014a5119d3 100644
--- a/usr.sbin/ntpd/server.c
+++ b/usr.sbin/ntpd/server.c
@@ -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");