1 /* $OpenBSD: config.c,v 1.14 2004/08/30 12:02:59 henning Exp $ */
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>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
34 struct ntp_addr *host_v4(const char *);
35 struct ntp_addr *host_v6(const char *);
37 static u_int32_t maxid = 0;
40 host(const char *s, struct ntp_addr **hn)
42 struct ntp_addr *h = NULL;
45 if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
63 host_v4(const char *s)
66 struct sockaddr_in *sa_in;
69 bzero(&ina, sizeof(struct in_addr));
70 if (inet_pton(AF_INET, s, &ina) != 1)
73 if ((h = calloc(1, sizeof(struct ntp_addr))) == 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;
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)
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;
113 host_dns(const char *s, struct ntp_addr **hn)
115 struct addrinfo hints, *res0, *res;
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 res_init(); /* XXX */
125 error = getaddrinfo(s, NULL, &hints, &res0);
127 log_warnx("could not parse \"%s\": %s", s,
128 gai_strerror(error));
129 if (error == EAI_AGAIN || error == EAI_NODATA ||
136 for (res = res0; res && cnt < MAX_SERVERS_DNS; res = res->ai_next) {
137 if (res->ai_family != AF_INET &&
138 res->ai_family != AF_INET6)
140 if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
142 h->ss.ss_family = res->ai_family;
143 if (res->ai_family == AF_INET) {
144 sa_in = (struct sockaddr_in *)&h->ss;
145 sa_in->sin_len = sizeof(struct sockaddr_in);
146 sa_in->sin_addr.s_addr = ((struct sockaddr_in *)
147 res->ai_addr)->sin_addr.s_addr;
149 sa_in6 = (struct sockaddr_in6 *)&h->ss;
150 sa_in6->sin6_len = sizeof(struct sockaddr_in6);
151 memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *)
152 res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
170 if ((p = calloc(1, sizeof(struct ntp_peer))) == NULL)
171 fatal("conf_main server calloc");