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.21 by pcg, Sun Mar 6 21:33:40 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
886 pkt [offs++] = 0; pkt [offs++] = 0; 897 pkt [offs++] = 0; pkt [offs++] = 0;
887 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
888 899
889 int rdlen_offs = offs += 2; 900 int rdlen_offs = offs += 2;
890 901
891 int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs;
892 // bind doesn't compress well, so reduce further by one label length
893 dlen -= qlen;
894
895 if (dns) 902 if (dns)
896 { 903 {
904 int dlen = ntohs (dns->cfg.max_size) - offs;
905
906 // bind doesn't compress well, so reduce further by one label length
907 dlen -= qlen;
908
897 // only put data into in-order sequence packets, if 909 // only put data into in-order sequence packets, if
898 // we receive out-of-order packets we generate empty 910 // we receive out-of-order packets we generate empty
899 // replies 911 // replies
900 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)
901 { 914 {
915 dns->repseq = seqno;
916
917 while (dlen > 1 && !dns->snddq.empty ())
918 {
902 int txtlen = dlen <= 255 ? dlen - 1 : 255; 919 int txtlen = dlen <= 255 ? dlen - 1 : 255;
903 920
904 if (txtlen > dns->snddq.size ()) 921 if (txtlen > dns->snddq.size ())
905 txtlen = dns->snddq.size (); 922 txtlen = dns->snddq.size ();
906 923
907 pkt[offs++] = txtlen; 924 pkt[offs++] = txtlen;
908 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); 925 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen);
909 offs += txtlen; 926 offs += txtlen;
910 dns->snddq.remove (txtlen); 927 dns->snddq.remove (txtlen);
911 928
912 dlen -= txtlen + 1; 929 dlen -= txtlen + 1;
930 }
913 } 931 }
914 932
915 // avoid empty TXT rdata 933 // avoid completely empty TXT rdata
916 if (offs == rdlen_offs) 934 if (offs == rdlen_offs)
917 pkt[offs++] = 0; 935 pkt[offs++] = 0;
918 936
919 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); 937 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
920 } 938 }
956 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
957 pkt [offs++] = 0; pkt [offs++] = 0; 975 pkt [offs++] = 0; pkt [offs++] = 0;
958 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL 976 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
959 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength 977 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
960 978
961 slog (L_INFO, _("DNS: client %d tries to connect"), client); 979 slog (L_INFO, _("DNS: client %d connects"), client);
962 980
963 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;
964 pkt [offs++] = CMD_IP_REJ; 982 pkt [offs++] = CMD_IP_REJ;
965 983
966 if (0 < client && client <= conns.size ()) 984 if (0 < client && client <= conns.size ())
1000 { 1018 {
1001 dns_connection *dns = (*i)->dns; 1019 dns_connection *dns = (*i)->dns;
1002 connection *c = dns->c; 1020 connection *c = dns->c;
1003 int seqno = (*i)->seqno; 1021 int seqno = (*i)->seqno;
1004 u8 data[MAXSIZE], *datap = data; 1022 u8 data[MAXSIZE], *datap = data;
1023 //printf ("rcv pkt %x\n", seqno);//D
1005 1024
1006 if ((*i)->retry) 1025 if ((*i)->retry)
1007 { 1026 {
1008 dns->send_interval *= 1.01; 1027 dns->send_interval *= 1.01;
1009 if (dns->send_interval > MAX_SEND_INTERVAL) 1028 if (dns->send_interval > MAX_SEND_INTERVAL)
1010 dns->send_interval = MAX_SEND_INTERVAL; 1029 dns->send_interval = MAX_SEND_INTERVAL;
1011 } 1030 }
1012 else 1031 else
1013 { 1032 {
1014#if 1 1033#if 0
1015 dns->send_interval *= 0.999; 1034 dns->send_interval *= 0.999;
1016#endif 1035#endif
1017 if (dns->send_interval < MIN_SEND_INTERVAL)
1018 dns->send_interval = MIN_SEND_INTERVAL;
1019
1020 // the latency surely puts an upper bound on 1036 // the latency surely puts an upper bound on
1021 // the minimum send interval 1037 // the minimum send interval
1022 double latency = NOW - (*i)->sent; 1038 double latency = ev_now () - (*i)->sent;
1039
1040 if (latency < dns->min_latency)
1023 dns->last_latency = latency; 1041 dns->min_latency = latency;
1024 1042
1025 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)
1026 dns->send_interval = latency; 1047 dns->send_interval = conf.dns_send_interval;
1027 } 1048 }
1028 1049
1029 delete *i; 1050 delete *i;
1030 dns_sndpq.erase (i); 1051 dns_sndpq.erase (i);
1031 1052
1136 break; 1157 break;
1137 } 1158 }
1138} 1159}
1139 1160
1140void 1161void
1141vpn::dnsv4_ev (io_watcher &w, short revents) 1162vpn::dnsv4_ev (ev::io &w, int revents)
1142{ 1163{
1143 if (revents & EVENT_READ) 1164 if (revents & EV_READ)
1144 { 1165 {
1145 dns_packet *pkt = new dns_packet; 1166 dns_packet *pkt = new dns_packet;
1146 struct sockaddr_in sa; 1167 struct sockaddr_in sa;
1147 socklen_t sa_len = sizeof (sa); 1168 socklen_t sa_len = sizeof (sa);
1148 1169
1149 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);
1150 1171
1151 if (pkt->len > 0) 1172 if (pkt->len > 0)
1152 { 1173 {
1153 if (THISNODE->dns_port) 1174 if (ntohs (pkt->flags) & FLAG_RESPONSE)
1175 dnsv4_client (*pkt);
1176 else
1154 { 1177 {
1155 dnsv4_server (*pkt); 1178 dnsv4_server (*pkt);
1156 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);
1157 } 1180 }
1158 else
1159 dnsv4_client (*pkt);
1160 1181
1161 delete pkt; 1182 delete pkt;
1162 } 1183 }
1163 } 1184 }
1164} 1185}
1165 1186
1166bool 1187bool
1167vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1188vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1168{ 1189{
1169 int client = ntohs (si.port); 1190 int client = ntohl (si.host);
1170 1191
1171 assert (0 < client && client <= conns.size ()); 1192 assert (0 < client && client <= conns.size ());
1172 1193
1173 connection *c = conns [client - 1]; 1194 connection *c = conns [client - 1];
1174 1195
1175 if (!c->dns) 1196 if (!c->dns)
1176 c->dns = new dns_connection (c); 1197 c->dns = new dns_connection (c);
1177 1198
1178 if (!c->dns->snddq.put (pkt)) 1199 if (c->dns->snddq.put (pkt))
1179 return false;
1180
1181 c->dns->tw.trigger (); 1200 c->dns->tw ();
1182 1201
1202 // always return true even if the buffer overflows
1183 return true; 1203 return true;
1184} 1204}
1185 1205
1186void 1206void
1187connection::dnsv4_reset_connection () 1207connection::dnsv4_reset_connection ()
1190} 1210}
1191 1211
1192#define NEXT(w) do { if (next > (w)) next = w; } while (0) 1212#define NEXT(w) do { if (next > (w)) next = w; } while (0)
1193 1213
1194void 1214void
1195dns_connection::time_cb (time_watcher &w) 1215dns_connection::time_cb (ev::timer &w, int revents)
1196{ 1216{
1197 // servers have to be polled 1217 // servers have to be polled
1198 if (THISNODE->dns_port) 1218 if (THISNODE->dns_port)
1199 return; 1219 return;
1200 1220
1201 // check for timeouts and (re)transmit 1221 // check for timeouts and (re)transmit
1202 tstamp next = NOW + poll_interval; 1222 tstamp next = ev::now () + poll_interval;
1203 dns_snd *send = 0; 1223 dns_snd *send = 0;
1204 1224
1205 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); 1225 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
1206 i != vpn->dns_sndpq.end (); 1226 i != vpn->dns_sndpq.end ();
1207 ++i) 1227 ++i)
1208 { 1228 {
1209 dns_snd *r = *i; 1229 dns_snd *r = *i;
1210 1230
1211 if (r->timeout <= NOW) 1231 if (r->timeout <= ev_now ())
1212 { 1232 {
1213 if (!send) 1233 if (!send)
1214 { 1234 {
1215 send = r; 1235 send = r;
1216 1236
1217 r->retry++; 1237 r->retry++;
1218 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
1219 1240
1220 // the following code changes the query section a bit, forcing 1241 // the following code changes the query section a bit, forcing
1221 // the forwarder to generate a new request 1242 // the forwarder to generate a new request
1222 if (r->stdhdr) 1243 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); 1244 encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry);
1226 }
1227 } 1245 }
1228 } 1246 }
1229 else 1247 else
1230 NEXT (r->timeout); 1248 NEXT (r->timeout);
1231 } 1249 }
1232 1250
1233 if (last_sent + send_interval <= NOW)
1234 {
1235 if (!send) 1251 if (!send)
1252 {
1253 // generate a new packet, if wise
1254
1255 if (!established)
1236 { 1256 {
1237 // generate a new packet, if wise 1257 if (vpn->dns_sndpq.empty ())
1238
1239 if (!established)
1240 { 1258 {
1241 if (vpn->dns_sndpq.empty ())
1242 {
1243 send = new dns_snd (this); 1259 send = new dns_snd (this);
1244 1260
1245 printf ("new conn %p %d\n", this, c->conf->id);//D
1246 cfg.reset (THISNODE->id); 1261 cfg.reset (THISNODE->id);
1247 send->gen_syn_req (); 1262 send->gen_syn_req ();
1248 }
1249 } 1263 }
1250 else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING 1264 }
1265 else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding
1251 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) 1266 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
1267 {
1268 if (last_sent + send_interval <= ev_now ())
1252 { 1269 {
1253 //printf ("sending data request etc.\n"); //D 1270 //printf ("sending data request etc.\n"); //D
1254 if (!snddq.empty ()) 1271 if (!snddq.empty () || last_received + 1. > ev_now ())
1255 { 1272 {
1256 poll_interval = send_interval; 1273 poll_interval = send_interval;
1257 NEXT (NOW + send_interval); 1274 NEXT (ev_now () + send_interval);
1258 } 1275 }
1259 1276
1260 send = new dns_snd (this); 1277 send = new dns_snd (this);
1261 send->gen_stream_req (sndseq, snddq); 1278 send->gen_stream_req (sndseq, snddq);
1262 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
1263 1281
1264 sndseq = (sndseq + 1) & SEQNO_MASK; 1282 sndseq = (sndseq + 1) & SEQNO_MASK;
1265 } 1283 }
1266 1284 else
1267 if (send) 1285 NEXT (last_sent + send_interval);
1268 vpn->dns_sndpq.push_back (send);
1269 } 1286 }
1270 1287
1271 if (send) 1288 if (send)
1272 { 1289 vpn->dns_sndpq.push_back (send);
1273 last_sent = NOW; 1290 }
1291
1292 if (send)
1293 {
1294 last_sent = ev_now ();
1274 sendto (vpn->dnsv4_fd, 1295 sendto (vpn->dnsv4_fd,
1275 send->pkt->at (0), send->pkt->len, 0, 1296 send->pkt->at (0), send->pkt->len, 0,
1276 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); 1297 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
1277 }
1278 } 1298 }
1279 else
1280 NEXT (last_sent + send_interval);
1281 1299
1282 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)",
1283 poll_interval, send_interval, next - NOW, 1301 poll_interval, send_interval, next - ev_now (),
1284 vpn->dns_sndpq.size (), snddq.size ()); 1302 vpn->dns_sndpq.size (), snddq.size (),
1303 rcvpq.size ());
1285 1304
1286 // 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
1287 if (next < NOW + 0.0001) 1307 if (next < ev_now () + 0.001)
1288 next = NOW + 0.1; 1308 next = ev_now () + 0.1;
1289 1309
1290 w.start (next); 1310 w.start (next - ev_now ());
1291} 1311}
1292 1312
1293#endif 1313#endif
1294 1314

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines