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.9 by pcg, Fri Mar 4 04:52:38 2005 UTC vs.
Revision 1.24 by pcg, Mon Mar 7 01:31:26 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines