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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines