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