60c924331ee4dc75ab144d1938663cdcfcd8ac7c
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)
20 #include <sys/param.h>
21 #include <sys/socket.h>
31 static void dns_main(void) _noreturn_
; /* the child process runs this */
33 #define CHILD 0 /* child process uses this socket */
36 static pid_t pid
= -1;
40 int error
; /* for gai_strerror(), or 0 if no error */
41 char name
[MAXHOSTNAMELEN
];
45 dns_init(const char *privdrop_user
)
47 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, sock
) == -1)
55 /* We are the child. */
56 privdrop(NULL
/* don't chroot */, privdrop_user
);
59 daemonize_finish(); /* drop our copy of the lifeline! */
60 if (signal(SIGUSR1
, SIG_IGN
) == SIG_ERR
)
61 errx(1, "signal(SIGUSR1, ignore) failed");
63 verbosef("fell out of dns_main()");
66 /* We are the parent. */
69 fd_set_nonblock(sock
[PARENT
]);
70 verbosef("DNS child has PID %d", pid
);
78 return; /* no child was started */
80 if (kill(pid
, SIGINT
) == -1)
82 verbosef("dns_stop() waiting for child");
83 if (waitpid(pid
, NULL
, 0) == -1)
85 verbosef("dns_stop() done waiting for child");
89 RB_ENTRY(tree_rec
) ptree
;
94 tree_cmp(struct tree_rec
*a
, struct tree_rec
*b
)
96 if (a
->ip
.family
!= b
->ip
.family
)
97 /* Sort IPv4 to the left of IPv6. */
98 return ((a
->ip
.family
== IPv4
) ? -1 : +1);
100 if (a
->ip
.family
== IPv4
)
101 return (memcmp(&a
->ip
.ip
.v4
, &b
->ip
.ip
.v4
, sizeof(a
->ip
.ip
.v4
)));
103 assert(a
->ip
.family
== IPv6
);
104 return (memcmp(&a
->ip
.ip
.v6
, &b
->ip
.ip
.v6
, sizeof(a
->ip
.ip
.v6
)));
108 static RB_HEAD(tree_t
, tree_rec
) ip_tree
= RB_INITIALIZER(&tree_rec
);
109 /* Quiet warnings. */
110 static struct tree_rec
* tree_t_RB_NEXT(struct tree_rec
*elm
)
112 static struct tree_rec
* tree_t_RB_MINMAX(struct tree_t
*head
, int val
)
114 RB_GENERATE(tree_t
, tree_rec
, ptree
, tree_cmp
)
117 dns_queue(const struct addr
*const ipaddr
)
119 struct tree_rec
*rec
;
123 return; /* no child was started - we're not doing any DNS */
125 if ((ipaddr
->family
!= IPv4
) && (ipaddr
->family
!= IPv6
)) {
126 verbosef("dns_queue() for unknown family %d", ipaddr
->family
);
130 rec
= xmalloc(sizeof(*rec
));
131 memcpy(&rec
->ip
, ipaddr
, sizeof(rec
->ip
));
133 if (RB_INSERT(tree_t
, &ip_tree
, rec
) != NULL
) {
134 /* Already queued - this happens seldom enough that we don't care about
135 * the performance hit of needlessly malloc()ing. */
136 verbosef("already queued %s", addr_to_str(ipaddr
));
141 num_w
= write(sock
[PARENT
], ipaddr
, sizeof(*ipaddr
)); /* won't block */
143 warnx("dns_queue: write: ignoring end of file");
144 else if (num_w
== -1)
145 warn("dns_queue: ignoring write error");
146 else if (num_w
!= sizeof(*ipaddr
))
147 err(1, "dns_queue: wrote %zu instead of %zu", num_w
, sizeof(*ipaddr
));
151 dns_unqueue(const struct addr
*const ipaddr
)
153 struct tree_rec tmp
, *rec
;
155 memcpy(&tmp
.ip
, ipaddr
, sizeof(tmp
.ip
));
156 if ((rec
= RB_FIND(tree_t
, &ip_tree
, &tmp
)) != NULL
) {
157 RB_REMOVE(tree_t
, &ip_tree
, rec
);
161 verbosef("couldn't unqueue %s - not in queue!", addr_to_str(ipaddr
));
165 * Returns non-zero if result waiting, stores IP and name into given pointers
166 * (name buffer is allocated by dns_poll)
169 dns_get_result(struct addr
*ipaddr
, char **name
)
171 struct dns_reply reply
;
174 numread
= read(sock
[PARENT
], &reply
, sizeof(reply
));
177 return (0); /* no input waiting */
182 goto error
; /* EOF */
183 if (numread
!= sizeof(reply
))
184 errx(1, "dns_get_result read got %zu, expected %zu",
185 numread
, sizeof(reply
));
187 /* Return successful reply. */
188 memcpy(ipaddr
, &reply
.addr
, sizeof(*ipaddr
));
189 #if DARKSTAT_USES_HOSTENT
190 if (reply
.error
!= 0)
191 xasprintf(name
, "(%s)", hstrerror(reply
.error
));
193 *name
= xstrdup(reply
.name
);
194 #else /* !DARKSTAT_USES_HOSTENT */
195 if (reply
.error
!= 0) {
196 /* Identify common special cases. */
197 const char *type
= "none";
199 if (reply
.addr
.family
== IPv6
) {
200 if (IN6_IS_ADDR_LINKLOCAL(&reply
.addr
.ip
.v6
))
202 else if (IN6_IS_ADDR_SITELOCAL(&reply
.addr
.ip
.v6
))
204 else if (IN6_IS_ADDR_MULTICAST(&reply
.addr
.ip
.v6
))
206 } else { /* AF_INET */
207 if (IN_MULTICAST(reply
.addr
.ip
.v4
))
210 xasprintf(name
, "(%s)", type
);
212 else /* Correctly resolved name. */
213 *name
= xstrdup(reply
.name
);
214 #endif /* !DARKSTAT_USES_HOSTENT */
216 dns_unqueue(&reply
.addr
);
220 warn("dns_get_result: ignoring read error");
221 /* FIXME: re-align to stream? restart dns child? */
232 return; /* no child was started - we're not doing any DNS */
234 while (dns_get_result(&ip
, &name
)) {
235 /* push into hosts_db */
236 struct bucket
*b
= host_find(&ip
);
239 verbosef("resolved %s to %s but it's not in the DB!",
240 addr_to_str(&ip
), name
);
243 if (b
->u
.host
.dns
!= NULL
) {
244 verbosef("resolved %s to %s but it's already in the DB!",
245 addr_to_str(&ip
), name
);
248 b
->u
.host
.dns
= name
;
252 /* ------------------------------------------------------------------------ */
255 STAILQ_ENTRY(qitem
) entries
;
259 STAILQ_HEAD(qhead
, qitem
) queue
= STAILQ_HEAD_INITIALIZER(queue
);
262 enqueue(const struct addr
*const ip
)
266 i
= xmalloc(sizeof(*i
));
267 memcpy(&i
->ip
, ip
, sizeof(i
->ip
));
268 STAILQ_INSERT_TAIL(&queue
, i
, entries
);
269 verbosef("DNS: enqueued %s", addr_to_str(ip
));
272 /* Return non-zero and populate <ip> pointer if queue isn't empty. */
274 dequeue(struct addr
*ip
)
278 i
= STAILQ_FIRST(&queue
);
281 STAILQ_REMOVE_HEAD(&queue
, entries
);
282 memcpy(ip
, &i
->ip
, sizeof(*ip
));
284 verbosef("DNS: dequeued %s", addr_to_str(ip
));
289 xwrite(const int d
, const void *buf
, const size_t nbytes
)
291 ssize_t ret
= write(d
, buf
, nbytes
);
295 if (ret
!= (ssize_t
)nbytes
)
296 err(1, "wrote %d bytes instead of all %d bytes", (int)ret
, (int)nbytes
);
304 #ifdef HAVE_SETPROCTITLE
305 setproctitle("DNS child");
307 fd_set_nonblock(sock
[CHILD
]);
308 verbosef("DNS child entering main DNS loop");
312 if (STAILQ_EMPTY(&queue
)) {
314 fd_set_block(sock
[CHILD
]);
315 verbosef("entering blocking read loop");
318 fd_set_nonblock(sock
[CHILD
]);
319 verbosef("non-blocking poll");
322 /* While we have input to process... */
323 ssize_t numread
= read(sock
[CHILD
], &ip
, sizeof(ip
));
325 exit(0); /* end of file, nothing more to do here. */
327 if (!blocking
&& (errno
== EAGAIN
))
328 break; /* ran out of input */
330 err(1, "DNS: read failed");
332 if (numread
!= sizeof(ip
))
333 err(1, "DNS: read got %zu bytes, expecting %zu",
334 numread
, sizeof(ip
));
337 /* After one blocking read, become non-blocking so that when we
338 * run out of input we fall through to queue processing.
341 fd_set_nonblock(sock
[CHILD
]);
347 struct dns_reply reply
;
348 #if DARKSTAT_USES_HOSTENT
351 memcpy(&reply
.addr
, &ip
, sizeof(reply
.addr
));
352 he
= gethostbyaddr((char *)&ip
.addr
.ip
, sizeof(ip
.addr
.ip
), ip
.af
); /* TODO MEA */
354 /* On some platforms (for example Linux with GLIBC 2.3.3), h_errno
355 * will be non-zero here even though the lookup succeeded.
358 reply
.name
[0] = '\0';
359 reply
.error
= h_errno
;
361 assert(sizeof(reply
.name
) > sizeof(char *)); /* not just a ptr */
362 strlcpy(reply
.name
, he
->h_name
, sizeof(reply
.name
));
365 fd_set_block(sock
[CHILD
]);
366 xwrite(sock
[CHILD
], &reply
, sizeof(reply
));
367 verbosef("DNS: %s is %s", addr_to_str(&reply
.addr
),
368 (h_errno
== 0)?reply
.name
:hstrerror(h_errno
));
369 #else /* !DARKSTAT_USES_HOSTENT */
370 struct sockaddr_in sin
;
371 struct sockaddr_in6 sin6
;
372 char host
[NI_MAXHOST
];
382 sin
.sin_family
= AF_INET
;
383 sin
.sin_addr
.s_addr
= ip
.ip
.v4
;
384 ret
= getnameinfo((struct sockaddr
*) &sin
, sizeof(sin
),
385 host
, sizeof(host
), NULL
, 0, flags
);
388 sin6
.sin6_family
= AF_INET6
;
389 memcpy(&sin6
.sin6_addr
, &ip
.ip
.v6
, sizeof(sin6
.sin6_addr
));
390 ret
= getnameinfo((struct sockaddr
*) &sin6
, sizeof(sin6
),
391 host
, sizeof(host
), NULL
, 0, flags
);
398 reply
.name
[0] = '\0';
401 assert(sizeof(reply
.name
) > sizeof(char *)); /* not just a ptr */
402 strlcpy(reply
.name
, host
, sizeof(reply
.name
));
405 fd_set_block(sock
[CHILD
]);
406 xwrite(sock
[CHILD
], &reply
, sizeof(reply
));
407 verbosef("DNS: %s is \"%s\".", addr_to_str(&reply
.addr
),
408 (ret
== 0) ? reply
.name
: gai_strerror(ret
));
409 #endif /* !DARKSTAT_USES_HOSTENT */
414 /* vim:set ts=3 sw=3 tw=78 expandtab: */