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