ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_dns.C
(Generate patch)

Comparing gvpe/src/vpn_dns.C (file contents):
Revision 1.8 by pcg, Fri Mar 4 03:43:09 2005 UTC vs.
Revision 1.40 by pcg, Sat Jul 9 20:29:29 2005 UTC

14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 GNU General Public License for more details.
16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with gvpe; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/ 20*/
21
22// TODO: EDNS0 option to increase dns mtu?
23// TODO: re-write dns packet parsing/creation using a safe mem-buffer
24// to ensure no buffer overflows or similar problems.
21 25
22#include "config.h" 26#include "config.h"
23 27
24#if ENABLE_DNS 28#if ENABLE_DNS
25 29
26// dns processing is EXTREMELY ugly. For obvious(?) reasons. 30// dns processing is EXTREMELY ugly. For obvious(?) reasons.
27// it's a hack, use only in emergency situations please. 31// it's a hack, use only in emergency situations please.
28 32
29#include <cstring> 33#include <cstring>
34#include <cassert>
30 35
31#include <sys/types.h> 36#include <sys/types.h>
32#include <sys/socket.h> 37#include <sys/socket.h>
33#include <sys/wait.h> 38#include <sys/wait.h>
34#include <sys/uio.h> 39#include <sys/uio.h>
43 48
44#include "netcompat.h" 49#include "netcompat.h"
45 50
46#include "vpn.h" 51#include "vpn.h"
47 52
48#define MIN_RETRY 1. 53#define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data
49#define MAX_RETRY 60. 54#define ACTIVITY_INTERVAL 5.
50 55
51#define MAX_OUTSTANDING 400 // max. outstanding requests 56#define INITIAL_TIMEOUT 0.1 // retry timeouts
57#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn
58
59#define MAX_SEND_INTERVAL 2. // optimistic?
60
52#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING 61#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog
53#define MAX_RATE 10000 // requests/s
54#define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE 62#define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE
55 63
56#define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well 64#define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well
57// 240 leaves about 4 bytes of server reply data 65// 240 leaves about 4 bytes of server reply data
58// every two request byte sless give room for one reply byte 66// every request byte less give room for two reply bytes
59 67
60// seqno has 12 bits (3 bytes a 4 bits in the header)
61#define SEQNO_MASK 0x7fff 68#define SEQNO_MASK 0x3fff
62#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) 69#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) )
63 70
64#define MAX_LBL_SIZE 63 71#define MAX_LBL_SIZE 63
65#define MAX_PKT_SIZE 512 72#define MAX_PKT_SIZE 512
66 73
74#define RR_TYPE_A 1
75#define RR_TYPE_NULL 10
67#define RR_TYPE_TXT 16 76#define RR_TYPE_TXT 16
68#define RR_TYPE_ANY 255 77#define RR_TYPE_ANY 255
78
69#define RR_CLASS_IN 1 79#define RR_CLASS_IN 1
80
81#define CMD_IP_1 207
82#define CMD_IP_2 46
83#define CMD_IP_3 236
84#define CMD_IP_RST 29
85#define CMD_IP_SYN 113
86#define CMD_IP_REJ 32
70 87
71// works for cmaps up to 255 (not 256!) 88// works for cmaps up to 255 (not 256!)
72struct charmap 89struct charmap
73{ 90{
74 enum { INVALID = (u8)255 }; 91 enum { INVALID = (u8)255 };
154 return dec_len [len]; 171 return dec_len [len];
155} 172}
156 173
157unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) 174unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len)
158{ 175{
159 if (!len) 176 if (!len || len > MAX_DEC_LEN)
160 return 0; 177 return 0;
161 178
162 int elen = encode_len (len); 179 int elen = encode_len (len);
163 180
164 mp_limb_t m [MAX_LIMBS]; 181 mp_limb_t m [MAX_LIMBS];
183 return elen; 200 return elen;
184} 201}
185 202
186unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) 203unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len)
187{ 204{
188 if (!len) 205 if (!len || len > MAX_ENC_LEN)
189 return 0; 206 return 0;
190 207
191 u8 src_ [MAX_ENC_LEN]; 208 u8 src_ [MAX_ENC_LEN];
192 unsigned int elen = 0; 209 unsigned int elen = 0;
193 210
240 } 257 }
241 abort (); 258 abort ();
242} 259}
243#endif 260#endif
244 261
245// the following sequence has been crafted to 262//static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-");
246// a) look somewhat random
247// b) the even (and odd) indices never share the same character as upper/lowercase
248// the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative)
249// the other sequences are obviously derived
250//static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); 263//static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
251static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); 264static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
252//static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet 265//static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet
253static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); 266static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO");
254 267
255///////////////////////////////////////////////////////////////////////////// 268/////////////////////////////////////////////////////////////////////////////
256 269
257#define HDRSIZE 6 270#define HDRSIZE 6
258 271
259inline void encode_header (char *data, int clientid, int seqno) 272inline void encode_header (char *data, int clientid, int seqno, int retry = 0)
260{ 273{
261 u8 hdr[3] = { clientid, seqno >> 8, seqno }; 274 seqno &= SEQNO_MASK;
275
276 u8 hdr[3] = {
277 clientid,
278 (seqno >> 8) | (retry << 6),
279 seqno,
280 };
262 281
263 assert (clientid < 256); 282 assert (clientid < 256);
264 283
265 cdc26.encode (data, hdr, 3); 284 cdc26.encode (data, hdr, 3);
266} 285}
269{ 288{
270 u8 hdr[3]; 289 u8 hdr[3];
271 290
272 cdc26.decode (hdr, data, HDRSIZE); 291 cdc26.decode (hdr, data, HDRSIZE);
273 292
274 printf ("DEC %02x %02x %02x %02x\n", hdr[0], hdr[1], hdr[2], hdr[3]);
275
276 clientid = hdr[0]; 293 clientid = hdr[0];
277 seqno = (hdr[1] << 8) | hdr[2]; 294 seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK;
278} 295}
279 296
280///////////////////////////////////////////////////////////////////////////// 297/////////////////////////////////////////////////////////////////////////////
281 298
282struct byte_stream 299struct byte_stream
311} 328}
312 329
313void byte_stream::remove (int count) 330void byte_stream::remove (int count)
314{ 331{
315 if (count > fill) 332 if (count > fill)
316 abort (); 333 assert (count <= fill);
317 334
318 memmove (data, data + count, fill -= count); 335 memmove (data, data + count, fill -= count);
319} 336}
320 337
321bool byte_stream::put (u8 *data, unsigned int datalen) 338bool byte_stream::put (u8 *data, unsigned int datalen)
334 return false; 351 return false;
335 352
336 data [fill++] = pkt->len >> 8; 353 data [fill++] = pkt->len >> 8;
337 data [fill++] = pkt->len; 354 data [fill++] = pkt->len;
338 355
339 memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len; 356 memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len;
340 357
341 return true; 358 return true;
342} 359}
343 360
344vpn_packet *byte_stream::get () 361vpn_packet *byte_stream::get ()
345{ 362{
363 unsigned int len;
364
365 for (;;)
366 {
346 int len = (data [0] << 8) | data [1]; 367 len = (data [0] << 8) | data [1];
347 368
348 if (len > MAXSIZE && fill >= 2) 369 if (len <= MAXSIZE || fill < 2)
349 abort (); // TODO handle this gracefully, connection reset 370 break;
350 371
372 // TODO: handle this better than skipping, e.g. by reset
373 slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte..."));
374 remove (1);
375 }
376
351 if (fill < len + 2) 377 if (fill < len + 2)
352 return 0; 378 return 0;
353 379
354 vpn_packet *pkt = new vpn_packet; 380 vpn_packet *pkt = new vpn_packet;
355 381
356 pkt->len = len; 382 pkt->len = len;
357 memcpy (&((*pkt)[0]), data + 2, len); 383 memcpy (pkt->at (0), data + 2, len);
358 remove (len + 2); 384 remove (len + 2);
359 385
360 return pkt; 386 return pkt;
361} 387}
362 388
363///////////////////////////////////////////////////////////////////////////// 389/////////////////////////////////////////////////////////////////////////////
364 390
365#define FLAG_QUERY ( 0 << 15) 391#define FLAG_QUERY ( 0 << 15)
366#define FLAG_RESPONSE ( 1 << 15) 392#define FLAG_RESPONSE ( 1 << 15)
367#define FLAG_OP_MASK (15 << 14) 393#define FLAG_OP_MASK (15 << 11)
368#define FLAG_OP_QUERY ( 0 << 11) 394#define FLAG_OP_QUERY ( 0 << 11)
369#define FLAG_AA ( 1 << 10) 395#define FLAG_AA ( 1 << 10)
370#define FLAG_TC ( 1 << 9) 396#define FLAG_TC ( 1 << 9)
371#define FLAG_RD ( 1 << 8) 397#define FLAG_RD ( 1 << 8)
372#define FLAG_RA ( 1 << 7) 398#define FLAG_RA ( 1 << 7)
379#define FLAG_RCODE_REFUSED ( 5 << 0) 405#define FLAG_RCODE_REFUSED ( 5 << 0)
380 406
381#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) 407#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD)
382#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) 408#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA)
383 409
410struct dns_cfg
411{
412 static int next_uid;
413
414 u8 id1, id2, id3, id4;
415
416 u8 version;
417 u8 flags;
418 u8 rrtype;
419 u8 def_ttl;
420
421 u16 client;
422 u16 uid; // to make request unique
423
424 u16 max_size;
425 u8 seq_cdc;
426 u8 req_cdc;
427
428 u8 rep_cdc;
429 u8 delay; // time in 0.01s units that the server may delay replying packets
430 u8 r3, r4;
431
432 u8 r5, r6, r7, r8;
433
434 void reset (int clientid);
435 bool valid ();
436};
437
438int dns_cfg::next_uid;
439
440void dns_cfg::reset (int clientid)
441{
442 id1 = 'G';
443 id2 = 'V';
444 id3 = 'P';
445 id4 = 'E';
446
447 version = 1;
448
449 rrtype = RR_TYPE_TXT;
450 flags = 0;
451 def_ttl = 0;
452 seq_cdc = 26;
453 req_cdc = 62;
454 rep_cdc = 0;
455 max_size = htons (MAX_PKT_SIZE);
456 client = htons (clientid);
457 uid = next_uid++;
458 delay = 0;
459
460 r3 = r4 = 0;
461 r4 = r5 = r6 = r7 = 0;
462}
463
464bool dns_cfg::valid ()
465{
466 // although the protocol itself allows for some configurability,
467 // only the following encoding/decoding settings are implemented.
468 return id1 == 'G'
469 && id2 == 'V'
470 && id3 == 'P'
471 && id4 == 'E'
472 && seq_cdc == 26
473 && req_cdc == 62
474 && rep_cdc == 0
475 && version == 1;
476}
477
384struct dns_packet : net_packet 478struct dns_packet : net_packet
385{ 479{
386 u16 id; 480 u16 id;
387 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 481 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4
388 u16 qdcount, ancount, nscount, arcount; 482 u16 qdcount, ancount, nscount, arcount;
389 483
390 u8 data[MAXSIZE - 6 * 2]; 484 u8 data [MAXSIZE - 6 * 2];
391 485
392 int decode_label (char *data, int size, int &offs); 486 int decode_label (char *data, int size, int &offs);
393}; 487};
394 488
395int dns_packet::decode_label (char *data, int size, int &offs) 489int dns_packet::decode_label (char *data, int size, int &offs)
426 return data - orig; 520 return data - orig;
427} 521}
428 522
429///////////////////////////////////////////////////////////////////////////// 523/////////////////////////////////////////////////////////////////////////////
430 524
431struct dns_req
432{
433 dns_packet *pkt;
434 tstamp next;
435 int retry;
436 connection *conn;
437 int seqno;
438
439 dns_req (connection *c);
440 void gen_stream_req (int seqno, byte_stream *stream);
441};
442
443static u16 dns_id = 12098; // TODO: should be per-vpn 525static u16 dns_id = 0; // TODO: should be per-vpn
444 526
445static u16 next_id () 527static u16 next_id ()
446{ 528{
529 if (!dns_id)
530 dns_id = time (0);
531
447 // the simplest lsfr with periodicity 65535 i could find 532 // the simplest lsfr with periodicity 65535 i could find
448 dns_id = (dns_id << 1) 533 dns_id = (dns_id << 1)
449 | (((dns_id >> 1) 534 | (((dns_id >> 1)
450 ^ (dns_id >> 2) 535 ^ (dns_id >> 2)
451 ^ (dns_id >> 4) 536 ^ (dns_id >> 4)
452 ^ (dns_id >> 15)) & 1); 537 ^ (dns_id >> 15)) & 1);
453 538
454 return dns_id; 539 return dns_id;
455} 540}
456 541
457dns_req::dns_req (connection *c) 542struct dns_rcv;
458: conn (c) 543struct dns_snd;
544
545struct dns_connection
459{ 546{
460 next = 0; 547 connection *c;
548 struct vpn *vpn;
549
550 dns_cfg cfg;
551
552 bool established;
553
554 tstamp last_received;
555 tstamp last_sent;
556 double min_latency;
557 double poll_interval, send_interval;
558
559 vector<dns_rcv *> rcvpq;
560
561 byte_stream rcvdq; int rcvseq; int repseq;
562 byte_stream snddq; int sndseq;
563
564 void time_cb (time_watcher &w); time_watcher tw;
565 void receive_rep (dns_rcv *r);
566
567 dns_connection (connection *c);
568 ~dns_connection ();
569};
570
571struct dns_snd
572{
573 dns_packet *pkt;
574 tstamp timeout, sent;
575 int retry;
576 struct dns_connection *dns;
577 int seqno;
578 bool stdhdr;
579
580 void gen_stream_req (int seqno, byte_stream &stream);
581 void gen_syn_req ();
582
583 dns_snd (dns_connection *dns);
584 ~dns_snd ();
585};
586
587dns_snd::dns_snd (dns_connection *dns)
588: dns (dns)
589{
590 timeout = 0;
461 retry = 0; 591 retry = 0;
592 seqno = 0;
593 sent = NOW;
594 stdhdr = false;
462 595
463 pkt = new dns_packet; 596 pkt = new dns_packet;
464 597
465 pkt->id = next_id (); 598 pkt->id = next_id ();
466} 599}
467 600
601dns_snd::~dns_snd ()
602{
603 delete pkt;
604}
605
606static void append_domain (dns_packet &pkt, int &offs, const char *domain)
607{
608 // add tunnel domain
609 for (;;)
610 {
611 const char *end = strchr (domain, '.');
612
613 if (!end)
614 end = domain + strlen (domain);
615
616 int len = end - domain;
617
618 pkt [offs++] = len;
619 memcpy (pkt.at (offs), domain, len);
620 offs += len;
621
622 if (!*end)
623 break;
624
625 domain = end + 1;
626 }
627}
628
468void dns_req::gen_stream_req (int seqno, byte_stream *stream) 629void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
469{ 630{
631 stdhdr = true;
470 this->seqno = seqno; 632 this->seqno = seqno;
633
634 timeout = NOW + INITIAL_TIMEOUT;
471 635
472 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 636 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
473 pkt->qdcount = htons (1); 637 pkt->qdcount = htons (1);
474 638
475 int offs = 6*2; 639 int offs = 6*2;
476 int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); 640 int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2);
477 // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, 641 // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well,
478 // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra 642 // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra
479 643
480 char enc[256], *encp = enc; 644 char enc[256], *encp = enc;
481 encode_header (enc, THISNODE->id, seqno); 645 encode_header (enc, THISNODE->id, seqno);
482 646
483 int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); 647 int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE);
484 648
485 if (datalen > stream->size ()) 649 if (datalen > stream.size ())
486 datalen = stream->size (); 650 datalen = stream.size ();
487 651
488 int enclen = cdc62.encode (enc + HDRSIZE, stream->begin (), datalen) + HDRSIZE; 652 int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE;
489
490 printf ("cdc62.encode %d->%d:%02x %02x %02x %02x\n", datalen, enclen,
491 stream->begin ()[0],
492 stream->begin ()[1],
493 stream->begin ()[2],
494 stream->begin ()[3]);
495
496 stream->remove (datalen); 653 stream.remove (datalen);
497 654
498 while (enclen) 655 while (enclen)
499 { 656 {
500 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; 657 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE;
501 658
506 encp += lbllen; 663 encp += lbllen;
507 664
508 enclen -= lbllen; 665 enclen -= lbllen;
509 } 666 }
510 667
511 const char *suffix = THISNODE->domain; 668 append_domain (*pkt, offs, dns->c->conf->domain);
512
513 // add tunnel domain
514 for (;;)
515 {
516 const char *end = strchr (suffix, '.');
517
518 if (!end)
519 end = suffix + strlen (suffix);
520
521 int len = end - suffix;
522
523 (*pkt)[offs++] = len;
524 memcpy (&((*pkt)[offs]), suffix, len);
525 offs += len;
526
527 if (!*end)
528 break;
529
530 suffix = end + 1;
531 }
532 669
533 (*pkt)[offs++] = 0; 670 (*pkt)[offs++] = 0;
534 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; 671 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY;
672 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
673
674 pkt->len = offs;
675}
676
677void dns_snd::gen_syn_req ()
678{
679 timeout = NOW + INITIAL_SYN_TIMEOUT;
680
681 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
682 pkt->qdcount = htons (1);
683
684 int offs = 6 * 2;
685
686 int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg));
687
688 assert (elen <= MAX_LBL_SIZE);
689
690 (*pkt)[offs] = elen;
691 offs += elen + 1;
692 append_domain (*pkt, offs, dns->c->conf->domain);
693
694 (*pkt)[offs++] = 0;
695 (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A;
535 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 696 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
536 697
537 pkt->len = offs; 698 pkt->len = offs;
538} 699}
539 700
558{ 719{
559 delete pkt; 720 delete pkt;
560} 721}
561 722
562///////////////////////////////////////////////////////////////////////////// 723/////////////////////////////////////////////////////////////////////////////
563 724
564struct dns_cfg 725dns_connection::dns_connection (connection *c)
726: c (c)
727, rcvdq (MAX_BACKLOG * 2)
728, snddq (MAX_BACKLOG)
729, tw (this, &dns_connection::time_cb)
565{ 730{
566 u8 id1, id2, id3; 731 vpn = c->vpn;
567 u8 def_ttl;
568 u8 unused1;
569 u16 max_size;
570 u8 flags1, flags2;
571};
572 732
733 established = false;
734
735 rcvseq = repseq = sndseq = 0;
736
737 last_sent = last_received = 0;
738 poll_interval = 0.5; // starting here
739 send_interval = 0.5; // starting rate
740 min_latency = INITIAL_TIMEOUT;
741}
742
743dns_connection::~dns_connection ()
744{
745 for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
746 i != rcvpq.end ();
747 ++i)
748 delete *i;
749}
750
573void connection::dnsv4_receive_rep (struct dns_rcv *r) 751void dns_connection::receive_rep (dns_rcv *r)
574{ 752{
753 if (r->datalen)
754 {
755 last_received = NOW;
756 tw.trigger ();
757
758 poll_interval = send_interval;
759 }
760 else
761 {
762 poll_interval *= 1.5;
763
764 if (poll_interval > MAX_POLL_INTERVAL)
765 poll_interval = MAX_POLL_INTERVAL;
766 }
767
575 dns_rcvpq.push_back (r); 768 rcvpq.push_back (r);
576 769
577 printf ("%d got inketc %d (%02x %02x %02x %02x)\n", THISNODE->id, r->seqno
578 ,r->data[0]
579 ,r->data[1]
580 ,r->data[2]
581 ,r->data[3]
582 );
583 redo: 770 redo:
584 771
585 // find next packet 772 // find next packet
586 for (vector<dns_rcv *>::iterator i = dns_rcvpq.end (); i-- != dns_rcvpq.begin (); ) 773 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); )
587 if (SEQNO_EQ (dns_rcvseq, (*i)->seqno)) 774 if (SEQNO_EQ (rcvseq, (*i)->seqno))
588 { 775 {
589 // enter the packet into our input stream 776 // enter the packet into our input stream
590 r = *i; 777 r = *i;
591 778
592 printf ("%d checking for older packet %d\n", THISNODE->id, dns_rcvseq);
593 // remove the oldest packet, look forward, as it's oldest first 779 // remove the oldest packet, look forward, as it's oldest first
594 for (vector<dns_rcv *>::iterator j = dns_rcvpq.begin (); j != dns_rcvpq.end (); ++j) 780 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j)
595 if (SEQNO_EQ ((*j)->seqno, dns_rcvseq - MAX_WINDOW)) 781 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW))
596 { 782 {
597 printf ("%d removing %d\n", THISNODE->id, (*j)->seqno);
598 delete *j; 783 delete *j;
599 dns_rcvpq.erase (j); 784 rcvpq.erase (j);
600 break; 785 break;
601 } 786 }
602 787
603 dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; 788 rcvseq = (rcvseq + 1) & SEQNO_MASK;
604 789
605 if (!dns_snddq && !dns_rcvdq) 790 if (!rcvdq.put (r->data, r->datalen))
606 { 791 {
607 dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); 792 slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)");
608 dns_snddq = new byte_stream (MAX_BACKLOG); 793 abort (); // MUST never overflow, can be caused by data corruption, TODO
609
610 dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4);
611 } 794 }
612 795
613 if (!dns_rcvdq->put (r->data, r->datalen))
614 abort (); // MUST never overflow, can be caused by data corruption, TODO
615
616 while (vpn_packet *pkt = dns_rcvdq->get ()) 796 while (vpn_packet *pkt = rcvdq.get ())
617 { 797 {
618 sockinfo si; 798 sockinfo si;
619 si.host = 0; si.port = 0; si.prot = PROT_DNSv4; 799 si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4;
620 800
621 vpn->recv_vpn_packet (pkt, si); 801 vpn->recv_vpn_packet (pkt, si);
802
803 delete pkt;
622 } 804 }
623 805
624 // check for further packets 806 // check for further packets
625 goto redo; 807 goto redo;
626 } 808 }
627} 809}
628 810
629dns_packet * 811void
630vpn::dnsv4_server (dns_packet *pkt) 812vpn::dnsv4_server (dns_packet &pkt)
631{ 813{
632 u16 flags = ntohs (pkt->flags); 814 u16 flags = ntohs (pkt.flags);
633 815
634 //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len);
635 int offs = 6 * 2; // skip header 816 int offs = 6 * 2; // skip header
636 817
637 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 818 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
638 819
639 if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) 820 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK))
640 && pkt->qdcount == htons (1)) 821 && pkt.qdcount == htons (1))
641 { 822 {
642 char qname[MAXSIZE]; 823 char qname [MAXSIZE];
643 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 824 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
644 825
645 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; 826 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
646 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; 827 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
647 828
648 pkt->qdcount = htons (1); 829 pkt.qdcount = htons (1);
649 pkt->ancount = 0; 830 pkt.ancount = 0;
650 pkt->nscount = 0; // should be self, as other nameservers reply like this 831 pkt.nscount = 0; // should be self, as other nameservers reply like this
651 pkt->arcount = 0; // a record for self, as other nameservers reply like this 832 pkt.arcount = 0; // a record for self, as other nameservers reply like this
652 833
653 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); 834 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL);
654 835
655 int dlen = strlen (THISNODE->domain); 836 int dlen = strlen (THISNODE->domain);
656 837
657 if (qclass == RR_CLASS_IN 838 if (qclass == RR_CLASS_IN
658 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT)
659 && qlen > dlen + 1 + HDRSIZE 839 && qlen > dlen + 1
660 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) 840 && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen))
661 { 841 {
662 // correct class, domain: parse 842 // now generate reply
663 int client, seqno; 843 pkt.ancount = htons (1); // one answer RR
664 decode_header (qname, client, seqno); 844 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
665 845
666 u8 data[MAXSIZE]; 846 if ((qtype == RR_TYPE_ANY
667 int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); 847 || qtype == RR_TYPE_TXT
668 848 || qtype == RR_TYPE_NULL)
669 printf ("cdc62.decode %d(%d): %02x %02x %02x %02x\n",
670 qlen - (dlen + 1 + HDRSIZE), datalen 849 && qlen > dlen + 1 + HDRSIZE)
671 ,data[0]
672 ,data[1]
673 ,data[2]
674 ,data[3]);
675
676 printf ("SRV got %d <%.*s>\n", seqno, qlen, qname + HDRSIZE);//D
677 printf ("SRV got %d <%.*s>\n", seqno, qlen - (dlen + 1 + HDRSIZE), qname + HDRSIZE);//D
678
679 if (0 < client && client <= conns.size ())
680 { 850 {
851 // correct class, domain: parse
852 int client, seqno;
853 decode_header (qname, client, seqno);
854
855 u8 data[MAXSIZE];
856 int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE));
857
858 if (0 < client && client <= conns.size ())
859 {
681 connection *c = conns [client - 1]; 860 connection *c = conns [client - 1];
861 dns_connection *dns = c->dns;
862 dns_rcv *rcv;
682 863
864 if (dns)
865 {
683 for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.end (); i-- != c->dns_rcvpq.begin (); ) 866 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); )
684 if (SEQNO_EQ ((*i)->seqno, seqno)) 867 if (SEQNO_EQ ((*i)->seqno, seqno))
868 {
869 // already seen that request: simply reply with the cached reply
870 dns_rcv *r = *i;
871
872 slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno);
873
874 // refresh header & id, as the retry count could have changed
875 memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE);
876 r->pkt->id = pkt.id;
877
878 memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len);
879
880 goto duplicate_request;
881 }
882
883 // new packet, queue
884 rcv = new dns_rcv (seqno, data, datalen);
885 dns->receive_rep (rcv);
886 }
887
685 { 888 {
686 // already seen that request: simply reply with the cached reply 889 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
687 dns_rcv *r = *i;
688 890
689 printf ("DUPLICATE %d\n", htons (r->pkt->id));//D 891 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A;
892 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type
893 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
894 pkt [offs++] = 0; pkt [offs++] = 0;
895 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL
690 896
691 memcpy (pkt->at (0), r->pkt->at (0), offs = r->pkt->len); 897 int rdlen_offs = offs += 2;
692 pkt->id = r->pkt->id; 898
693 goto duplicate_request; 899 if (dns)
900 {
901 int dlen = ntohs (dns->cfg.max_size) - offs;
902
903 // bind doesn't compress well, so reduce further by one label length
904 dlen -= qlen;
905
906 // only put data into in-order sequence packets, if
907 // we receive out-of-order packets we generate empty
908 // replies
909 //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D
910 if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW)
911 {
912 dns->repseq = seqno;
913
914 while (dlen > 1 && !dns->snddq.empty ())
915 {
916 int txtlen = dlen <= 255 ? dlen - 1 : 255;
917
918 if (txtlen > dns->snddq.size ())
919 txtlen = dns->snddq.size ();
920
921 pkt[offs++] = txtlen;
922 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen);
923 offs += txtlen;
924 dns->snddq.remove (txtlen);
925
926 dlen -= txtlen + 1;
927 }
928 }
929
930 // avoid completely empty TXT rdata
931 if (offs == rdlen_offs)
932 pkt[offs++] = 0;
933
934 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
935 }
936 else
937 {
938 // send RST
939 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
940 pkt [offs++] = CMD_IP_RST;
941 }
942
943 int rdlen = offs - rdlen_offs;
944
945 pkt [rdlen_offs - 2] = rdlen >> 8;
946 pkt [rdlen_offs - 1] = rdlen;
947
948 if (dns)
949 {
950 // now update dns_rcv copy
951 rcv->pkt->len = offs;
952 memcpy (rcv->pkt->at (0), pkt.at (0), offs);
953 }
694 } 954 }
695 955
696 // new packet, queue 956 duplicate_request: ;
697 dns_rcv *rcv = new dns_rcv (seqno, data, datalen); 957 }
698 c->dnsv4_receive_rep (rcv); 958 else
699
700 // now generate reply
701 pkt->ancount = htons (1); // one answer RR
702 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); 959 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
960 }
961 else if (qtype == RR_TYPE_A
962 && qlen > dlen + 1 + cdc26.encode_len (sizeof (dns_cfg)))
963 {
964 dns_cfg cfg;
965 cdc26.decode ((u8 *)&cfg, qname, cdc26.encode_len (sizeof (dns_cfg)));
966 int client = ntohs (cfg.client);
703 967
704 (*pkt) [offs++] = 0xc0;
705 (*pkt) [offs++] = 6 * 2; // same as in query section 968 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
706 969
707 (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; 970 pkt [offs++] = RR_TYPE_A >> 8; pkt [offs++] = RR_TYPE_A; // type
708 (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; 971 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
709
710 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; 972 pkt [offs++] = 0; pkt [offs++] = 0;
711 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL 973 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
974 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
712 975
713 int dlen = MAX_PKT_SIZE - offs - 2; 976 slog (L_INFO, _("DNS: client %d connects"), client);
714 977
715 // bind doesn't compress well, so reduce further by one label length 978 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
716 dlen -= qlen; 979 pkt [offs++] = CMD_IP_REJ;
717 980
718 int rdlen_offs = offs += 2; 981 if (0 < client && client <= conns.size ())
719
720 while (c->dns_snddq
721 && !c->dns_snddq->empty ()
722 && dlen > 1)
723 { 982 {
724 int txtlen = dlen <= 255 ? dlen - 1 : 255; 983 connection *c = conns [client - 1];
725 984
726 if (txtlen > c->dns_snddq->size ()) 985 if (cfg.valid ())
727 txtlen = c->dns_snddq->size (); 986 {
987 pkt [offs - 1] = CMD_IP_SYN;
728 988
729 (*pkt)[offs++] = txtlen; 989 delete c->dns;
730 memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen); 990 c->dns = new dns_connection (c);
731 offs += txtlen; 991 c->dns->cfg = cfg;
732 c->dns_snddq->remove (txtlen); 992 }
733
734 dlen -= txtlen + 1;
735 } 993 }
736
737 // avoid empty TXT rdata
738 if (offs == rdlen_offs)
739 (*pkt)[offs++] = 0;
740
741 int rdlen = offs - rdlen_offs;
742
743 (*pkt) [rdlen_offs - 2] = rdlen >> 8;
744 (*pkt) [rdlen_offs - 1] = rdlen;
745
746 // now update dns_rcv copy
747 rcv->pkt->len = offs;
748 memcpy (rcv->pkt->at (0), pkt->at (0), offs);
749
750 duplicate_request: ;
751 } 994 }
752 else
753 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
754 } 995 }
755 996
756 pkt->len = offs; 997 pkt.len = offs;
757 } 998 }
758
759 return pkt;
760} 999}
761 1000
762void 1001void
763vpn::dnsv4_client (dns_packet *pkt) 1002vpn::dnsv4_client (dns_packet &pkt)
764{ 1003{
765 u16 flags = ntohs (pkt->flags); 1004 u16 flags = ntohs (pkt.flags);
766 int offs = 6 * 2; // skip header 1005 int offs = 6 * 2; // skip header
767 1006
768 pkt->qdcount = ntohs (pkt->qdcount); 1007 pkt.qdcount = ntohs (pkt.qdcount);
769 pkt->ancount = ntohs (pkt->ancount); 1008 pkt.ancount = ntohs (pkt.ancount);
770 1009
771 // go through our request list and find the corresponding request 1010 // go through our request list and find the corresponding request
772 for (vector<dns_req *>::iterator i = dns_sndpq.begin (); 1011 for (vector<dns_snd *>::iterator i = dns_sndpq.begin ();
773 i != dns_sndpq.end (); 1012 i != dns_sndpq.end ();
774 ++i) 1013 ++i)
775 if ((*i)->pkt->id == pkt->id) 1014 if ((*i)->pkt->id == pkt.id)
776 { 1015 {
1016 dns_connection *dns = (*i)->dns;
777 connection *c = (*i)->conn; 1017 connection *c = dns->c;
778 int seqno = (*i)->seqno; 1018 int seqno = (*i)->seqno;
779 u8 data[MAXSIZE], *datap = data; 1019 u8 data[MAXSIZE], *datap = data;
780 1020
1021 if ((*i)->retry)
1022 {
1023 dns->send_interval *= 1.01;
1024 if (dns->send_interval > MAX_SEND_INTERVAL)
1025 dns->send_interval = MAX_SEND_INTERVAL;
1026 }
1027 else
1028 {
1029#if 0
1030 dns->send_interval *= 0.999;
1031#endif
1032 // the latency surely puts an upper bound on
1033 // the minimum send interval
1034 double latency = NOW - (*i)->sent;
1035
1036 if (latency < dns->min_latency)
1037 dns->min_latency = latency;
1038
1039 if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor)
1040 dns->send_interval = dns->min_latency * conf.dns_overlap_factor;
1041
1042 if (dns->send_interval < conf.dns_send_interval)
1043 dns->send_interval = conf.dns_send_interval;
1044 }
1045
781 delete *i; 1046 delete *i;
782 dns_sndpq.erase (i); 1047 dns_sndpq.erase (i);
783 1048
784 if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) 1049 if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK))
785 { 1050 {
786 char qname[MAXSIZE]; 1051 char qname[MAXSIZE];
787 1052
788 while (pkt->qdcount-- && offs < MAXSIZE - 4) 1053 while (pkt.qdcount-- && offs < MAXSIZE - 4)
789 { 1054 {
790 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 1055 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
791 offs += 4; // skip qtype, qclass 1056 offs += 4; // skip qtype, qclass
792 } 1057 }
793 1058
794 while (pkt->ancount-- && offs < MAXSIZE - 10) 1059 while (pkt.ancount-- && offs < MAXSIZE - 10 && datap)
795 { 1060 {
796 int qlen = //D
797 pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 1061 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
798 1062
799 printf ("got reply to <%.*s>\n", qlen, qname);//D
800
801 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; 1063 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
802 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; 1064 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
803 u32 ttl = (*pkt) [offs++] << 24; 1065 u32 ttl = pkt [offs++] << 24;
804 ttl |= (*pkt) [offs++] << 16; 1066 ttl |= pkt [offs++] << 16;
805 ttl |= (*pkt) [offs++] << 8; 1067 ttl |= pkt [offs++] << 8;
806 ttl |= (*pkt) [offs++]; 1068 ttl |= pkt [offs++];
807
808 u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; 1069 u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++];
809 1070
810 if (rdlen <= MAXSIZE - offs) 1071 if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT)
811 { 1072 {
812 // decode bytes, finally 1073 if (rdlen <= MAXSIZE - offs)
813
814 while (rdlen)
815 { 1074 {
1075 // decode bytes, finally
1076
1077 while (rdlen)
1078 {
816 int txtlen = (*pkt) [offs++]; 1079 int txtlen = pkt [offs++];
817 1080
818 assert (txtlen + offs < MAXSIZE - 1); 1081 assert (txtlen + offs < MAXSIZE - 1);
819 1082
820 memcpy (datap, pkt->at (offs), txtlen); 1083 memcpy (datap, pkt.at (offs), txtlen);
821 datap += txtlen; offs += txtlen; 1084 datap += txtlen; offs += txtlen;
822 1085
823 rdlen -= txtlen + 1; 1086 rdlen -= txtlen + 1;
1087 }
824 } 1088 }
825
826 } 1089 }
1090 else if (qtype == RR_TYPE_A)
1091 {
1092 u8 ip [4];
827 1093
1094 ip [0] = pkt [offs++];
1095 ip [1] = pkt [offs++];
1096 ip [2] = pkt [offs++];
1097 ip [3] = pkt [offs++];
1098
1099 if (ip [0] == CMD_IP_1
1100 && ip [1] == CMD_IP_2
1101 && ip [2] == CMD_IP_3)
1102 {
1103 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]);
1104
1105 if (ip [3] == CMD_IP_RST)
1106 {
1107 slog (L_DEBUG, _("DNS: got tunnel RST request"));
1108
1109 delete dns; c->dns = 0;
1110
1111 return;
1112 }
1113 else if (ip [3] == CMD_IP_SYN)
1114 {
1115 slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us."));
1116 dns->established = true;
1117 }
1118 else if (ip [3] == CMD_IP_REJ)
1119 {
1120 slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting."));
1121 abort ();
1122 }
1123 else
1124 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]);
1125 }
1126 else
1127 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"),
1128 ip [0], ip [1], ip [2], ip [3]);
1129
1130 return;
1131 }
1132
1133 int client, rseqno;
1134 decode_header (qname, client, rseqno);
1135
1136 if (client != THISNODE->id)
1137 {
1138 slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring"));
1139 datap = 0;
1140 }
1141 else if (rseqno != seqno)
1142 {
1143 slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?"));
1144 datap = 0;
1145 }
828 } 1146 }
829 } 1147 }
830 1148
831 // todo: pkt now used 1149 // todo: pkt now used
1150 if (datap)
832 c->dnsv4_receive_rep (new dns_rcv (seqno, data, datap - data)); 1151 dns->receive_rep (new dns_rcv (seqno, data, datap - data));
833 1152
834 break; 1153 break;
835 } 1154 }
836
837 delete pkt;
838} 1155}
839 1156
840void 1157void
841vpn::dnsv4_ev (io_watcher &w, short revents) 1158vpn::dnsv4_ev (io_watcher &w, short revents)
842{ 1159{
844 { 1161 {
845 dns_packet *pkt = new dns_packet; 1162 dns_packet *pkt = new dns_packet;
846 struct sockaddr_in sa; 1163 struct sockaddr_in sa;
847 socklen_t sa_len = sizeof (sa); 1164 socklen_t sa_len = sizeof (sa);
848 1165
849 pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 1166 pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
850 1167
851 if (pkt->len > 0) 1168 if (pkt->len > 0)
852 { 1169 {
853 if (pkt->flags & htons (FLAG_TC)) 1170 if (ntohs (pkt->flags) & FLAG_RESPONSE)
1171 dnsv4_client (*pkt);
1172 else
854 { 1173 {
855 slog (L_WARN, _("DNS request/response truncated, check protocol settings.")); 1174 dnsv4_server (*pkt);
856 //TODO connection reset 1175 sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len);
857 } 1176 }
858 1177
859 if (THISNODE->dns_port) 1178 delete pkt;
860 {
861 pkt = dnsv4_server (pkt);
862 sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len);
863 }
864 else
865 dnsv4_client (pkt);
866 } 1179 }
867 } 1180 }
868} 1181}
869 1182
870bool 1183bool
871connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1184vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
872{ 1185{
873 // never initialized 1186 int client = ntohl (si.host);
874 if (!dns_snddq && !dns_rcvdq)
875 {
876 dns_rcvdq = new byte_stream (MAX_BACKLOG * 2);
877 dns_snddq = new byte_stream (MAX_BACKLOG);
878 1187
879 //dns_rcvseq = dns_sndseq = 0; 1188 assert (0 < client && client <= conns.size ());
880 1189
881 dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); 1190 connection *c = conns [client - 1];
882 } 1191
1192 if (!c->dns)
1193 c->dns = new dns_connection (c);
883 1194
884 if (!dns_snddq->put (pkt)) 1195 if (c->dns->snddq.put (pkt))
885 return false; 1196 c->dns->tw.trigger ();
886 1197
887 // start timer if neccessary 1198 // always return true even if the buffer overflows
888 if (!THISNODE->dns_port && !dnsv4_tw.active)
889 dnsv4_cb (dnsv4_tw);
890
891 return true; 1199 return true;
892} 1200}
893 1201
894void 1202void
1203connection::dnsv4_reset_connection ()
1204{
1205 //delete dns; dns = 0; //TODO
1206}
1207
1208#define NEXT(w) do { if (next > (w)) next = w; } while (0)
1209
1210void
895connection::dnsv4_cb (time_watcher &w) 1211dns_connection::time_cb (time_watcher &w)
896{ 1212{
1213 // servers have to be polled
1214 if (THISNODE->dns_port)
1215 return;
1216
897 // check for timeouts and (re)transmit 1217 // check for timeouts and (re)transmit
898 tstamp next = NOW + 60; 1218 tstamp next = NOW + poll_interval;
899 dns_req *send = 0; 1219 dns_snd *send = 0;
900 1220
901 for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); 1221 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
902 i != vpn->dns_sndpq.end (); 1222 i != vpn->dns_sndpq.end ();
903 ++i) 1223 ++i)
904 { 1224 {
905 dns_req *r = *i; 1225 dns_snd *r = *i;
906 1226
907 if (r->next <= NOW) 1227 if (r->timeout <= NOW)
908 { 1228 {
909 if (!send) 1229 if (!send)
910 { 1230 {
911 send = r; 1231 send = r;
912 1232
913 if (r->retry)//D
914 printf ("req %d:%d, retry %d\n", r->seqno, r->pkt->id, r->retry);
915 r->retry++; 1233 r->retry++;
916 r->next = NOW + r->retry; 1234 r->timeout = NOW + (r->retry * min_latency * conf.dns_timeout_factor);
1235
1236 // the following code changes the query section a bit, forcing
1237 // the forwarder to generate a new request
1238 if (r->stdhdr)
1239 {
1240 //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry);
1241 //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry);
1242 }
917 } 1243 }
918 } 1244 }
919 1245 else
920 if (r->next < next) 1246 NEXT (r->timeout);
921 next = r->next;
922 } 1247 }
923 1248
924 if (!send 1249 if (!send)
925 && vpn->dns_sndpq.size () < MAX_OUTSTANDING)
926 { 1250 {
1251 // generate a new packet, if wise
1252
1253 if (!established)
1254 {
1255 if (vpn->dns_sndpq.empty ())
1256 {
927 send = new dns_req (this); 1257 send = new dns_snd (this);
1258
1259 cfg.reset (THISNODE->id);
1260 send->gen_syn_req ();
1261 }
1262 }
1263 else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding
1264 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
1265 {
1266 if (last_sent + send_interval <= NOW)
1267 {
1268 //printf ("sending data request etc.\n"); //D
1269 if (!snddq.empty () || last_received + 1. > NOW)
1270 {
1271 poll_interval = send_interval;
1272 NEXT (NOW + send_interval);
1273 }
1274
1275 send = new dns_snd (this);
928 send->gen_stream_req (dns_sndseq, dns_snddq); 1276 send->gen_stream_req (sndseq, snddq);
1277 send->timeout = NOW + min_latency * conf.dns_timeout_factor;
1278
1279 sndseq = (sndseq + 1) & SEQNO_MASK;
1280 }
1281 else
1282 NEXT (last_sent + send_interval);
1283 }
1284
1285 if (send)
929 vpn->dns_sndpq.push_back (send); 1286 vpn->dns_sndpq.push_back (send);
930
931 dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK;
932 } 1287 }
933
934 tstamp min_next = NOW + (1. / (tstamp)MAX_RATE);
935 1288
936 if (send) 1289 if (send)
937 { 1290 {
938 dns_packet *pkt = send->pkt; 1291 last_sent = NOW;
939 1292 sendto (vpn->dnsv4_fd,
940 next = min_next; 1293 send->pkt->at (0), send->pkt->len, 0,
941 1294 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
942 sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ());
943 } 1295 }
944 else if (next < min_next) 1296
945 next = min_next; 1297 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)",
1298 poll_interval, send_interval, next - NOW,
1299 vpn->dns_sndpq.size (), snddq.size (),
1300 rcvpq.size ());
1301
1302 // TODO: no idea when this happens, but when next < NOW, we have a problem
1303 // doesn't seem to happen anymore
1304 if (next < NOW + 0.001)
1305 next = NOW + 0.1;
946 1306
947 w.start (next); 1307 w.start (next);
948} 1308}
949 1309
950#endif 1310#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines