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