Activate accounting for IPv6 hosts.
[darkstat] / dns.c
1 /* darkstat 3
2 * copyright (c) 2001-2008 Emil Mikulic.
3 *
4 * dns.c: synchronous DNS in a child process.
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9
10 #include "darkstat.h"
11 #include "conv.h"
12 #include "decode.h"
13 #include "dns.h"
14 #include "err.h"
15 #include "hosts_db.h"
16 #include "queue.h"
17 #include "tree.h"
18
19 #include <sys/param.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 static void dns_main(void); /* this is what the child process runs */
31
32 #define CHILD 0 /* child process uses this socket */
33 #define PARENT 1
34 static int sock[2];
35 static pid_t pid = -1;
36
37 struct dns_reply {
38 struct addr46 addr;
39 int error; /* for gai_strerror(), or 0 if no error */
40 char name[MAXHOSTNAMELEN];
41 };
42
43 void
44 dns_init(const char *privdrop_user)
45 {
46 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) == -1)
47 err(1, "socketpair");
48
49 pid = fork();
50 if (pid == -1)
51 err(1, "fork");
52
53 if (pid == 0) {
54 /* We are the child. */
55 privdrop(NULL /* don't chroot */, privdrop_user);
56 close(sock[PARENT]);
57 sock[PARENT] = -1;
58 daemonize_finish(); /* drop our copy of the lifeline! */
59 if (signal(SIGUSR1, SIG_IGN) == SIG_ERR)
60 errx(1, "signal(SIGUSR1, ignore) failed");
61 dns_main();
62 verbosef("fell out of dns_main()");
63 exit(0);
64 } else {
65 /* We are the parent. */
66 close(sock[CHILD]);
67 sock[CHILD] = -1;
68 fd_set_nonblock(sock[PARENT]);
69 verbosef("DNS child has PID %d", pid);
70 }
71 }
72
73 void
74 dns_stop(void)
75 {
76 if (pid == -1)
77 return; /* no child was started */
78 close(sock[PARENT]);
79 if (kill(pid, SIGINT) == -1)
80 err(1, "kill");
81 verbosef("dns_stop() waiting for child");
82 if (waitpid(pid, NULL, 0) == -1)
83 err(1, "waitpid");
84 verbosef("dns_stop() done waiting for child");
85 }
86
87 struct tree_rec {
88 RB_ENTRY(tree_rec) ptree;
89 struct addr46 ip;
90 };
91
92 static int
93 tree_cmp(struct tree_rec *a, struct tree_rec *b)
94 {
95 if (a->ip.af != b->ip.af)
96 /* Sort IPv4 to the left of IPv6. */
97 return (a->ip.af == AF_INET ? -1 : +1);
98
99 if (a->ip.af == AF_INET) {
100 return (memcmp(&a->ip.addr.ip, &b->ip.addr.ip, sizeof(a->ip.addr.ip)));
101 }
102
103 /* AF_INET6 should remain. */
104 if (a->ip.af == AF_INET6)
105 return (memcmp(&a->ip.addr.ip6, &b->ip.addr.ip6, sizeof(a->ip.addr.ip6)));
106
107 /* Last resort. */
108 return -1;
109 }
110
111 static RB_HEAD(tree_t, tree_rec) ip_tree = RB_INITIALIZER(&tree_rec);
112 /* Quiet warnings. */
113 static struct tree_rec * tree_t_RB_NEXT(struct tree_rec *elm)
114 _unused_;
115 static struct tree_rec * tree_t_RB_MINMAX(struct tree_t *head, int val)
116 _unused_;
117 RB_GENERATE(tree_t, tree_rec, ptree, tree_cmp)
118
119 void
120 dns_queue(const struct addr46 *const ipaddr)
121 {
122 struct tree_rec *rec;
123 ssize_t num_w;
124
125 if (pid == -1)
126 return; /* no child was started - we're not doing any DNS */
127
128 if (ipaddr->af != AF_INET && ipaddr->af != AF_INET6) {
129 verbosef("dns_queue() for unknown family %d.\n", ipaddr->af);
130 return;
131 }
132
133 rec = xmalloc(sizeof(*rec));
134 memcpy(&rec->ip, ipaddr, sizeof(rec->ip));
135
136 if (RB_INSERT(tree_t, &ip_tree, rec) != NULL) {
137 /* Already queued - this happens seldom enough that we don't care about
138 * the performance hit of needlessly malloc()ing. */
139 verbosef("already queued %s", ip_to_str(ipaddr));
140 free(rec);
141 return;
142 }
143
144 num_w = write(sock[PARENT], ipaddr, sizeof(*ipaddr)); /* won't block */
145 if (num_w == 0)
146 warnx("dns_queue: write: ignoring end of file");
147 else if (num_w == -1)
148 warn("dns_queue: ignoring write error");
149 else if (num_w != sizeof(*ipaddr))
150 err(1, "dns_queue: wrote %z instead of %z", num_w, sizeof(*ipaddr));
151 }
152
153 static void
154 dns_unqueue(const struct addr46 *const ipaddr)
155 {
156 struct tree_rec tmp, *rec;
157
158 memcpy(&tmp.ip, ipaddr, sizeof(tmp.ip));
159 if ((rec = RB_FIND(tree_t, &ip_tree, &tmp)) != NULL) {
160 RB_REMOVE(tree_t, &ip_tree, rec);
161 free(rec);
162 }
163 else
164 verbosef("couldn't unqueue %s - not in queue!", ip_to_str(ipaddr));
165 }
166
167 /*
168 * Returns non-zero if result waiting, stores IP and name into given pointers
169 * (name buffer is allocated by dns_poll)
170 */
171 static int
172 dns_get_result(struct addr46 *ipaddr, char **name)
173 {
174 struct dns_reply reply;
175 ssize_t numread;
176
177 numread = read(sock[PARENT], &reply, sizeof(reply));
178 if (numread == -1) {
179 if (errno == EAGAIN)
180 return (0); /* no input waiting */
181 else
182 goto error;
183 }
184 if (numread == 0)
185 goto error; /* EOF */
186 if (numread != sizeof(reply))
187 errx(1, "dns_get_result read got %z, expected %z", numread, sizeof(reply));
188
189 /* Return successful reply. */
190 memcpy(ipaddr, &reply.addr, sizeof(*ipaddr));
191 #if DARKSTAT_USES_HOSTENT
192 if (reply.error != 0)
193 xasprintf(name, "(%s)", hstrerror(reply.error));
194 else
195 *name = xstrdup(reply.name);
196 #else /* !DARKSTAT_USES_HOSTENT */
197 if (reply.error != 0) {
198 /* Identify common special cases. */
199 char *type = "none";
200
201 if (reply.addr.af == AF_INET6) {
202 if (IN6_IS_ADDR_LINKLOCAL(&reply.addr.addr.ip6))
203 type = "link-local";
204 else if (IN6_IS_ADDR_SITELOCAL(&reply.addr.addr.ip6))
205 type = "site-local";
206 else if (IN6_IS_ADDR_MULTICAST(&reply.addr.addr.ip6))
207 type = "multicast";
208 } else { /* AF_INET */
209 if (IN_MULTICAST(reply.addr.addr.ip.s_addr))
210 type = "multicast";
211 }
212 xasprintf(name, "(%s)", type);
213 }
214 else /* Correctly resolved name. */
215 *name = xstrdup(reply.name);
216 #endif /* !DARKSTAT_USES_HOSTENT */
217
218 dns_unqueue(&reply.addr);
219 return (1);
220
221 error:
222 warn("dns_get_result: ignoring read error");
223 /* FIXME: re-align to stream? restart dns child? */
224 return (0);
225 }
226
227 void
228 dns_poll(void)
229 {
230 struct addr46 ip;
231 char *name;
232
233 if (pid == -1)
234 return; /* no child was started - we're not doing any DNS */
235
236 while (dns_get_result(&ip, &name)) {
237 /* push into hosts_db */
238 struct bucket *b = host_find(&ip);
239
240 if (b == NULL) {
241 verbosef("resolved %s to %s but it's not in the DB!",
242 ip_to_str(&ip), name);
243 return;
244 }
245 if (b->u.host.dns != NULL) {
246 verbosef("resolved %s to %s but it's already in the DB!",
247 ip_to_str(&ip), name);
248 return;
249 }
250 b->u.host.dns = name;
251 }
252 }
253
254 /* ------------------------------------------------------------------------ */
255
256 struct qitem {
257 STAILQ_ENTRY(qitem) entries;
258 struct addr46 ip;
259 };
260
261 STAILQ_HEAD(qhead, qitem) queue = STAILQ_HEAD_INITIALIZER(queue);
262
263 static void
264 enqueue(const struct addr46 *const ip)
265 {
266 struct qitem *i;
267
268 i = xmalloc(sizeof(*i));
269 memcpy(&i->ip, ip, sizeof(i->ip));
270 STAILQ_INSERT_TAIL(&queue, i, entries);
271 verbosef("DNS: enqueued %s", ip_to_str(ip));
272 }
273
274 /* Return non-zero and populate <ip> pointer if queue isn't empty. */
275 static int
276 dequeue(struct addr46 *ip)
277 {
278 struct qitem *i;
279
280 i = STAILQ_FIRST(&queue);
281 if (i == NULL)
282 return (0);
283 STAILQ_REMOVE_HEAD(&queue, entries);
284 memcpy(ip, &i->ip, sizeof(*ip));
285 free(i);
286 verbosef("DNS: dequeued %s", ip_to_str(ip));
287 return 1;
288 }
289
290 static void
291 xwrite(const int d, const void *buf, const size_t nbytes)
292 {
293 ssize_t ret = write(d, buf, nbytes);
294
295 if (ret == -1)
296 err(1, "write");
297 if (ret != (ssize_t)nbytes)
298 err(1, "wrote %d bytes instead of all %d bytes", (int)ret, (int)nbytes);
299 }
300
301 static void
302 dns_main(void)
303 {
304 struct addr46 ip;
305
306 #ifdef HAVE_SETPROCTITLE
307 setproctitle("DNS child");
308 #endif
309 fd_set_nonblock(sock[CHILD]);
310 verbosef("DNS child entering main DNS loop");
311 for (;;) {
312 int blocking;
313
314 if (STAILQ_EMPTY(&queue)) {
315 blocking = 1;
316 fd_set_block(sock[CHILD]);
317 verbosef("entering blocking read loop");
318 } else {
319 blocking = 0;
320 fd_set_nonblock(sock[CHILD]);
321 verbosef("non-blocking poll");
322 }
323 for (;;) {
324 /* While we have input to process... */
325 ssize_t numread = read(sock[CHILD], &ip, sizeof(ip));
326 if (numread == 0)
327 exit(0); /* end of file, nothing more to do here. */
328 if (numread == -1) {
329 if (!blocking && (errno == EAGAIN))
330 break; /* ran out of input */
331 /* else error */
332 err(1, "DNS: read failed");
333 }
334 if (numread != sizeof(ip))
335 err(1, "DNS: read got %z bytes, expecting %z", numread, sizeof(ip));
336 enqueue(&ip);
337 if (blocking) {
338 /* After one blocking read, become non-blocking so that when we
339 * run out of input we fall through to queue processing.
340 */
341 blocking = 0;
342 fd_set_nonblock(sock[CHILD]);
343 }
344 }
345
346 /* Process queue. */
347 if (dequeue(&ip)) {
348 struct dns_reply reply;
349 #if DARKSTAT_USES_HOSTENT
350 struct hostent *he;
351
352 memcpy(&reply.addr, &ip, sizeof(reply.addr));
353 he = gethostbyaddr((char *)&ip.addr.ip, sizeof(ip.addr.ip), ip.af); /* TODO MEA */
354
355 /* On some platforms (for example Linux with GLIBC 2.3.3), h_errno
356 * will be non-zero here even though the lookup succeeded.
357 */
358 if (he == NULL) {
359 reply.name[0] = '\0';
360 reply.error = h_errno;
361 } else {
362 assert(sizeof(reply.name) > sizeof(char *)); /* not just a ptr */
363 strlcpy(reply.name, he->h_name, sizeof(reply.name));
364 reply.error = 0;
365 }
366 fd_set_block(sock[CHILD]);
367 xwrite(sock[CHILD], &reply, sizeof(reply));
368 verbosef("DNS: %s is %s", ip_to_str(&reply.addr),
369 (h_errno == 0)?reply.name:hstrerror(h_errno));
370 #else /* !DARKSTAT_USES_HOSTENT */
371 struct sockaddr_in sin;
372 struct sockaddr_in6 sin6;
373 char host[NI_MAXHOST];
374 int ret, flags;
375
376 reply.addr.af = ip.af;
377 flags = NI_NAMEREQD;
378 # ifdef NI_IDN
379 flags |= NI_IDN;
380 # endif
381 switch (ip.af) {
382 case AF_INET:
383 sin.sin_family = ip.af;
384 memcpy(&reply.addr.addr.ip, &ip.addr.ip, sizeof(reply.addr.addr.ip));
385 memcpy(&sin.sin_addr, &ip.addr.ip, sizeof(sin.sin_addr));
386 ret = getnameinfo((struct sockaddr *) &sin, sizeof(sin),
387 host, sizeof(host), NULL, 0, flags);
388 break;
389 case AF_INET6:
390 sin6.sin6_family = ip.af;
391 memcpy(&reply.addr.addr.ip6, &ip.addr.ip6, sizeof(reply.addr.addr.ip6));
392 memcpy(&sin6.sin6_addr, &ip.addr.ip6, sizeof(sin6.sin6_addr));
393 ret = getnameinfo((struct sockaddr *) &sin6, sizeof(sin6),
394 host, sizeof(host), NULL, 0, flags);
395 break;
396 default:
397 ret = EAI_FAMILY;
398
399 }
400
401 if (ret != 0) {
402 reply.name[0] = '\0';
403 reply.error = ret;
404 } else {
405 assert(sizeof(reply.name) > sizeof(char *)); /* not just a ptr */
406 strlcpy(reply.name, host, sizeof(reply.name));
407 reply.error = 0;
408 }
409 fd_set_block(sock[CHILD]);
410 xwrite(sock[CHILD], &reply, sizeof(reply));
411 verbosef("DNS: %s is \"%s\".", ip_to_str(&reply.addr),
412 (ret == 0) ? reply.name : gai_strerror(ret));
413 #endif /* !DARKSTAT_USES_HOSTENT */
414 }
415 }
416 }
417
418 /* vim:set ts=3 sw=3 tw=78 expandtab: */