2 * copyright (c) 2001-2008 Emil Mikulic.
4 * dns.c: synchronous DNS in a child process.
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
19 #include <sys/param.h>
20 #include <sys/socket.h>
30 static void dns_main(void); /* this is what the child process runs */
32 #define CHILD 0 /* child process uses this socket */
35 static pid_t pid
= -1;
39 int error
; /* for gai_strerror(), or 0 if no error */
40 char name
[MAXHOSTNAMELEN
];
44 dns_init(const char *privdrop_user
)
46 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sock
) == -1)
54 /* We are the child. */
55 privdrop(NULL
/* don't chroot */, privdrop_user
);
58 daemonize_finish(); /* drop our copy of the lifeline! */
59 if (signal(SIGUSR1
, SIG_IGN
) == SIG_ERR
)
60 errx(1, "signal(SIGUSR1, ignore) failed");
62 verbosef("fell out of dns_main()");
65 /* We are the parent. */
68 fd_set_nonblock(sock
[PARENT
]);
69 verbosef("DNS child has PID %d", pid
);
77 return; /* no child was started */
79 if (kill(pid
, SIGINT
) == -1)
81 verbosef("dns_stop() waiting for child");
82 if (waitpid(pid
, NULL
, 0) == -1)
84 verbosef("dns_stop() done waiting for child");
88 RB_ENTRY(tree_rec
) ptree
;
93 tree_cmp(struct tree_rec
*a
, struct tree_rec
*b
)
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);
99 if (a
->ip
.af
== AF_INET
) {
100 return (memcmp(&a
->ip
.addr
.ip
, &b
->ip
.addr
.ip
, sizeof(a
->ip
.addr
.ip
)));
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
)));
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
)
115 static struct tree_rec
* tree_t_RB_MINMAX(struct tree_t
*head
, int val
)
117 RB_GENERATE(tree_t
, tree_rec
, ptree
, tree_cmp
)
120 dns_queue(const struct addr46
*const ipaddr
)
122 struct tree_rec
*rec
;
126 return; /* no child was started - we're not doing any DNS */
129 if (ipaddr
->af
!= AF_INET
) {
130 verbosef("dns_queue() for unknown family %d.\n", ipaddr
->af
);
131 /* Not yet IPv6 capable. */
136 rec
= xmalloc(sizeof(*rec
));
137 memcpy(&rec
->ip
, ipaddr
, sizeof(rec
->ip
));
139 if (RB_INSERT(tree_t
, &ip_tree
, rec
) != NULL
) {
140 /* Already queued - this happens seldom enough that we don't care about
141 * the performance hit of needlessly malloc()ing. */
142 verbosef("already queued %s", ip_to_str(ipaddr
));
147 num_w
= write(sock
[PARENT
], ipaddr
, sizeof(*ipaddr
)); /* won't block */
149 warnx("dns_queue: write: ignoring end of file");
150 else if (num_w
== -1)
151 warn("dns_queue: ignoring write error");
152 else if (num_w
!= sizeof(*ipaddr
))
153 err(1, "dns_queue: wrote %z instead of %z", num_w
, sizeof(*ipaddr
));
157 dns_unqueue(const struct addr46
*const ipaddr
)
159 struct tree_rec tmp
, *rec
;
161 memcpy(&tmp
.ip
, ipaddr
, sizeof(tmp
.ip
));
162 if ((rec
= RB_FIND(tree_t
, &ip_tree
, &tmp
)) != NULL
) {
163 RB_REMOVE(tree_t
, &ip_tree
, rec
);
167 verbosef("couldn't unqueue %s - not in queue!", ip_to_str(ipaddr
));
171 * Returns non-zero if result waiting, stores IP and name into given pointers
172 * (name buffer is allocated by dns_poll)
175 dns_get_result(struct addr46
*ipaddr
, char **name
)
177 struct dns_reply reply
;
180 numread
= read(sock
[PARENT
], &reply
, sizeof(reply
));
183 return (0); /* no input waiting */
188 goto error
; /* EOF */
189 if (numread
!= sizeof(reply
))
190 errx(1, "dns_get_result read got %z, expected %z", numread
, sizeof(reply
));
192 /* Return successful reply. */
193 memcpy(ipaddr
, &reply
.addr
, sizeof(*ipaddr
));
194 #if DARKSTAT_USES_HOSTENT
195 if (reply
.error
!= 0)
196 xasprintf(name
, "(%s)", hstrerror(reply
.error
));
198 *name
= xstrdup(reply
.name
);
199 #else /* !DARKSTAT_USES_HOSTENT */
200 if (reply
.error
!= 0) {
201 /* Identify common special cases. */
204 if (reply
.addr
.af
== AF_INET6
) {
205 if (IN6_IS_ADDR_LINKLOCAL(&reply
.addr
.addr
.ip6
))
207 else if (IN6_IS_ADDR_SITELOCAL(&reply
.addr
.addr
.ip6
))
209 else if (IN6_IS_ADDR_MULTICAST(&reply
.addr
.addr
.ip6
))
211 } else { /* AF_INET */
212 if (IN_MULTICAST(reply
.addr
.addr
.ip
.s_addr
))
215 xasprintf(name
, "(%s)", type
);
217 else /* Correctly resolved name. */
218 *name
= xstrdup(reply
.name
);
219 #endif /* !DARKSTAT_USES_HOSTENT */
221 dns_unqueue(&reply
.addr
);
225 warn("dns_get_result: ignoring read error");
226 /* FIXME: re-align to stream? restart dns child? */
237 return; /* no child was started - we're not doing any DNS */
239 while (dns_get_result(&ip
, &name
)) {
240 /* push into hosts_db */
241 struct bucket
*b
= host_find(&ip
);
244 verbosef("resolved %s to %s but it's not in the DB!",
245 ip_to_str(&ip
), name
);
248 if (b
->u
.host
.dns
!= NULL
) {
249 verbosef("resolved %s to %s but it's already in the DB!",
250 ip_to_str(&ip
), name
);
253 b
->u
.host
.dns
= name
;
257 /* ------------------------------------------------------------------------ */
260 STAILQ_ENTRY(qitem
) entries
;
264 STAILQ_HEAD(qhead
, qitem
) queue
= STAILQ_HEAD_INITIALIZER(queue
);
267 enqueue(const struct addr46
*const ip
)
271 i
= xmalloc(sizeof(*i
));
272 memcpy(&i
->ip
, ip
, sizeof(i
->ip
));
273 STAILQ_INSERT_TAIL(&queue
, i
, entries
);
274 verbosef("DNS: enqueued %s", ip_to_str(ip
));
277 /* Return non-zero and populate <ip> pointer if queue isn't empty. */
279 dequeue(struct addr46
*ip
)
283 i
= STAILQ_FIRST(&queue
);
286 STAILQ_REMOVE_HEAD(&queue
, entries
);
287 memcpy(ip
, &i
->ip
, sizeof(*ip
));
289 verbosef("DNS: dequeued %s", ip_to_str(ip
));
294 xwrite(const int d
, const void *buf
, const size_t nbytes
)
296 ssize_t ret
= write(d
, buf
, nbytes
);
300 if (ret
!= (ssize_t
)nbytes
)
301 err(1, "wrote %d bytes instead of all %d bytes", (int)ret
, (int)nbytes
);
309 #ifdef HAVE_SETPROCTITLE
310 setproctitle("DNS child");
312 fd_set_nonblock(sock
[CHILD
]);
313 verbosef("DNS child entering main DNS loop");
317 if (STAILQ_EMPTY(&queue
)) {
319 fd_set_block(sock
[CHILD
]);
320 verbosef("entering blocking read loop");
323 fd_set_nonblock(sock
[CHILD
]);
324 verbosef("non-blocking poll");
327 /* While we have input to process... */
328 ssize_t numread
= read(sock
[CHILD
], &ip
, sizeof(ip
));
330 exit(0); /* end of file, nothing more to do here. */
332 if (!blocking
&& (errno
== EAGAIN
))
333 break; /* ran out of input */
335 err(1, "DNS: read failed");
337 if (numread
!= sizeof(ip
))
338 err(1, "DNS: read got %z bytes, expecting %z", numread
, sizeof(ip
));
341 /* After one blocking read, become non-blocking so that when we
342 * run out of input we fall through to queue processing.
345 fd_set_nonblock(sock
[CHILD
]);
351 struct dns_reply reply
;
352 #if DARKSTAT_USES_HOSTENT
355 memcpy(&reply
.addr
, &ip
, sizeof(reply
.addr
));
356 he
= gethostbyaddr((char *)&ip
.addr
.ip
, sizeof(ip
.addr
.ip
), ip
.af
); /* TODO MEA */
358 /* On some platforms (for example Linux with GLIBC 2.3.3), h_errno
359 * will be non-zero here even though the lookup succeeded.
362 reply
.name
[0] = '\0';
363 reply
.error
= h_errno
;
365 assert(sizeof(reply
.name
) > sizeof(char *)); /* not just a ptr */
366 strlcpy(reply
.name
, he
->h_name
, sizeof(reply
.name
));
369 fd_set_block(sock
[CHILD
]);
370 xwrite(sock
[CHILD
], &reply
, sizeof(reply
));
371 verbosef("DNS: %s is %s", ip_to_str(&reply
.addr
),
372 (h_errno
== 0)?reply
.name
:hstrerror(h_errno
));
373 #else /* !DARKSTAT_USES_HOSTENT */
374 struct sockaddr_in sin
;
375 struct sockaddr_in6 sin6
;
376 char host
[NI_MAXHOST
];
379 reply
.addr
.af
= ip
.af
;
386 sin
.sin_family
= ip
.af
;
387 memcpy(&reply
.addr
.addr
.ip
, &ip
.addr
.ip
, sizeof(reply
.addr
.addr
.ip
));
388 memcpy(&sin
.sin_addr
, &ip
.addr
.ip
, sizeof(sin
.sin_addr
));
389 ret
= getnameinfo((struct sockaddr
*) &sin
, sizeof(sin
),
390 host
, sizeof(host
), NULL
, 0, flags
);
393 sin6
.sin6_family
= ip
.af
;
394 memcpy(&reply
.addr
.addr
.ip6
, &ip
.addr
.ip6
, sizeof(reply
.addr
.addr
.ip6
));
395 memcpy(&sin6
.sin6_addr
, &ip
.addr
.ip6
, sizeof(sin6
.sin6_addr
));
396 ret
= getnameinfo((struct sockaddr
*) &sin6
, sizeof(sin6
),
397 host
, sizeof(host
), NULL
, 0, flags
);
405 reply
.name
[0] = '\0';
408 assert(sizeof(reply
.name
) > sizeof(char *)); /* not just a ptr */
409 strlcpy(reply
.name
, host
, sizeof(reply
.name
));
412 fd_set_block(sock
[CHILD
]);
413 xwrite(sock
[CHILD
], &reply
, sizeof(reply
));
414 verbosef("DNS: %s is \"%s\".", ip_to_str(&reply
.addr
),
415 (ret
== 0) ? reply
.name
: gai_strerror(ret
));
416 #endif /* !DARKSTAT_USES_HOSTENT */
421 /* vim:set ts=3 sw=3 tw=78 expandtab: */