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.19 by pcg, Sun Mar 6 18:34:46 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines