1 /* $OpenBSD: src/usr.sbin/ntpd/config.c,v 1.17 2005/03/09 20:31:11 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 error
= getaddrinfo(s
, NULL
, &hints
, &res0
);
125 if (error
== EAI_AGAIN
|| error
== EAI_NODATA
|| error
== EAI_NONAME
)
128 log_warnx("could not parse \"%s\": %s", s
,
129 gai_strerror(error
));
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
)
137 if ((h
= calloc(1, sizeof(struct ntp_addr
))) == 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
;
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
));
167 if ((p
= calloc(1, sizeof(struct ntp_peer
))) == NULL
)
168 fatal("conf_main server calloc");