Add READMEs.
[dragonfly.git] / contrib / ntpd / config.c
blob4dc6df12b33a1edf40fba1e77c560783b139ca50
1 /* $OpenBSD: src/usr.sbin/ntpd/config.c,v 1.17 2005/03/09 20:31:11 henning Exp $ */
3 /*
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
26 #include <errno.h>
27 #include <resolv.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "ntpd.h"
34 struct ntp_addr *host_v4(const char *);
35 struct ntp_addr *host_v6(const char *);
37 static u_int32_t maxid = 0;
39 int
40 host(const char *s, struct ntp_addr **hn)
42 struct ntp_addr *h = NULL;
44 if (!strcmp(s, "*"))
45 if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
46 fatal(NULL);
48 /* IPv4 address? */
49 if (h == NULL)
50 h = host_v4(s);
52 /* IPv6 address? */
53 if (h == NULL)
54 h = host_v6(s);
56 *hn = h;
57 if (h == NULL)
58 return (0);
59 return (1);
62 struct ntp_addr *
63 host_v4(const char *s)
65 struct in_addr ina;
66 struct sockaddr_in *sa_in;
67 struct ntp_addr *h;
69 bzero(&ina, sizeof(struct in_addr));
70 if (inet_pton(AF_INET, s, &ina) != 1)
71 return (NULL);
73 if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
74 fatal(NULL);
75 sa_in = (struct sockaddr_in *)&h->ss;
76 sa_in->sin_len = sizeof(struct sockaddr_in);
77 sa_in->sin_family = AF_INET;
78 sa_in->sin_addr.s_addr = ina.s_addr;
80 return (h);
83 struct ntp_addr *
84 host_v6(const char *s)
86 struct addrinfo hints, *res;
87 struct sockaddr_in6 *sa_in6;
88 struct ntp_addr *h = NULL;
90 bzero(&hints, sizeof(hints));
91 hints.ai_family = AF_INET6;
92 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
93 hints.ai_flags = AI_NUMERICHOST;
94 if (getaddrinfo(s, "0", &hints, &res) == 0) {
95 if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
96 fatal(NULL);
97 sa_in6 = (struct sockaddr_in6 *)&h->ss;
98 sa_in6->sin6_len = sizeof(struct sockaddr_in6);
99 sa_in6->sin6_family = AF_INET6;
100 memcpy(&sa_in6->sin6_addr,
101 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
102 sizeof(sa_in6->sin6_addr));
103 sa_in6->sin6_scope_id =
104 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
106 freeaddrinfo(res);
109 return (h);
113 host_dns(const char *s, struct ntp_addr **hn)
115 struct addrinfo hints, *res0, *res;
116 int error, cnt = 0;
117 struct sockaddr_in *sa_in;
118 struct sockaddr_in6 *sa_in6;
119 struct ntp_addr *h, *hh = NULL;
121 bzero(&hints, sizeof(hints));
122 hints.ai_family = PF_UNSPEC;
123 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
124 error = getaddrinfo(s, NULL, &hints, &res0);
125 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
126 return (0);
127 if (error) {
128 log_warnx("could not parse \"%s\": %s", s,
129 gai_strerror(error));
130 return (-1);
133 for (res = res0; res && cnt < MAX_SERVERS_DNS; res = res->ai_next) {
134 if (res->ai_family != AF_INET &&
135 res->ai_family != AF_INET6)
136 continue;
137 if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
138 fatal(NULL);
139 h->ss.ss_family = res->ai_family;
140 if (res->ai_family == AF_INET) {
141 sa_in = (struct sockaddr_in *)&h->ss;
142 sa_in->sin_len = sizeof(struct sockaddr_in);
143 sa_in->sin_addr.s_addr = ((struct sockaddr_in *)
144 res->ai_addr)->sin_addr.s_addr;
145 } else {
146 sa_in6 = (struct sockaddr_in6 *)&h->ss;
147 sa_in6->sin6_len = sizeof(struct sockaddr_in6);
148 memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *)
149 res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
152 h->next = hh;
153 hh = h;
154 cnt++;
156 freeaddrinfo(res0);
158 *hn = hh;
159 return (cnt);
162 struct ntp_peer *
163 new_peer(void)
165 struct ntp_peer *p;
167 if ((p = calloc(1, sizeof(struct ntp_peer))) == NULL)
168 fatal("conf_main server calloc");
169 p->id = ++maxid;
171 return (p);