Import (slightly modified) ru.koi8-r.win.kbd:1.1 from FreeBSD (fjoe):
[dragonfly.git] / contrib / ntpd / config.c
1 /*      $OpenBSD: src/usr.sbin/ntpd/config.c,v 1.14 2004/08/30 12:02:59 henning Exp $ */
2
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  *
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.
9  *
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.
17  */
18
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
25
26 #include <errno.h>
27 #include <resolv.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "ntpd.h"
33
34 struct ntp_addr *host_v4(const char *);
35 struct ntp_addr *host_v6(const char *);
36
37 static u_int32_t                 maxid = 0;
38
39 int
40 host(const char *s, struct ntp_addr **hn)
41 {
42         struct ntp_addr *h = NULL;
43
44         if (!strcmp(s, "*"))
45                 if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
46                         fatal(NULL);
47
48         /* IPv4 address? */
49         if (h == NULL)
50                 h = host_v4(s);
51
52         /* IPv6 address? */
53         if (h == NULL)
54                 h = host_v6(s);
55
56         *hn = h;
57         if (h == NULL)
58                 return (0);
59         return (1);
60 }
61
62 struct ntp_addr *
63 host_v4(const char *s)
64 {
65         struct in_addr           ina;
66         struct sockaddr_in      *sa_in;
67         struct ntp_addr         *h;
68
69         bzero(&ina, sizeof(struct in_addr));
70         if (inet_pton(AF_INET, s, &ina) != 1)
71                 return (NULL);
72
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;
79
80         return (h);
81 }
82
83 struct ntp_addr *
84 host_v6(const char *s)
85 {
86         struct addrinfo          hints, *res;
87         struct sockaddr_in6     *sa_in6;
88         struct ntp_addr         *h = NULL;
89
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;
105
106                 freeaddrinfo(res);
107         }
108
109         return (h);
110 }
111
112 int
113 host_dns(const char *s, struct ntp_addr **hn)
114 {
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;
120
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);
126         if (error) {
127                 log_warnx("could not parse \"%s\": %s", s,
128                     gai_strerror(error));
129                 if (error == EAI_AGAIN || error == EAI_NODATA ||
130                     error == EAI_NONAME)
131                         return (0);
132                 else
133                         return (-1);
134         }
135
136         for (res = res0; res; res = res->ai_next) {
137                 if (res->ai_family != AF_INET &&
138                     res->ai_family != AF_INET6)
139                         continue;
140                 if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
141                         fatal(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;
148                 } else {
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));
153                 }
154
155                 h->next = hh;
156                 hh = h;
157                 cnt++;
158         }
159         freeaddrinfo(res0);
160
161         *hn = hh;
162         return (cnt);
163 }
164
165 struct ntp_peer *
166 new_peer(void)
167 {
168         struct ntp_peer *p;
169
170         if ((p = calloc(1, sizeof(struct ntp_peer))) == NULL)
171                 fatal("conf_main server calloc");
172         p->id = ++maxid;
173
174         return (p);
175 }