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.10 by pcg, Fri Mar 4 08:15:04 2005 UTC vs.
Revision 1.20 by pcg, Sun Mar 6 21:32:15 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_POLL_INTERVAL .2 // how often to poll minimally when the server is having data 49#define MIN_POLL_INTERVAL .02 // how often to poll minimally when the server has data
49#define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data 50#define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data
50#define ACTIVITY_INTERVAL 5. 51#define ACTIVITY_INTERVAL 5.
51 52
52#define INITIAL_TIMEOUT 1. 53#define INITIAL_TIMEOUT 0.1 // retry timeouts
53#define INITIAL_SYN_TIMEOUT 2. 54#define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn
54 55
55#define MIN_SEND_INTERVAL (1./1000.) 56#define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests
56#define MAX_SEND_INTERVAL 0.5 // optimistic? 57#define MAX_SEND_INTERVAL 0.5 // optimistic?
57 58
58#define MAX_OUTSTANDING 400 // max. outstanding requests 59#define MAX_OUTSTANDING 10 // max. outstanding requests
59#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING 60#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog
60#define MAX_RATE 100 // requests/s
61#define MAX_BACKLOG (100*1024) // size of protocol backlog, must be > MAXSIZE 61#define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE
62 62
63#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
64// 240 leaves about 4 bytes of server reply data 64// 240 leaves about 4 bytes of server reply data
65// every two request byte sless give room for one reply byte 65// every two request bytes less give room for one reply byte
66 66
67#define SEQNO_MASK 0xffff 67#define SEQNO_MASK 0x3fff
68#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) 68#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) )
69 69
70#define MAX_LBL_SIZE 63 70#define MAX_LBL_SIZE 63
71#define MAX_PKT_SIZE 512 71#define MAX_PKT_SIZE 512
72 72
266 266
267///////////////////////////////////////////////////////////////////////////// 267/////////////////////////////////////////////////////////////////////////////
268 268
269#define HDRSIZE 6 269#define HDRSIZE 6
270 270
271inline void encode_header (char *data, int clientid, int seqno) 271inline void encode_header (char *data, int clientid, int seqno, int retry = 0)
272{ 272{
273 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 };
274 280
275 assert (clientid < 256); 281 assert (clientid < 256);
276 282
277 cdc26.encode (data, hdr, 3); 283 cdc26.encode (data, hdr, 3);
278} 284}
282 u8 hdr[3]; 288 u8 hdr[3];
283 289
284 cdc26.decode (hdr, data, HDRSIZE); 290 cdc26.decode (hdr, data, HDRSIZE);
285 291
286 clientid = hdr[0]; 292 clientid = hdr[0];
287 seqno = (hdr[1] << 8) | hdr[2]; 293 seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK;
288} 294}
289 295
290///////////////////////////////////////////////////////////////////////////// 296/////////////////////////////////////////////////////////////////////////////
291 297
292struct byte_stream 298struct byte_stream
321} 327}
322 328
323void byte_stream::remove (int count) 329void byte_stream::remove (int count)
324{ 330{
325 if (count > fill) 331 if (count > fill)
326 abort (); 332 assert (count <= fill);
327 333
328 memmove (data, data + count, fill -= count); 334 memmove (data, data + count, fill -= count);
329} 335}
330 336
331bool byte_stream::put (u8 *data, unsigned int datalen) 337bool byte_stream::put (u8 *data, unsigned int datalen)
351 return true; 357 return true;
352} 358}
353 359
354vpn_packet *byte_stream::get () 360vpn_packet *byte_stream::get ()
355{ 361{
362 unsigned int len;
363
364 for (;;)
365 {
356 int len = (data [0] << 8) | data [1]; 366 len = (data [0] << 8) | data [1];
357 367
358 if (len > MAXSIZE && fill >= 2) 368 if (len <= MAXSIZE || fill < 2)
359 abort (); // TODO handle this gracefully, connection reset 369 break;
360 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
361 if (fill < len + 2) 376 if (fill < len + 2)
362 return 0; 377 return 0;
363 378
364 vpn_packet *pkt = new vpn_packet; 379 vpn_packet *pkt = new vpn_packet;
365 380
423 438
424 version = 1; 439 version = 1;
425 440
426 rrtype = RR_TYPE_TXT; 441 rrtype = RR_TYPE_TXT;
427 flags = 0; 442 flags = 0;
428 def_ttl = 0; 443 def_ttl = 1;
429 rcv_cdc = 0; 444 rcv_cdc = 0;
430 snd_cdc = 62; 445 snd_cdc = 62;
431 max_size = ntohs (MAX_PKT_SIZE); 446 max_size = ntohs (MAX_PKT_SIZE);
432 client = ntohs (clientid); 447 client = ntohs (clientid);
433 uid = next_uid++; 448 uid = next_uid++;
493 return data - orig; 508 return data - orig;
494} 509}
495 510
496///////////////////////////////////////////////////////////////////////////// 511/////////////////////////////////////////////////////////////////////////////
497 512
498struct dns_snd
499{
500 dns_packet *pkt;
501 tstamp timeout, sent;
502 int retry;
503 struct dns_connection *dns;
504 int seqno;
505
506 dns_snd (dns_connection *dns);
507 void gen_stream_req (int seqno, byte_stream &stream);
508 void gen_syn_req (const dns_cfg &cfg);
509};
510
511static u16 dns_id = 12098; // TODO: should be per-vpn 513static u16 dns_id = 0; // TODO: should be per-vpn
512 514
513static u16 next_id () 515static u16 next_id ()
514{ 516{
517 if (!dns_id)
518 dns_id = time (0);
519
515 // the simplest lsfr with periodicity 65535 i could find 520 // the simplest lsfr with periodicity 65535 i could find
516 dns_id = (dns_id << 1) 521 dns_id = (dns_id << 1)
517 | (((dns_id >> 1) 522 | (((dns_id >> 1)
518 ^ (dns_id >> 2) 523 ^ (dns_id >> 2)
519 ^ (dns_id >> 4) 524 ^ (dns_id >> 4)
520 ^ (dns_id >> 15)) & 1); 525 ^ (dns_id >> 15)) & 1);
521 526
522 return dns_id; 527 return dns_id;
523} 528}
524 529
530struct dns_rcv;
531struct dns_snd;
532
533struct dns_connection
534{
535 connection *c;
536 struct vpn *vpn;
537
538 dns_cfg cfg;
539
540 bool established;
541
542 tstamp last_received;
543 tstamp last_sent;
544 double last_latency;
545 double poll_interval, send_interval;
546
547 vector<dns_rcv *> rcvpq;
548
549 byte_stream rcvdq; int rcvseq;
550 byte_stream snddq; int sndseq;
551
552 void time_cb (time_watcher &w); time_watcher tw;
553 void receive_rep (dns_rcv *r);
554
555 dns_connection (connection *c);
556 ~dns_connection ();
557};
558
559struct dns_snd
560{
561 dns_packet *pkt;
562 tstamp timeout, sent;
563 int retry;
564 struct dns_connection *dns;
565 int seqno;
566 bool stdhdr;
567
568 void gen_stream_req (int seqno, byte_stream &stream);
569 void gen_syn_req ();
570
571 dns_snd (dns_connection *dns);
572 ~dns_snd ();
573};
574
525dns_snd::dns_snd (dns_connection *dns) 575dns_snd::dns_snd (dns_connection *dns)
526: dns (dns) 576: dns (dns)
527{ 577{
528 timeout = 0; 578 timeout = 0;
529 retry = 0; 579 retry = 0;
530 seqno = 0; 580 seqno = 0;
581 sent = NOW;
582 stdhdr = false;
531 583
532 pkt = new dns_packet; 584 pkt = new dns_packet;
533 585
534 pkt->id = next_id (); 586 pkt->id = next_id ();
587}
588
589dns_snd::~dns_snd ()
590{
591 delete pkt;
535} 592}
536 593
537static void append_domain (dns_packet &pkt, int &offs, const char *domain) 594static void append_domain (dns_packet &pkt, int &offs, const char *domain)
538{ 595{
539 // add tunnel domain 596 // add tunnel domain
557 } 614 }
558} 615}
559 616
560void dns_snd::gen_stream_req (int seqno, byte_stream &stream) 617void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
561{ 618{
619 stdhdr = true;
562 this->seqno = seqno; 620 this->seqno = seqno;
563 621
564 timeout = NOW + INITIAL_TIMEOUT; 622 timeout = NOW + INITIAL_TIMEOUT;
565 623
566 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 624 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
567 pkt->qdcount = htons (1); 625 pkt->qdcount = htons (1);
568 626
569 int offs = 6*2; 627 int offs = 6*2;
570 int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); 628 int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2);
571 // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, 629 // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well,
572 // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra 630 // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra
573 631
574 char enc[256], *encp = enc; 632 char enc[256], *encp = enc;
575 encode_header (enc, THISNODE->id, seqno); 633 encode_header (enc, THISNODE->id, seqno);
593 encp += lbllen; 651 encp += lbllen;
594 652
595 enclen -= lbllen; 653 enclen -= lbllen;
596 } 654 }
597 655
598 append_domain (*pkt, offs, THISNODE->domain); 656 append_domain (*pkt, offs, dns->c->conf->domain);
599 657
600 (*pkt)[offs++] = 0; 658 (*pkt)[offs++] = 0;
601 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; 659 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY;
602 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 660 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
603 661
604 pkt->len = offs; 662 pkt->len = offs;
605} 663}
606 664
607void dns_snd::gen_syn_req (const dns_cfg &cfg) 665void dns_snd::gen_syn_req ()
608{ 666{
609 timeout = NOW + INITIAL_SYN_TIMEOUT; 667 timeout = NOW + INITIAL_SYN_TIMEOUT;
668
669 printf ("send syn\n");//D
610 670
611 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 671 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
612 pkt->qdcount = htons (1); 672 pkt->qdcount = htons (1);
613 673
614 int offs = 6*2; 674 int offs = 6 * 2;
615 675
616 int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg)); 676 int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg));
617 677
618 assert (elen <= MAX_LBL_SIZE); 678 assert (elen <= MAX_LBL_SIZE);
619 679
620 (*pkt)[offs] = elen; 680 (*pkt)[offs] = elen;
621 offs += elen + 1; 681 offs += elen + 1;
622 append_domain (*pkt, offs, THISNODE->domain); 682 append_domain (*pkt, offs, dns->c->conf->domain);
623 683
624 (*pkt)[offs++] = 0; 684 (*pkt)[offs++] = 0;
625 (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; 685 (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A;
626 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 686 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
627 687
650 delete pkt; 710 delete pkt;
651} 711}
652 712
653///////////////////////////////////////////////////////////////////////////// 713/////////////////////////////////////////////////////////////////////////////
654 714
655struct dns_connection
656{
657 connection *c;
658 struct vpn *vpn;
659
660 dns_cfg cfg;
661
662 bool established;
663
664 tstamp last_received;
665 tstamp last_sent;
666 double poll_interval, send_interval;
667
668 vector<dns_rcv *> rcvpq;
669
670 byte_stream rcvdq; int rcvseq;
671 byte_stream snddq; int sndseq;
672
673 void time_cb (time_watcher &w); time_watcher tw;
674 void receive_rep (dns_rcv *r);
675
676 dns_connection (connection *c);
677 ~dns_connection ();
678};
679
680dns_connection::dns_connection (connection *c) 715dns_connection::dns_connection (connection *c)
681: c (c) 716: c (c)
682, rcvdq (MAX_BACKLOG * 2) 717, rcvdq (MAX_BACKLOG * 2)
683, snddq (MAX_BACKLOG * 2) 718, snddq (MAX_BACKLOG * 2)
684, tw (this, &dns_connection::time_cb) 719, tw (this, &dns_connection::time_cb)
689 724
690 rcvseq = sndseq = 0; 725 rcvseq = sndseq = 0;
691 726
692 last_sent = last_received = 0; 727 last_sent = last_received = 0;
693 poll_interval = MIN_POLL_INTERVAL; 728 poll_interval = MIN_POLL_INTERVAL;
694 send_interval = 0.2; // starting rate 729 send_interval = 0.5; // starting rate
730 last_latency = INITIAL_TIMEOUT;
695} 731}
696 732
697dns_connection::~dns_connection () 733dns_connection::~dns_connection ()
698{ 734{
699 for (vector<dns_rcv *>::iterator i = rcvpq.begin (); 735 for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
707 if (r->datalen) 743 if (r->datalen)
708 { 744 {
709 last_received = NOW; 745 last_received = NOW;
710 tw.trigger (); 746 tw.trigger ();
711 747
712 poll_interval *= 0.99; 748 poll_interval = send_interval;
713 if (poll_interval > MIN_POLL_INTERVAL)
714 poll_interval = MIN_POLL_INTERVAL;
715 } 749 }
716 else 750 else
717 { 751 {
718 poll_interval *= 1.1; 752 poll_interval *= 1.5;
719 if (poll_interval > MAX_POLL_INTERVAL) 753 if (poll_interval > MAX_POLL_INTERVAL)
720 poll_interval = MAX_POLL_INTERVAL; 754 poll_interval = MAX_POLL_INTERVAL;
721 } 755 }
722 756
723 rcvpq.push_back (r); 757 rcvpq.push_back (r);
741 } 775 }
742 776
743 rcvseq = (rcvseq + 1) & SEQNO_MASK; 777 rcvseq = (rcvseq + 1) & SEQNO_MASK;
744 778
745 if (!rcvdq.put (r->data, r->datalen)) 779 if (!rcvdq.put (r->data, r->datalen))
780 {
781 slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)");
746 abort (); // MUST never overflow, can be caused by data corruption, TODO 782 abort (); // MUST never overflow, can be caused by data corruption, TODO
783 }
747 784
748 while (vpn_packet *pkt = rcvdq.get ()) 785 while (vpn_packet *pkt = rcvdq.get ())
749 { 786 {
750 sockinfo si; 787 sockinfo si;
751 si.host = 0; si.port = 0; si.prot = PROT_DNSv4; 788 si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4;
752 789
753 vpn->recv_vpn_packet (pkt, si); 790 vpn->recv_vpn_packet (pkt, si);
791
792 delete pkt;
754 } 793 }
755 794
756 // check for further packets 795 // check for further packets
757 goto redo; 796 goto redo;
758 } 797 }
779 pkt.qdcount = htons (1); 818 pkt.qdcount = htons (1);
780 pkt.ancount = 0; 819 pkt.ancount = 0;
781 pkt.nscount = 0; // should be self, as other nameservers reply like this 820 pkt.nscount = 0; // should be self, as other nameservers reply like this
782 pkt.arcount = 0; // a record for self, as other nameservers reply like this 821 pkt.arcount = 0; // a record for self, as other nameservers reply like this
783 822
784 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); 823 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL);
785 824
786 int dlen = strlen (THISNODE->domain); 825 int dlen = strlen (THISNODE->domain);
787 826
788 if (qclass == RR_CLASS_IN 827 if (qclass == RR_CLASS_IN
789 && qlen > dlen + 1 828 && qlen > dlen + 1
808 if (0 < client && client <= conns.size ()) 847 if (0 < client && client <= conns.size ())
809 { 848 {
810 connection *c = conns [client - 1]; 849 connection *c = conns [client - 1];
811 dns_connection *dns = c->dns; 850 dns_connection *dns = c->dns;
812 dns_rcv *rcv; 851 dns_rcv *rcv;
852 bool in_seq;
813 853
814 if (dns) 854 if (dns)
815 { 855 {
816 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) 856 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); )
817 if (SEQNO_EQ ((*i)->seqno, seqno)) 857 if (SEQNO_EQ ((*i)->seqno, seqno))
818 { 858 {
819 // already seen that request: simply reply with the cached reply 859 // already seen that request: simply reply with the cached reply
820 dns_rcv *r = *i; 860 dns_rcv *r = *i;
821 861
822 printf ("DUPLICATE %d\n", htons (r->pkt->id));//D 862 slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno);
863
864 // refresh header & id, as the retry count could have changed
865 memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE);
866 r->pkt->id = pkt.id;
823 867
824 memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); 868 memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len);
825 pkt.id = r->pkt->id; 869
826 goto duplicate_request; 870 goto duplicate_request;
827 } 871 }
872
873 in_seq = dns->rcvseq == seqno;
828 874
829 // new packet, queue 875 // new packet, queue
830 rcv = new dns_rcv (seqno, data, datalen); 876 rcv = new dns_rcv (seqno, data, datalen);
831 dns->receive_rep (rcv); 877 dns->receive_rep (rcv);
832 } 878 }
833 879
834 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section 880 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
835 881
836 // type
837 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; 882 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A;
838 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; 883 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type
839
840 // class
841 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; 884 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
842
843 // TTL
844 pkt [offs++] = 0; pkt [offs++] = 0; 885 pkt [offs++] = 0; pkt [offs++] = 0;
845 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; 886 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL
846 887
847 int rdlen_offs = offs += 2; 888 int rdlen_offs = offs += 2;
848 889
849 int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; 890 int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs;
850 // bind doesn't compress well, so reduce further by one label length 891 // bind doesn't compress well, so reduce further by one label length
851 dlen -= qlen; 892 dlen -= qlen;
852 893
853 if (dns) 894 if (dns)
854 { 895 {
896 // only put data into in-order sequence packets, if
897 // we receive out-of-order packets we generate empty
898 // replies
855 while (dlen > 1 && !dns->snddq.empty ()) 899 while (dlen > 1 && !dns->snddq.empty () && in_seq)
856 { 900 {
857 int txtlen = dlen <= 255 ? dlen - 1 : 255; 901 int txtlen = dlen <= 255 ? dlen - 1 : 255;
858 902
859 if (txtlen > dns->snddq.size ()) 903 if (txtlen > dns->snddq.size ())
860 txtlen = dns->snddq.size (); 904 txtlen = dns->snddq.size ();
868 } 912 }
869 913
870 // avoid empty TXT rdata 914 // avoid empty TXT rdata
871 if (offs == rdlen_offs) 915 if (offs == rdlen_offs)
872 pkt[offs++] = 0; 916 pkt[offs++] = 0;
917
918 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
873 } 919 }
874 else 920 else
875 { 921 {
876 // send RST 922 // send RST
877 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; 923 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
908 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class 954 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
909 pkt [offs++] = 0; pkt [offs++] = 0; 955 pkt [offs++] = 0; pkt [offs++] = 0;
910 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL 956 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
911 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength 957 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
912 958
913 slog (L_INFO, _("DNS tunnel: client %d tries to connect"), client); 959 slog (L_INFO, _("DNS: client %d tries to connect"), client);
914 960
915 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; 961 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
916 pkt [offs++] = CMD_IP_REJ; 962 pkt [offs++] = CMD_IP_REJ;
917 963
918 if (0 < client && client <= conns.size ()) 964 if (0 < client && client <= conns.size ())
949 i != dns_sndpq.end (); 995 i != dns_sndpq.end ();
950 ++i) 996 ++i)
951 if ((*i)->pkt->id == pkt.id) 997 if ((*i)->pkt->id == pkt.id)
952 { 998 {
953 dns_connection *dns = (*i)->dns; 999 dns_connection *dns = (*i)->dns;
1000 connection *c = dns->c;
954 int seqno = (*i)->seqno; 1001 int seqno = (*i)->seqno;
955 u8 data[MAXSIZE], *datap = data; 1002 u8 data[MAXSIZE], *datap = data;
956 1003
957 if ((*i)->retry) 1004 if ((*i)->retry)
958 { 1005 {
959 dns->send_interval *= 1.01; 1006 dns->send_interval *= 1.01;
960 if (dns->send_interval < MAX_SEND_INTERVAL) 1007 if (dns->send_interval > MAX_SEND_INTERVAL)
961 dns->send_interval = MAX_SEND_INTERVAL; 1008 dns->send_interval = MAX_SEND_INTERVAL;
962 } 1009 }
963 else 1010 else
964 { 1011 {
1012#if 1
965 dns->send_interval *= 0.99; 1013 dns->send_interval *= 0.999;
1014#endif
966 if (dns->send_interval < MIN_SEND_INTERVAL) 1015 if (dns->send_interval < MIN_SEND_INTERVAL)
967 dns->send_interval = MIN_SEND_INTERVAL; 1016 dns->send_interval = MIN_SEND_INTERVAL;
968 1017
969 // the latency surely puts an upper bound on 1018 // the latency surely puts an upper bound on
970 // the minimum send interval 1019 // the minimum send interval
1020 double latency = NOW - (*i)->sent;
1021 dns->last_latency = latency;
1022
971 if (dns->send_interval > NOW - (*i)->sent) 1023 if (dns->send_interval > latency)
972 dns->send_interval = NOW - (*i)->sent; 1024 dns->send_interval = latency;
973 } 1025 }
974 1026
975 delete *i; 1027 delete *i;
976 dns_sndpq.erase (i); 1028 dns_sndpq.erase (i);
977 1029
1027 1079
1028 if (ip [0] == CMD_IP_1 1080 if (ip [0] == CMD_IP_1
1029 && ip [1] == CMD_IP_2 1081 && ip [1] == CMD_IP_2
1030 && ip [2] == CMD_IP_3) 1082 && ip [2] == CMD_IP_3)
1031 { 1083 {
1032 slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]); 1084 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]);
1033 1085
1034 if (ip [3] == CMD_IP_RST) 1086 if (ip [3] == CMD_IP_RST)
1035 { 1087 {
1036 slog (L_DEBUG, _("got tunnel RST request")); 1088 slog (L_DEBUG, _("DNS: got tunnel RST request"));
1037 1089
1038 connection *c = dns->c;
1039 delete c->dns; c->dns = 0; 1090 delete dns; c->dns = 0;
1040 1091
1041 return; 1092 return;
1042 } 1093 }
1043 else if (ip [3] == CMD_IP_SYN) 1094 else if (ip [3] == CMD_IP_SYN)
1095 {
1096 slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us."));
1044 dns->established = true; 1097 dns->established = true;
1098 }
1099 else if (ip [3] == CMD_IP_REJ)
1100 {
1101 slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting."));
1102 abort ();
1103 }
1045 else 1104 else
1046 slog (L_INFO, _("got unknown meta command %02x"), ip [3]); 1105 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]);
1047 } 1106 }
1048 else 1107 else
1049 slog (L_INFO, _("got spurious a record %d.%d.%d.%d"), 1108 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"),
1050 ip [0], ip [1], ip [2], ip [3]); 1109 ip [0], ip [1], ip [2], ip [3]);
1051 1110
1052 return; 1111 return;
1053 } 1112 }
1054 1113
1055 int client, rseqno; 1114 int client, rseqno;
1056 decode_header (qname, client, rseqno); 1115 decode_header (qname, client, rseqno);
1057 1116
1058 if (client != THISNODE->id) 1117 if (client != THISNODE->id)
1059 { 1118 {
1060 slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); 1119 slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring"));
1061 datap = 0; 1120 datap = 0;
1062 } 1121 }
1063 else if (rseqno != seqno) 1122 else if (rseqno != seqno)
1064 { 1123 {
1065 slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?")); 1124 slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?"));
1066 datap = 0; 1125 datap = 0;
1067 } 1126 }
1068 } 1127 }
1069 } 1128 }
1070 1129
1101 } 1160 }
1102 } 1161 }
1103} 1162}
1104 1163
1105bool 1164bool
1106connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1165vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1107{ 1166{
1167 int client = ntohs (si.port);
1168
1169 assert (0 < client && client <= conns.size ());
1170
1171 connection *c = conns [client - 1];
1172
1108 if (!dns) 1173 if (!c->dns)
1109 dns = new dns_connection (this); 1174 c->dns = new dns_connection (c);
1110 1175
1111 if (!dns->snddq.put (pkt)) 1176 if (!c->dns->snddq.put (pkt))
1112 return false; 1177 return false;
1113 1178
1114 dns->tw.trigger (); 1179 c->dns->tw.trigger ();
1115 1180
1116 return true; 1181 return true;
1182}
1183
1184void
1185connection::dnsv4_reset_connection ()
1186{
1187 //delete dns; dns = 0; //TODO
1117} 1188}
1118 1189
1119#define NEXT(w) do { if (next > (w)) next = w; } while (0) 1190#define NEXT(w) do { if (next > (w)) next = w; } while (0)
1120 1191
1121void 1192void
1140 if (!send) 1211 if (!send)
1141 { 1212 {
1142 send = r; 1213 send = r;
1143 1214
1144 r->retry++; 1215 r->retry++;
1145 r->timeout = NOW + r->retry; 1216 r->timeout = NOW + (r->retry * last_latency * 8.);
1217
1218 // the following code changes the query section a bit, forcing
1219 // the forwarder to generate a new request
1220 if (r->stdhdr)
1221 {
1222 //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);
1223 //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry);
1224 }
1146 } 1225 }
1147 } 1226 }
1148 else if (r->timeout < next) 1227 else
1149 NEXT (r->timeout); 1228 NEXT (r->timeout);
1150 } 1229 }
1151 1230
1152 if (last_sent + send_interval <= NOW) 1231 if (last_sent + send_interval <= NOW)
1153 { 1232 {
1159 { 1238 {
1160 if (vpn->dns_sndpq.empty ()) 1239 if (vpn->dns_sndpq.empty ())
1161 { 1240 {
1162 send = new dns_snd (this); 1241 send = new dns_snd (this);
1163 1242
1243 printf ("new conn %p %d\n", this, c->conf->id);//D
1164 cfg.reset (THISNODE->id); 1244 cfg.reset (THISNODE->id);
1165 send->gen_syn_req (cfg); 1245 send->gen_syn_req ();
1166 } 1246 }
1167 } 1247 }
1168 else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING) 1248 else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING
1249 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
1169 { 1250 {
1251 //printf ("sending data request etc.\n"); //D
1252 if (!snddq.empty ())
1253 {
1254 poll_interval = send_interval;
1255 NEXT (NOW + send_interval);
1256 }
1257
1170 send = new dns_snd (this); 1258 send = new dns_snd (this);
1171 send->gen_stream_req (sndseq, snddq); 1259 send->gen_stream_req (sndseq, snddq);
1260 send->timeout = NOW + last_latency * 8.;
1172 1261
1173 sndseq = (sndseq + 1) & SEQNO_MASK; 1262 sndseq = (sndseq + 1) & SEQNO_MASK;
1174 } 1263 }
1175 1264
1176 if (send) 1265 if (send)
1177 vpn->dns_sndpq.push_back (send); 1266 vpn->dns_sndpq.push_back (send);
1178 } 1267 }
1179 1268
1180 if (send) 1269 if (send)
1181 { 1270 {
1182 printf ("send pkt\n");
1183 last_sent = NOW; 1271 last_sent = NOW;
1184
1185 if (!send->retry)
1186 send->sent = NOW;
1187
1188 sendto (vpn->dnsv4_fd, 1272 sendto (vpn->dnsv4_fd,
1189 send->pkt->at (0), send->pkt->len, 0, 1273 send->pkt->at (0), send->pkt->len, 0,
1190 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); 1274 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
1191 } 1275 }
1192 } 1276 }
1193 else 1277 else
1194 NEXT (last_sent + send_interval); 1278 NEXT (last_sent + send_interval);
1195 1279
1196 printf ("pi %f si %f N %f (%d:%d)\n", poll_interval, send_interval, next - NOW, vpn->dns_sndpq.size (), snddq.size ()); 1280 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)",
1281 poll_interval, send_interval, next - NOW,
1282 vpn->dns_sndpq.size (), snddq.size ());
1283
1284 // TODO: no idea when this happens, but when next < NOW, we have a problem
1285 if (next < NOW + 0.0001)
1286 next = NOW + 0.1;
1197 1287
1198 w.start (next); 1288 w.start (next);
1199} 1289}
1200 1290
1201#endif 1291#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines