125ab620854e150c275268bd2bc0cac629d79788
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 */
128 if (ipaddr
->af
!= AF_INET
&& ipaddr
->af
!= AF_INET6
) {
129 verbosef("dns_queue() for unknown family %d.\n", ipaddr
->af
);
133 rec
= xmalloc(sizeof(*rec
));
134 memcpy(&rec
->ip
, ipaddr
, sizeof(rec
->ip
));
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
));
144 num_w
= write(sock
[PARENT
], ipaddr
, sizeof(*ipaddr
)); /* won't block */
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
));
154 dns_unqueue(const struct addr46
*const ipaddr
)
156 struct tree_rec tmp
, *rec
;
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
);
164 verbosef("couldn't unqueue %s - not in queue!", ip_to_str(ipaddr
));
168 * Returns non-zero if result waiting, stores IP and name into given pointers
169 * (name buffer is allocated by dns_poll)
172 dns_get_result(struct addr46
*ipaddr
, char **name
)
174 struct dns_reply reply
;
177 numread
= read(sock
[PARENT
], &reply
, sizeof(reply
));
180 return (0); /* no input waiting */
185 goto error
; /* EOF */
186 if (numread
!= sizeof(reply
))
187 errx(1, "dns_get_result read got %z, expected %z", numread
, sizeof(reply
));
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
));
195 *name
= xstrdup(reply
.name
);
196 #else /* !DARKSTAT_USES_HOSTENT */
197 if (reply
.error
!= 0) {
198 /* Identify common special cases. */
201 if (reply
.addr
.af
== AF_INET6
) {
202 if (IN6_IS_ADDR_LINKLOCAL(&reply
.addr
.addr
.ip6
))
204 else if (IN6_IS_ADDR_SITELOCAL(&reply
.addr
.addr
.ip6
))
206 else if (IN6_IS_ADDR_MULTICAST(&reply
.addr
.addr
.ip6
))
208 } else { /* AF_INET */
209 if (IN_MULTICAST(reply
.addr
.addr
.ip
.s_addr
))
212 xasprintf(name
, "(%s)", type
);
214 else /* Correctly resolved name. */
215 *name
= xstrdup(reply
.name
);
216 #endif /* !DARKSTAT_USES_HOSTENT */
218 dns_unqueue(&reply
.addr
);
222 warn("dns_get_result: ignoring read error");
223 /* FIXME: re-align to stream? restart dns child? */
234 return; /* no child was started - we're not doing any DNS */
236 while (dns_get_result(&ip
, &name
)) {
237 /* push into hosts_db */
238 struct bucket
*b
= host_find(&ip
);
241 verbosef("resolved %s to %s but it's not in the DB!",
242 ip_to_str(&ip
), name
);
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
);
250 b
->u
.host
.dns
= name
;
254 /* ------------------------------------------------------------------------ */
257 STAILQ_ENTRY(qitem
) entries
;
261 STAILQ_HEAD(qhead
, qitem
) queue
= STAILQ_HEAD_INITIALIZER(queue
);
264 enqueue(const struct addr46
*const ip
)
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
));
274 /* Return non-zero and populate <ip> pointer if queue isn't empty. */
276 dequeue(struct addr46
*ip
)
280 i
= STAILQ_FIRST(&queue
);
283 STAILQ_REMOVE_HEAD(&queue
, entries
);
284 memcpy(ip
, &i
->ip
, sizeof(*ip
));
286 verbosef("DNS: dequeued %s", ip_to_str(ip
));
291 xwrite(const int d
, const void *buf
, const size_t nbytes
)
293 ssize_t ret
= write(d
, buf
, nbytes
);
297 if (ret
!= (ssize_t
)nbytes
)
298 err(1, "wrote %d bytes instead of all %d bytes", (int)ret
, (int)nbytes
);
306 #ifdef HAVE_SETPROCTITLE
307 setproctitle("DNS child");
309 fd_set_nonblock(sock
[CHILD
]);
310 verbosef("DNS child entering main DNS loop");
314 if (STAILQ_EMPTY(&queue
)) {
316 fd_set_block(sock
[CHILD
]);
317 verbosef("entering blocking read loop");
320 fd_set_nonblock(sock
[CHILD
]);
321 verbosef("non-blocking poll");
324 /* While we have input to process... */
325 ssize_t numread
= read(sock
[CHILD
], &ip
, sizeof(ip
));
327 exit(0); /* end of file, nothing more to do here. */
329 if (!blocking
&& (errno
== EAGAIN
))
330 break; /* ran out of input */
332 err(1, "DNS: read failed");
334 if (numread
!= sizeof(ip
))
335 err(1, "DNS: read got %z bytes, expecting %z", numread
, sizeof(ip
));
338 /* After one blocking read, become non-blocking so that when we
339 * run out of input we fall through to queue processing.
342 fd_set_nonblock(sock
[CHILD
]);
348 struct dns_reply reply
;
349 #if DARKSTAT_USES_HOSTENT
352 memcpy(&reply
.addr
, &ip
, sizeof(reply
.addr
));
353 he
= gethostbyaddr((char *)&ip
.addr
.ip
, sizeof(ip
.addr
.ip
), ip
.af
); /* TODO MEA */
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.
359 reply
.name
[0] = '\0';
360 reply
.error
= h_errno
;
362 assert(sizeof(reply
.name
) > sizeof(char *)); /* not just a ptr */
363 strlcpy(reply
.name
, he
->h_name
, sizeof(reply
.name
));
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
];
376 reply
.addr
.af
= ip
.af
;
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
);
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
);
402 reply
.name
[0] = '\0';
405 assert(sizeof(reply
.name
) > sizeof(char *)); /* not just a ptr */
406 strlcpy(reply
.name
, host
, sizeof(reply
.name
));
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 */
418 /* vim:set ts=3 sw=3 tw=78 expandtab: */