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.12 by pcg, Fri Mar 4 09:36:45 2005 UTC vs.
Revision 1.21 by pcg, Sun Mar 6 21:33:40 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 .02 // 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 0.01 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 800 // 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_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
61 62
62#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
63// 240 leaves about 4 bytes of server reply data 64// 240 leaves about 4 bytes of server reply data
64// every two request byte sless give room for one reply byte 65// every two request bytes less give room for one reply byte
65 66
66#define SEQNO_MASK 0xffff 67#define SEQNO_MASK 0x3fff
67#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) 68#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) )
68 69
69#define MAX_LBL_SIZE 63 70#define MAX_LBL_SIZE 63
70#define MAX_PKT_SIZE 512 71#define MAX_PKT_SIZE 512
71 72
265 266
266///////////////////////////////////////////////////////////////////////////// 267/////////////////////////////////////////////////////////////////////////////
267 268
268#define HDRSIZE 6 269#define HDRSIZE 6
269 270
270inline void encode_header (char *data, int clientid, int seqno) 271inline void encode_header (char *data, int clientid, int seqno, int retry = 0)
271{ 272{
272 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 };
273 280
274 assert (clientid < 256); 281 assert (clientid < 256);
275 282
276 cdc26.encode (data, hdr, 3); 283 cdc26.encode (data, hdr, 3);
277} 284}
281 u8 hdr[3]; 288 u8 hdr[3];
282 289
283 cdc26.decode (hdr, data, HDRSIZE); 290 cdc26.decode (hdr, data, HDRSIZE);
284 291
285 clientid = hdr[0]; 292 clientid = hdr[0];
286 seqno = (hdr[1] << 8) | hdr[2]; 293 seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK;
287} 294}
288 295
289///////////////////////////////////////////////////////////////////////////// 296/////////////////////////////////////////////////////////////////////////////
290 297
291struct byte_stream 298struct byte_stream
320} 327}
321 328
322void byte_stream::remove (int count) 329void byte_stream::remove (int count)
323{ 330{
324 if (count > fill) 331 if (count > fill)
325 abort (); 332 assert (count <= fill);
326 333
327 memmove (data, data + count, fill -= count); 334 memmove (data, data + count, fill -= count);
328} 335}
329 336
330bool byte_stream::put (u8 *data, unsigned int datalen) 337bool byte_stream::put (u8 *data, unsigned int datalen)
350 return true; 357 return true;
351} 358}
352 359
353vpn_packet *byte_stream::get () 360vpn_packet *byte_stream::get ()
354{ 361{
362 unsigned int len;
363
364 for (;;)
365 {
355 unsigned int len = (data [0] << 8) | data [1]; 366 len = (data [0] << 8) | data [1];
356 367
357 if (len > MAXSIZE && fill >= 2) 368 if (len <= MAXSIZE || fill < 2)
358 abort (); // TODO handle this gracefully, connection reset 369 break;
359 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
360 if (fill < len + 2) 376 if (fill < len + 2)
361 return 0; 377 return 0;
362 378
363 vpn_packet *pkt = new vpn_packet; 379 vpn_packet *pkt = new vpn_packet;
364 380
422 438
423 version = 1; 439 version = 1;
424 440
425 rrtype = RR_TYPE_TXT; 441 rrtype = RR_TYPE_TXT;
426 flags = 0; 442 flags = 0;
427 def_ttl = 0; 443 def_ttl = 1;
428 rcv_cdc = 0; 444 rcv_cdc = 0;
429 snd_cdc = 62; 445 snd_cdc = 62;
430 max_size = ntohs (MAX_PKT_SIZE); 446 max_size = ntohs (MAX_PKT_SIZE);
431 client = ntohs (clientid); 447 client = ntohs (clientid);
432 uid = next_uid++; 448 uid = next_uid++;
492 return data - orig; 508 return data - orig;
493} 509}
494 510
495///////////////////////////////////////////////////////////////////////////// 511/////////////////////////////////////////////////////////////////////////////
496 512
497struct dns_snd
498{
499 dns_packet *pkt;
500 tstamp timeout, sent;
501 int retry;
502 struct dns_connection *dns;
503 int seqno;
504
505 void gen_stream_req (int seqno, byte_stream &stream);
506 void gen_syn_req (const dns_cfg &cfg);
507
508 dns_snd (dns_connection *dns);
509 ~dns_snd ();
510};
511
512static u16 dns_id = 12098; // TODO: should be per-vpn 513static u16 dns_id = 0; // TODO: should be per-vpn
513 514
514static u16 next_id () 515static u16 next_id ()
515{ 516{
517 if (!dns_id)
518 dns_id = time (0);
519
516 // the simplest lsfr with periodicity 65535 i could find 520 // the simplest lsfr with periodicity 65535 i could find
517 dns_id = (dns_id << 1) 521 dns_id = (dns_id << 1)
518 | (((dns_id >> 1) 522 | (((dns_id >> 1)
519 ^ (dns_id >> 2) 523 ^ (dns_id >> 2)
520 ^ (dns_id >> 4) 524 ^ (dns_id >> 4)
521 ^ (dns_id >> 15)) & 1); 525 ^ (dns_id >> 15)) & 1);
522 526
523 return dns_id; 527 return dns_id;
524} 528}
525 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
526dns_snd::dns_snd (dns_connection *dns) 575dns_snd::dns_snd (dns_connection *dns)
527: dns (dns) 576: dns (dns)
528{ 577{
529 timeout = 0; 578 timeout = 0;
530 retry = 0; 579 retry = 0;
531 seqno = 0; 580 seqno = 0;
532 sent = NOW; 581 sent = NOW;
582 stdhdr = false;
533 583
534 pkt = new dns_packet; 584 pkt = new dns_packet;
535 585
536 pkt->id = next_id (); 586 pkt->id = next_id ();
537} 587}
564 } 614 }
565} 615}
566 616
567void dns_snd::gen_stream_req (int seqno, byte_stream &stream) 617void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
568{ 618{
619 stdhdr = true;
569 this->seqno = seqno; 620 this->seqno = seqno;
570 621
571 timeout = NOW + INITIAL_TIMEOUT; 622 timeout = NOW + INITIAL_TIMEOUT;
572 623
573 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 624 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
574 pkt->qdcount = htons (1); 625 pkt->qdcount = htons (1);
575 626
576 int offs = 6*2; 627 int offs = 6*2;
577 int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); 628 int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2);
578 // 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,
579 // 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
580 631
581 char enc[256], *encp = enc; 632 char enc[256], *encp = enc;
582 encode_header (enc, THISNODE->id, seqno); 633 encode_header (enc, THISNODE->id, seqno);
600 encp += lbllen; 651 encp += lbllen;
601 652
602 enclen -= lbllen; 653 enclen -= lbllen;
603 } 654 }
604 655
605 append_domain (*pkt, offs, THISNODE->domain); 656 append_domain (*pkt, offs, dns->c->conf->domain);
606 657
607 (*pkt)[offs++] = 0; 658 (*pkt)[offs++] = 0;
608 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; 659 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY;
609 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 660 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
610 661
611 pkt->len = offs; 662 pkt->len = offs;
612} 663}
613 664
614void dns_snd::gen_syn_req (const dns_cfg &cfg) 665void dns_snd::gen_syn_req ()
615{ 666{
616 timeout = NOW + INITIAL_SYN_TIMEOUT; 667 timeout = NOW + INITIAL_SYN_TIMEOUT;
668
669 printf ("send syn\n");//D
617 670
618 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 671 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
619 pkt->qdcount = htons (1); 672 pkt->qdcount = htons (1);
620 673
621 int offs = 6*2; 674 int offs = 6 * 2;
622 675
623 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));
624 677
625 assert (elen <= MAX_LBL_SIZE); 678 assert (elen <= MAX_LBL_SIZE);
626 679
627 (*pkt)[offs] = elen; 680 (*pkt)[offs] = elen;
628 offs += elen + 1; 681 offs += elen + 1;
629 append_domain (*pkt, offs, THISNODE->domain); 682 append_domain (*pkt, offs, dns->c->conf->domain);
630 683
631 (*pkt)[offs++] = 0; 684 (*pkt)[offs++] = 0;
632 (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; 685 (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A;
633 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 686 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
634 687
657 delete pkt; 710 delete pkt;
658} 711}
659 712
660///////////////////////////////////////////////////////////////////////////// 713/////////////////////////////////////////////////////////////////////////////
661 714
662struct dns_connection
663{
664 connection *c;
665 struct vpn *vpn;
666
667 dns_cfg cfg;
668
669 bool established;
670
671 tstamp last_received;
672 tstamp last_sent;
673 double poll_interval, send_interval;
674
675 vector<dns_rcv *> rcvpq;
676
677 byte_stream rcvdq; int rcvseq;
678 byte_stream snddq; int sndseq;
679
680 void time_cb (time_watcher &w); time_watcher tw;
681 void receive_rep (dns_rcv *r);
682
683 dns_connection (connection *c);
684 ~dns_connection ();
685};
686
687dns_connection::dns_connection (connection *c) 715dns_connection::dns_connection (connection *c)
688: c (c) 716: c (c)
689, rcvdq (MAX_BACKLOG * 2) 717, rcvdq (MAX_BACKLOG * 2)
690, snddq (MAX_BACKLOG * 2) 718, snddq (MAX_BACKLOG * 2)
691, tw (this, &dns_connection::time_cb) 719, tw (this, &dns_connection::time_cb)
696 724
697 rcvseq = sndseq = 0; 725 rcvseq = sndseq = 0;
698 726
699 last_sent = last_received = 0; 727 last_sent = last_received = 0;
700 poll_interval = MIN_POLL_INTERVAL; 728 poll_interval = MIN_POLL_INTERVAL;
701 send_interval = 0.2; // starting rate 729 send_interval = 0.5; // starting rate
730 last_latency = INITIAL_TIMEOUT;
702} 731}
703 732
704dns_connection::~dns_connection () 733dns_connection::~dns_connection ()
705{ 734{
706 for (vector<dns_rcv *>::iterator i = rcvpq.begin (); 735 for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
718 747
719 poll_interval = send_interval; 748 poll_interval = send_interval;
720 } 749 }
721 else 750 else
722 { 751 {
723 poll_interval *= 1.1; 752 poll_interval *= 1.5;
724 if (poll_interval > MAX_POLL_INTERVAL) 753 if (poll_interval > MAX_POLL_INTERVAL)
725 poll_interval = MAX_POLL_INTERVAL; 754 poll_interval = MAX_POLL_INTERVAL;
726 } 755 }
727 756
728 rcvpq.push_back (r); 757 rcvpq.push_back (r);
746 } 775 }
747 776
748 rcvseq = (rcvseq + 1) & SEQNO_MASK; 777 rcvseq = (rcvseq + 1) & SEQNO_MASK;
749 778
750 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)");
751 abort (); // MUST never overflow, can be caused by data corruption, TODO 782 abort (); // MUST never overflow, can be caused by data corruption, TODO
783 }
752 784
753 while (vpn_packet *pkt = rcvdq.get ()) 785 while (vpn_packet *pkt = rcvdq.get ())
754 { 786 {
755 sockinfo si; 787 sockinfo si;
756 si.host = 0; si.port = 0; si.prot = PROT_DNSv4; 788 si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4;
757 789
758 vpn->recv_vpn_packet (pkt, si); 790 vpn->recv_vpn_packet (pkt, si);
759 791
760 delete pkt; 792 delete pkt;
761 } 793 }
786 pkt.qdcount = htons (1); 818 pkt.qdcount = htons (1);
787 pkt.ancount = 0; 819 pkt.ancount = 0;
788 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
789 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
790 822
791 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); 823 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL);
792 824
793 int dlen = strlen (THISNODE->domain); 825 int dlen = strlen (THISNODE->domain);
794 826
795 if (qclass == RR_CLASS_IN 827 if (qclass == RR_CLASS_IN
796 && qlen > dlen + 1 828 && qlen > dlen + 1
815 if (0 < client && client <= conns.size ()) 847 if (0 < client && client <= conns.size ())
816 { 848 {
817 connection *c = conns [client - 1]; 849 connection *c = conns [client - 1];
818 dns_connection *dns = c->dns; 850 dns_connection *dns = c->dns;
819 dns_rcv *rcv; 851 dns_rcv *rcv;
852 bool in_seq;
820 853
821 if (dns) 854 if (dns)
822 { 855 {
823 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 (); )
824 if (SEQNO_EQ ((*i)->seqno, seqno)) 857 if (SEQNO_EQ ((*i)->seqno, seqno))
825 { 858 {
826 // already seen that request: simply reply with the cached reply 859 // already seen that request: simply reply with the cached reply
827 dns_rcv *r = *i; 860 dns_rcv *r = *i;
828 861
829 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;
830 867
831 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);
832 pkt.id = r->pkt->id; 869
833 goto duplicate_request; 870 goto duplicate_request;
834 } 871 }
872
873 in_seq = dns->rcvseq == seqno;
835 874
836 // new packet, queue 875 // new packet, queue
837 rcv = new dns_rcv (seqno, data, datalen); 876 rcv = new dns_rcv (seqno, data, datalen);
838 dns->receive_rep (rcv); 877 dns->receive_rep (rcv);
839 } 878 }
840 879
880 {
841 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section 881 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
842 882
843 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; 883 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A;
844 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type 884 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type
845 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class 885 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
846 pkt [offs++] = 0; pkt [offs++] = 0; 886 pkt [offs++] = 0; pkt [offs++] = 0;
847 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL 887 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL
848 888
849 int rdlen_offs = offs += 2; 889 int rdlen_offs = offs += 2;
850 890
851 int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; 891 int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs;
852 // bind doesn't compress well, so reduce further by one label length 892 // bind doesn't compress well, so reduce further by one label length
853 dlen -= qlen; 893 dlen -= qlen;
854 894
855 if (dns) 895 if (dns)
856 { 896 {
897 // only put data into in-order sequence packets, if
898 // we receive out-of-order packets we generate empty
899 // replies
857 while (dlen > 1 && !dns->snddq.empty ()) 900 while (dlen > 1 && !dns->snddq.empty () && in_seq)
858 { 901 {
859 int txtlen = dlen <= 255 ? dlen - 1 : 255; 902 int txtlen = dlen <= 255 ? dlen - 1 : 255;
860 903
861 if (txtlen > dns->snddq.size ()) 904 if (txtlen > dns->snddq.size ())
862 txtlen = dns->snddq.size (); 905 txtlen = dns->snddq.size ();
863 906
864 pkt[offs++] = txtlen; 907 pkt[offs++] = txtlen;
865 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); 908 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen);
866 offs += txtlen; 909 offs += txtlen;
867 dns->snddq.remove (txtlen); 910 dns->snddq.remove (txtlen);
868 911
869 dlen -= txtlen + 1; 912 dlen -= txtlen + 1;
870 } 913 }
871 914
872 // avoid empty TXT rdata 915 // avoid empty TXT rdata
873 if (offs == rdlen_offs) 916 if (offs == rdlen_offs)
874 pkt[offs++] = 0; 917 pkt[offs++] = 0;
918
919 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
875 } 920 }
876 else 921 else
877 { 922 {
878 // send RST 923 // send RST
879 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; 924 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
880 pkt [offs++] = CMD_IP_RST; 925 pkt [offs++] = CMD_IP_RST;
881 } 926 }
882 927
883 int rdlen = offs - rdlen_offs; 928 int rdlen = offs - rdlen_offs;
884 929
885 pkt [rdlen_offs - 2] = rdlen >> 8; 930 pkt [rdlen_offs - 2] = rdlen >> 8;
886 pkt [rdlen_offs - 1] = rdlen; 931 pkt [rdlen_offs - 1] = rdlen;
887 932
888 if (dns) 933 if (dns)
889 { 934 {
890 // now update dns_rcv copy 935 // now update dns_rcv copy
891 rcv->pkt->len = offs; 936 rcv->pkt->len = offs;
892 memcpy (rcv->pkt->at (0), pkt.at (0), offs); 937 memcpy (rcv->pkt->at (0), pkt.at (0), offs);
893 } 938 }
939 }
894 940
895 duplicate_request: ; 941 duplicate_request: ;
896 } 942 }
897 else 943 else
898 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 944 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
910 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class 956 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
911 pkt [offs++] = 0; pkt [offs++] = 0; 957 pkt [offs++] = 0; pkt [offs++] = 0;
912 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL 958 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
913 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength 959 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
914 960
915 slog (L_INFO, _("DNS tunnel: client %d tries to connect"), client); 961 slog (L_INFO, _("DNS: client %d tries to connect"), client);
916 962
917 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; 963 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
918 pkt [offs++] = CMD_IP_REJ; 964 pkt [offs++] = CMD_IP_REJ;
919 965
920 if (0 < client && client <= conns.size ()) 966 if (0 < client && client <= conns.size ())
957 int seqno = (*i)->seqno; 1003 int seqno = (*i)->seqno;
958 u8 data[MAXSIZE], *datap = data; 1004 u8 data[MAXSIZE], *datap = data;
959 1005
960 if ((*i)->retry) 1006 if ((*i)->retry)
961 { 1007 {
962 dns->send_interval *= 1.001; 1008 dns->send_interval *= 1.01;
963 if (dns->send_interval > MAX_SEND_INTERVAL) 1009 if (dns->send_interval > MAX_SEND_INTERVAL)
964 dns->send_interval = MAX_SEND_INTERVAL; 1010 dns->send_interval = MAX_SEND_INTERVAL;
965 } 1011 }
966 else 1012 else
967 { 1013 {
968#if 1 1014#if 1
969 dns->send_interval *= 0.9999; 1015 dns->send_interval *= 0.999;
970#endif 1016#endif
971 if (dns->send_interval < MIN_SEND_INTERVAL) 1017 if (dns->send_interval < MIN_SEND_INTERVAL)
972 dns->send_interval = MIN_SEND_INTERVAL; 1018 dns->send_interval = MIN_SEND_INTERVAL;
973 1019
974 // the latency surely puts an upper bound on 1020 // the latency surely puts an upper bound on
975 // the minimum send interval 1021 // the minimum send interval
976 double latency = NOW - (*i)->sent; 1022 double latency = NOW - (*i)->sent;
1023 dns->last_latency = latency;
977 1024
978 if (dns->send_interval > latency) 1025 if (dns->send_interval > latency)
979 dns->send_interval = latency; 1026 dns->send_interval = latency;
980 } 1027 }
981 1028
1034 1081
1035 if (ip [0] == CMD_IP_1 1082 if (ip [0] == CMD_IP_1
1036 && ip [1] == CMD_IP_2 1083 && ip [1] == CMD_IP_2
1037 && ip [2] == CMD_IP_3) 1084 && ip [2] == CMD_IP_3)
1038 { 1085 {
1039 slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]); 1086 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]);
1040 1087
1041 if (ip [3] == CMD_IP_RST) 1088 if (ip [3] == CMD_IP_RST)
1042 { 1089 {
1043 slog (L_DEBUG, _("got tunnel RST request")); 1090 slog (L_DEBUG, _("DNS: got tunnel RST request"));
1044 1091
1045 delete dns; c->dns = 0; 1092 delete dns; c->dns = 0;
1046 1093
1047 return; 1094 return;
1048 } 1095 }
1049 else if (ip [3] == CMD_IP_SYN) 1096 else if (ip [3] == CMD_IP_SYN)
1050 { 1097 {
1051 slog (L_DEBUG, _("got tunnel SYN reply, server likes us.")); 1098 slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us."));
1052 dns->established = true; 1099 dns->established = true;
1053 } 1100 }
1054 else if (ip [3] == CMD_IP_REJ) 1101 else if (ip [3] == CMD_IP_REJ)
1055 { 1102 {
1056 slog (L_DEBUG, _("got tunnel REJ reply, server does not like us, aborting.")); 1103 slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting."));
1057 abort (); 1104 abort ();
1058 } 1105 }
1059 else 1106 else
1060 slog (L_INFO, _("got unknown meta command %02x"), ip [3]); 1107 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]);
1061 } 1108 }
1062 else 1109 else
1063 slog (L_INFO, _("got spurious a record %d.%d.%d.%d"), 1110 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"),
1064 ip [0], ip [1], ip [2], ip [3]); 1111 ip [0], ip [1], ip [2], ip [3]);
1065 1112
1066 return; 1113 return;
1067 } 1114 }
1068 1115
1069 int client, rseqno; 1116 int client, rseqno;
1070 decode_header (qname, client, rseqno); 1117 decode_header (qname, client, rseqno);
1071 1118
1072 if (client != THISNODE->id) 1119 if (client != THISNODE->id)
1073 { 1120 {
1074 slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); 1121 slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring"));
1075 datap = 0; 1122 datap = 0;
1076 } 1123 }
1077 else if (rseqno != seqno) 1124 else if (rseqno != seqno)
1078 { 1125 {
1079 slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?")); 1126 slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?"));
1080 datap = 0; 1127 datap = 0;
1081 } 1128 }
1082 } 1129 }
1083 } 1130 }
1084 1131
1115 } 1162 }
1116 } 1163 }
1117} 1164}
1118 1165
1119bool 1166bool
1120connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1167vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1121{ 1168{
1169 int client = ntohs (si.port);
1170
1171 assert (0 < client && client <= conns.size ());
1172
1173 connection *c = conns [client - 1];
1174
1122 if (!dns) 1175 if (!c->dns)
1123 dns = new dns_connection (this); 1176 c->dns = new dns_connection (c);
1124 1177
1125 if (!dns->snddq.put (pkt)) 1178 if (!c->dns->snddq.put (pkt))
1126 return false; 1179 return false;
1127 1180
1128 dns->tw.trigger (); 1181 c->dns->tw.trigger ();
1129 1182
1130 return true; 1183 return true;
1131} 1184}
1132 1185
1133void 1186void
1160 if (!send) 1213 if (!send)
1161 { 1214 {
1162 send = r; 1215 send = r;
1163 1216
1164 r->retry++; 1217 r->retry++;
1165 r->timeout = NOW + r->retry; 1218 r->timeout = NOW + (r->retry * last_latency * 8.);
1219
1220 // the following code changes the query section a bit, forcing
1221 // the forwarder to generate a new request
1222 if (r->stdhdr)
1223 {
1224 //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);
1225 //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry);
1226 }
1166 } 1227 }
1167 } 1228 }
1168 else 1229 else
1169 NEXT (r->timeout); 1230 NEXT (r->timeout);
1170 } 1231 }
1179 { 1240 {
1180 if (vpn->dns_sndpq.empty ()) 1241 if (vpn->dns_sndpq.empty ())
1181 { 1242 {
1182 send = new dns_snd (this); 1243 send = new dns_snd (this);
1183 1244
1245 printf ("new conn %p %d\n", this, c->conf->id);//D
1184 cfg.reset (THISNODE->id); 1246 cfg.reset (THISNODE->id);
1185 send->gen_syn_req (cfg); 1247 send->gen_syn_req ();
1186 } 1248 }
1187 } 1249 }
1188 else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING) 1250 else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING
1251 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
1189 { 1252 {
1253 //printf ("sending data request etc.\n"); //D
1254 if (!snddq.empty ())
1255 {
1256 poll_interval = send_interval;
1257 NEXT (NOW + send_interval);
1258 }
1259
1190 send = new dns_snd (this); 1260 send = new dns_snd (this);
1191 send->gen_stream_req (sndseq, snddq); 1261 send->gen_stream_req (sndseq, snddq);
1262 send->timeout = NOW + last_latency * 8.;
1192 1263
1193 sndseq = (sndseq + 1) & SEQNO_MASK; 1264 sndseq = (sndseq + 1) & SEQNO_MASK;
1194 } 1265 }
1195 1266
1196 if (send) 1267 if (send)
1206 } 1277 }
1207 } 1278 }
1208 else 1279 else
1209 NEXT (last_sent + send_interval); 1280 NEXT (last_sent + send_interval);
1210 1281
1211 slog (L_NOISE, "pi %f si %f N %f (%d:%d)", 1282 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)",
1212 poll_interval, send_interval, next - NOW, 1283 poll_interval, send_interval, next - NOW,
1213 vpn->dns_sndpq.size (), snddq.size ()); 1284 vpn->dns_sndpq.size (), snddq.size ());
1214 1285
1215 // TODO: no idea when this happens, but when next < NOW, we have a problem 1286 // TODO: no idea when this happens, but when next < NOW, we have a problem
1216 if (next < NOW + 0.0001) 1287 if (next < NOW + 0.0001)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines