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