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.20 by pcg, Sun Mar 6 21:32:15 2005 UTC vs.
Revision 1.46 by pcg, Thu Dec 6 00:35:29 2007 UTC

14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 GNU General Public License for more details.
16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with gvpe; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/ 20*/
21
22// TODO: EDNS0 option to increase dns mtu?
23// TODO: re-write dns packet parsing/creation using a safe mem-buffer
24// to ensure no buffer overflows or similar problems.
21 25
22#include "config.h" 26#include "config.h"
23 27
24#if ENABLE_DNS 28#if ENABLE_DNS
25 29
38#include <unistd.h> 42#include <unistd.h>
39#include <fcntl.h> 43#include <fcntl.h>
40 44
41#include <map> 45#include <map>
42 46
47#include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */
43#include <gmp.h> 48#include <gmp.h>
44 49
45#include "netcompat.h" 50#include "netcompat.h"
46 51
47#include "vpn.h" 52#include "vpn.h"
48 53
49#define MIN_POLL_INTERVAL .02 // how often to poll minimally when the server has data
50#define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data 54#define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data
51#define ACTIVITY_INTERVAL 5. 55#define ACTIVITY_INTERVAL 5.
52 56
53#define INITIAL_TIMEOUT 0.1 // retry timeouts 57#define INITIAL_TIMEOUT 0.1 // retry timeouts
54#define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn 58#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn
55 59
56#define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests
57#define MAX_SEND_INTERVAL 0.5 // optimistic? 60#define MAX_SEND_INTERVAL 2. // optimistic?
58 61
59#define MAX_OUTSTANDING 10 // max. outstanding requests
60#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog 62#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog
61#define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE 63#define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE
62 64
63#define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well 65#define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well
64// 240 leaves about 4 bytes of server reply data 66// 240 leaves about 4 bytes of server reply data
65// every two request bytes less give room for one reply byte 67// every request byte less give room for two reply bytes
66 68
67#define SEQNO_MASK 0x3fff 69#define SEQNO_MASK 0x3fff
68#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) 70#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) )
69 71
70#define MAX_LBL_SIZE 63 72#define MAX_LBL_SIZE 63
265static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); 267static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO");
266 268
267///////////////////////////////////////////////////////////////////////////// 269/////////////////////////////////////////////////////////////////////////////
268 270
269#define HDRSIZE 6 271#define HDRSIZE 6
270 272
271inline void encode_header (char *data, int clientid, int seqno, int retry = 0) 273inline void encode_header (char *data, int clientid, int seqno, int retry = 0)
272{ 274{
273 seqno &= SEQNO_MASK; 275 seqno &= SEQNO_MASK;
274 276
275 u8 hdr[3] = { 277 u8 hdr[3] = {
409struct dns_cfg 411struct dns_cfg
410{ 412{
411 static int next_uid; 413 static int next_uid;
412 414
413 u8 id1, id2, id3, id4; 415 u8 id1, id2, id3, id4;
416
414 u8 version; 417 u8 version;
418 u8 flags;
415 u8 rrtype; 419 u8 rrtype;
416 u8 flags;
417 u8 def_ttl; 420 u8 def_ttl;
418 u8 rcv_cdc; 421
419 u8 snd_cdc;
420 u16 max_size;
421 u16 client; 422 u16 client;
422 u16 uid; // to make request unique 423 u16 uid; // to make request unique
423 424
424 u8 reserved[8]; 425 u16 max_size;
426 u8 seq_cdc;
427 u8 req_cdc;
428
429 u8 rep_cdc;
430 u8 delay; // time in 0.01s units that the server may delay replying packets
431 u8 r3, r4;
432
433 u8 r5, r6, r7, r8;
425 434
426 void reset (int clientid); 435 void reset (int clientid);
427 bool valid (); 436 bool valid ();
428}; 437};
429 438
438 447
439 version = 1; 448 version = 1;
440 449
441 rrtype = RR_TYPE_TXT; 450 rrtype = RR_TYPE_TXT;
442 flags = 0; 451 flags = 0;
443 def_ttl = 1; 452 def_ttl = 0;
453 seq_cdc = 26;
454 req_cdc = 62;
444 rcv_cdc = 0; 455 rep_cdc = 0;
445 snd_cdc = 62;
446 max_size = ntohs (MAX_PKT_SIZE); 456 max_size = htons (MAX_PKT_SIZE);
447 client = ntohs (clientid); 457 client = htons (clientid);
448 uid = next_uid++; 458 uid = next_uid++;
459 delay = 0;
449 460
450 memset (reserved, 0, 8); 461 r3 = r4 = 0;
462 r4 = r5 = r6 = r7 = 0;
451} 463}
452 464
453bool dns_cfg::valid () 465bool dns_cfg::valid ()
454{ 466{
467 // although the protocol itself allows for some configurability,
468 // only the following encoding/decoding settings are implemented.
455 return id1 == 'G' 469 return id1 == 'G'
456 && id2 == 'V' 470 && id2 == 'V'
457 && id3 == 'P' 471 && id3 == 'P'
458 && id4 == 'E' 472 && id4 == 'E'
473 && seq_cdc == 26
474 && req_cdc == 62
475 && rep_cdc == 0
459 && version == 1 476 && version == 1;
460 && flags == 0
461 && rcv_cdc == 0
462 && snd_cdc == 62
463 && max_size == ntohs (MAX_PKT_SIZE);
464} 477}
465 478
466struct dns_packet : net_packet 479struct dns_packet : net_packet
467{ 480{
468 u16 id; 481 u16 id;
469 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 482 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4
470 u16 qdcount, ancount, nscount, arcount; 483 u16 qdcount, ancount, nscount, arcount;
471 484
472 u8 data[MAXSIZE - 6 * 2]; 485 u8 data [MAXSIZE - 6 * 2];
473 486
474 int decode_label (char *data, int size, int &offs); 487 int decode_label (char *data, int size, int &offs);
475}; 488};
476 489
477int dns_packet::decode_label (char *data, int size, int &offs) 490int dns_packet::decode_label (char *data, int size, int &offs)
539 552
540 bool established; 553 bool established;
541 554
542 tstamp last_received; 555 tstamp last_received;
543 tstamp last_sent; 556 tstamp last_sent;
544 double last_latency; 557 double min_latency;
545 double poll_interval, send_interval; 558 double poll_interval, send_interval;
546 559
547 vector<dns_rcv *> rcvpq; 560 vector<dns_rcv *> rcvpq;
548 561
549 byte_stream rcvdq; int rcvseq; 562 byte_stream rcvdq; int rcvseq; int repseq;
550 byte_stream snddq; int sndseq; 563 byte_stream snddq; int sndseq;
551 564
552 void time_cb (time_watcher &w); time_watcher tw; 565 inline void time_cb (ev::timer &w, int revents); ev::timer tw;
553 void receive_rep (dns_rcv *r); 566 void receive_rep (dns_rcv *r);
554 567
555 dns_connection (connection *c); 568 dns_connection (connection *c);
556 ~dns_connection (); 569 ~dns_connection ();
557}; 570};
576: dns (dns) 589: dns (dns)
577{ 590{
578 timeout = 0; 591 timeout = 0;
579 retry = 0; 592 retry = 0;
580 seqno = 0; 593 seqno = 0;
581 sent = NOW; 594 sent = ev_now ();
582 stdhdr = false; 595 stdhdr = false;
583 596
584 pkt = new dns_packet; 597 pkt = new dns_packet;
585 598
586 pkt->id = next_id (); 599 pkt->id = next_id ();
617void dns_snd::gen_stream_req (int seqno, byte_stream &stream) 630void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
618{ 631{
619 stdhdr = true; 632 stdhdr = true;
620 this->seqno = seqno; 633 this->seqno = seqno;
621 634
622 timeout = NOW + INITIAL_TIMEOUT; 635 timeout = ev_now () + INITIAL_TIMEOUT;
623 636
624 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 637 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
625 pkt->qdcount = htons (1); 638 pkt->qdcount = htons (1);
626 639
627 int offs = 6*2; 640 int offs = 6*2;
662 pkt->len = offs; 675 pkt->len = offs;
663} 676}
664 677
665void dns_snd::gen_syn_req () 678void dns_snd::gen_syn_req ()
666{ 679{
667 timeout = NOW + INITIAL_SYN_TIMEOUT; 680 timeout = ev_now () + INITIAL_SYN_TIMEOUT;
668
669 printf ("send syn\n");//D
670 681
671 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 682 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
672 pkt->qdcount = htons (1); 683 pkt->qdcount = htons (1);
673 684
674 int offs = 6 * 2; 685 int offs = 6 * 2;
713///////////////////////////////////////////////////////////////////////////// 724/////////////////////////////////////////////////////////////////////////////
714 725
715dns_connection::dns_connection (connection *c) 726dns_connection::dns_connection (connection *c)
716: c (c) 727: c (c)
717, rcvdq (MAX_BACKLOG * 2) 728, rcvdq (MAX_BACKLOG * 2)
718, snddq (MAX_BACKLOG * 2) 729, snddq (MAX_BACKLOG)
719, tw (this, &dns_connection::time_cb)
720{ 730{
731 tw.set<dns_connection, &dns_connection::time_cb> (this);
732
721 vpn = c->vpn; 733 vpn = c->vpn;
722 734
723 established = false; 735 established = false;
724 736
725 rcvseq = sndseq = 0; 737 rcvseq = repseq = sndseq = 0;
726 738
727 last_sent = last_received = 0; 739 last_sent = last_received = 0;
728 poll_interval = MIN_POLL_INTERVAL; 740 poll_interval = 0.5; // starting here
729 send_interval = 0.5; // starting rate 741 send_interval = 0.5; // starting rate
730 last_latency = INITIAL_TIMEOUT; 742 min_latency = INITIAL_TIMEOUT;
731} 743}
732 744
733dns_connection::~dns_connection () 745dns_connection::~dns_connection ()
734{ 746{
735 for (vector<dns_rcv *>::iterator i = rcvpq.begin (); 747 for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
740 752
741void dns_connection::receive_rep (dns_rcv *r) 753void dns_connection::receive_rep (dns_rcv *r)
742{ 754{
743 if (r->datalen) 755 if (r->datalen)
744 { 756 {
745 last_received = NOW; 757 last_received = ev_now ();
746 tw.trigger (); 758 tw ();
747 759
748 poll_interval = send_interval; 760 poll_interval = send_interval;
749 } 761 }
750 else 762 else
751 { 763 {
752 poll_interval *= 1.5; 764 poll_interval *= 1.5;
765
753 if (poll_interval > MAX_POLL_INTERVAL) 766 if (poll_interval > MAX_POLL_INTERVAL)
754 poll_interval = MAX_POLL_INTERVAL; 767 poll_interval = MAX_POLL_INTERVAL;
755 } 768 }
756 769
757 rcvpq.push_back (r); 770 rcvpq.push_back (r);
760 773
761 // find next packet 774 // find next packet
762 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) 775 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); )
763 if (SEQNO_EQ (rcvseq, (*i)->seqno)) 776 if (SEQNO_EQ (rcvseq, (*i)->seqno))
764 { 777 {
778 //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D
765 // enter the packet into our input stream 779 // enter the packet into our input stream
766 r = *i; 780 r = *i;
767 781
768 // remove the oldest packet, look forward, as it's oldest first 782 // remove the oldest packet, look forward, as it's oldest first
769 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) 783 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j)
770 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) 784 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW))
771 { 785 {
786 //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D
772 delete *j; 787 delete *j;
773 rcvpq.erase (j); 788 rcvpq.erase (j);
774 break; 789 break;
775 } 790 }
776 791
783 } 798 }
784 799
785 while (vpn_packet *pkt = rcvdq.get ()) 800 while (vpn_packet *pkt = rcvdq.get ())
786 { 801 {
787 sockinfo si; 802 sockinfo si;
788 si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4; 803 si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4;
789 804
790 vpn->recv_vpn_packet (pkt, si); 805 vpn->recv_vpn_packet (pkt, si);
791 806
792 delete pkt; 807 delete pkt;
793 } 808 }
807 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 822 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
808 823
809 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) 824 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK))
810 && pkt.qdcount == htons (1)) 825 && pkt.qdcount == htons (1))
811 { 826 {
812 char qname[MAXSIZE]; 827 char qname [MAXSIZE];
813 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); 828 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
814 829
815 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; 830 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
816 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; 831 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
817 832
824 839
825 int dlen = strlen (THISNODE->domain); 840 int dlen = strlen (THISNODE->domain);
826 841
827 if (qclass == RR_CLASS_IN 842 if (qclass == RR_CLASS_IN
828 && qlen > dlen + 1 843 && qlen > dlen + 1
829 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) 844 && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen))
830 { 845 {
831 // now generate reply 846 // now generate reply
832 pkt.ancount = htons (1); // one answer RR 847 pkt.ancount = htons (1); // one answer RR
833 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); 848 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
834 849
847 if (0 < client && client <= conns.size ()) 862 if (0 < client && client <= conns.size ())
848 { 863 {
849 connection *c = conns [client - 1]; 864 connection *c = conns [client - 1];
850 dns_connection *dns = c->dns; 865 dns_connection *dns = c->dns;
851 dns_rcv *rcv; 866 dns_rcv *rcv;
852 bool in_seq;
853 867
854 if (dns) 868 if (dns)
855 { 869 {
856 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) 870 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); )
857 if (SEQNO_EQ ((*i)->seqno, seqno)) 871 if (SEQNO_EQ ((*i)->seqno, seqno))
868 memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); 882 memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len);
869 883
870 goto duplicate_request; 884 goto duplicate_request;
871 } 885 }
872 886
873 in_seq = dns->rcvseq == seqno;
874
875 // new packet, queue 887 // new packet, queue
876 rcv = new dns_rcv (seqno, data, datalen); 888 rcv = new dns_rcv (seqno, data, datalen);
877 dns->receive_rep (rcv); 889 dns->receive_rep (rcv);
878 } 890 }
879 891
892 {
880 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section 893 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
881 894
882 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; 895 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A;
883 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type 896 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type
884 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class 897 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
885 pkt [offs++] = 0; pkt [offs++] = 0; 898 pkt [offs++] = 0; pkt [offs++] = 0;
886 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL 899 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL
887 900
888 int rdlen_offs = offs += 2; 901 int rdlen_offs = offs += 2;
889 902
903 if (dns)
904 {
890 int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; 905 int dlen = ntohs (dns->cfg.max_size) - offs;
906
891 // bind doesn't compress well, so reduce further by one label length 907 // bind doesn't compress well, so reduce further by one label length
892 dlen -= qlen; 908 dlen -= qlen;
893 909
894 if (dns)
895 {
896 // only put data into in-order sequence packets, if 910 // only put data into in-order sequence packets, if
897 // we receive out-of-order packets we generate empty 911 // we receive out-of-order packets we generate empty
898 // replies 912 // replies
899 while (dlen > 1 && !dns->snddq.empty () && in_seq) 913 //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D
914 if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW)
900 { 915 {
916 dns->repseq = seqno;
917
918 while (dlen > 1 && !dns->snddq.empty ())
919 {
901 int txtlen = dlen <= 255 ? dlen - 1 : 255; 920 int txtlen = dlen <= 255 ? dlen - 1 : 255;
902 921
903 if (txtlen > dns->snddq.size ()) 922 if (txtlen > dns->snddq.size ())
904 txtlen = dns->snddq.size (); 923 txtlen = dns->snddq.size ();
905 924
906 pkt[offs++] = txtlen; 925 pkt[offs++] = txtlen;
907 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); 926 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen);
908 offs += txtlen; 927 offs += txtlen;
909 dns->snddq.remove (txtlen); 928 dns->snddq.remove (txtlen);
910 929
911 dlen -= txtlen + 1; 930 dlen -= txtlen + 1;
931 }
912 } 932 }
913 933
914 // avoid empty TXT rdata 934 // avoid completely empty TXT rdata
915 if (offs == rdlen_offs) 935 if (offs == rdlen_offs)
916 pkt[offs++] = 0; 936 pkt[offs++] = 0;
917 937
918 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); 938 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
919 } 939 }
920 else 940 else
921 { 941 {
922 // send RST 942 // send RST
923 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; 943 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
924 pkt [offs++] = CMD_IP_RST; 944 pkt [offs++] = CMD_IP_RST;
925 } 945 }
926 946
927 int rdlen = offs - rdlen_offs; 947 int rdlen = offs - rdlen_offs;
928 948
929 pkt [rdlen_offs - 2] = rdlen >> 8; 949 pkt [rdlen_offs - 2] = rdlen >> 8;
930 pkt [rdlen_offs - 1] = rdlen; 950 pkt [rdlen_offs - 1] = rdlen;
931 951
932 if (dns) 952 if (dns)
933 { 953 {
934 // now update dns_rcv copy 954 // now update dns_rcv copy
935 rcv->pkt->len = offs; 955 rcv->pkt->len = offs;
936 memcpy (rcv->pkt->at (0), pkt.at (0), offs); 956 memcpy (rcv->pkt->at (0), pkt.at (0), offs);
937 } 957 }
958 }
938 959
939 duplicate_request: ; 960 duplicate_request: ;
940 } 961 }
941 else 962 else
942 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 963 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
954 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class 975 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
955 pkt [offs++] = 0; pkt [offs++] = 0; 976 pkt [offs++] = 0; pkt [offs++] = 0;
956 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL 977 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
957 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength 978 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
958 979
959 slog (L_INFO, _("DNS: client %d tries to connect"), client); 980 slog (L_INFO, _("DNS: client %d connects"), client);
960 981
961 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; 982 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
962 pkt [offs++] = CMD_IP_REJ; 983 pkt [offs++] = CMD_IP_REJ;
963 984
964 if (0 < client && client <= conns.size ()) 985 if (0 < client && client <= conns.size ())
998 { 1019 {
999 dns_connection *dns = (*i)->dns; 1020 dns_connection *dns = (*i)->dns;
1000 connection *c = dns->c; 1021 connection *c = dns->c;
1001 int seqno = (*i)->seqno; 1022 int seqno = (*i)->seqno;
1002 u8 data[MAXSIZE], *datap = data; 1023 u8 data[MAXSIZE], *datap = data;
1024 //printf ("rcv pkt %x\n", seqno);//D
1003 1025
1004 if ((*i)->retry) 1026 if ((*i)->retry)
1005 { 1027 {
1006 dns->send_interval *= 1.01; 1028 dns->send_interval *= 1.01;
1007 if (dns->send_interval > MAX_SEND_INTERVAL) 1029 if (dns->send_interval > MAX_SEND_INTERVAL)
1008 dns->send_interval = MAX_SEND_INTERVAL; 1030 dns->send_interval = MAX_SEND_INTERVAL;
1009 } 1031 }
1010 else 1032 else
1011 { 1033 {
1012#if 1 1034#if 0
1013 dns->send_interval *= 0.999; 1035 dns->send_interval *= 0.999;
1014#endif 1036#endif
1015 if (dns->send_interval < MIN_SEND_INTERVAL)
1016 dns->send_interval = MIN_SEND_INTERVAL;
1017
1018 // the latency surely puts an upper bound on 1037 // the latency surely puts an upper bound on
1019 // the minimum send interval 1038 // the minimum send interval
1020 double latency = NOW - (*i)->sent; 1039 double latency = ev_now () - (*i)->sent;
1040
1041 if (latency < dns->min_latency)
1021 dns->last_latency = latency; 1042 dns->min_latency = latency;
1022 1043
1023 if (dns->send_interval > latency) 1044 if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor)
1045 dns->send_interval = dns->min_latency * conf.dns_overlap_factor;
1046
1047 if (dns->send_interval < conf.dns_send_interval)
1024 dns->send_interval = latency; 1048 dns->send_interval = conf.dns_send_interval;
1025 } 1049 }
1026 1050
1027 delete *i; 1051 delete *i;
1028 dns_sndpq.erase (i); 1052 dns_sndpq.erase (i);
1029 1053
1134 break; 1158 break;
1135 } 1159 }
1136} 1160}
1137 1161
1138void 1162void
1139vpn::dnsv4_ev (io_watcher &w, short revents) 1163vpn::dnsv4_ev (ev::io &w, int revents)
1140{ 1164{
1141 if (revents & EVENT_READ) 1165 if (revents & EV_READ)
1142 { 1166 {
1143 dns_packet *pkt = new dns_packet; 1167 dns_packet *pkt = new dns_packet;
1144 struct sockaddr_in sa; 1168 struct sockaddr_in sa;
1145 socklen_t sa_len = sizeof (sa); 1169 socklen_t sa_len = sizeof (sa);
1146 1170
1147 pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 1171 pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
1148 1172
1149 if (pkt->len > 0) 1173 if (pkt->len > 0)
1150 { 1174 {
1151 if (THISNODE->dns_port) 1175 if (ntohs (pkt->flags) & FLAG_RESPONSE)
1176 dnsv4_client (*pkt);
1177 else
1152 { 1178 {
1153 dnsv4_server (*pkt); 1179 dnsv4_server (*pkt);
1154 sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); 1180 sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len);
1155 } 1181 }
1156 else
1157 dnsv4_client (*pkt);
1158 1182
1159 delete pkt; 1183 delete pkt;
1160 } 1184 }
1161 } 1185 }
1162} 1186}
1163 1187
1164bool 1188bool
1165vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1189vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1166{ 1190{
1167 int client = ntohs (si.port); 1191 int client = ntohl (si.host);
1168 1192
1169 assert (0 < client && client <= conns.size ()); 1193 assert (0 < client && client <= conns.size ());
1170 1194
1171 connection *c = conns [client - 1]; 1195 connection *c = conns [client - 1];
1172 1196
1173 if (!c->dns) 1197 if (!c->dns)
1174 c->dns = new dns_connection (c); 1198 c->dns = new dns_connection (c);
1175 1199
1176 if (!c->dns->snddq.put (pkt)) 1200 if (c->dns->snddq.put (pkt))
1177 return false;
1178
1179 c->dns->tw.trigger (); 1201 c->dns->tw ();
1180 1202
1203 // always return true even if the buffer overflows
1181 return true; 1204 return true;
1182} 1205}
1183 1206
1184void 1207void
1185connection::dnsv4_reset_connection () 1208connection::dnsv4_reset_connection ()
1188} 1211}
1189 1212
1190#define NEXT(w) do { if (next > (w)) next = w; } while (0) 1213#define NEXT(w) do { if (next > (w)) next = w; } while (0)
1191 1214
1192void 1215void
1193dns_connection::time_cb (time_watcher &w) 1216dns_connection::time_cb (ev::timer &w, int revents)
1194{ 1217{
1195 // servers have to be polled 1218 // servers have to be polled
1196 if (THISNODE->dns_port) 1219 if (THISNODE->dns_port)
1197 return; 1220 return;
1198 1221
1199 // check for timeouts and (re)transmit 1222 // check for timeouts and (re)transmit
1200 tstamp next = NOW + poll_interval; 1223 tstamp next = ev::now () + poll_interval;
1201 dns_snd *send = 0; 1224 dns_snd *send = 0;
1202 1225
1203 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); 1226 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
1204 i != vpn->dns_sndpq.end (); 1227 i != vpn->dns_sndpq.end ();
1205 ++i) 1228 ++i)
1206 { 1229 {
1207 dns_snd *r = *i; 1230 dns_snd *r = *i;
1208 1231
1209 if (r->timeout <= NOW) 1232 if (r->timeout <= ev_now ())
1210 { 1233 {
1211 if (!send) 1234 if (!send)
1212 { 1235 {
1213 send = r; 1236 send = r;
1214 1237
1215 r->retry++; 1238 r->retry++;
1216 r->timeout = NOW + (r->retry * last_latency * 8.); 1239 r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor);
1240 //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D
1217 1241
1218 // the following code changes the query section a bit, forcing 1242 // the following code changes the query section a bit, forcing
1219 // the forwarder to generate a new request 1243 // the forwarder to generate a new request
1220 if (r->stdhdr) 1244 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); 1245 encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry);
1224 }
1225 } 1246 }
1226 } 1247 }
1227 else 1248 else
1228 NEXT (r->timeout); 1249 NEXT (r->timeout);
1229 } 1250 }
1230 1251
1231 if (last_sent + send_interval <= NOW)
1232 {
1233 if (!send) 1252 if (!send)
1253 {
1254 // generate a new packet, if wise
1255
1256 if (!established)
1234 { 1257 {
1235 // generate a new packet, if wise 1258 if (vpn->dns_sndpq.empty ())
1236
1237 if (!established)
1238 { 1259 {
1239 if (vpn->dns_sndpq.empty ())
1240 {
1241 send = new dns_snd (this); 1260 send = new dns_snd (this);
1242 1261
1243 printf ("new conn %p %d\n", this, c->conf->id);//D
1244 cfg.reset (THISNODE->id); 1262 cfg.reset (THISNODE->id);
1245 send->gen_syn_req (); 1263 send->gen_syn_req ();
1246 }
1247 } 1264 }
1248 else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING 1265 }
1266 else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding
1249 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) 1267 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
1268 {
1269 if (last_sent + send_interval <= ev_now ())
1250 { 1270 {
1251 //printf ("sending data request etc.\n"); //D 1271 //printf ("sending data request etc.\n"); //D
1252 if (!snddq.empty ()) 1272 if (!snddq.empty () || last_received + 1. > ev_now ())
1253 { 1273 {
1254 poll_interval = send_interval; 1274 poll_interval = send_interval;
1255 NEXT (NOW + send_interval); 1275 NEXT (ev_now () + send_interval);
1256 } 1276 }
1257 1277
1258 send = new dns_snd (this); 1278 send = new dns_snd (this);
1259 send->gen_stream_req (sndseq, snddq); 1279 send->gen_stream_req (sndseq, snddq);
1260 send->timeout = NOW + last_latency * 8.; 1280 send->timeout = ev_now () + min_latency * conf.dns_timeout_factor;
1281 //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D
1261 1282
1262 sndseq = (sndseq + 1) & SEQNO_MASK; 1283 sndseq = (sndseq + 1) & SEQNO_MASK;
1263 } 1284 }
1264 1285 else
1265 if (send) 1286 NEXT (last_sent + send_interval);
1266 vpn->dns_sndpq.push_back (send);
1267 } 1287 }
1268 1288
1269 if (send) 1289 if (send)
1270 { 1290 vpn->dns_sndpq.push_back (send);
1271 last_sent = NOW; 1291 }
1292
1293 if (send)
1294 {
1295 last_sent = ev_now ();
1272 sendto (vpn->dnsv4_fd, 1296 sendto (vpn->dnsv4_fd,
1273 send->pkt->at (0), send->pkt->len, 0, 1297 send->pkt->at (0), send->pkt->len, 0,
1274 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); 1298 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
1275 }
1276 } 1299 }
1277 else
1278 NEXT (last_sent + send_interval);
1279 1300
1280 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)", 1301 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)",
1281 poll_interval, send_interval, next - NOW, 1302 poll_interval, send_interval, next - ev_now (),
1282 vpn->dns_sndpq.size (), snddq.size ()); 1303 vpn->dns_sndpq.size (), snddq.size (),
1304 rcvpq.size ());
1283 1305
1284 // TODO: no idea when this happens, but when next < NOW, we have a problem 1306 // TODO: no idea when this happens, but when next < ev_now (), we have a problem
1307 // doesn't seem to happen anymore
1285 if (next < NOW + 0.0001) 1308 if (next < ev_now () + 0.001)
1286 next = NOW + 0.1; 1309 next = ev_now () + 0.1;
1287 1310
1288 w.start (next); 1311 w.start (next - ev_now ());
1289} 1312}
1290 1313
1291#endif 1314#endif
1292 1315

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines