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