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.44 by pcg, Tue Dec 4 14:55:59 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 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) 730, tw (this, &dns_connection::time_cb)
720{ 731{
721 vpn = c->vpn; 732 vpn = c->vpn;
722 733
723 established = false; 734 established = false;
724 735
725 rcvseq = sndseq = 0; 736 rcvseq = repseq = sndseq = 0;
726 737
727 last_sent = last_received = 0; 738 last_sent = last_received = 0;
728 poll_interval = MIN_POLL_INTERVAL; 739 poll_interval = 0.5; // starting here
729 send_interval = 0.5; // starting rate 740 send_interval = 0.5; // starting rate
730 last_latency = INITIAL_TIMEOUT; 741 min_latency = INITIAL_TIMEOUT;
731} 742}
732 743
733dns_connection::~dns_connection () 744dns_connection::~dns_connection ()
734{ 745{
735 for (vector<dns_rcv *>::iterator i = rcvpq.begin (); 746 for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
740 751
741void dns_connection::receive_rep (dns_rcv *r) 752void dns_connection::receive_rep (dns_rcv *r)
742{ 753{
743 if (r->datalen) 754 if (r->datalen)
744 { 755 {
745 last_received = NOW; 756 last_received = ev_now ();
746 tw.trigger (); 757 tw ();
747 758
748 poll_interval = send_interval; 759 poll_interval = send_interval;
749 } 760 }
750 else 761 else
751 { 762 {
752 poll_interval *= 1.5; 763 poll_interval *= 1.5;
764
753 if (poll_interval > MAX_POLL_INTERVAL) 765 if (poll_interval > MAX_POLL_INTERVAL)
754 poll_interval = MAX_POLL_INTERVAL; 766 poll_interval = MAX_POLL_INTERVAL;
755 } 767 }
756 768
757 rcvpq.push_back (r); 769 rcvpq.push_back (r);
760 772
761 // find next packet 773 // find next packet
762 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) 774 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); )
763 if (SEQNO_EQ (rcvseq, (*i)->seqno)) 775 if (SEQNO_EQ (rcvseq, (*i)->seqno))
764 { 776 {
777 //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D
765 // enter the packet into our input stream 778 // enter the packet into our input stream
766 r = *i; 779 r = *i;
767 780
768 // remove the oldest packet, look forward, as it's oldest first 781 // remove the oldest packet, look forward, as it's oldest first
769 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) 782 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j)
770 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) 783 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW))
771 { 784 {
785 //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D
772 delete *j; 786 delete *j;
773 rcvpq.erase (j); 787 rcvpq.erase (j);
774 break; 788 break;
775 } 789 }
776 790
783 } 797 }
784 798
785 while (vpn_packet *pkt = rcvdq.get ()) 799 while (vpn_packet *pkt = rcvdq.get ())
786 { 800 {
787 sockinfo si; 801 sockinfo si;
788 si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4; 802 si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4;
789 803
790 vpn->recv_vpn_packet (pkt, si); 804 vpn->recv_vpn_packet (pkt, si);
791 805
792 delete pkt; 806 delete pkt;
793 } 807 }
807 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 821 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
808 822
809 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) 823 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK))
810 && pkt.qdcount == htons (1)) 824 && pkt.qdcount == htons (1))
811 { 825 {
812 char qname[MAXSIZE]; 826 char qname [MAXSIZE];
813 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); 827 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
814 828
815 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; 829 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
816 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; 830 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
817 831
824 838
825 int dlen = strlen (THISNODE->domain); 839 int dlen = strlen (THISNODE->domain);
826 840
827 if (qclass == RR_CLASS_IN 841 if (qclass == RR_CLASS_IN
828 && qlen > dlen + 1 842 && qlen > dlen + 1
829 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) 843 && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen))
830 { 844 {
831 // now generate reply 845 // now generate reply
832 pkt.ancount = htons (1); // one answer RR 846 pkt.ancount = htons (1); // one answer RR
833 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); 847 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
834 848
847 if (0 < client && client <= conns.size ()) 861 if (0 < client && client <= conns.size ())
848 { 862 {
849 connection *c = conns [client - 1]; 863 connection *c = conns [client - 1];
850 dns_connection *dns = c->dns; 864 dns_connection *dns = c->dns;
851 dns_rcv *rcv; 865 dns_rcv *rcv;
852 bool in_seq;
853 866
854 if (dns) 867 if (dns)
855 { 868 {
856 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) 869 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); )
857 if (SEQNO_EQ ((*i)->seqno, seqno)) 870 if (SEQNO_EQ ((*i)->seqno, seqno))
868 memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); 881 memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len);
869 882
870 goto duplicate_request; 883 goto duplicate_request;
871 } 884 }
872 885
873 in_seq = dns->rcvseq == seqno;
874
875 // new packet, queue 886 // new packet, queue
876 rcv = new dns_rcv (seqno, data, datalen); 887 rcv = new dns_rcv (seqno, data, datalen);
877 dns->receive_rep (rcv); 888 dns->receive_rep (rcv);
878 } 889 }
879 890
891 {
880 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section 892 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
881 893
882 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; 894 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A;
883 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type 895 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type
884 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class 896 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
885 pkt [offs++] = 0; pkt [offs++] = 0; 897 pkt [offs++] = 0; pkt [offs++] = 0;
886 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL 898 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL
887 899
888 int rdlen_offs = offs += 2; 900 int rdlen_offs = offs += 2;
889 901
902 if (dns)
903 {
890 int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; 904 int dlen = ntohs (dns->cfg.max_size) - offs;
905
891 // bind doesn't compress well, so reduce further by one label length 906 // bind doesn't compress well, so reduce further by one label length
892 dlen -= qlen; 907 dlen -= qlen;
893 908
894 if (dns)
895 {
896 // only put data into in-order sequence packets, if 909 // only put data into in-order sequence packets, if
897 // we receive out-of-order packets we generate empty 910 // we receive out-of-order packets we generate empty
898 // replies 911 // replies
899 while (dlen > 1 && !dns->snddq.empty () && in_seq) 912 //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D
913 if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW)
900 { 914 {
915 dns->repseq = seqno;
916
917 while (dlen > 1 && !dns->snddq.empty ())
918 {
901 int txtlen = dlen <= 255 ? dlen - 1 : 255; 919 int txtlen = dlen <= 255 ? dlen - 1 : 255;
902 920
903 if (txtlen > dns->snddq.size ()) 921 if (txtlen > dns->snddq.size ())
904 txtlen = dns->snddq.size (); 922 txtlen = dns->snddq.size ();
905 923
906 pkt[offs++] = txtlen; 924 pkt[offs++] = txtlen;
907 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); 925 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen);
908 offs += txtlen; 926 offs += txtlen;
909 dns->snddq.remove (txtlen); 927 dns->snddq.remove (txtlen);
910 928
911 dlen -= txtlen + 1; 929 dlen -= txtlen + 1;
930 }
912 } 931 }
913 932
914 // avoid empty TXT rdata 933 // avoid completely empty TXT rdata
915 if (offs == rdlen_offs) 934 if (offs == rdlen_offs)
916 pkt[offs++] = 0; 935 pkt[offs++] = 0;
917 936
918 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); 937 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
919 } 938 }
920 else 939 else
921 { 940 {
922 // send RST 941 // send RST
923 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; 942 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
924 pkt [offs++] = CMD_IP_RST; 943 pkt [offs++] = CMD_IP_RST;
925 } 944 }
926 945
927 int rdlen = offs - rdlen_offs; 946 int rdlen = offs - rdlen_offs;
928 947
929 pkt [rdlen_offs - 2] = rdlen >> 8; 948 pkt [rdlen_offs - 2] = rdlen >> 8;
930 pkt [rdlen_offs - 1] = rdlen; 949 pkt [rdlen_offs - 1] = rdlen;
931 950
932 if (dns) 951 if (dns)
933 { 952 {
934 // now update dns_rcv copy 953 // now update dns_rcv copy
935 rcv->pkt->len = offs; 954 rcv->pkt->len = offs;
936 memcpy (rcv->pkt->at (0), pkt.at (0), offs); 955 memcpy (rcv->pkt->at (0), pkt.at (0), offs);
937 } 956 }
957 }
938 958
939 duplicate_request: ; 959 duplicate_request: ;
940 } 960 }
941 else 961 else
942 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 962 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
954 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class 974 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
955 pkt [offs++] = 0; pkt [offs++] = 0; 975 pkt [offs++] = 0; pkt [offs++] = 0;
956 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL 976 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
957 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength 977 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
958 978
959 slog (L_INFO, _("DNS: client %d tries to connect"), client); 979 slog (L_INFO, _("DNS: client %d connects"), client);
960 980
961 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; 981 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
962 pkt [offs++] = CMD_IP_REJ; 982 pkt [offs++] = CMD_IP_REJ;
963 983
964 if (0 < client && client <= conns.size ()) 984 if (0 < client && client <= conns.size ())
998 { 1018 {
999 dns_connection *dns = (*i)->dns; 1019 dns_connection *dns = (*i)->dns;
1000 connection *c = dns->c; 1020 connection *c = dns->c;
1001 int seqno = (*i)->seqno; 1021 int seqno = (*i)->seqno;
1002 u8 data[MAXSIZE], *datap = data; 1022 u8 data[MAXSIZE], *datap = data;
1023 //printf ("rcv pkt %x\n", seqno);//D
1003 1024
1004 if ((*i)->retry) 1025 if ((*i)->retry)
1005 { 1026 {
1006 dns->send_interval *= 1.01; 1027 dns->send_interval *= 1.01;
1007 if (dns->send_interval > MAX_SEND_INTERVAL) 1028 if (dns->send_interval > MAX_SEND_INTERVAL)
1008 dns->send_interval = MAX_SEND_INTERVAL; 1029 dns->send_interval = MAX_SEND_INTERVAL;
1009 } 1030 }
1010 else 1031 else
1011 { 1032 {
1012#if 1 1033#if 0
1013 dns->send_interval *= 0.999; 1034 dns->send_interval *= 0.999;
1014#endif 1035#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 1036 // the latency surely puts an upper bound on
1019 // the minimum send interval 1037 // the minimum send interval
1020 double latency = NOW - (*i)->sent; 1038 double latency = ev_now () - (*i)->sent;
1039
1040 if (latency < dns->min_latency)
1021 dns->last_latency = latency; 1041 dns->min_latency = latency;
1022 1042
1023 if (dns->send_interval > latency) 1043 if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor)
1044 dns->send_interval = dns->min_latency * conf.dns_overlap_factor;
1045
1046 if (dns->send_interval < conf.dns_send_interval)
1024 dns->send_interval = latency; 1047 dns->send_interval = conf.dns_send_interval;
1025 } 1048 }
1026 1049
1027 delete *i; 1050 delete *i;
1028 dns_sndpq.erase (i); 1051 dns_sndpq.erase (i);
1029 1052
1134 break; 1157 break;
1135 } 1158 }
1136} 1159}
1137 1160
1138void 1161void
1139vpn::dnsv4_ev (io_watcher &w, short revents) 1162vpn::dnsv4_ev (ev::io &w, int revents)
1140{ 1163{
1141 if (revents & EVENT_READ) 1164 if (revents & EV_READ)
1142 { 1165 {
1143 dns_packet *pkt = new dns_packet; 1166 dns_packet *pkt = new dns_packet;
1144 struct sockaddr_in sa; 1167 struct sockaddr_in sa;
1145 socklen_t sa_len = sizeof (sa); 1168 socklen_t sa_len = sizeof (sa);
1146 1169
1147 pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 1170 pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
1148 1171
1149 if (pkt->len > 0) 1172 if (pkt->len > 0)
1150 { 1173 {
1151 if (THISNODE->dns_port) 1174 if (ntohs (pkt->flags) & FLAG_RESPONSE)
1175 dnsv4_client (*pkt);
1176 else
1152 { 1177 {
1153 dnsv4_server (*pkt); 1178 dnsv4_server (*pkt);
1154 sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); 1179 sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len);
1155 } 1180 }
1156 else
1157 dnsv4_client (*pkt);
1158 1181
1159 delete pkt; 1182 delete pkt;
1160 } 1183 }
1161 } 1184 }
1162} 1185}
1163 1186
1164bool 1187bool
1165vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1188vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1166{ 1189{
1167 int client = ntohs (si.port); 1190 int client = ntohl (si.host);
1168 1191
1169 assert (0 < client && client <= conns.size ()); 1192 assert (0 < client && client <= conns.size ());
1170 1193
1171 connection *c = conns [client - 1]; 1194 connection *c = conns [client - 1];
1172 1195
1173 if (!c->dns) 1196 if (!c->dns)
1174 c->dns = new dns_connection (c); 1197 c->dns = new dns_connection (c);
1175 1198
1176 if (!c->dns->snddq.put (pkt)) 1199 if (c->dns->snddq.put (pkt))
1177 return false;
1178
1179 c->dns->tw.trigger (); 1200 c->dns->tw ();
1180 1201
1202 // always return true even if the buffer overflows
1181 return true; 1203 return true;
1182} 1204}
1183 1205
1184void 1206void
1185connection::dnsv4_reset_connection () 1207connection::dnsv4_reset_connection ()
1188} 1210}
1189 1211
1190#define NEXT(w) do { if (next > (w)) next = w; } while (0) 1212#define NEXT(w) do { if (next > (w)) next = w; } while (0)
1191 1213
1192void 1214void
1193dns_connection::time_cb (time_watcher &w) 1215dns_connection::time_cb (ev::timer &w, int revents)
1194{ 1216{
1195 // servers have to be polled 1217 // servers have to be polled
1196 if (THISNODE->dns_port) 1218 if (THISNODE->dns_port)
1197 return; 1219 return;
1198 1220
1199 // check for timeouts and (re)transmit 1221 // check for timeouts and (re)transmit
1200 tstamp next = NOW + poll_interval; 1222 tstamp next = ev::now () + poll_interval;
1201 dns_snd *send = 0; 1223 dns_snd *send = 0;
1202 1224
1203 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); 1225 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
1204 i != vpn->dns_sndpq.end (); 1226 i != vpn->dns_sndpq.end ();
1205 ++i) 1227 ++i)
1206 { 1228 {
1207 dns_snd *r = *i; 1229 dns_snd *r = *i;
1208 1230
1209 if (r->timeout <= NOW) 1231 if (r->timeout <= ev_now ())
1210 { 1232 {
1211 if (!send) 1233 if (!send)
1212 { 1234 {
1213 send = r; 1235 send = r;
1214 1236
1215 r->retry++; 1237 r->retry++;
1216 r->timeout = NOW + (r->retry * last_latency * 8.); 1238 r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor);
1239 //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D
1217 1240
1218 // the following code changes the query section a bit, forcing 1241 // the following code changes the query section a bit, forcing
1219 // the forwarder to generate a new request 1242 // the forwarder to generate a new request
1220 if (r->stdhdr) 1243 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); 1244 encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry);
1224 }
1225 } 1245 }
1226 } 1246 }
1227 else 1247 else
1228 NEXT (r->timeout); 1248 NEXT (r->timeout);
1229 } 1249 }
1230 1250
1231 if (last_sent + send_interval <= NOW)
1232 {
1233 if (!send) 1251 if (!send)
1252 {
1253 // generate a new packet, if wise
1254
1255 if (!established)
1234 { 1256 {
1235 // generate a new packet, if wise 1257 if (vpn->dns_sndpq.empty ())
1236
1237 if (!established)
1238 { 1258 {
1239 if (vpn->dns_sndpq.empty ())
1240 {
1241 send = new dns_snd (this); 1259 send = new dns_snd (this);
1242 1260
1243 printf ("new conn %p %d\n", this, c->conf->id);//D
1244 cfg.reset (THISNODE->id); 1261 cfg.reset (THISNODE->id);
1245 send->gen_syn_req (); 1262 send->gen_syn_req ();
1246 }
1247 } 1263 }
1248 else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING 1264 }
1265 else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding
1249 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) 1266 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
1267 {
1268 if (last_sent + send_interval <= ev_now ())
1250 { 1269 {
1251 //printf ("sending data request etc.\n"); //D 1270 //printf ("sending data request etc.\n"); //D
1252 if (!snddq.empty ()) 1271 if (!snddq.empty () || last_received + 1. > ev_now ())
1253 { 1272 {
1254 poll_interval = send_interval; 1273 poll_interval = send_interval;
1255 NEXT (NOW + send_interval); 1274 NEXT (ev_now () + send_interval);
1256 } 1275 }
1257 1276
1258 send = new dns_snd (this); 1277 send = new dns_snd (this);
1259 send->gen_stream_req (sndseq, snddq); 1278 send->gen_stream_req (sndseq, snddq);
1260 send->timeout = NOW + last_latency * 8.; 1279 send->timeout = ev_now () + min_latency * conf.dns_timeout_factor;
1280 //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D
1261 1281
1262 sndseq = (sndseq + 1) & SEQNO_MASK; 1282 sndseq = (sndseq + 1) & SEQNO_MASK;
1263 } 1283 }
1264 1284 else
1265 if (send) 1285 NEXT (last_sent + send_interval);
1266 vpn->dns_sndpq.push_back (send);
1267 } 1286 }
1268 1287
1269 if (send) 1288 if (send)
1270 { 1289 vpn->dns_sndpq.push_back (send);
1271 last_sent = NOW; 1290 }
1291
1292 if (send)
1293 {
1294 last_sent = ev_now ();
1272 sendto (vpn->dnsv4_fd, 1295 sendto (vpn->dnsv4_fd,
1273 send->pkt->at (0), send->pkt->len, 0, 1296 send->pkt->at (0), send->pkt->len, 0,
1274 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); 1297 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
1275 }
1276 } 1298 }
1277 else
1278 NEXT (last_sent + send_interval);
1279 1299
1280 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)", 1300 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)",
1281 poll_interval, send_interval, next - NOW, 1301 poll_interval, send_interval, next - ev_now (),
1282 vpn->dns_sndpq.size (), snddq.size ()); 1302 vpn->dns_sndpq.size (), snddq.size (),
1303 rcvpq.size ());
1283 1304
1284 // TODO: no idea when this happens, but when next < NOW, we have a problem 1305 // TODO: no idea when this happens, but when next < ev_now (), we have a problem
1306 // doesn't seem to happen anymore
1285 if (next < NOW + 0.0001) 1307 if (next < ev_now () + 0.001)
1286 next = NOW + 0.1; 1308 next = ev_now () + 0.1;
1287 1309
1288 w.start (next); 1310 w.start (next - ev_now ());
1289} 1311}
1290 1312
1291#endif 1313#endif
1292 1314

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines