From bbe549999cf14e8d04c110955f79a8652c755add Mon Sep 17 00:00:00 2001 From: henning <> Date: Tue, 10 Aug 2004 12:41:15 +0000 Subject: [PATCH] move memory allocation for new peers into a new function, makes ID allocation easier --- src/usr.sbin/ntpd/config.c | 24 +++++++++++++++++++++--- src/usr.sbin/ntpd/ntpd.h | 7 ++++--- src/usr.sbin/ntpd/parse.y | 13 +++---------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/usr.sbin/ntpd/config.c b/src/usr.sbin/ntpd/config.c index b3a0dba1..140052de 100644 --- a/src/usr.sbin/ntpd/config.c +++ b/src/usr.sbin/ntpd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.9 2004/07/28 16:38:43 henning Exp $ */ +/* $OpenBSD: config.c,v 1.10 2004/08/10 12:41:15 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -20,7 +20,11 @@ #include #include +#include +#include + #include +#include #include #include #include @@ -30,6 +34,8 @@ struct ntp_addr *host_v4(const char *); struct ntp_addr *host_v6(const char *); +static u_int32_t maxid = 0; + int host(const char *s, struct ntp_addr **hn) { @@ -115,9 +121,9 @@ host_dns(const char *s, struct ntp_addr **hn) struct sockaddr_in6 *sa_in6; struct ntp_addr *h, *hh = NULL; - memset(&hints, 0, sizeof(hints)); + bzero(&hints, sizeof(hints)); hints.ai_family = PF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; /* DUMMY */ + hints.ai_socktype = SOCK_DGRAM; /* DUMMY */ error = getaddrinfo(s, NULL, &hints, &res0); if (error) { log_warnx("could not parse \"%s\": %s", s, @@ -156,3 +162,15 @@ host_dns(const char *s, struct ntp_addr **hn) *hn = hh; return (cnt); } + +struct ntp_peer * +new_peer(void) +{ + struct ntp_peer *p; + + if ((p = calloc(1, sizeof(struct ntp_peer))) == NULL) + fatal("conf_main server calloc"); + p->id = ++maxid; + + return (p); +} diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index d093ca5d..db3efbc8 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.31 2004/07/29 11:01:48 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.32 2004/08/10 12:41:15 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -207,8 +207,9 @@ int parse_config(char *, struct ntpd_conf *); int cmdline_symset(char *); /* config.c */ -int host(const char *, struct ntp_addr **); -int host_dns(const char *, struct ntp_addr **); +int host(const char *, struct ntp_addr **); +int host_dns(const char *, struct ntp_addr **); +struct ntp_peer *new_peer(void); /* ntp_msg.c */ int ntp_getmsg(char *, ssize_t, struct ntp_msg *); diff --git a/src/usr.sbin/ntpd/parse.y b/src/usr.sbin/ntpd/parse.y index 508df8d0..00a51546 100644 --- a/src/usr.sbin/ntpd/parse.y +++ b/src/usr.sbin/ntpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.17 2004/07/29 11:01:48 henning Exp $ */ +/* $OpenBSD: parse.y,v 1.18 2004/08/10 12:41:15 henning Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -41,7 +41,6 @@ static int lineno = 1; static int errors = 0; static int pdebug = 1; char *infile; -static u_int32_t maxid; int yyerror(const char *, ...); int yyparse(void); @@ -169,10 +168,7 @@ conf_main : LISTEN ON address { free($2); YYERROR; } - p = calloc(1, sizeof(struct ntp_peer)); - if (p == NULL) - fatal("conf_main server calloc"); - p->id = ++maxid; + p = new_peer(); h->next = NULL; p->addr = h; p->addr_head.a = h; @@ -189,9 +185,7 @@ conf_main : LISTEN ON address { struct ntp_peer *p; struct ntp_addr *h, *next; - if ((p = calloc(1, sizeof(struct ntp_peer))) == NULL) - fatal("conf_main server calloc"); - p->id = ++maxid; + p = new_peer(); for (h = $2->a; h != NULL; h = next) { next = h->next; if (h->ss.ss_family != AF_INET && @@ -481,7 +475,6 @@ parse_config(char *filename, struct ntpd_conf *xconf) conf = xconf; lineno = 1; errors = 0; - maxid = 0; TAILQ_INIT(&conf->listen_addrs); TAILQ_INIT(&conf->ntp_peers);