1 |
/* |
2 |
vpn_dns.C -- handle the dns tunnel part of the protocol. |
3 |
Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License |
16 |
along with this program; if not, write to the Free Software |
17 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
*/ |
19 |
|
20 |
#include "config.h" |
21 |
|
22 |
#if ENABLE_DNS |
23 |
|
24 |
// dns processing is EXTREMELY ugly. For obvious(?) reasons. |
25 |
// it's a hack, use only in emergency situations please. |
26 |
|
27 |
#include <cstring> |
28 |
|
29 |
#include <sys/types.h> |
30 |
#include <sys/socket.h> |
31 |
#include <sys/wait.h> |
32 |
#include <sys/uio.h> |
33 |
#include <errno.h> |
34 |
#include <time.h> |
35 |
#include <unistd.h> |
36 |
#include <fcntl.h> |
37 |
|
38 |
#include <map> |
39 |
|
40 |
#include "netcompat.h" |
41 |
|
42 |
#include "vpn.h" |
43 |
|
44 |
#define MIN_RETRY 1. |
45 |
#define MAX_RETRY 60. |
46 |
|
47 |
#define MAX_OUTSTANDING 40 // max. outstanding requests |
48 |
#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
49 |
#define MAX_RATE 1000 // requests/s |
50 |
#define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE |
51 |
|
52 |
#define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
53 |
// 240 leaves about 4 bytes of server reply data |
54 |
// every two request byte sless give room for one reply byte |
55 |
|
56 |
// seqno has 12 bits, but the lower bit is always left as zero |
57 |
// as bind caches ttl=0 records and we have to generate |
58 |
// sequence numbers that always differ case-insensitively |
59 |
#define SEQNO_MASK 0x07ff |
60 |
|
61 |
/* |
62 |
|
63 |
protocol, in shorthand :) |
64 |
|
65 |
client -> server <req> ANY? |
66 |
server -> client <req> TXT <rep> |
67 |
|
68 |
<req> is dns64-encoded <client-id:12><recv-seqno:10>[<send-seqno:10><data>] |
69 |
<rep> is dns64-encoded <0:12><recv-seqno:10>[<send-seqno:10><data>] |
70 |
|
71 |
if <client-id> is zero, the connection will be configured: |
72 |
|
73 |
<0:12><0:4>client-id:12><default-ttl:8><max-size:16><flags:16> |
74 |
|
75 |
*/ |
76 |
|
77 |
#define MAX_LBL_SIZE 63 |
78 |
#define MAX_PKT_SIZE 512 |
79 |
|
80 |
#define RR_TYPE_TXT 16 |
81 |
#define RR_TYPE_ANY 255 |
82 |
#define RR_CLASS_IN 1 |
83 |
|
84 |
// the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) |
85 |
struct dns64 |
86 |
{ |
87 |
static const char encode_chars[64 + 1]; |
88 |
static s8 decode_chars[256]; |
89 |
|
90 |
static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } |
91 |
static int decode_len (int bytes) { return (bytes * 6) / 8; } |
92 |
static int encode (char *dst, u8 *src, int len); |
93 |
static int decode (u8 *dst, char *src, int len); |
94 |
|
95 |
dns64 (); |
96 |
} dns64; |
97 |
|
98 |
// the following sequence has been crafted to |
99 |
// a) look somewhat random |
100 |
// b) the even (and odd) indices never share the same character as upper/lowercase |
101 |
const char dns64::encode_chars[64 + 1] = "_-dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"; |
102 |
s8 dns64::decode_chars[256]; |
103 |
|
104 |
dns64::dns64 () |
105 |
{ |
106 |
for (int i = 0; i < 64; i++) |
107 |
decode_chars [encode_chars [i]] = i + 1; |
108 |
} |
109 |
|
110 |
int dns64::encode (char *dst, u8 *src, int len) |
111 |
{ |
112 |
// slow, but easy to debug |
113 |
char *beg = dst; |
114 |
unsigned int accum, bits = 0; |
115 |
|
116 |
while (len--) |
117 |
{ |
118 |
accum <<= 8; |
119 |
accum |= *src++; |
120 |
bits += 8; |
121 |
|
122 |
while (bits >= 6) |
123 |
{ |
124 |
*dst++ = encode_chars [(accum >> (bits - 6)) & 63]; |
125 |
bits -= 6; |
126 |
} |
127 |
} |
128 |
|
129 |
if (bits) |
130 |
*dst++ = encode_chars [(accum << (6 - bits)) & 63]; |
131 |
|
132 |
return dst - beg; |
133 |
} |
134 |
|
135 |
int dns64::decode (u8 *dst, char *src, int len) |
136 |
{ |
137 |
// slow, but easy to debug |
138 |
u8 *beg = dst; |
139 |
unsigned int accum, bits = 0; |
140 |
|
141 |
while (len--) |
142 |
{ |
143 |
s8 chr = decode_chars [(u8)*src++]; |
144 |
|
145 |
if (!chr) |
146 |
continue; |
147 |
|
148 |
accum <<= 6; |
149 |
accum |= chr - 1; |
150 |
bits += 6; |
151 |
|
152 |
while (bits >= 8) |
153 |
{ |
154 |
*dst++ = accum >> (bits - 8); |
155 |
bits -= 8; |
156 |
} |
157 |
} |
158 |
|
159 |
return dst - beg; |
160 |
} |
161 |
|
162 |
///////////////////////////////////////////////////////////////////////////// |
163 |
|
164 |
struct byte_stream |
165 |
{ |
166 |
u8 *data; |
167 |
int maxsize; |
168 |
int fill; |
169 |
|
170 |
byte_stream (int maxsize); |
171 |
~byte_stream (); |
172 |
|
173 |
bool empty () { return !fill; } |
174 |
int size () { return fill; } |
175 |
|
176 |
bool put (u8 *data, unsigned int datalen); |
177 |
bool put (vpn_packet *pkt); |
178 |
vpn_packet *get (); |
179 |
|
180 |
u8 *begin () { return data; } |
181 |
void remove (int count); |
182 |
}; |
183 |
|
184 |
byte_stream::byte_stream (int maxsize) |
185 |
: maxsize (maxsize), fill (0) |
186 |
{ |
187 |
data = new u8 [maxsize]; |
188 |
} |
189 |
|
190 |
byte_stream::~byte_stream () |
191 |
{ |
192 |
delete data; |
193 |
} |
194 |
|
195 |
void byte_stream::remove (int count) |
196 |
{ |
197 |
if (count > fill) |
198 |
abort (); |
199 |
|
200 |
memmove (data, data + count, fill -= count); |
201 |
} |
202 |
|
203 |
bool byte_stream::put (u8 *data, unsigned int datalen) |
204 |
{ |
205 |
if (maxsize - fill < datalen) |
206 |
return false; |
207 |
|
208 |
memcpy (this->data + fill, data, datalen); fill += datalen; |
209 |
|
210 |
return true; |
211 |
} |
212 |
|
213 |
bool byte_stream::put (vpn_packet *pkt) |
214 |
{ |
215 |
if (maxsize - fill < pkt->len + 2) |
216 |
return false; |
217 |
|
218 |
data [fill++] = pkt->len >> 8; |
219 |
data [fill++] = pkt->len; |
220 |
|
221 |
memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len; |
222 |
|
223 |
return true; |
224 |
} |
225 |
|
226 |
vpn_packet *byte_stream::get () |
227 |
{ |
228 |
int len = (data [0] << 8) | data [1]; |
229 |
|
230 |
if (len > MAXSIZE && fill >= 2) |
231 |
abort (); // TODO handle this gracefully, connection reset |
232 |
|
233 |
if (fill < len + 2) |
234 |
return 0; |
235 |
|
236 |
vpn_packet *pkt = new vpn_packet; |
237 |
|
238 |
pkt->len = len; |
239 |
memcpy (&((*pkt)[0]), data + 2, len); |
240 |
remove (len + 2); |
241 |
|
242 |
return pkt; |
243 |
} |
244 |
|
245 |
///////////////////////////////////////////////////////////////////////////// |
246 |
|
247 |
#define FLAG_QUERY ( 0 << 15) |
248 |
#define FLAG_RESPONSE ( 1 << 15) |
249 |
#define FLAG_OP_MASK (15 << 14) |
250 |
#define FLAG_OP_QUERY ( 0 << 11) |
251 |
#define FLAG_AA ( 1 << 10) |
252 |
#define FLAG_TC ( 1 << 9) |
253 |
#define FLAG_RD ( 1 << 8) |
254 |
#define FLAG_RA ( 1 << 7) |
255 |
#define FLAG_AUTH ( 1 << 5) |
256 |
#define FLAG_RCODE_MASK (15 << 0) |
257 |
#define FLAG_RCODE_OK ( 0 << 0) |
258 |
#define FLAG_RCODE_FORMERR ( 1 << 0) |
259 |
#define FLAG_RCODE_SERVFAIL ( 2 << 0) |
260 |
#define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
261 |
#define FLAG_RCODE_REFUSED ( 5 << 0) |
262 |
|
263 |
#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
264 |
#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
265 |
|
266 |
struct dns_packet : net_packet |
267 |
{ |
268 |
u16 id; |
269 |
u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
270 |
u16 qdcount, ancount, nscount, arcount; |
271 |
|
272 |
u8 data[MAXSIZE - 6 * 2]; |
273 |
|
274 |
int decode_label (char *data, int size, int &offs); |
275 |
}; |
276 |
|
277 |
int dns_packet::decode_label (char *data, int size, int &offs) |
278 |
{ |
279 |
char *orig = data; |
280 |
|
281 |
memset (data, 0, size); |
282 |
|
283 |
while (offs < size - 1) |
284 |
{ |
285 |
u8 len = (*this)[offs++]; |
286 |
|
287 |
if (!len) |
288 |
break; |
289 |
else if (len < 64) |
290 |
{ |
291 |
if (size < len + 1 || offs + len >= MAXSIZE - 1) |
292 |
break; |
293 |
|
294 |
memcpy (data, &((*this)[offs]), len); |
295 |
|
296 |
data += len; size -= len; offs += len; |
297 |
*data++ = '.'; size--; |
298 |
} |
299 |
else |
300 |
{ |
301 |
int offs2 = ((len & 63) << 8) + (*this)[offs++]; |
302 |
|
303 |
data += decode_label (data, size, offs2); |
304 |
break; |
305 |
} |
306 |
} |
307 |
|
308 |
return data - orig; |
309 |
} |
310 |
|
311 |
///////////////////////////////////////////////////////////////////////////// |
312 |
|
313 |
struct dns_req |
314 |
{ |
315 |
dns_packet *pkt; |
316 |
tstamp next; |
317 |
int retry; |
318 |
connection *conn; |
319 |
int seqno; |
320 |
|
321 |
dns_req (connection *c); |
322 |
void gen_stream_req (int seqno, byte_stream *stream); |
323 |
}; |
324 |
|
325 |
static u16 dns_id = 12098; // TODO: should be per-vpn |
326 |
|
327 |
static u16 next_id () |
328 |
{ |
329 |
// the simplest lsfr with periodicity 65535 i could find |
330 |
dns_id = (dns_id << 1) |
331 |
| (((dns_id >> 1) |
332 |
^ (dns_id >> 2) |
333 |
^ (dns_id >> 4) |
334 |
^ (dns_id >> 15)) & 1); |
335 |
|
336 |
return dns_id; |
337 |
} |
338 |
|
339 |
dns_req::dns_req (connection *c) |
340 |
: conn (c) |
341 |
{ |
342 |
next = 0; |
343 |
retry = 0; |
344 |
|
345 |
pkt = new dns_packet; |
346 |
|
347 |
pkt->id = next_id (); |
348 |
} |
349 |
|
350 |
void dns_req::gen_stream_req (int seqno, byte_stream *stream) |
351 |
{ |
352 |
this->seqno = seqno; |
353 |
|
354 |
pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
355 |
pkt->qdcount = htons (1); |
356 |
|
357 |
int offs = 6*2; |
358 |
int dlen = MAX_DOMAIN_SIZE - strlen (THISNODE->domain) - 2; |
359 |
// MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
360 |
// so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
361 |
|
362 |
u8 data[256]; //TODO |
363 |
|
364 |
data[0] = THISNODE->id; //TODO |
365 |
data[1] = seqno >> 7; //TODO |
366 |
data[2] = seqno << 1; //TODO |
367 |
|
368 |
int datalen = dns64::decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE) - 3; |
369 |
|
370 |
if (datalen > stream->size ()) |
371 |
datalen = stream->size (); |
372 |
|
373 |
char enc[256], *encp = enc; |
374 |
|
375 |
memcpy (data + 3, stream->begin (), datalen); |
376 |
int enclen = dns64::encode (enc, data, datalen + 3); |
377 |
stream->remove (datalen); |
378 |
|
379 |
while (enclen) |
380 |
{ |
381 |
int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
382 |
|
383 |
(*pkt)[offs++] = lbllen; |
384 |
memcpy (pkt->at (offs), encp, lbllen); |
385 |
|
386 |
offs += lbllen; |
387 |
encp += lbllen; |
388 |
|
389 |
enclen -= lbllen; |
390 |
} |
391 |
|
392 |
const char *suffix = THISNODE->domain; |
393 |
|
394 |
// add tunnel domain |
395 |
for (;;) |
396 |
{ |
397 |
const char *end = strchr (suffix, '.'); |
398 |
|
399 |
if (!end) |
400 |
end = suffix + strlen (suffix); |
401 |
|
402 |
int len = end - suffix; |
403 |
|
404 |
(*pkt)[offs++] = len; |
405 |
memcpy (&((*pkt)[offs]), suffix, len); |
406 |
offs += len; |
407 |
|
408 |
if (!*end) |
409 |
break; |
410 |
|
411 |
suffix = end + 1; |
412 |
} |
413 |
|
414 |
(*pkt)[offs++] = 0; |
415 |
(*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
416 |
(*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
417 |
|
418 |
pkt->len = offs; |
419 |
} |
420 |
|
421 |
struct dns_rcv |
422 |
{ |
423 |
int seqno; |
424 |
dns_packet *pkt; // reply packet |
425 |
u8 data [MAXSIZE]; // actually part of the reply packet... |
426 |
int datalen; |
427 |
|
428 |
dns_rcv (int seqno, u8 *data, int datalen); |
429 |
~dns_rcv (); |
430 |
}; |
431 |
|
432 |
dns_rcv::dns_rcv (int seqno, u8 *data, int datalen) |
433 |
: seqno (seqno), pkt (new dns_packet), datalen (datalen) |
434 |
{ |
435 |
memcpy (this->data, data, datalen); |
436 |
} |
437 |
|
438 |
dns_rcv::~dns_rcv () |
439 |
{ |
440 |
delete pkt; |
441 |
} |
442 |
|
443 |
///////////////////////////////////////////////////////////////////////////// |
444 |
|
445 |
struct dns_cfg |
446 |
{ |
447 |
u8 id1, id2, id3; |
448 |
u8 def_ttl; |
449 |
u8 unused1; |
450 |
u16 max_size; |
451 |
u8 flags1, flags2; |
452 |
}; |
453 |
|
454 |
void connection::dnsv4_receive_rep (struct dns_rcv *r) |
455 |
{ |
456 |
dns_rcvpq.push_back (r); |
457 |
|
458 |
redo: |
459 |
|
460 |
for (vector<dns_rcv *>::iterator i = dns_rcvpq.begin (); |
461 |
i != dns_rcvpq.end (); |
462 |
++i) |
463 |
if (dns_rcvseq == (*i)->seqno) |
464 |
{ |
465 |
dns_rcv *r = *i; |
466 |
|
467 |
dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; |
468 |
|
469 |
if (!dns_snddq && !dns_rcvdq) |
470 |
{ |
471 |
dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
472 |
dns_snddq = new byte_stream (MAX_BACKLOG); |
473 |
|
474 |
dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
475 |
} |
476 |
|
477 |
if (!dns_rcvdq->put (r->data, r->datalen)) |
478 |
abort (); // MUST never overflow, can be caused by data corruption, TODO |
479 |
|
480 |
while (vpn_packet *pkt = dns_rcvdq->get ()) |
481 |
{ |
482 |
sockinfo si; |
483 |
si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
484 |
|
485 |
vpn->recv_vpn_packet (pkt, si); |
486 |
} |
487 |
} |
488 |
else if ((u32)(*i)->seqno - (u32)dns_rcvseq + MAX_WINDOW > MAX_WINDOW * 2) |
489 |
{ |
490 |
//D |
491 |
//abort(); |
492 |
printf ("%d erasing %d (%d)\n", THISNODE->id, (u32)(*i)->seqno, dns_rcvseq); |
493 |
dns_rcvpq.erase (i); |
494 |
goto redo; |
495 |
} |
496 |
} |
497 |
|
498 |
dns_packet * |
499 |
vpn::dnsv4_server (dns_packet *pkt) |
500 |
{ |
501 |
u16 flags = ntohs (pkt->flags); |
502 |
|
503 |
//memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
504 |
int offs = 6 * 2; // skip header |
505 |
|
506 |
pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
507 |
|
508 |
if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
509 |
&& pkt->qdcount == htons (1)) |
510 |
{ |
511 |
char qname[MAXSIZE]; |
512 |
int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
513 |
|
514 |
u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
515 |
u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
516 |
|
517 |
pkt->qdcount = htons (1); |
518 |
pkt->ancount = 0; |
519 |
pkt->nscount = 0; // should be self, as other nameservers reply like this |
520 |
pkt->arcount = 0; // a record for self, as other nameservers reply like this |
521 |
|
522 |
pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
523 |
|
524 |
int dlen = strlen (THISNODE->domain); |
525 |
|
526 |
if (qclass == RR_CLASS_IN |
527 |
&& (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
528 |
&& qlen > dlen + 1 |
529 |
&& !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
530 |
{ |
531 |
// correct class, domain: parse |
532 |
u8 data[MAXSIZE]; |
533 |
int datalen = dns64::decode (data, qname, qlen - dlen - 1); |
534 |
|
535 |
int client = data[0]; |
536 |
int seqno = ((data[1] << 7) | (data[2] >> 1)) & SEQNO_MASK; |
537 |
|
538 |
if (0 < client && client <= conns.size ()) |
539 |
{ |
540 |
connection *c = conns [client - 1]; |
541 |
|
542 |
redo: |
543 |
|
544 |
for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.begin (); |
545 |
i != c->dns_rcvpq.end (); |
546 |
++i) |
547 |
if ((*i)->seqno == seqno) |
548 |
{ |
549 |
// already seen that request: simply reply with the cached reply |
550 |
dns_rcv *r = *i; |
551 |
|
552 |
printf ("DUPLICATE %d\n", htons (r->pkt->id));//D |
553 |
|
554 |
offs = r->pkt->len; |
555 |
memcpy (pkt->at (0), r->pkt->at (0), offs); |
556 |
goto duplicate_request; |
557 |
} |
558 |
|
559 |
// new packet, queue |
560 |
dns_rcv *rcv = new dns_rcv (seqno, data + 3, datalen - 3); |
561 |
c->dnsv4_receive_rep (rcv); |
562 |
|
563 |
// now generate reply |
564 |
pkt->ancount = htons (1); // one answer RR |
565 |
pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
566 |
|
567 |
(*pkt) [offs++] = 0xc0; |
568 |
(*pkt) [offs++] = 6 * 2; // same as in query section |
569 |
|
570 |
(*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; |
571 |
(*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; |
572 |
|
573 |
(*pkt) [offs++] = 0; (*pkt) [offs++] = 0; |
574 |
(*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL |
575 |
|
576 |
int dlen = MAX_PKT_SIZE - offs - 2; |
577 |
|
578 |
// bind doesn't compress well, so reduce further by one label length |
579 |
dlen -= qlen; |
580 |
|
581 |
int rdlen_offs = offs += 2; |
582 |
|
583 |
while (c->dns_snddq |
584 |
&& !c->dns_snddq->empty () |
585 |
&& dlen > 1) |
586 |
{ |
587 |
int txtlen = dlen <= 255 ? dlen - 1 : 255; |
588 |
|
589 |
if (txtlen > c->dns_snddq->size ()) |
590 |
txtlen = c->dns_snddq->size (); |
591 |
|
592 |
(*pkt)[offs++] = txtlen; |
593 |
memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen); |
594 |
offs += txtlen; |
595 |
c->dns_snddq->remove (txtlen); |
596 |
|
597 |
dlen -= txtlen + 1; |
598 |
} |
599 |
|
600 |
// avoid empty TXT rdata |
601 |
if (offs == rdlen_offs) |
602 |
(*pkt)[offs++] = 0; |
603 |
|
604 |
int rdlen = offs - rdlen_offs; |
605 |
|
606 |
(*pkt) [rdlen_offs - 2] = rdlen >> 8; |
607 |
(*pkt) [rdlen_offs - 1] = rdlen; |
608 |
|
609 |
// now update dns_rcv copy |
610 |
rcv->pkt->len = offs; |
611 |
memcpy (rcv->pkt->at (0), pkt->at (0), offs); |
612 |
|
613 |
duplicate_request: ; |
614 |
} |
615 |
else |
616 |
pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
617 |
} |
618 |
|
619 |
pkt->len = offs; |
620 |
} |
621 |
|
622 |
return pkt; |
623 |
} |
624 |
|
625 |
void |
626 |
vpn::dnsv4_client (dns_packet *pkt) |
627 |
{ |
628 |
u16 flags = ntohs (pkt->flags); |
629 |
int offs = 6 * 2; // skip header |
630 |
|
631 |
pkt->qdcount = ntohs (pkt->qdcount); |
632 |
pkt->ancount = ntohs (pkt->ancount); |
633 |
|
634 |
// go through our request list and find the corresponding request |
635 |
for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
636 |
i != dns_sndpq.end (); |
637 |
++i) |
638 |
if ((*i)->pkt->id == pkt->id) |
639 |
{ |
640 |
connection *c = (*i)->conn; |
641 |
int seqno = (*i)->seqno; |
642 |
u8 data[MAXSIZE], *datap = data; |
643 |
|
644 |
delete *i; |
645 |
dns_sndpq.erase (i); |
646 |
|
647 |
if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
648 |
{ |
649 |
char qname[MAXSIZE]; |
650 |
|
651 |
while (pkt->qdcount-- && offs < MAXSIZE - 4) |
652 |
{ |
653 |
int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
654 |
offs += 4; // skip qtype, qclass |
655 |
} |
656 |
|
657 |
while (pkt->ancount-- && offs < MAXSIZE - 10) |
658 |
{ |
659 |
pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
660 |
|
661 |
u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
662 |
u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
663 |
u32 ttl = (*pkt) [offs++] << 24; |
664 |
ttl |= (*pkt) [offs++] << 16; |
665 |
ttl |= (*pkt) [offs++] << 8; |
666 |
ttl |= (*pkt) [offs++]; |
667 |
|
668 |
u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
669 |
|
670 |
if (rdlen <= MAXSIZE - offs) |
671 |
{ |
672 |
// decode bytes, finally |
673 |
|
674 |
while (rdlen) |
675 |
{ |
676 |
int txtlen = (*pkt) [offs++]; |
677 |
|
678 |
assert (txtlen + offs < MAXSIZE - 1); |
679 |
|
680 |
memcpy (datap, pkt->at (offs), txtlen); |
681 |
datap += txtlen; offs += txtlen; |
682 |
|
683 |
rdlen -= txtlen + 1; |
684 |
} |
685 |
|
686 |
} |
687 |
|
688 |
} |
689 |
} |
690 |
|
691 |
// todo: pkt now used |
692 |
c->dnsv4_receive_rep (new dns_rcv (seqno, data, datap - data)); |
693 |
|
694 |
break; |
695 |
} |
696 |
|
697 |
delete pkt; |
698 |
} |
699 |
|
700 |
void |
701 |
vpn::dnsv4_ev (io_watcher &w, short revents) |
702 |
{ |
703 |
if (revents & EVENT_READ) |
704 |
{ |
705 |
dns_packet *pkt = new dns_packet; |
706 |
struct sockaddr_in sa; |
707 |
socklen_t sa_len = sizeof (sa); |
708 |
|
709 |
pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
710 |
|
711 |
if (pkt->len > 0) |
712 |
{ |
713 |
if (pkt->flags & htons (FLAG_TC)) |
714 |
{ |
715 |
slog (L_WARN, _("DNS request/response truncated, check protocol settings.")); |
716 |
//TODO connection reset |
717 |
} |
718 |
|
719 |
if (THISNODE->dns_port) |
720 |
{ |
721 |
pkt = dnsv4_server (pkt); |
722 |
sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
723 |
} |
724 |
else |
725 |
dnsv4_client (pkt); |
726 |
} |
727 |
} |
728 |
} |
729 |
|
730 |
bool |
731 |
connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
732 |
{ |
733 |
// never initialized |
734 |
if (!dns_snddq && !dns_rcvdq) |
735 |
{ |
736 |
dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
737 |
dns_snddq = new byte_stream (MAX_BACKLOG); |
738 |
|
739 |
//dns_rcvseq = dns_sndseq = 0; |
740 |
|
741 |
dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
742 |
} |
743 |
|
744 |
if (!dns_snddq->put (pkt)) |
745 |
return false; |
746 |
|
747 |
// start timer if neccessary |
748 |
if (!THISNODE->dns_port && !dnsv4_tw.active) |
749 |
dnsv4_cb (dnsv4_tw); |
750 |
|
751 |
return true; |
752 |
} |
753 |
|
754 |
void |
755 |
connection::dnsv4_cb (time_watcher &w) |
756 |
{ |
757 |
// check for timeouts and (re)transmit |
758 |
tstamp next = NOW + 60; |
759 |
dns_req *send = 0; |
760 |
|
761 |
for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); |
762 |
i != vpn->dns_sndpq.end (); |
763 |
++i) |
764 |
{ |
765 |
dns_req *r = *i; |
766 |
|
767 |
if (r->next <= NOW) |
768 |
{ |
769 |
if (!send) |
770 |
{ |
771 |
send = r; |
772 |
|
773 |
if (r->retry)//D |
774 |
printf ("req %d, retry %d\n", r->pkt->id, r->retry); |
775 |
r->retry++; |
776 |
r->next = NOW + r->retry; |
777 |
} |
778 |
} |
779 |
|
780 |
if (r->next < next) |
781 |
next = r->next; |
782 |
} |
783 |
|
784 |
if (!send |
785 |
&& vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
786 |
{ |
787 |
send = new dns_req (this); |
788 |
send->gen_stream_req (dns_sndseq, dns_snddq); |
789 |
vpn->dns_sndpq.push_back (send); |
790 |
|
791 |
dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK; |
792 |
} |
793 |
|
794 |
tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
795 |
|
796 |
if (send) |
797 |
{ |
798 |
dns_packet *pkt = send->pkt; |
799 |
|
800 |
next = min_next; |
801 |
|
802 |
sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ()); |
803 |
} |
804 |
else if (next < min_next) |
805 |
next = min_next; |
806 |
|
807 |
w.start (next); |
808 |
} |
809 |
|
810 |
#endif |
811 |
|