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