Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini DNS server implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name) |
| 6 | * Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no) |
| 7 | * Copyright (C) 2003 Paul Sheer |
Bernhard Reutner-Fischer | 2c99851 | 2006-04-12 18:09:26 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 10 | * |
| 11 | * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 12 | * it into a shape which I believe is both easier to understand and maintain. |
| 13 | * I also reused the input buffer for output and removed services he did not |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 14 | * need. [1] http://threading.2038bug.com/sheerdns/ |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 15 | * |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 16 | * Some bugfix and minor changes was applied by Roberto A. Foglietta who made |
| 17 | * the first porting of oao' scdns to busybox also. |
| 18 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 19 | //config:config DNSD |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 20 | //config: bool "dnsd (9.8 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 21 | //config: default y |
| 22 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 23 | //config: Small and static DNS server daemon. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 24 | |
| 25 | //applet:IF_DNSD(APPLET(dnsd, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 26 | |
| 27 | //kbuild:lib-$(CONFIG_DNSD) += dnsd.o |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 28 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 29 | //usage:#define dnsd_trivial_usage |
| 30 | //usage: "[-dvs] [-c CONFFILE] [-t TTL_SEC] [-p PORT] [-i ADDR]" |
| 31 | //usage:#define dnsd_full_usage "\n\n" |
| 32 | //usage: "Small static DNS server daemon\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 33 | //usage: "\n -c FILE Config file" |
| 34 | //usage: "\n -t SEC TTL" |
| 35 | //usage: "\n -p PORT Listen on PORT" |
| 36 | //usage: "\n -i ADDR Listen on ADDR" |
| 37 | //usage: "\n -d Daemonize" |
| 38 | //usage: "\n -v Verbose" |
| 39 | //usage: "\n -s Send successful replies only. Use this if you want" |
| 40 | //usage: "\n to use /etc/resolv.conf with two nameserver lines:" |
| 41 | //usage: "\n nameserver DNSD_SERVER" |
Dan Fandrich | b5de0c1 | 2011-07-08 05:47:49 +0200 | [diff] [blame] | 42 | //usage: "\n nameserver NORMAL_DNS_SERVER" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 43 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 44 | #include "libbb.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 45 | #include <syslog.h> |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 47 | //#define DEBUG 1 |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 48 | #define DEBUG 0 |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 49 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 50 | enum { |
Denis Vlasenko | ef1b439 | 2009-04-12 19:03:01 +0000 | [diff] [blame] | 51 | /* can tweak this */ |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 52 | DEFAULT_TTL = 120, |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 53 | |
Denis Vlasenko | ef1b439 | 2009-04-12 19:03:01 +0000 | [diff] [blame] | 54 | /* cannot get bigger packets than 512 per RFC1035. */ |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 55 | MAX_PACK_LEN = 512, |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 56 | IP_STRING_LEN = sizeof(".xxx.xxx.xxx.xxx"), |
| 57 | MAX_NAME_LEN = IP_STRING_LEN - 1 + sizeof(".in-addr.arpa"), |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 58 | REQ_A = 1, |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 59 | REQ_PTR = 12, |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 60 | }; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 62 | /* the message from client and first part of response msg */ |
| 63 | struct dns_head { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 64 | uint16_t id; |
| 65 | uint16_t flags; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 66 | uint16_t nquer; |
| 67 | uint16_t nansw; |
| 68 | uint16_t nauth; |
| 69 | uint16_t nadd; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 70 | }; |
Denys Vlasenko | 6646de0 | 2010-04-26 14:25:33 +0200 | [diff] [blame] | 71 | /* Structure used to access type and class fields. |
| 72 | * They are totally unaligned, but gcc 4.3.4 thinks that pointer of type uint16_t* |
| 73 | * is 16-bit aligned and replaces 16-bit memcpy (in move_from_unaligned16 macro) |
| 74 | * with aligned halfword access on arm920t! |
| 75 | * Oh well. Slapping PACKED everywhere seems to help: */ |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 76 | struct type_and_class { |
Denys Vlasenko | 6646de0 | 2010-04-26 14:25:33 +0200 | [diff] [blame] | 77 | uint16_t type PACKED; |
| 78 | uint16_t class PACKED; |
| 79 | } PACKED; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 80 | /* element of known name, ip address and reversed ip address */ |
| 81 | struct dns_entry { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 82 | struct dns_entry *next; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 83 | uint32_t ip; |
| 84 | char rip[IP_STRING_LEN]; /* length decimal reversed IP */ |
| 85 | char name[1]; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 88 | #define OPT_verbose (option_mask32 & 1) |
| 89 | #define OPT_silent (option_mask32 & 2) |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 90 | |
| 91 | |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 92 | /* |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 93 | * Insert length of substrings instead of dots |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 94 | */ |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 95 | static void undot(char *rip) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 96 | { |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 97 | int i = 0; |
| 98 | int s = 0; |
| 99 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 100 | while (rip[i]) |
| 101 | i++; |
| 102 | for (--i; i >= 0; i--) { |
| 103 | if (rip[i] == '.') { |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 104 | rip[i] = s; |
| 105 | s = 0; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 106 | } else { |
| 107 | s++; |
| 108 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 112 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 113 | * Read hostname/IP records from file |
| 114 | */ |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 115 | static struct dns_entry *parse_conf_file(const char *fileconf) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 116 | { |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 117 | char *token[2]; |
Denis Vlasenko | a34f1ed | 2008-07-20 17:48:59 +0000 | [diff] [blame] | 118 | parser_t *parser; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 119 | struct dns_entry *m, *conf_data; |
| 120 | struct dns_entry **nextp; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 122 | conf_data = NULL; |
| 123 | nextp = &conf_data; |
| 124 | |
Denis Vlasenko | a34f1ed | 2008-07-20 17:48:59 +0000 | [diff] [blame] | 125 | parser = config_open(fileconf); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 126 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 127 | struct in_addr ip; |
| 128 | uint32_t v32; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 129 | |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 130 | if (inet_aton(token[1], &ip) == 0) { |
| 131 | bb_error_msg("error at line %u, skipping", parser->lineno); |
| 132 | continue; |
| 133 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 134 | |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 135 | if (OPT_verbose) |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 136 | bb_info_msg("name:%s, ip:%s", token[0], token[1]); |
Denis Vlasenko | a34f1ed | 2008-07-20 17:48:59 +0000 | [diff] [blame] | 137 | |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 138 | /* sizeof(*m) includes 1 byte for m->name[0] */ |
| 139 | m = xzalloc(sizeof(*m) + strlen(token[0]) + 1); |
| 140 | /*m->next = NULL;*/ |
| 141 | *nextp = m; |
| 142 | nextp = &m->next; |
| 143 | |
| 144 | m->name[0] = '.'; |
| 145 | strcpy(m->name + 1, token[0]); |
| 146 | undot(m->name); |
| 147 | m->ip = ip.s_addr; /* in network order */ |
| 148 | v32 = ntohl(m->ip); |
| 149 | /* inverted order */ |
| 150 | sprintf(m->rip, ".%u.%u.%u.%u", |
| 151 | (uint8_t)(v32), |
| 152 | (uint8_t)(v32 >> 8), |
| 153 | (uint8_t)(v32 >> 16), |
| 154 | (v32 >> 24) |
| 155 | ); |
| 156 | undot(m->rip); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 157 | } |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 158 | config_close(parser); |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 159 | return conf_data; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 160 | } |
| 161 | |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 162 | /* |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 163 | * Look query up in dns records and return answer if found. |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 164 | */ |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 165 | static char *table_lookup(struct dns_entry *d, |
| 166 | uint16_t type, |
| 167 | char* query_string) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 168 | { |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 169 | while (d) { |
| 170 | unsigned len = d->name[0]; |
| 171 | /* d->name[len] is the last (non NUL) char */ |
Denis Vlasenko | 91f20ab | 2007-01-20 01:47:44 +0000 | [diff] [blame] | 172 | #if DEBUG |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 173 | char *p, *q; |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 174 | q = query_string + 1; |
| 175 | p = d->name + 1; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 176 | fprintf(stderr, "%d/%d p:%s q:%s %d\n", |
| 177 | (int)strlen(p), len, |
| 178 | p, q, (int)strlen(q) |
| 179 | ); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 180 | #endif |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 181 | if (type == htons(REQ_A)) { |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 182 | /* search by host name */ |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 183 | if (len != 1 || d->name[1] != '*') { |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 184 | /* we are lax, hope no name component is ever >64 so that length |
| 185 | * (which will be represented as 'A','B'...) matches a lowercase letter. |
| 186 | * Actually, I think false matches are hard to construct. |
| 187 | * Example. |
| 188 | * [31] len is represented as '1', [65] as 'A', [65+32] as 'a'. |
| 189 | * [65] <65 same chars>[31]<31 same chars>NUL |
| 190 | * [65+32]<65 same chars>1 <31 same chars>NUL |
| 191 | * This example seems to be the minimal case when false match occurs. |
| 192 | */ |
| 193 | if (strcasecmp(d->name, query_string) != 0) |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 194 | goto next; |
| 195 | } |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 196 | return (char *)&d->ip; |
Denis Vlasenko | 91f20ab | 2007-01-20 01:47:44 +0000 | [diff] [blame] | 197 | #if DEBUG |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 198 | fprintf(stderr, "Found IP:%x\n", (int)d->ip); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 199 | #endif |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 200 | return 0; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 201 | } |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 202 | /* search by IP-address */ |
| 203 | if ((len != 1 || d->name[1] != '*') |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 204 | /* we assume (do not check) that query_string |
| 205 | * ends in ".in-addr.arpa" */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 206 | && is_prefixed_with(query_string, d->rip) |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 207 | ) { |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 208 | #if DEBUG |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 209 | fprintf(stderr, "Found name:%s\n", d->name); |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 210 | #endif |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 211 | return d->name; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 212 | } |
| 213 | next: |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 214 | d = d->next; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 217 | return NULL; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 218 | } |
| 219 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 220 | /* |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 221 | * Decode message and generate answer |
| 222 | */ |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 223 | /* RFC 1035 |
| 224 | ... |
| 225 | Whenever an octet represents a numeric quantity, the left most bit |
| 226 | in the diagram is the high order or most significant bit. |
| 227 | That is, the bit labeled 0 is the most significant bit. |
| 228 | ... |
| 229 | |
| 230 | 4.1.1. Header section format |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 231 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 232 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 233 | | ID | |
| 234 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 235 | |QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE | |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 236 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 237 | | QDCOUNT | |
| 238 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 239 | | ANCOUNT | |
| 240 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 241 | | NSCOUNT | |
| 242 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 243 | | ARCOUNT | |
| 244 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 245 | ID 16 bit random identifier assigned by querying peer. |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 246 | Used to match query/response. |
| 247 | QR message is a query (0), or a response (1). |
| 248 | OPCODE 0 standard query (QUERY) |
| 249 | 1 inverse query (IQUERY) |
| 250 | 2 server status request (STATUS) |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 251 | AA Authoritative Answer - this bit is valid in responses. |
| 252 | Responding name server is an authority for the domain name |
Denis Vlasenko | ef1b439 | 2009-04-12 19:03:01 +0000 | [diff] [blame] | 253 | in question section. Answer section may have multiple owner names |
| 254 | because of aliases. The AA bit corresponds to the name which matches |
| 255 | the query name, or the first owner name in the answer section. |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 256 | TC TrunCation - this message was truncated. |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 257 | RD Recursion Desired - this bit may be set in a query and |
| 258 | is copied into the response. If RD is set, it directs |
| 259 | the name server to pursue the query recursively. |
| 260 | Recursive query support is optional. |
| 261 | RA Recursion Available - this be is set or cleared in a |
| 262 | response, and denotes whether recursive query support is |
| 263 | available in the name server. |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 264 | RCODE Response code. |
| 265 | 0 No error condition |
| 266 | 1 Format error |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 267 | 2 Server failure - server was unable to process the query |
Denis Vlasenko | ef1b439 | 2009-04-12 19:03:01 +0000 | [diff] [blame] | 268 | due to a problem with the name server. |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 269 | 3 Name Error - meaningful only for responses from |
Denis Vlasenko | ef1b439 | 2009-04-12 19:03:01 +0000 | [diff] [blame] | 270 | an authoritative name server. The referenced domain name |
| 271 | does not exist. |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 272 | 4 Not Implemented. |
| 273 | 5 Refused. |
| 274 | QDCOUNT number of entries in the question section. |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 275 | ANCOUNT number of records in the answer section. |
| 276 | NSCOUNT number of records in the authority records section. |
| 277 | ARCOUNT number of records in the additional records section. |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 278 | |
| 279 | 4.1.2. Question section format |
| 280 | |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 281 | The section contains QDCOUNT (usually 1) entries, each of this format: |
| 282 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 283 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 284 | / QNAME / |
| 285 | / / |
| 286 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 287 | | QTYPE | |
| 288 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 289 | | QCLASS | |
| 290 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 291 | QNAME a domain name represented as a sequence of labels, where |
| 292 | each label consists of a length octet followed by that |
| 293 | number of octets. The domain name terminates with the |
| 294 | zero length octet for the null label of the root. Note |
| 295 | that this field may be an odd number of octets; no |
| 296 | padding is used. |
| 297 | QTYPE a two octet type of the query. |
| 298 | 1 a host address [REQ_A const] |
| 299 | 2 an authoritative name server |
| 300 | 3 a mail destination (Obsolete - use MX) |
| 301 | 4 a mail forwarder (Obsolete - use MX) |
| 302 | 5 the canonical name for an alias |
| 303 | 6 marks the start of a zone of authority |
| 304 | 7 a mailbox domain name (EXPERIMENTAL) |
| 305 | 8 a mail group member (EXPERIMENTAL) |
| 306 | 9 a mail rename domain name (EXPERIMENTAL) |
| 307 | 10 a null RR (EXPERIMENTAL) |
| 308 | 11 a well known service description |
| 309 | 12 a domain name pointer [REQ_PTR const] |
| 310 | 13 host information |
| 311 | 14 mailbox or mail list information |
| 312 | 15 mail exchange |
| 313 | 16 text strings |
| 314 | 0x1c IPv6? |
| 315 | 252 a request for a transfer of an entire zone |
| 316 | 253 a request for mailbox-related records (MB, MG or MR) |
| 317 | 254 a request for mail agent RRs (Obsolete - see MX) |
| 318 | 255 a request for all records |
| 319 | QCLASS a two octet code that specifies the class of the query. |
| 320 | 1 the Internet |
Denis Vlasenko | ef1b439 | 2009-04-12 19:03:01 +0000 | [diff] [blame] | 321 | (others are historic only) |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 322 | 255 any class |
| 323 | |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 324 | 4.1.3. Resource Record format |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 325 | |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 326 | The answer, authority, and additional sections all share the same format: |
| 327 | a variable number of resource records, where the number of records |
| 328 | is specified in the corresponding count field in the header. |
| 329 | Each resource record has this format: |
| 330 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 331 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 332 | / / |
| 333 | / NAME / |
| 334 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 335 | | TYPE | |
| 336 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 337 | | CLASS | |
| 338 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 339 | | TTL | |
| 340 | | | |
| 341 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 342 | | RDLENGTH | |
| 343 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| |
| 344 | / RDATA / |
| 345 | / / |
| 346 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 347 | NAME a domain name to which this resource record pertains. |
| 348 | TYPE two octets containing one of the RR type codes. This |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 349 | field specifies the meaning of the data in the RDATA field. |
| 350 | CLASS two octets which specify the class of the data in the RDATA field. |
| 351 | TTL a 32 bit unsigned integer that specifies the time interval |
| 352 | (in seconds) that the record may be cached. |
| 353 | RDLENGTH a 16 bit integer, length in octets of the RDATA field. |
| 354 | RDATA a variable length string of octets that describes the resource. |
| 355 | The format of this information varies according to the TYPE |
| 356 | and CLASS of the resource record. |
| 357 | If the TYPE is A and the CLASS is IN, it's a 4 octet IP address. |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 358 | |
| 359 | 4.1.4. Message compression |
| 360 | |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 361 | In order to reduce the size of messages, domain names coan be compressed. |
| 362 | An entire domain name or a list of labels at the end of a domain name |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 363 | is replaced with a pointer to a prior occurrence of the same name. |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 364 | |
| 365 | The pointer takes the form of a two octet sequence: |
| 366 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 367 | | 1 1| OFFSET | |
| 368 | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 369 | The first two bits are ones. This allows a pointer to be distinguished |
| 370 | from a label, since the label must begin with two zero bits because |
| 371 | labels are restricted to 63 octets or less. The OFFSET field specifies |
| 372 | an offset from the start of the message (i.e., the first octet |
| 373 | of the ID field in the domain header). |
| 374 | A zero offset specifies the first byte of the ID field, etc. |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 375 | Domain name in a message can be represented as either: |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 376 | - a sequence of labels ending in a zero octet |
| 377 | - a pointer |
| 378 | - a sequence of labels ending with a pointer |
| 379 | */ |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 380 | static int process_packet(struct dns_entry *conf_data, |
| 381 | uint32_t conf_ttl, |
Denys Vlasenko | 9fa7d7d | 2021-02-22 15:36:07 +0100 | [diff] [blame] | 382 | uint8_t *buf, |
| 383 | unsigned buflen) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 384 | { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 385 | struct dns_head *head; |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 386 | struct type_and_class *unaligned_type_class; |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 387 | const char *err_msg; |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 388 | char *query_string; |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 389 | char *answstr; |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 390 | uint8_t *answb; |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 391 | uint16_t outr_rlen; |
| 392 | uint16_t outr_flags; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 393 | uint16_t type; |
| 394 | uint16_t class; |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 395 | int query_len; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 396 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 397 | head = (struct dns_head *)buf; |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 398 | if (head->nquer == 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 399 | bb_simple_error_msg("packet has 0 queries, ignored"); |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 400 | return 0; /* don't reply */ |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 401 | } |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 402 | if (head->flags & htons(0x8000)) { /* QR bit */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 403 | bb_simple_error_msg("response packet, ignored"); |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 404 | return 0; /* don't reply */ |
| 405 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 406 | |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 407 | /* start of query string */ |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 408 | query_string = (void *)(head + 1); |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 409 | /* caller guarantees strlen is <= MAX_PACK_LEN */ |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 410 | query_len = strlen(query_string) + 1; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 411 | /* may be unaligned! */ |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 412 | unaligned_type_class = (void *)(query_string + query_len); |
Denys Vlasenko | dc8ef35 | 2010-10-29 00:37:56 +0200 | [diff] [blame] | 413 | query_len += sizeof(*unaligned_type_class); |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 414 | /* where to append answer block */ |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 415 | answb = (void *)(unaligned_type_class + 1); |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 416 | |
Denys Vlasenko | 9fa7d7d | 2021-02-22 15:36:07 +0100 | [diff] [blame] | 417 | if (buflen < answb - buf) { |
| 418 | bb_simple_error_msg("packet too short"); |
| 419 | return 0; /* don't reply */ |
| 420 | } |
| 421 | |
| 422 | /* QR = 1 "response", RCODE = 4 "Not Implemented" */ |
| 423 | outr_flags = htons(0x8000 | 4); |
| 424 | err_msg = NULL; |
| 425 | |
Denys Vlasenko | cbcc123 | 2010-03-05 23:38:54 +0100 | [diff] [blame] | 426 | /* OPCODE != 0 "standard query"? */ |
| 427 | if ((head->flags & htons(0x7800)) != 0) { |
| 428 | err_msg = "opcode != 0"; |
| 429 | goto empty_packet; |
| 430 | } |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 431 | move_from_unaligned16(class, &unaligned_type_class->class); |
| 432 | if (class != htons(1)) { /* not class INET? */ |
| 433 | err_msg = "class != 1"; |
| 434 | goto empty_packet; |
| 435 | } |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 436 | move_from_unaligned16(type, &unaligned_type_class->type); |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 437 | if (type != htons(REQ_A) && type != htons(REQ_PTR)) { |
Denys Vlasenko | e66a09b | 2010-02-07 01:11:18 +0100 | [diff] [blame] | 438 | /* we can't handle this query type */ |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 439 | //TODO: happens all the time with REQ_AAAA (0x1c) requests - implement those? |
| 440 | err_msg = "type is !REQ_A and !REQ_PTR"; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 441 | goto empty_packet; |
| 442 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 443 | |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 444 | /* look up the name */ |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 445 | answstr = table_lookup(conf_data, type, query_string); |
Denys Vlasenko | e66a09b | 2010-02-07 01:11:18 +0100 | [diff] [blame] | 446 | #if DEBUG |
| 447 | /* Shows lengths instead of dots, unusable for !DEBUG */ |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 448 | bb_info_msg("'%s'->'%s'", query_string, answstr); |
Denys Vlasenko | e66a09b | 2010-02-07 01:11:18 +0100 | [diff] [blame] | 449 | #endif |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 450 | outr_rlen = 4; |
| 451 | if (answstr && type == htons(REQ_PTR)) { |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 452 | /* returning a host name */ |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 453 | outr_rlen = strlen(answstr) + 1; |
| 454 | } |
| 455 | if (!answstr |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 456 | || (unsigned)(answb - buf) + query_len + 4 + 2 + outr_rlen > MAX_PACK_LEN |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 457 | ) { |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 458 | /* QR = 1 "response" |
| 459 | * AA = 1 "Authoritative Answer" |
| 460 | * RCODE = 3 "Name Error" */ |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 461 | err_msg = "name is not found"; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 462 | outr_flags = htons(0x8000 | 0x0400 | 3); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 463 | goto empty_packet; |
| 464 | } |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 465 | |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 466 | /* Append answer Resource Record */ |
| 467 | memcpy(answb, query_string, query_len); /* name, type, class */ |
| 468 | answb += query_len; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 469 | move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl)); |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 470 | answb += 4; |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 471 | move_to_unaligned16((uint16_t *)answb, htons(outr_rlen)); |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 472 | answb += 2; |
| 473 | memcpy(answb, answstr, outr_rlen); |
| 474 | answb += outr_rlen; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 475 | |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 476 | /* QR = 1 "response", |
| 477 | * AA = 1 "Authoritative Answer", |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 478 | * TODO: need to set RA bit 0x80? One user says nslookup complains |
| 479 | * "Got recursion not available from SERVER, trying next server" |
| 480 | * "** server can't find HOSTNAME" |
| 481 | * RCODE = 0 "success" |
| 482 | */ |
Denys Vlasenko | e66a09b | 2010-02-07 01:11:18 +0100 | [diff] [blame] | 483 | if (OPT_verbose) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 484 | bb_simple_info_msg("returning positive reply"); |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 485 | outr_flags = htons(0x8000 | 0x0400 | 0); |
| 486 | /* we have one answer */ |
| 487 | head->nansw = htons(1); |
| 488 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 489 | empty_packet: |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 490 | if ((outr_flags & htons(0xf)) != 0) { /* not a positive response */ |
| 491 | if (OPT_verbose) { |
| 492 | bb_error_msg("%s, %s", |
| 493 | err_msg, |
| 494 | OPT_silent ? "dropping query" : "sending error reply" |
| 495 | ); |
| 496 | } |
| 497 | if (OPT_silent) |
| 498 | return 0; |
| 499 | } |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 500 | head->flags |= outr_flags; |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 501 | head->nauth = head->nadd = 0; |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 502 | head->nquer = htons(1); // why??? |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 503 | |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 504 | return answb - buf; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 507 | int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 508 | int dnsd_main(int argc UNUSED_PARAM, char **argv) |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 509 | { |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 510 | const char *listen_interface = "0.0.0.0"; |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 511 | const char *fileconf = "/etc/dnsd.conf"; |
| 512 | struct dns_entry *conf_data; |
| 513 | uint32_t conf_ttl = DEFAULT_TTL; |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 514 | char *sttl, *sport; |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 515 | len_and_sockaddr *lsa, *from, *to; |
| 516 | unsigned lsa_size; |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 517 | int udps, opts; |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 518 | uint16_t port = 53; |
Denys Vlasenko | 0ecc116 | 2010-04-14 10:14:25 -0700 | [diff] [blame] | 519 | /* Ensure buf is 32bit aligned (we need 16bit, but 32bit can't hurt) */ |
| 520 | uint8_t buf[MAX_PACK_LEN + 1] ALIGN4; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 521 | |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 522 | opts = getopt32(argv, "vsi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport); |
| 523 | //if (opts & (1 << 0)) // -v |
| 524 | //if (opts & (1 << 1)) // -s |
| 525 | //if (opts & (1 << 2)) // -i |
| 526 | //if (opts & (1 << 3)) // -c |
| 527 | if (opts & (1 << 4)) // -t |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 528 | conf_ttl = xatou_range(sttl, 1, 0xffffffff); |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 529 | if (opts & (1 << 5)) // -p |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 530 | port = xatou_range(sport, 1, 0xffff); |
Denys Vlasenko | 343dfd7 | 2010-02-07 02:45:03 +0100 | [diff] [blame] | 531 | if (opts & (1 << 6)) { // -d |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 532 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); |
Denis Vlasenko | 5b27fbe | 2007-03-24 14:06:51 +0000 | [diff] [blame] | 533 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 534 | logmode = LOGMODE_SYSLOG; |
| 535 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 536 | |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 537 | conf_data = parse_conf_file(fileconf); |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 538 | |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 539 | lsa = xdotted2sockaddr(listen_interface, port); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 540 | udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0); |
| 541 | xbind(udps, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 542 | socket_want_pktinfo(udps); /* needed for recv_from_to to work */ |
| 543 | lsa_size = LSA_LEN_SIZE + lsa->len; |
| 544 | from = xzalloc(lsa_size); |
| 545 | to = xzalloc(lsa_size); |
| 546 | |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 547 | { |
| 548 | char *p = xmalloc_sockaddr2dotted(&lsa->u.sa); |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 549 | bb_info_msg("accepting UDP packets on %s", p); |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 550 | free(p); |
| 551 | } |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 552 | |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 553 | while (1) { |
"Vladimir N. Oleynik" | 2e5ee8e | 2006-01-25 14:40:24 +0000 | [diff] [blame] | 554 | int r; |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 555 | /* Try to get *DEST* address (to which of our addresses |
| 556 | * this query was directed), and reply from the same address. |
| 557 | * Or else we can exhibit usual UDP ugliness: |
| 558 | * [ip1.multihomed.ip2] <= query to ip1 <= peer |
| 559 | * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */ |
| 560 | memcpy(to, lsa, lsa_size); |
| 561 | r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len); |
| 562 | if (r < 12 || r > MAX_PACK_LEN) { |
Denis Vlasenko | ddbf3bf | 2009-04-12 04:09:09 +0000 | [diff] [blame] | 563 | bb_error_msg("packet size %d, ignored", r); |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 564 | continue; |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 565 | } |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 566 | if (OPT_verbose) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 567 | bb_simple_info_msg("got UDP packet"); |
Denis Vlasenko | 081eb71 | 2008-03-17 09:02:21 +0000 | [diff] [blame] | 568 | buf[r] = '\0'; /* paranoia */ |
Denys Vlasenko | 9fa7d7d | 2021-02-22 15:36:07 +0100 | [diff] [blame] | 569 | r = process_packet(conf_data, conf_ttl, buf, r); |
Denis Vlasenko | d3bac03 | 2007-03-24 12:13:04 +0000 | [diff] [blame] | 570 | if (r <= 0) |
| 571 | continue; |
Denis Vlasenko | e9b76e1 | 2008-05-22 17:41:01 +0000 | [diff] [blame] | 572 | send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len); |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 573 | } |
Denis Vlasenko | b5b45a9 | 2007-03-24 13:09:07 +0000 | [diff] [blame] | 574 | return 0; |
"Vladimir N. Oleynik" | 7b4aa6f | 2006-01-25 14:19:11 +0000 | [diff] [blame] | 575 | } |