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