From 05a94ce7791b8154378e80166e6bb49d540fee29 Mon Sep 17 00:00:00 2001 From: henning <> Date: Wed, 7 Jul 2004 03:15:37 +0000 Subject: [PATCH] swicth all the host_* functions to work on a newly invented struct ntp_addr, which just wraps a sockaddr_storage and a next pointer, so that host_dns can return more than one entry. let host_dns do exactly that, return a list of all IPs for that hostname adjust all callers in the grammar to cope with that --- src/usr.sbin/ntpd/config.c | 73 +++++++++++++++++++++----------------- src/usr.sbin/ntpd/ntpd.h | 11 ++++-- src/usr.sbin/ntpd/parse.y | 68 +++++++++++++++++++++-------------- 3 files changed, 90 insertions(+), 62 deletions(-) diff --git a/src/usr.sbin/ntpd/config.c b/src/usr.sbin/ntpd/config.c index 1e1f92c4..106a5302 100644 --- a/src/usr.sbin/ntpd/config.c +++ b/src/usr.sbin/ntpd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.3 2004/07/06 18:03:07 henning Exp $ */ +/* $OpenBSD: config.c,v 1.4 2004/07/07 03:15:37 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -27,9 +27,9 @@ #include "ntpd.h" -int host_v4(const char *, struct sockaddr *, u_int8_t *); -int host_v6(const char *, struct sockaddr *); -int host_dns(const char *, struct sockaddr *, u_int8_t *); +struct ntp_addr *host_v4(const char *, u_int8_t *); +struct ntp_addr *host_v6(const char *); +struct ntp_addr *host_dns(const char *, u_int8_t *); int check_file_secrecy(int fd, const char *fname) @@ -54,12 +54,12 @@ check_file_secrecy(int fd, const char *fname) return (0); } -int -host(const char *s, struct sockaddr *sa, u_int8_t *len) +struct ntp_addr * +host(const char *s, u_int8_t *len) { - int done = 0; int mask; char *p, *q, *ps; + struct ntp_addr *h = NULL; if ((p = strrchr(s, '/')) != NULL) { errno = 0; @@ -78,29 +78,30 @@ host(const char *s, struct sockaddr *sa, u_int8_t *len) } /* IPv4 address? */ - if (!done) - done = host_v4(s, sa, len); + if (h == NULL) + h = host_v4(s, len); /* IPv6 address? */ - if (!done) { - done = host_v6(ps, sa); + if (h == NULL) { + h = host_v6(ps); *len = mask; } /* Hostname? */ - if (!done) - done = host_dns(ps, sa, len); + if (h == NULL) + h = host_dns(ps, len); free(ps); - return (done); + return (h); } -int -host_v4(const char *s, struct sockaddr *sa, u_int8_t *len) +struct ntp_addr * +host_v4(const char *s, u_int8_t *len) { struct in_addr ina; struct sockaddr_in *sa_in; + struct ntp_addr *h; int bits = 32; bzero(&ina, sizeof(struct in_addr)); @@ -112,28 +113,33 @@ host_v4(const char *s, struct sockaddr *sa, u_int8_t *len) return (0); } - sa_in = (struct sockaddr_in *)sa; + if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL) + fatal(NULL); + sa_in = (struct sockaddr_in *)&h->ss; sa_in->sin_len = sizeof(struct sockaddr_in); sa_in->sin_family = AF_INET; sa_in->sin_addr.s_addr = ina.s_addr; *len = bits; - return (1); + return (h); } -int -host_v6(const char *s, struct sockaddr *sa) +struct ntp_addr * +host_v6(const char *s) { struct addrinfo hints, *res; struct sockaddr_in6 *sa_in6; + struct ntp_addr *h = NULL; - sa_in6 = (struct sockaddr_in6 *)sa; - sa_in6->sin6_len = sizeof(struct sockaddr_in6); bzero(&hints, sizeof(hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_DGRAM; /*dummy*/ hints.ai_flags = AI_NUMERICHOST; if (getaddrinfo(s, "0", &hints, &res) == 0) { + if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL) + fatal(NULL); + sa_in6 = (struct sockaddr_in6 *)&h->ss; + sa_in6->sin6_len = sizeof(struct sockaddr_in6); sa_in6->sin6_family = AF_INET6; memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, @@ -142,20 +148,19 @@ host_v6(const char *s, struct sockaddr *sa) ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; freeaddrinfo(res); - return (1); } - return (0); + return (h); } -int -host_dns(const char *s, struct sockaddr *sa, u_int8_t *len) +struct ntp_addr * +host_dns(const char *s, u_int8_t *len) { struct addrinfo hints, *res0, *res; int error; - int got = 0; struct sockaddr_in *sa_in; struct sockaddr_in6 *sa_in6; + struct ntp_addr *h, *hh = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; @@ -168,25 +173,27 @@ host_dns(const char *s, struct sockaddr *sa, u_int8_t *len) if (res->ai_family != AF_INET && res->ai_family != AF_INET6) continue; - sa->sa_family = res->ai_family; + if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL) + fatal(NULL); + h->ss.ss_family = res->ai_family; if (res->ai_family == AF_INET) { - sa_in = (struct sockaddr_in *)sa; + sa_in = (struct sockaddr_in *)&h->ss; sa_in->sin_len = sizeof(struct sockaddr_in); sa_in->sin_addr.s_addr = ((struct sockaddr_in *) res->ai_addr)->sin_addr.s_addr; *len = 32; } else { - sa_in6 = (struct sockaddr_in6 *)sa; + sa_in6 = (struct sockaddr_in6 *)&h->ss; sa_in6->sin6_len = sizeof(struct sockaddr_in6); memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(struct in6_addr)); *len = 128; } - got++; - break; /* XXX should expand to all results... */ + h->next = hh; + hh = h; } freeaddrinfo(res0); - return (got); + return (hh); } diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index 48b6be87..bef5936a 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.14 2004/07/07 01:01:27 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.15 2004/07/07 03:15:37 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -61,6 +61,11 @@ struct listen_addr { int fd; }; +struct ntp_addr { + struct ntp_addr *next; + struct sockaddr_storage ss; +}; + struct ntp_offset { u_int8_t good; double offset; @@ -177,8 +182,8 @@ int parse_config(char *, struct ntpd_conf *); int cmdline_symset(char *); /* config.c */ -int check_file_secrecy(int, const char *); -int host(const char *, struct sockaddr *, u_int8_t *); +int check_file_secrecy(int, const char *); +struct ntp_addr *host(const char *, u_int8_t *); /* ntp_msg.c */ void get_ts(struct l_fixedpt *); diff --git a/src/usr.sbin/ntpd/parse.y b/src/usr.sbin/ntpd/parse.y index 6deef0b5..c06d3b35 100644 --- a/src/usr.sbin/ntpd/parse.y +++ b/src/usr.sbin/ntpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.4 2004/07/06 23:08:26 henning Exp $ */ +/* $OpenBSD: parse.y,v 1.5 2004/07/07 03:15:37 henning Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -69,7 +69,7 @@ typedef struct { union { u_int32_t number; char *string; - struct sockaddr_storage ss; + struct ntp_addr *addr; } v; int lineno; } YYSTYPE; @@ -82,7 +82,7 @@ typedef struct { %token STRING %type number %type string -%type address +%type address %% grammar : /* empty */ @@ -126,30 +126,43 @@ varset : STRING '=' string { conf_main : LISTEN ON address { struct listen_addr *la; - - if ((la = calloc(1, sizeof(struct listen_addr))) == - NULL) - fatal("parse conf_main listen on calloc"); - - la->fd = -1; - memcpy(&la->sa, &$3, sizeof(struct sockaddr_storage)); - TAILQ_INSERT_TAIL(&conf->listen_addrs, la, entry); + struct ntp_addr *h, *next; + + for (h = $3; h != NULL; h = next) { + next = h->next; + la = calloc(1, sizeof(struct listen_addr)); + if (la == NULL) + fatal("listen on calloc"); + la->fd = -1; + memcpy(&la->sa, &h->ss, + sizeof(struct sockaddr_storage)); + TAILQ_INSERT_TAIL(&conf->listen_addrs, la, + entry); + free(h); + } } | SERVER address { struct ntp_peer *p; - - if ((p = calloc(1, sizeof(struct ntp_peer))) == NULL) - fatal("parse conf_main server on calloc"); - memcpy(&p->ss, &$2, sizeof(struct sockaddr_storage)); - TAILQ_INSERT_TAIL(&conf->ntp_peers, p, entry); + struct ntp_addr *h, *next; + + for (h = $2; h != NULL; h = next) { + next = h->next; + p = calloc(1, sizeof(struct ntp_peer)); + if (p == NULL) + fatal("conf_main server calloc"); + memcpy(&p->ss, &h->ss, + sizeof(struct sockaddr_storage)); + TAILQ_INSERT_TAIL(&conf->ntp_peers, p, entry); + free(h); + } } ; address : STRING { - u_int8_t len; + u_int8_t len; + struct ntp_addr *h; - bzero(&$$, sizeof($$)); - if (!host($1, (struct sockaddr *)&$$, &len)) { + if (($$ = host($1, &len)) == NULL) { yyerror("could not parse address spec \"%s\"", $1); free($1); @@ -157,13 +170,16 @@ address : STRING { } free($1); - if (($$.ss_family == AF_INET && len != 32) || - ($$.ss_family == AF_INET6 && len != 128)) { - /* unreachable */ - yyerror("got prefixlen %u, expected %u", - len, $$.ss_family == AF_INET ? 32 : 128); - YYERROR; - } + for (h = $$; h != NULL; h = h->next) + if ((h->ss.ss_family == AF_INET && len != 32) || + (h->ss.ss_family == AF_INET6 && len != 128)) + { + /* unreachable */ + yyerror("got prefixlen %u, expected %u", + len, h->ss.ss_family == + AF_INET ? 32 : 128); + YYERROR; + } } ;