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.15 by pcg, Fri Mar 4 10:15:45 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>
47 48
48#define MIN_POLL_INTERVAL .02 // how often to poll minimally when the server has 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. // retry timeouts 53#define INITIAL_TIMEOUT 0.1 // retry timeouts
53#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn 54#define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn
54 55
55#define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests 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 40 // max. outstanding requests 59#define MAX_OUTSTANDING 10 // max. outstanding requests
59#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING 60#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog
60#define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), 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 bytes less 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 slog (L_DEBUG, "DUPLICATE %d\n", htons (r->pkt->id)); 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 }
852 // 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
853 dlen -= qlen; 892 dlen -= qlen;
854 893
855 if (dns) 894 if (dns)
856 { 895 {
896 // only put data into in-order sequence packets, if
897 // we receive out-of-order packets we generate empty
898 // replies
857 while (dlen > 1 && !dns->snddq.empty ()) 899 while (dlen > 1 && !dns->snddq.empty () && in_seq)
858 { 900 {
859 int txtlen = dlen <= 255 ? dlen - 1 : 255; 901 int txtlen = dlen <= 255 ? dlen - 1 : 255;
860 902
861 if (txtlen > dns->snddq.size ()) 903 if (txtlen > dns->snddq.size ())
862 txtlen = dns->snddq.size (); 904 txtlen = dns->snddq.size ();
871 913
872 // avoid empty TXT rdata 914 // avoid empty TXT rdata
873 if (offs == rdlen_offs) 915 if (offs == rdlen_offs)
874 pkt[offs++] = 0; 916 pkt[offs++] = 0;
875 917
876 slog (L_NOISE, "snddq %d", dns->snddq.size ()); 918 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
877 } 919 }
878 else 920 else
879 { 921 {
880 // send RST 922 // send RST
881 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;
912 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
913 pkt [offs++] = 0; pkt [offs++] = 0; 955 pkt [offs++] = 0; pkt [offs++] = 0;
914 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL 956 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
915 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength 957 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
916 958
917 slog (L_INFO, _("DNS tunnel: client %d tries to connect"), client); 959 slog (L_INFO, _("DNS: client %d tries to connect"), client);
918 960
919 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;
920 pkt [offs++] = CMD_IP_REJ; 962 pkt [offs++] = CMD_IP_REJ;
921 963
922 if (0 < client && client <= conns.size ()) 964 if (0 < client && client <= conns.size ())
959 int seqno = (*i)->seqno; 1001 int seqno = (*i)->seqno;
960 u8 data[MAXSIZE], *datap = data; 1002 u8 data[MAXSIZE], *datap = data;
961 1003
962 if ((*i)->retry) 1004 if ((*i)->retry)
963 { 1005 {
964 dns->send_interval *= 1.001; 1006 dns->send_interval *= 1.01;
965 if (dns->send_interval > MAX_SEND_INTERVAL) 1007 if (dns->send_interval > MAX_SEND_INTERVAL)
966 dns->send_interval = MAX_SEND_INTERVAL; 1008 dns->send_interval = MAX_SEND_INTERVAL;
967 } 1009 }
968 else 1010 else
969 { 1011 {
970#if 1 1012#if 1
971 dns->send_interval *= 0.9999; 1013 dns->send_interval *= 0.999;
972#endif 1014#endif
973 if (dns->send_interval < MIN_SEND_INTERVAL) 1015 if (dns->send_interval < MIN_SEND_INTERVAL)
974 dns->send_interval = MIN_SEND_INTERVAL; 1016 dns->send_interval = MIN_SEND_INTERVAL;
975 1017
976 // the latency surely puts an upper bound on 1018 // the latency surely puts an upper bound on
977 // the minimum send interval 1019 // the minimum send interval
978 double latency = NOW - (*i)->sent; 1020 double latency = NOW - (*i)->sent;
1021 dns->last_latency = latency;
979 1022
980 if (dns->send_interval > latency) 1023 if (dns->send_interval > latency)
981 dns->send_interval = latency; 1024 dns->send_interval = latency;
982 } 1025 }
983 1026
1036 1079
1037 if (ip [0] == CMD_IP_1 1080 if (ip [0] == CMD_IP_1
1038 && ip [1] == CMD_IP_2 1081 && ip [1] == CMD_IP_2
1039 && ip [2] == CMD_IP_3) 1082 && ip [2] == CMD_IP_3)
1040 { 1083 {
1041 slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]); 1084 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]);
1042 1085
1043 if (ip [3] == CMD_IP_RST) 1086 if (ip [3] == CMD_IP_RST)
1044 { 1087 {
1045 slog (L_DEBUG, _("got tunnel RST request")); 1088 slog (L_DEBUG, _("DNS: got tunnel RST request"));
1046 1089
1047 delete dns; c->dns = 0; 1090 delete dns; c->dns = 0;
1048 1091
1049 return; 1092 return;
1050 } 1093 }
1051 else if (ip [3] == CMD_IP_SYN) 1094 else if (ip [3] == CMD_IP_SYN)
1052 { 1095 {
1053 slog (L_DEBUG, _("got tunnel SYN reply, server likes us.")); 1096 slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us."));
1054 dns->established = true; 1097 dns->established = true;
1055 } 1098 }
1056 else if (ip [3] == CMD_IP_REJ) 1099 else if (ip [3] == CMD_IP_REJ)
1057 { 1100 {
1058 slog (L_DEBUG, _("got tunnel REJ reply, server does not like us, aborting.")); 1101 slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting."));
1059 abort (); 1102 abort ();
1060 } 1103 }
1061 else 1104 else
1062 slog (L_INFO, _("got unknown meta command %02x"), ip [3]); 1105 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]);
1063 } 1106 }
1064 else 1107 else
1065 slog (L_INFO, _("got spurious a record %d.%d.%d.%d"), 1108 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"),
1066 ip [0], ip [1], ip [2], ip [3]); 1109 ip [0], ip [1], ip [2], ip [3]);
1067 1110
1068 return; 1111 return;
1069 } 1112 }
1070 1113
1071 int client, rseqno; 1114 int client, rseqno;
1072 decode_header (qname, client, rseqno); 1115 decode_header (qname, client, rseqno);
1073 1116
1074 if (client != THISNODE->id) 1117 if (client != THISNODE->id)
1075 { 1118 {
1076 slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); 1119 slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring"));
1077 datap = 0; 1120 datap = 0;
1078 } 1121 }
1079 else if (rseqno != seqno) 1122 else if (rseqno != seqno)
1080 { 1123 {
1081 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?"));
1082 datap = 0; 1125 datap = 0;
1083 } 1126 }
1084 } 1127 }
1085 } 1128 }
1086 1129
1117 } 1160 }
1118 } 1161 }
1119} 1162}
1120 1163
1121bool 1164bool
1122connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1165vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1123{ 1166{
1167 int client = ntohs (si.port);
1168
1169 assert (0 < client && client <= conns.size ());
1170
1171 connection *c = conns [client - 1];
1172
1124 if (!dns) 1173 if (!c->dns)
1125 dns = new dns_connection (this); 1174 c->dns = new dns_connection (c);
1126 1175
1127 if (!dns->snddq.put (pkt)) 1176 if (!c->dns->snddq.put (pkt))
1128 return false; 1177 return false;
1129 1178
1130 dns->tw.trigger (); 1179 c->dns->tw.trigger ();
1131 1180
1132 return true; 1181 return true;
1133} 1182}
1134 1183
1135void 1184void
1162 if (!send) 1211 if (!send)
1163 { 1212 {
1164 send = r; 1213 send = r;
1165 1214
1166 r->retry++; 1215 r->retry++;
1167 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 }
1168 } 1225 }
1169 } 1226 }
1170 else 1227 else
1171 NEXT (r->timeout); 1228 NEXT (r->timeout);
1172 } 1229 }
1181 { 1238 {
1182 if (vpn->dns_sndpq.empty ()) 1239 if (vpn->dns_sndpq.empty ())
1183 { 1240 {
1184 send = new dns_snd (this); 1241 send = new dns_snd (this);
1185 1242
1243 printf ("new conn %p %d\n", this, c->conf->id);//D
1186 cfg.reset (THISNODE->id); 1244 cfg.reset (THISNODE->id);
1187 send->gen_syn_req (cfg); 1245 send->gen_syn_req ();
1188 } 1246 }
1189 } 1247 }
1190 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)))
1191 { 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
1192 send = new dns_snd (this); 1258 send = new dns_snd (this);
1193 send->gen_stream_req (sndseq, snddq); 1259 send->gen_stream_req (sndseq, snddq);
1260 send->timeout = NOW + last_latency * 8.;
1194 1261
1195 sndseq = (sndseq + 1) & SEQNO_MASK; 1262 sndseq = (sndseq + 1) & SEQNO_MASK;
1196 } 1263 }
1197 1264
1198 if (send) 1265 if (send)
1208 } 1275 }
1209 } 1276 }
1210 else 1277 else
1211 NEXT (last_sent + send_interval); 1278 NEXT (last_sent + send_interval);
1212 1279
1213 slog (L_NOISE, "pi %f si %f N %f (%d:%d)", 1280 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)",
1214 poll_interval, send_interval, next - NOW, 1281 poll_interval, send_interval, next - NOW,
1215 vpn->dns_sndpq.size (), snddq.size ()); 1282 vpn->dns_sndpq.size (), snddq.size ());
1216 1283
1217 // TODO: no idea when this happens, but when next < NOW, we have a problem 1284 // TODO: no idea when this happens, but when next < NOW, we have a problem
1218 if (next < NOW + 0.0001) 1285 if (next < NOW + 0.0001)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines