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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines