xref: /freebsd/contrib/netbsd-tests/lib/libc/net/h_dns_server.c (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
157718be8SEnji Cooper /*	$NetBSD: h_dns_server.c,v 1.4 2014/03/29 16:10:54 gson Exp $	*/
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2013 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper  * by Andreas Gustafsson.
957718be8SEnji Cooper  *
1057718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper  * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper  * are met:
1357718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper  *
1957718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper  */
3157718be8SEnji Cooper 
3257718be8SEnji Cooper /*
3357718be8SEnji Cooper  * A minimal DNS server capable of providing canned answers to the
3457718be8SEnji Cooper  * specific queries issued by t_hostent.sh and nothing more.
3557718be8SEnji Cooper  */
3657718be8SEnji Cooper 
3757718be8SEnji Cooper #include <sys/cdefs.h>
3857718be8SEnji Cooper __RCSID("$NetBSD: h_dns_server.c,v 1.4 2014/03/29 16:10:54 gson Exp $");
3957718be8SEnji Cooper 
4057718be8SEnji Cooper #include <ctype.h>
4157718be8SEnji Cooper #include <err.h>
4257718be8SEnji Cooper #include <errno.h>
4357718be8SEnji Cooper #include <fcntl.h>
4457718be8SEnji Cooper #include <memory.h>
4557718be8SEnji Cooper #include <stdio.h>
4657718be8SEnji Cooper #include <stdlib.h>
4757718be8SEnji Cooper #include <unistd.h>
4857718be8SEnji Cooper 
4957718be8SEnji Cooper #include <sys/socket.h>
5057718be8SEnji Cooper 
5157718be8SEnji Cooper #include <netinet/in.h>
52*b0b9a540SEnji Cooper #ifdef __NetBSD__
5357718be8SEnji Cooper #include <netinet6/in6.h>
54*b0b9a540SEnji Cooper #endif
55*b0b9a540SEnji Cooper 
56*b0b9a540SEnji Cooper #ifdef __FreeBSD__
57*b0b9a540SEnji Cooper #include <paths.h>
58*b0b9a540SEnji Cooper #endif
5957718be8SEnji Cooper 
6057718be8SEnji Cooper union sockaddr_either {
6157718be8SEnji Cooper 	struct sockaddr s;
6257718be8SEnji Cooper 	struct sockaddr_in sin;
6357718be8SEnji Cooper 	struct sockaddr_in6 sin6;
6457718be8SEnji Cooper };
6557718be8SEnji Cooper 
6657718be8SEnji Cooper #ifdef DEBUG
6757718be8SEnji Cooper #define DPRINTF(...)	fprintf(stderr, __VA_ARGS__)
6857718be8SEnji Cooper #else
6957718be8SEnji Cooper #define DPRINTF(...)
7057718be8SEnji Cooper #endif
7157718be8SEnji Cooper 
7257718be8SEnji Cooper /* A DNS question and its corresponding answer */
7357718be8SEnji Cooper 
7457718be8SEnji Cooper struct dns_data {
7557718be8SEnji Cooper 	size_t qname_size;
7657718be8SEnji Cooper 	const char *qname; /* Wire-encode question name */
7757718be8SEnji Cooper 	int qtype;
7857718be8SEnji Cooper 	size_t answer_size;
7957718be8SEnji Cooper 	const char *answer; /* One wire-encoded answer RDATA */
8057718be8SEnji Cooper };
8157718be8SEnji Cooper 
8257718be8SEnji Cooper /* Convert C string constant to length + data pair */
8357718be8SEnji Cooper #define STR_DATA(s) sizeof(s) - 1, s
8457718be8SEnji Cooper 
8557718be8SEnji Cooper /* Canned DNS queestion-answer pairs */
8657718be8SEnji Cooper struct dns_data data[] = {
8757718be8SEnji Cooper 	/* Forward mappings */
8857718be8SEnji Cooper 	/* localhost IN A -> 127.0.0.1 */
8957718be8SEnji Cooper 	{ STR_DATA("\011localhost\000"), 1,
9057718be8SEnji Cooper 	  STR_DATA("\177\000\000\001") },
9157718be8SEnji Cooper 	/* localhost IN AAAA -> ::1 */
9257718be8SEnji Cooper 	{ STR_DATA("\011localhost\000"), 28,
9357718be8SEnji Cooper 	  STR_DATA("\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001") },
9457718be8SEnji Cooper 	/* sixthavenue.astron.com IN A -> 38.117.134.16 */
9557718be8SEnji Cooper 	{ STR_DATA("\013sixthavenue\006astron\003com\000"), 1,
9657718be8SEnji Cooper 	  STR_DATA("\046\165\206\020") },
9757718be8SEnji Cooper 	/* sixthavenue.astron.com IN AAAA -> 2620:106:3003:1f00:3e4a:92ff:fef4:e180 */
9857718be8SEnji Cooper 	{ STR_DATA("\013sixthavenue\006astron\003com\000"), 28,
9957718be8SEnji Cooper 	  STR_DATA("\x26\x20\x01\x06\x30\x03\x1f\x00\x3e\x4a\x92\xff\xfe\xf4\xe1\x80") },
10057718be8SEnji Cooper 	/* Reverse mappings */
10157718be8SEnji Cooper 	{ STR_DATA("\0011\0010\0010\003127\007in-addr\004arpa\000"), 12,
10257718be8SEnji Cooper 	  STR_DATA("\011localhost\000") },
10357718be8SEnji Cooper 	{ STR_DATA("\0011\0010\0010\0010\0010\0010\0010\0010"
10457718be8SEnji Cooper 		   "\0010\0010\0010\0010\0010\0010\0010\0010"
10557718be8SEnji Cooper 		   "\0010\0010\0010\0010\0010\0010\0010\0010"
10657718be8SEnji Cooper 		   "\0010\0010\0010\0010\0010\0010\0010\0010"
10757718be8SEnji Cooper 		   "\003ip6\004arpa\000"), 12,
10857718be8SEnji Cooper 	  STR_DATA("\011localhost\000") },
10957718be8SEnji Cooper 	{ STR_DATA("\00216\003134\003117\00238"
11057718be8SEnji Cooper 		   "\007in-addr\004arpa\000"), 12,
11157718be8SEnji Cooper 	  STR_DATA("\013sixthavenue\006astron\003com\000") },
11257718be8SEnji Cooper 	{ STR_DATA("\0010\0018\0011\001e\0014\001f\001e\001f"
11357718be8SEnji Cooper 		   "\001f\001f\0012\0019\001a\0014\001e\0013"
11457718be8SEnji Cooper 		   "\0010\0010\001f\0011\0013\0010\0010\0013"
11557718be8SEnji Cooper 		   "\0016\0010\0011\0010\0010\0012\0016\0012"
11657718be8SEnji Cooper 		   "\003ip6\004arpa\000"), 12,
11757718be8SEnji Cooper 	  STR_DATA("\013sixthavenue\006astron\003com\000") },
11857718be8SEnji Cooper 	/* End marker */
11957718be8SEnji Cooper 	{ STR_DATA(""), 0, STR_DATA("") }
12057718be8SEnji Cooper };
12157718be8SEnji Cooper 
12257718be8SEnji Cooper /*
12357718be8SEnji Cooper  * Compare two DNS names for equality.	If equal, return their
12457718be8SEnji Cooper  * length, and if not, return zero.  Does not handle compression.
12557718be8SEnji Cooper  */
12657718be8SEnji Cooper static int
name_eq(const unsigned char * a,const unsigned char * b)12757718be8SEnji Cooper name_eq(const unsigned char *a, const unsigned char *b) {
12857718be8SEnji Cooper 	const unsigned char *a_save = a;
12957718be8SEnji Cooper 	for (;;) {
13057718be8SEnji Cooper 		int i;
13157718be8SEnji Cooper 		int lena = *a++;
13257718be8SEnji Cooper 		int lenb = *b++;
13357718be8SEnji Cooper 		if (lena != lenb)
13457718be8SEnji Cooper 			return 0;
13557718be8SEnji Cooper 		if (lena == 0)
13657718be8SEnji Cooper 			return a - a_save;
13757718be8SEnji Cooper 		for (i = 0; i < lena; i++)
13857718be8SEnji Cooper 			if (tolower(a[i]) != tolower(b[i]))
13957718be8SEnji Cooper 				return 0;
14057718be8SEnji Cooper 		a += lena;
14157718be8SEnji Cooper 		b += lena;
14257718be8SEnji Cooper 	}
14357718be8SEnji Cooper }
14457718be8SEnji Cooper 
14557718be8SEnji Cooper #ifdef DEBUG
14657718be8SEnji Cooper static char *
name2str(const void * v,char * buf,size_t buflen)14757718be8SEnji Cooper name2str(const void *v, char *buf, size_t buflen) {
14857718be8SEnji Cooper 	const unsigned char *a = v;
14957718be8SEnji Cooper 	char *b = buf;
15057718be8SEnji Cooper 	char *eb = buf + buflen;
15157718be8SEnji Cooper 
15257718be8SEnji Cooper #define ADDC(c) do { \
15357718be8SEnji Cooper 		if (b < eb) \
15457718be8SEnji Cooper 			*b++ = c; \
15557718be8SEnji Cooper 		else \
15657718be8SEnji Cooper 			return NULL; \
15757718be8SEnji Cooper 	} while (/*CONSTCOND*/0)
15857718be8SEnji Cooper 	for (int did = 0;; did++) {
15957718be8SEnji Cooper 		int lena = *a++;
16057718be8SEnji Cooper 		if (lena == 0) {
16157718be8SEnji Cooper 			ADDC('\0');
16257718be8SEnji Cooper 			return buf;
16357718be8SEnji Cooper 		}
16457718be8SEnji Cooper 		if (did)
16557718be8SEnji Cooper 			ADDC('.');
16657718be8SEnji Cooper 		for (int i = 0; i < lena; i++)
16757718be8SEnji Cooper 			ADDC(a[i]);
16857718be8SEnji Cooper 		a += lena;
16957718be8SEnji Cooper 	}
17057718be8SEnji Cooper }
17157718be8SEnji Cooper #endif
17257718be8SEnji Cooper 
173*b0b9a540SEnji Cooper #ifdef __FreeBSD__
174*b0b9a540SEnji Cooper /* XXX the daemon2_* functions should be in a library */
175*b0b9a540SEnji Cooper 
176*b0b9a540SEnji Cooper int __daemon2_detach_pipe[2];
177*b0b9a540SEnji Cooper 
178*b0b9a540SEnji Cooper static int
daemon2_fork(void)179*b0b9a540SEnji Cooper daemon2_fork(void)
180*b0b9a540SEnji Cooper {
181*b0b9a540SEnji Cooper 	int r;
182*b0b9a540SEnji Cooper 	int fd;
183*b0b9a540SEnji Cooper 	int i;
184*b0b9a540SEnji Cooper 
185*b0b9a540SEnji Cooper 	/*
186*b0b9a540SEnji Cooper 	 * Set up the pipe, making sure the write end does not
187*b0b9a540SEnji Cooper 	 * get allocated one of the file descriptors that will
188*b0b9a540SEnji Cooper 	 * be closed in daemon2_detach().
189*b0b9a540SEnji Cooper 	 */
190*b0b9a540SEnji Cooper 	for (i = 0; i < 3; i++) {
191*b0b9a540SEnji Cooper 	    r = pipe(__daemon2_detach_pipe);
192*b0b9a540SEnji Cooper 	    if (r < 0)
193*b0b9a540SEnji Cooper 		    return -1;
194*b0b9a540SEnji Cooper 	    if (__daemon2_detach_pipe[1] <= STDERR_FILENO &&
195*b0b9a540SEnji Cooper 		(fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
196*b0b9a540SEnji Cooper 		    (void)dup2(fd, __daemon2_detach_pipe[0]);
197*b0b9a540SEnji Cooper 		    (void)dup2(fd, __daemon2_detach_pipe[1]);
198*b0b9a540SEnji Cooper 		    if (fd > STDERR_FILENO)
199*b0b9a540SEnji Cooper 			    (void)close(fd);
200*b0b9a540SEnji Cooper 		    continue;
201*b0b9a540SEnji Cooper 	    }
202*b0b9a540SEnji Cooper 	    break;
203*b0b9a540SEnji Cooper 	}
204*b0b9a540SEnji Cooper 
205*b0b9a540SEnji Cooper 	r = fork();
206*b0b9a540SEnji Cooper 	if (r < 0) {
207*b0b9a540SEnji Cooper 		return -1;
208*b0b9a540SEnji Cooper 	} else if (r == 0) {
209*b0b9a540SEnji Cooper 		/* child */
210*b0b9a540SEnji Cooper 		close(__daemon2_detach_pipe[0]);
211*b0b9a540SEnji Cooper 		return 0;
212*b0b9a540SEnji Cooper        }
213*b0b9a540SEnji Cooper        /* Parent */
214*b0b9a540SEnji Cooper 
215*b0b9a540SEnji Cooper        (void) close(__daemon2_detach_pipe[1]);
216*b0b9a540SEnji Cooper 
217*b0b9a540SEnji Cooper        for (;;) {
218*b0b9a540SEnji Cooper 	       char dummy;
219*b0b9a540SEnji Cooper 	       r = read(__daemon2_detach_pipe[0], &dummy, 1);
220*b0b9a540SEnji Cooper 	       if (r < 0) {
221*b0b9a540SEnji Cooper 		       if (errno == EINTR)
222*b0b9a540SEnji Cooper 			       continue;
223*b0b9a540SEnji Cooper 		       _exit(1);
224*b0b9a540SEnji Cooper 	       } else if (r == 0) {
225*b0b9a540SEnji Cooper 		       _exit(1);
226*b0b9a540SEnji Cooper 	       } else { /* r > 0 */
227*b0b9a540SEnji Cooper 		       _exit(0);
228*b0b9a540SEnji Cooper 	       }
229*b0b9a540SEnji Cooper        }
230*b0b9a540SEnji Cooper }
231*b0b9a540SEnji Cooper 
232*b0b9a540SEnji Cooper static int
daemon2_detach(int nochdir,int noclose)233*b0b9a540SEnji Cooper daemon2_detach(int nochdir, int noclose)
234*b0b9a540SEnji Cooper {
235*b0b9a540SEnji Cooper 	int r;
236*b0b9a540SEnji Cooper 	int fd;
237*b0b9a540SEnji Cooper 
238*b0b9a540SEnji Cooper 	if (setsid() == -1)
239*b0b9a540SEnji Cooper 		return -1;
240*b0b9a540SEnji Cooper 
241*b0b9a540SEnji Cooper 	if (!nochdir)
242*b0b9a540SEnji Cooper 		(void)chdir("/");
243*b0b9a540SEnji Cooper 
244*b0b9a540SEnji Cooper 	if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
245*b0b9a540SEnji Cooper 		(void)dup2(fd, STDIN_FILENO);
246*b0b9a540SEnji Cooper 		(void)dup2(fd, STDOUT_FILENO);
247*b0b9a540SEnji Cooper 		(void)dup2(fd, STDERR_FILENO);
248*b0b9a540SEnji Cooper 		if (fd > STDERR_FILENO)
249*b0b9a540SEnji Cooper 			(void)close(fd);
250*b0b9a540SEnji Cooper 	}
251*b0b9a540SEnji Cooper 
252*b0b9a540SEnji Cooper 	while (1) {
253*b0b9a540SEnji Cooper 		r = write(__daemon2_detach_pipe[1], "", 1);
254*b0b9a540SEnji Cooper 		if (r < 0) {
255*b0b9a540SEnji Cooper 			if (errno == EINTR)
256*b0b9a540SEnji Cooper 				continue;
257*b0b9a540SEnji Cooper 			/* May get "broken pipe" here if parent is killed */
258*b0b9a540SEnji Cooper 			return -1;
259*b0b9a540SEnji Cooper 		} else if (r == 0) {
260*b0b9a540SEnji Cooper 			/* Should not happen */
261*b0b9a540SEnji Cooper 			return -1;
262*b0b9a540SEnji Cooper 		} else {
263*b0b9a540SEnji Cooper 			break;
264*b0b9a540SEnji Cooper 		}
265*b0b9a540SEnji Cooper 	}
266*b0b9a540SEnji Cooper 
267*b0b9a540SEnji Cooper 	(void) close(__daemon2_detach_pipe[1]);
268*b0b9a540SEnji Cooper 
269*b0b9a540SEnji Cooper 	return 0;
270*b0b9a540SEnji Cooper }
271*b0b9a540SEnji Cooper #endif
272*b0b9a540SEnji Cooper 
main(int argc,char ** argv)27357718be8SEnji Cooper int main(int argc, char **argv) {
27457718be8SEnji Cooper 	int s, r, protocol;
27557718be8SEnji Cooper 	union sockaddr_either saddr;
27657718be8SEnji Cooper 	struct dns_data *dp;
27757718be8SEnji Cooper 	unsigned char *p;
27857718be8SEnji Cooper 	char pidfile_name[40];
27957718be8SEnji Cooper 	FILE *f;
28057718be8SEnji Cooper 	int one = 1;
28157718be8SEnji Cooper #ifdef DEBUG
28257718be8SEnji Cooper 	char buf1[1024], buf2[1024];
28357718be8SEnji Cooper #endif
28457718be8SEnji Cooper 
285*b0b9a540SEnji Cooper #ifdef __FreeBSD__
286*b0b9a540SEnji Cooper 	daemon2_fork();
287*b0b9a540SEnji Cooper #endif
28857718be8SEnji Cooper 	if (argc < 2 || ((protocol = argv[1][0]) != '4' && protocol != '6'))
28957718be8SEnji Cooper 		errx(1, "usage: dns_server 4 | 6");
29057718be8SEnji Cooper 	s = socket(protocol == '4' ? PF_INET : PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
29157718be8SEnji Cooper 	if (s < 0)
29257718be8SEnji Cooper 		err(1, "socket");
29357718be8SEnji Cooper 	if (protocol == '4') {
29457718be8SEnji Cooper 		memset(&saddr.sin, 0, sizeof(saddr.sin));
29557718be8SEnji Cooper 		saddr.sin.sin_family = AF_INET;
29657718be8SEnji Cooper 		saddr.sin.sin_len = sizeof(saddr.sin);
29757718be8SEnji Cooper 		saddr.sin.sin_port = htons(53);
29857718be8SEnji Cooper 		saddr.sin.sin_addr.s_addr = INADDR_ANY;
29957718be8SEnji Cooper 	} else {
30057718be8SEnji Cooper 		static struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
30157718be8SEnji Cooper 		memset(&saddr.sin6, 0, sizeof(saddr.sin6));
30257718be8SEnji Cooper 		saddr.sin6.sin6_family = AF_INET6;
30357718be8SEnji Cooper 		saddr.sin6.sin6_len = sizeof(saddr.sin6);
30457718be8SEnji Cooper 		saddr.sin6.sin6_port = htons(53);
30557718be8SEnji Cooper 		saddr.sin6.sin6_addr = loopback;
30657718be8SEnji Cooper 	}
30757718be8SEnji Cooper 
30857718be8SEnji Cooper 	r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
30957718be8SEnji Cooper 	if (r < 0)
31057718be8SEnji Cooper 		err(1, "setsockopt");
31157718be8SEnji Cooper 
31257718be8SEnji Cooper 	r = bind(s,
31357718be8SEnji Cooper 		 (struct sockaddr *) &saddr,
31457718be8SEnji Cooper 		 protocol == '4' ? sizeof(struct sockaddr_in) :
31557718be8SEnji Cooper 				   sizeof(struct sockaddr_in6));
31657718be8SEnji Cooper 	if (r < 0)
31757718be8SEnji Cooper 		err(1, "bind");
31857718be8SEnji Cooper 
31957718be8SEnji Cooper 	snprintf(pidfile_name, sizeof pidfile_name,
32057718be8SEnji Cooper 		 "dns_server_%c.pid", protocol);
32157718be8SEnji Cooper 	f = fopen(pidfile_name, "w");
32257718be8SEnji Cooper 	fprintf(f, "%d", getpid());
32357718be8SEnji Cooper 	fclose(f);
324*b0b9a540SEnji Cooper #ifdef __FreeBSD__
325*b0b9a540SEnji Cooper #ifdef DEBUG
326*b0b9a540SEnji Cooper 	daemon2_detach(0, 1);
327*b0b9a540SEnji Cooper #else
328*b0b9a540SEnji Cooper 	daemon2_detach(0, 0);
329*b0b9a540SEnji Cooper #endif
330*b0b9a540SEnji Cooper #else
33157718be8SEnji Cooper #ifdef DEBUG
33257718be8SEnji Cooper 	daemon(0, 1);
33357718be8SEnji Cooper #else
33457718be8SEnji Cooper 	daemon(0, 0);
33557718be8SEnji Cooper #endif
336*b0b9a540SEnji Cooper #endif
33757718be8SEnji Cooper 
33857718be8SEnji Cooper 	for (;;) {
33957718be8SEnji Cooper 		unsigned char buf[512];
34057718be8SEnji Cooper 		union sockaddr_either from;
34157718be8SEnji Cooper 		ssize_t nrecv, nsent;
34257718be8SEnji Cooper 		socklen_t fromlen =
34357718be8SEnji Cooper 			protocol == '4' ? sizeof(struct sockaddr_in) :
34457718be8SEnji Cooper 					  sizeof(struct sockaddr_in6);
34557718be8SEnji Cooper 		memset(buf, 0, sizeof buf);
34657718be8SEnji Cooper 		nrecv = recvfrom(s, buf, sizeof buf, 0, &from.s, &fromlen);
34757718be8SEnji Cooper 		if (nrecv < 0)
34857718be8SEnji Cooper 			err(1, "recvfrom");
34957718be8SEnji Cooper 		if (nrecv < 12) {
35057718be8SEnji Cooper 			DPRINTF("Too short %zd\n", nrecv);
35157718be8SEnji Cooper 			continue;
35257718be8SEnji Cooper 		}
35357718be8SEnji Cooper 		if ((buf[2] & 0x80) != 0) {
35457718be8SEnji Cooper 			DPRINTF("Not a query 0x%x\n", buf[2]);
35557718be8SEnji Cooper 			continue;
35657718be8SEnji Cooper 		}
35757718be8SEnji Cooper 		if (!(buf[4] == 0 && buf[5] == 1)) {
35857718be8SEnji Cooper 			DPRINTF("QCOUNT is not 1 0x%x 0x%x\n", buf[4], buf[5]);
35957718be8SEnji Cooper 			continue; /* QDCOUNT is not 1 */
36057718be8SEnji Cooper 		}
36157718be8SEnji Cooper 
36257718be8SEnji Cooper 		for (dp = data; dp->qname_size != 0; dp++) {
36357718be8SEnji Cooper 			int qtype, qclass;
36457718be8SEnji Cooper 			p = buf + 12; /* Point to QNAME */
36557718be8SEnji Cooper 			int n = name_eq(p, (const unsigned char *) dp->qname);
36657718be8SEnji Cooper 			if (n == 0) {
36757718be8SEnji Cooper 				DPRINTF("no match name %s != %s\n",
36857718be8SEnji Cooper 				    name2str(p, buf1, sizeof(buf1)),
36957718be8SEnji Cooper 				    name2str(dp->qname, buf2, sizeof(buf2)));
37057718be8SEnji Cooper 				continue; /* Name does not match */
37157718be8SEnji Cooper 			}
37257718be8SEnji Cooper 			DPRINTF("match name %s\n",
37357718be8SEnji Cooper 			    name2str(p, buf1, sizeof(buf1)));
37457718be8SEnji Cooper 			p += n; /* Skip QNAME */
37557718be8SEnji Cooper 			qtype = *p++ << 8;
37657718be8SEnji Cooper 			qtype |= *p++;
37757718be8SEnji Cooper 			if (qtype != dp->qtype) {
37857718be8SEnji Cooper 				DPRINTF("no match name 0x%x != 0x%x\n",
37957718be8SEnji Cooper 				    qtype, dp->qtype);
38057718be8SEnji Cooper 				continue;
38157718be8SEnji Cooper 			}
38257718be8SEnji Cooper 			DPRINTF("match type 0x%x\n", qtype);
38357718be8SEnji Cooper 			qclass = *p++ << 8;
38457718be8SEnji Cooper 			qclass |= *p++;
38557718be8SEnji Cooper 			if (qclass != 1) { /* IN */
38657718be8SEnji Cooper 				DPRINTF("no match class %d != 1\n", qclass);
38757718be8SEnji Cooper 				continue;
38857718be8SEnji Cooper 			}
38957718be8SEnji Cooper 			DPRINTF("match class %d\n", qclass);
39057718be8SEnji Cooper 			goto found;
39157718be8SEnji Cooper 		}
39257718be8SEnji Cooper 		continue;
39357718be8SEnji Cooper 	found:
39457718be8SEnji Cooper 		buf[2] |= 0x80; /* QR */
39557718be8SEnji Cooper 		buf[3] |= 0x80; /* RA */
39657718be8SEnji Cooper 		memset(buf + 6, 0, 6); /* Clear ANCOUNT, NSCOUNT, ARCOUNT */
39757718be8SEnji Cooper 		buf[7] = 1; /* ANCOUNT */
39857718be8SEnji Cooper 		memcpy(p, dp->qname, dp->qname_size);
39957718be8SEnji Cooper 		p += dp->qname_size;
40057718be8SEnji Cooper 		*p++ = dp->qtype >> 8;
40157718be8SEnji Cooper 		*p++ = dp->qtype & 0xFF;
40257718be8SEnji Cooper 		*p++ = 0;
40357718be8SEnji Cooper 		*p++ = 1; /* IN */
40457718be8SEnji Cooper 		memset(p, 0, 4); /* TTL = 0 */
40557718be8SEnji Cooper 		p += 4;
40657718be8SEnji Cooper 		*p++ = 0;		/* RDLENGTH MSB */
40757718be8SEnji Cooper 		*p++ = dp->answer_size;	/* RDLENGTH LSB */
40857718be8SEnji Cooper 		memcpy(p, dp->answer, dp->answer_size);
40957718be8SEnji Cooper 		p += dp->answer_size;
41057718be8SEnji Cooper 		nsent = sendto(s, buf, p - buf, 0, &from.s, fromlen);
41157718be8SEnji Cooper 		DPRINTF("sent %zd\n", nsent);
41257718be8SEnji Cooper 		if (nsent != p - buf)
41357718be8SEnji Cooper 			warn("sendto");
41457718be8SEnji Cooper 	}
41557718be8SEnji Cooper }
416