xref: /minix/tests/lib/libc/net/h_dns_server.c (revision 0a6a1f1d)
1 /*	$NetBSD: h_dns_server.c,v 1.4 2014/03/29 16:10:54 gson Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andreas Gustafsson.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * A minimal DNS server capable of providing canned answers to the
34  * specific queries issued by t_hostent.sh and nothing more.
35  */
36 
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: h_dns_server.c,v 1.4 2014/03/29 16:10:54 gson Exp $");
39 
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <memory.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 
49 #include <sys/socket.h>
50 
51 #include <netinet/in.h>
52 #include <netinet6/in6.h>
53 
54 union sockaddr_either {
55 	struct sockaddr s;
56 	struct sockaddr_in sin;
57 	struct sockaddr_in6 sin6;
58 };
59 
60 #ifdef DEBUG
61 #define DPRINTF(...)	fprintf(stderr, __VA_ARGS__)
62 #else
63 #define DPRINTF(...)
64 #endif
65 
66 /* A DNS question and its corresponding answer */
67 
68 struct dns_data {
69 	size_t qname_size;
70 	const char *qname; /* Wire-encode question name */
71 	int qtype;
72 	size_t answer_size;
73 	const char *answer; /* One wire-encoded answer RDATA */
74 };
75 
76 /* Convert C string constant to length + data pair */
77 #define STR_DATA(s) sizeof(s) - 1, s
78 
79 /* Canned DNS queestion-answer pairs */
80 struct dns_data data[] = {
81 	/* Forward mappings */
82 	/* localhost IN A -> 127.0.0.1 */
83 	{ STR_DATA("\011localhost\000"), 1,
84 	  STR_DATA("\177\000\000\001") },
85 	/* localhost IN AAAA -> ::1 */
86 	{ STR_DATA("\011localhost\000"), 28,
87 	  STR_DATA("\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001") },
88 	/* sixthavenue.astron.com IN A -> 38.117.134.16 */
89 	{ STR_DATA("\013sixthavenue\006astron\003com\000"), 1,
90 	  STR_DATA("\046\165\206\020") },
91 	/* sixthavenue.astron.com IN AAAA -> 2620:106:3003:1f00:3e4a:92ff:fef4:e180 */
92 	{ STR_DATA("\013sixthavenue\006astron\003com\000"), 28,
93 	  STR_DATA("\x26\x20\x01\x06\x30\x03\x1f\x00\x3e\x4a\x92\xff\xfe\xf4\xe1\x80") },
94 	/* Reverse mappings */
95 	{ STR_DATA("\0011\0010\0010\003127\007in-addr\004arpa\000"), 12,
96 	  STR_DATA("\011localhost\000") },
97 	{ STR_DATA("\0011\0010\0010\0010\0010\0010\0010\0010"
98 		   "\0010\0010\0010\0010\0010\0010\0010\0010"
99 		   "\0010\0010\0010\0010\0010\0010\0010\0010"
100 		   "\0010\0010\0010\0010\0010\0010\0010\0010"
101 		   "\003ip6\004arpa\000"), 12,
102 	  STR_DATA("\011localhost\000") },
103 	{ STR_DATA("\00216\003134\003117\00238"
104 		   "\007in-addr\004arpa\000"), 12,
105 	  STR_DATA("\013sixthavenue\006astron\003com\000") },
106 	{ STR_DATA("\0010\0018\0011\001e\0014\001f\001e\001f"
107 		   "\001f\001f\0012\0019\001a\0014\001e\0013"
108 		   "\0010\0010\001f\0011\0013\0010\0010\0013"
109 		   "\0016\0010\0011\0010\0010\0012\0016\0012"
110 		   "\003ip6\004arpa\000"), 12,
111 	  STR_DATA("\013sixthavenue\006astron\003com\000") },
112 	/* End marker */
113 	{ STR_DATA(""), 0, STR_DATA("") }
114 };
115 
116 /*
117  * Compare two DNS names for equality.	If equal, return their
118  * length, and if not, return zero.  Does not handle compression.
119  */
120 static int
name_eq(const unsigned char * a,const unsigned char * b)121 name_eq(const unsigned char *a, const unsigned char *b) {
122 	const unsigned char *a_save = a;
123 	for (;;) {
124 		int i;
125 		int lena = *a++;
126 		int lenb = *b++;
127 		if (lena != lenb)
128 			return 0;
129 		if (lena == 0)
130 			return a - a_save;
131 		for (i = 0; i < lena; i++)
132 			if (tolower(a[i]) != tolower(b[i]))
133 				return 0;
134 		a += lena;
135 		b += lena;
136 	}
137 }
138 
139 #ifdef DEBUG
140 static char *
name2str(const void * v,char * buf,size_t buflen)141 name2str(const void *v, char *buf, size_t buflen) {
142 	const unsigned char *a = v;
143 	char *b = buf;
144 	char *eb = buf + buflen;
145 
146 #define ADDC(c) do { \
147 		if (b < eb) \
148 			*b++ = c; \
149 		else \
150 			return NULL; \
151 	} while (/*CONSTCOND*/0)
152 	for (int did = 0;; did++) {
153 		int lena = *a++;
154 		if (lena == 0) {
155 			ADDC('\0');
156 			return buf;
157 		}
158 		if (did)
159 			ADDC('.');
160 		for (int i = 0; i < lena; i++)
161 			ADDC(a[i]);
162 		a += lena;
163 	}
164 }
165 #endif
166 
main(int argc,char ** argv)167 int main(int argc, char **argv) {
168 	int s, r, protocol;
169 	union sockaddr_either saddr;
170 	struct dns_data *dp;
171 	unsigned char *p;
172 	char pidfile_name[40];
173 	FILE *f;
174 	int one = 1;
175 #ifdef DEBUG
176 	char buf1[1024], buf2[1024];
177 #endif
178 
179 	if (argc < 2 || ((protocol = argv[1][0]) != '4' && protocol != '6'))
180 		errx(1, "usage: dns_server 4 | 6");
181 	s = socket(protocol == '4' ? PF_INET : PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
182 	if (s < 0)
183 		err(1, "socket");
184 	if (protocol == '4') {
185 		memset(&saddr.sin, 0, sizeof(saddr.sin));
186 		saddr.sin.sin_family = AF_INET;
187 		saddr.sin.sin_len = sizeof(saddr.sin);
188 		saddr.sin.sin_port = htons(53);
189 		saddr.sin.sin_addr.s_addr = INADDR_ANY;
190 	} else {
191 		static struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
192 		memset(&saddr.sin6, 0, sizeof(saddr.sin6));
193 		saddr.sin6.sin6_family = AF_INET6;
194 		saddr.sin6.sin6_len = sizeof(saddr.sin6);
195 		saddr.sin6.sin6_port = htons(53);
196 		saddr.sin6.sin6_addr = loopback;
197 	}
198 
199 	r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
200 	if (r < 0)
201 		err(1, "setsockopt");
202 
203 	r = bind(s,
204 		 (struct sockaddr *) &saddr,
205 		 protocol == '4' ? sizeof(struct sockaddr_in) :
206 				   sizeof(struct sockaddr_in6));
207 	if (r < 0)
208 		err(1, "bind");
209 
210 	snprintf(pidfile_name, sizeof pidfile_name,
211 		 "dns_server_%c.pid", protocol);
212 	f = fopen(pidfile_name, "w");
213 	fprintf(f, "%d", getpid());
214 	fclose(f);
215 #ifdef DEBUG
216 	daemon(0, 1);
217 #else
218 	daemon(0, 0);
219 #endif
220 
221 	for (;;) {
222 		unsigned char buf[512];
223 		union sockaddr_either from;
224 		ssize_t nrecv, nsent;
225 		socklen_t fromlen =
226 			protocol == '4' ? sizeof(struct sockaddr_in) :
227 					  sizeof(struct sockaddr_in6);
228 		memset(buf, 0, sizeof buf);
229 		nrecv = recvfrom(s, buf, sizeof buf, 0, &from.s, &fromlen);
230 		if (nrecv < 0)
231 			err(1, "recvfrom");
232 		if (nrecv < 12) {
233 			DPRINTF("Too short %zd\n", nrecv);
234 			continue;
235 		}
236 		if ((buf[2] & 0x80) != 0) {
237 			DPRINTF("Not a query 0x%x\n", buf[2]);
238 			continue;
239 		}
240 		if (!(buf[4] == 0 && buf[5] == 1)) {
241 			DPRINTF("QCOUNT is not 1 0x%x 0x%x\n", buf[4], buf[5]);
242 			continue; /* QDCOUNT is not 1 */
243 		}
244 
245 		for (dp = data; dp->qname_size != 0; dp++) {
246 			int qtype, qclass;
247 			p = buf + 12; /* Point to QNAME */
248 			int n = name_eq(p, (const unsigned char *) dp->qname);
249 			if (n == 0) {
250 				DPRINTF("no match name %s != %s\n",
251 				    name2str(p, buf1, sizeof(buf1)),
252 				    name2str(dp->qname, buf2, sizeof(buf2)));
253 				continue; /* Name does not match */
254 			}
255 			DPRINTF("match name %s\n",
256 			    name2str(p, buf1, sizeof(buf1)));
257 			p += n; /* Skip QNAME */
258 			qtype = *p++ << 8;
259 			qtype |= *p++;
260 			if (qtype != dp->qtype) {
261 				DPRINTF("no match name 0x%x != 0x%x\n",
262 				    qtype, dp->qtype);
263 				continue;
264 			}
265 			DPRINTF("match type 0x%x\n", qtype);
266 			qclass = *p++ << 8;
267 			qclass |= *p++;
268 			if (qclass != 1) { /* IN */
269 				DPRINTF("no match class %d != 1\n", qclass);
270 				continue;
271 			}
272 			DPRINTF("match class %d\n", qclass);
273 			goto found;
274 		}
275 		continue;
276 	found:
277 		buf[2] |= 0x80; /* QR */
278 		buf[3] |= 0x80; /* RA */
279 		memset(buf + 6, 0, 6); /* Clear ANCOUNT, NSCOUNT, ARCOUNT */
280 		buf[7] = 1; /* ANCOUNT */
281 		memcpy(p, dp->qname, dp->qname_size);
282 		p += dp->qname_size;
283 		*p++ = dp->qtype >> 8;
284 		*p++ = dp->qtype & 0xFF;
285 		*p++ = 0;
286 		*p++ = 1; /* IN */
287 		memset(p, 0, 4); /* TTL = 0 */
288 		p += 4;
289 		*p++ = 0;		/* RDLENGTH MSB */
290 		*p++ = dp->answer_size;	/* RDLENGTH LSB */
291 		memcpy(p, dp->answer, dp->answer_size);
292 		p += dp->answer_size;
293 		nsent = sendto(s, buf, p - buf, 0, &from.s, fromlen);
294 		DPRINTF("sent %zd\n", nsent);
295 		if (nsent != p - buf)
296 			warn("sendto");
297 	}
298 }
299