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.11 by pcg, Fri Mar 4 08:45:24 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 0.001 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_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 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 ();
714 if (r->datalen) 743 if (r->datalen)
715 { 744 {
716 last_received = NOW; 745 last_received = NOW;
717 tw.trigger (); 746 tw.trigger ();
718 747
719 poll_interval *= 0.99; 748 poll_interval = send_interval;
720 if (poll_interval > MIN_POLL_INTERVAL)
721 poll_interval = MIN_POLL_INTERVAL;
722 } 749 }
723 else 750 else
724 { 751 {
725 poll_interval *= 1.1; 752 poll_interval *= 1.5;
726 if (poll_interval > MAX_POLL_INTERVAL) 753 if (poll_interval > MAX_POLL_INTERVAL)
727 poll_interval = MAX_POLL_INTERVAL; 754 poll_interval = MAX_POLL_INTERVAL;
728 } 755 }
729 756
730 rcvpq.push_back (r); 757 rcvpq.push_back (r);
748 } 775 }
749 776
750 rcvseq = (rcvseq + 1) & SEQNO_MASK; 777 rcvseq = (rcvseq + 1) & SEQNO_MASK;
751 778
752 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)");
753 abort (); // MUST never overflow, can be caused by data corruption, TODO 782 abort (); // MUST never overflow, can be caused by data corruption, TODO
783 }
754 784
755 while (vpn_packet *pkt = rcvdq.get ()) 785 while (vpn_packet *pkt = rcvdq.get ())
756 { 786 {
757 sockinfo si; 787 sockinfo si;
758 si.host = 0; si.port = 0; si.prot = PROT_DNSv4; 788 si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4;
759 789
760 vpn->recv_vpn_packet (pkt, si); 790 vpn->recv_vpn_packet (pkt, si);
761 791
762 delete pkt; 792 delete pkt;
763 } 793 }
788 pkt.qdcount = htons (1); 818 pkt.qdcount = htons (1);
789 pkt.ancount = 0; 819 pkt.ancount = 0;
790 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
791 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
792 822
793 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); 823 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL);
794 824
795 int dlen = strlen (THISNODE->domain); 825 int dlen = strlen (THISNODE->domain);
796 826
797 if (qclass == RR_CLASS_IN 827 if (qclass == RR_CLASS_IN
798 && qlen > dlen + 1 828 && qlen > dlen + 1
817 if (0 < client && client <= conns.size ()) 847 if (0 < client && client <= conns.size ())
818 { 848 {
819 connection *c = conns [client - 1]; 849 connection *c = conns [client - 1];
820 dns_connection *dns = c->dns; 850 dns_connection *dns = c->dns;
821 dns_rcv *rcv; 851 dns_rcv *rcv;
852 bool in_seq;
822 853
823 if (dns) 854 if (dns)
824 { 855 {
825 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 (); )
826 if (SEQNO_EQ ((*i)->seqno, seqno)) 857 if (SEQNO_EQ ((*i)->seqno, seqno))
827 { 858 {
828 // already seen that request: simply reply with the cached reply 859 // already seen that request: simply reply with the cached reply
829 dns_rcv *r = *i; 860 dns_rcv *r = *i;
830 861
831 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;
832 867
833 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);
834 pkt.id = r->pkt->id; 869
835 goto duplicate_request; 870 goto duplicate_request;
836 } 871 }
872
873 in_seq = dns->rcvseq == seqno;
837 874
838 // new packet, queue 875 // new packet, queue
839 rcv = new dns_rcv (seqno, data, datalen); 876 rcv = new dns_rcv (seqno, data, datalen);
840 dns->receive_rep (rcv); 877 dns->receive_rep (rcv);
841 } 878 }
854 // 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
855 dlen -= qlen; 892 dlen -= qlen;
856 893
857 if (dns) 894 if (dns)
858 { 895 {
896 // only put data into in-order sequence packets, if
897 // we receive out-of-order packets we generate empty
898 // replies
859 while (dlen > 1 && !dns->snddq.empty ()) 899 while (dlen > 1 && !dns->snddq.empty () && in_seq)
860 { 900 {
861 int txtlen = dlen <= 255 ? dlen - 1 : 255; 901 int txtlen = dlen <= 255 ? dlen - 1 : 255;
862 902
863 if (txtlen > dns->snddq.size ()) 903 if (txtlen > dns->snddq.size ())
864 txtlen = dns->snddq.size (); 904 txtlen = dns->snddq.size ();
872 } 912 }
873 913
874 // avoid empty TXT rdata 914 // avoid empty TXT rdata
875 if (offs == rdlen_offs) 915 if (offs == rdlen_offs)
876 pkt[offs++] = 0; 916 pkt[offs++] = 0;
917
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 ())
953 i != dns_sndpq.end (); 995 i != dns_sndpq.end ();
954 ++i) 996 ++i)
955 if ((*i)->pkt->id == pkt.id) 997 if ((*i)->pkt->id == pkt.id)
956 { 998 {
957 dns_connection *dns = (*i)->dns; 999 dns_connection *dns = (*i)->dns;
1000 connection *c = dns->c;
958 int seqno = (*i)->seqno; 1001 int seqno = (*i)->seqno;
959 u8 data[MAXSIZE], *datap = data; 1002 u8 data[MAXSIZE], *datap = data;
960 1003
961 if ((*i)->retry) 1004 if ((*i)->retry)
962 { 1005 {
963 dns->send_interval *= 1.01; 1006 dns->send_interval *= 1.01;
964 if (dns->send_interval < MAX_SEND_INTERVAL) 1007 if (dns->send_interval > MAX_SEND_INTERVAL)
965 dns->send_interval = MAX_SEND_INTERVAL; 1008 dns->send_interval = MAX_SEND_INTERVAL;
966 } 1009 }
967 else 1010 else
968 { 1011 {
1012#if 1
969 dns->send_interval *= 0.99; 1013 dns->send_interval *= 0.999;
1014#endif
970 if (dns->send_interval < MIN_SEND_INTERVAL) 1015 if (dns->send_interval < MIN_SEND_INTERVAL)
971 dns->send_interval = MIN_SEND_INTERVAL; 1016 dns->send_interval = MIN_SEND_INTERVAL;
972 1017
973 // the latency surely puts an upper bound on 1018 // the latency surely puts an upper bound on
974 // the minimum send interval 1019 // the minimum send interval
1020 double latency = NOW - (*i)->sent;
1021 dns->last_latency = latency;
1022
975 if (dns->send_interval > NOW - (*i)->sent) 1023 if (dns->send_interval > latency)
976 dns->send_interval = NOW - (*i)->sent; 1024 dns->send_interval = latency;
977 } 1025 }
978 1026
979 delete *i; 1027 delete *i;
980 dns_sndpq.erase (i); 1028 dns_sndpq.erase (i);
981 1029
1031 1079
1032 if (ip [0] == CMD_IP_1 1080 if (ip [0] == CMD_IP_1
1033 && ip [1] == CMD_IP_2 1081 && ip [1] == CMD_IP_2
1034 && ip [2] == CMD_IP_3) 1082 && ip [2] == CMD_IP_3)
1035 { 1083 {
1036 slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]); 1084 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]);
1037 1085
1038 if (ip [3] == CMD_IP_RST) 1086 if (ip [3] == CMD_IP_RST)
1039 { 1087 {
1040 slog (L_DEBUG, _("got tunnel RST request")); 1088 slog (L_DEBUG, _("DNS: got tunnel RST request"));
1041 1089
1042 connection *c = dns->c;
1043 delete c->dns; c->dns = 0; 1090 delete dns; c->dns = 0;
1044 1091
1045 return; 1092 return;
1046 } 1093 }
1047 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."));
1048 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 }
1049 else 1104 else
1050 slog (L_INFO, _("got unknown meta command %02x"), ip [3]); 1105 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]);
1051 } 1106 }
1052 else 1107 else
1053 slog (L_INFO, _("got spurious a record %d.%d.%d.%d"), 1108 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"),
1054 ip [0], ip [1], ip [2], ip [3]); 1109 ip [0], ip [1], ip [2], ip [3]);
1055 1110
1056 return; 1111 return;
1057 } 1112 }
1058 1113
1059 int client, rseqno; 1114 int client, rseqno;
1060 decode_header (qname, client, rseqno); 1115 decode_header (qname, client, rseqno);
1061 1116
1062 if (client != THISNODE->id) 1117 if (client != THISNODE->id)
1063 { 1118 {
1064 slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); 1119 slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring"));
1065 datap = 0; 1120 datap = 0;
1066 } 1121 }
1067 else if (rseqno != seqno) 1122 else if (rseqno != seqno)
1068 { 1123 {
1069 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?"));
1070 datap = 0; 1125 datap = 0;
1071 } 1126 }
1072 } 1127 }
1073 } 1128 }
1074 1129
1105 } 1160 }
1106 } 1161 }
1107} 1162}
1108 1163
1109bool 1164bool
1110connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1165vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1111{ 1166{
1167 int client = ntohs (si.port);
1168
1169 assert (0 < client && client <= conns.size ());
1170
1171 connection *c = conns [client - 1];
1172
1112 if (!dns) 1173 if (!c->dns)
1113 dns = new dns_connection (this); 1174 c->dns = new dns_connection (c);
1114 1175
1115 if (!dns->snddq.put (pkt)) 1176 if (!c->dns->snddq.put (pkt))
1116 return false; 1177 return false;
1117 1178
1118 dns->tw.trigger (); 1179 c->dns->tw.trigger ();
1119 1180
1120 return true; 1181 return true;
1182}
1183
1184void
1185connection::dnsv4_reset_connection ()
1186{
1187 //delete dns; dns = 0; //TODO
1121} 1188}
1122 1189
1123#define NEXT(w) do { if (next > (w)) next = w; } while (0) 1190#define NEXT(w) do { if (next > (w)) next = w; } while (0)
1124 1191
1125void 1192void
1144 if (!send) 1211 if (!send)
1145 { 1212 {
1146 send = r; 1213 send = r;
1147 1214
1148 r->retry++; 1215 r->retry++;
1149 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 }
1150 } 1225 }
1151 } 1226 }
1152 else 1227 else
1153 NEXT (r->timeout); 1228 NEXT (r->timeout);
1154 } 1229 }
1163 { 1238 {
1164 if (vpn->dns_sndpq.empty ()) 1239 if (vpn->dns_sndpq.empty ())
1165 { 1240 {
1166 send = new dns_snd (this); 1241 send = new dns_snd (this);
1167 1242
1243 printf ("new conn %p %d\n", this, c->conf->id);//D
1168 cfg.reset (THISNODE->id); 1244 cfg.reset (THISNODE->id);
1169 send->gen_syn_req (cfg); 1245 send->gen_syn_req ();
1170 } 1246 }
1171 } 1247 }
1172 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)))
1173 { 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
1174 send = new dns_snd (this); 1258 send = new dns_snd (this);
1175 send->gen_stream_req (sndseq, snddq); 1259 send->gen_stream_req (sndseq, snddq);
1260 send->timeout = NOW + last_latency * 8.;
1176 1261
1177 sndseq = (sndseq + 1) & SEQNO_MASK; 1262 sndseq = (sndseq + 1) & SEQNO_MASK;
1178 } 1263 }
1179 1264
1180 if (send) 1265 if (send)
1182 } 1267 }
1183 1268
1184 if (send) 1269 if (send)
1185 { 1270 {
1186 last_sent = NOW; 1271 last_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 ());
1197 1283
1198 // 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
1199 if (next < NOW + 0.0001) 1285 if (next < NOW + 0.0001)
1200 next = NOW + 0.1; 1286 next = NOW + 0.1;
1201 1287

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines