… | |
… | |
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 | |
681 | |
669 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
682 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
670 | pkt->qdcount = htons (1); |
683 | pkt->qdcount = htons (1); |
671 | |
684 | |
672 | int offs = 6 * 2; |
685 | int offs = 6 * 2; |
… | |
… | |
711 | ///////////////////////////////////////////////////////////////////////////// |
724 | ///////////////////////////////////////////////////////////////////////////// |
712 | |
725 | |
713 | dns_connection::dns_connection (connection *c) |
726 | dns_connection::dns_connection (connection *c) |
714 | : c (c) |
727 | : c (c) |
715 | , rcvdq (MAX_BACKLOG * 2) |
728 | , rcvdq (MAX_BACKLOG * 2) |
716 | , snddq (MAX_BACKLOG * 2) |
729 | , snddq (MAX_BACKLOG) |
717 | , tw (this, &dns_connection::time_cb) |
730 | , tw (this, &dns_connection::time_cb) |
718 | { |
731 | { |
719 | vpn = c->vpn; |
732 | vpn = c->vpn; |
720 | |
733 | |
721 | established = false; |
734 | established = false; |
722 | |
735 | |
723 | rcvseq = sndseq = 0; |
736 | rcvseq = repseq = sndseq = 0; |
724 | |
737 | |
725 | last_sent = last_received = 0; |
738 | last_sent = last_received = 0; |
726 | poll_interval = MIN_POLL_INTERVAL; |
739 | poll_interval = 0.5; // starting here |
727 | send_interval = 0.5; // starting rate |
740 | send_interval = 0.5; // starting rate |
728 | last_latency = INITIAL_TIMEOUT; |
741 | min_latency = INITIAL_TIMEOUT; |
729 | } |
742 | } |
730 | |
743 | |
731 | dns_connection::~dns_connection () |
744 | dns_connection::~dns_connection () |
732 | { |
745 | { |
733 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
746 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
… | |
… | |
738 | |
751 | |
739 | void dns_connection::receive_rep (dns_rcv *r) |
752 | void dns_connection::receive_rep (dns_rcv *r) |
740 | { |
753 | { |
741 | if (r->datalen) |
754 | if (r->datalen) |
742 | { |
755 | { |
743 | last_received = NOW; |
756 | last_received = ev_now (); |
744 | tw.trigger (); |
757 | tw (); |
745 | |
758 | |
746 | poll_interval = send_interval; |
759 | poll_interval = send_interval; |
747 | } |
760 | } |
748 | else |
761 | else |
749 | { |
762 | { |
750 | poll_interval *= 1.5; |
763 | poll_interval *= 1.5; |
|
|
764 | |
751 | if (poll_interval > MAX_POLL_INTERVAL) |
765 | if (poll_interval > MAX_POLL_INTERVAL) |
752 | poll_interval = MAX_POLL_INTERVAL; |
766 | poll_interval = MAX_POLL_INTERVAL; |
753 | } |
767 | } |
754 | |
768 | |
755 | rcvpq.push_back (r); |
769 | rcvpq.push_back (r); |
… | |
… | |
758 | |
772 | |
759 | // find next packet |
773 | // find next packet |
760 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
774 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
761 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
775 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
762 | { |
776 | { |
|
|
777 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
763 | // enter the packet into our input stream |
778 | // enter the packet into our input stream |
764 | r = *i; |
779 | r = *i; |
765 | |
780 | |
766 | // remove the oldest packet, look forward, as it's oldest first |
781 | // remove the oldest packet, look forward, as it's oldest first |
767 | 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) |
768 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
783 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
769 | { |
784 | { |
|
|
785 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
770 | delete *j; |
786 | delete *j; |
771 | rcvpq.erase (j); |
787 | rcvpq.erase (j); |
772 | break; |
788 | break; |
773 | } |
789 | } |
774 | |
790 | |
… | |
… | |
781 | } |
797 | } |
782 | |
798 | |
783 | while (vpn_packet *pkt = rcvdq.get ()) |
799 | while (vpn_packet *pkt = rcvdq.get ()) |
784 | { |
800 | { |
785 | sockinfo si; |
801 | sockinfo si; |
786 | 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; |
787 | |
803 | |
788 | vpn->recv_vpn_packet (pkt, si); |
804 | vpn->recv_vpn_packet (pkt, si); |
789 | |
805 | |
790 | delete pkt; |
806 | delete pkt; |
791 | } |
807 | } |
… | |
… | |
805 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
821 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
806 | |
822 | |
807 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
823 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
808 | && pkt.qdcount == htons (1)) |
824 | && pkt.qdcount == htons (1)) |
809 | { |
825 | { |
810 | char qname[MAXSIZE]; |
826 | char qname [MAXSIZE]; |
811 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
827 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
812 | |
828 | |
813 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
829 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
814 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
830 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
815 | |
831 | |
… | |
… | |
822 | |
838 | |
823 | int dlen = strlen (THISNODE->domain); |
839 | int dlen = strlen (THISNODE->domain); |
824 | |
840 | |
825 | if (qclass == RR_CLASS_IN |
841 | if (qclass == RR_CLASS_IN |
826 | && qlen > dlen + 1 |
842 | && qlen > dlen + 1 |
827 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
843 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
828 | { |
844 | { |
829 | // now generate reply |
845 | // now generate reply |
830 | pkt.ancount = htons (1); // one answer RR |
846 | pkt.ancount = htons (1); // one answer RR |
831 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
847 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
832 | |
848 | |
… | |
… | |
845 | if (0 < client && client <= conns.size ()) |
861 | if (0 < client && client <= conns.size ()) |
846 | { |
862 | { |
847 | connection *c = conns [client - 1]; |
863 | connection *c = conns [client - 1]; |
848 | dns_connection *dns = c->dns; |
864 | dns_connection *dns = c->dns; |
849 | dns_rcv *rcv; |
865 | dns_rcv *rcv; |
850 | bool in_seq; |
|
|
851 | |
866 | |
852 | if (dns) |
867 | if (dns) |
853 | { |
868 | { |
854 | 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 (); ) |
855 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
870 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
… | |
… | |
866 | 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); |
867 | |
882 | |
868 | goto duplicate_request; |
883 | goto duplicate_request; |
869 | } |
884 | } |
870 | |
885 | |
871 | in_seq = dns->rcvseq == seqno; |
|
|
872 | |
|
|
873 | // new packet, queue |
886 | // new packet, queue |
874 | rcv = new dns_rcv (seqno, data, datalen); |
887 | rcv = new dns_rcv (seqno, data, datalen); |
875 | dns->receive_rep (rcv); |
888 | dns->receive_rep (rcv); |
876 | } |
889 | } |
877 | |
890 | |
… | |
… | |
884 | pkt [offs++] = 0; pkt [offs++] = 0; |
897 | pkt [offs++] = 0; pkt [offs++] = 0; |
885 | 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 |
886 | |
899 | |
887 | int rdlen_offs = offs += 2; |
900 | int rdlen_offs = offs += 2; |
888 | |
901 | |
889 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
|
|
890 | // bind doesn't compress well, so reduce further by one label length |
|
|
891 | dlen -= qlen; |
|
|
892 | |
|
|
893 | if (dns) |
902 | if (dns) |
894 | { |
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 | |
895 | // only put data into in-order sequence packets, if |
909 | // only put data into in-order sequence packets, if |
896 | // we receive out-of-order packets we generate empty |
910 | // we receive out-of-order packets we generate empty |
897 | // replies |
911 | // replies |
898 | 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) |
899 | { |
914 | { |
|
|
915 | dns->repseq = seqno; |
|
|
916 | |
|
|
917 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
918 | { |
900 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
919 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
901 | |
920 | |
902 | if (txtlen > dns->snddq.size ()) |
921 | if (txtlen > dns->snddq.size ()) |
903 | txtlen = dns->snddq.size (); |
922 | txtlen = dns->snddq.size (); |
904 | |
923 | |
905 | pkt[offs++] = txtlen; |
924 | pkt[offs++] = txtlen; |
906 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
925 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
907 | offs += txtlen; |
926 | offs += txtlen; |
908 | dns->snddq.remove (txtlen); |
927 | dns->snddq.remove (txtlen); |
909 | |
928 | |
910 | dlen -= txtlen + 1; |
929 | dlen -= txtlen + 1; |
|
|
930 | } |
911 | } |
931 | } |
912 | |
932 | |
913 | // avoid empty TXT rdata |
933 | // avoid completely empty TXT rdata |
914 | if (offs == rdlen_offs) |
934 | if (offs == rdlen_offs) |
915 | pkt[offs++] = 0; |
935 | pkt[offs++] = 0; |
916 | |
936 | |
917 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
937 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
918 | } |
938 | } |
… | |
… | |
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 | |
1138 | void |
1161 | void |
1139 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1162 | vpn::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 | |
1164 | bool |
1187 | bool |
1165 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1188 | vpn::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 | |
1184 | void |
1206 | void |
1185 | connection::dnsv4_reset_connection () |
1207 | connection::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 | |
1192 | void |
1214 | void |
1193 | dns_connection::time_cb (time_watcher &w) |
1215 | dns_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 | cfg.reset (THISNODE->id); |
1261 | cfg.reset (THISNODE->id); |
1244 | send->gen_syn_req (); |
1262 | send->gen_syn_req (); |
1245 | } |
|
|
1246 | } |
1263 | } |
1247 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1264 | } |
|
|
1265 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1248 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1266 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
|
|
1267 | { |
|
|
1268 | if (last_sent + send_interval <= ev_now ()) |
1249 | { |
1269 | { |
1250 | //printf ("sending data request etc.\n"); //D |
1270 | //printf ("sending data request etc.\n"); //D |
1251 | if (!snddq.empty ()) |
1271 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
1252 | { |
1272 | { |
1253 | poll_interval = send_interval; |
1273 | poll_interval = send_interval; |
1254 | NEXT (NOW + send_interval); |
1274 | NEXT (ev_now () + send_interval); |
1255 | } |
1275 | } |
1256 | |
1276 | |
1257 | send = new dns_snd (this); |
1277 | send = new dns_snd (this); |
1258 | send->gen_stream_req (sndseq, snddq); |
1278 | send->gen_stream_req (sndseq, snddq); |
1259 | 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 |
1260 | |
1281 | |
1261 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1282 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1262 | } |
1283 | } |
1263 | |
1284 | else |
1264 | if (send) |
1285 | NEXT (last_sent + send_interval); |
1265 | vpn->dns_sndpq.push_back (send); |
|
|
1266 | } |
1286 | } |
1267 | |
1287 | |
1268 | if (send) |
1288 | if (send) |
1269 | { |
1289 | vpn->dns_sndpq.push_back (send); |
1270 | last_sent = NOW; |
1290 | } |
|
|
1291 | |
|
|
1292 | if (send) |
|
|
1293 | { |
|
|
1294 | last_sent = ev_now (); |
1271 | sendto (vpn->dnsv4_fd, |
1295 | sendto (vpn->dnsv4_fd, |
1272 | send->pkt->at (0), send->pkt->len, 0, |
1296 | send->pkt->at (0), send->pkt->len, 0, |
1273 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1297 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1274 | } |
|
|
1275 | } |
1298 | } |
1276 | else |
|
|
1277 | NEXT (last_sent + send_interval); |
|
|
1278 | |
1299 | |
1279 | 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)", |
1280 | poll_interval, send_interval, next - NOW, |
1301 | poll_interval, send_interval, next - ev_now (), |
1281 | vpn->dns_sndpq.size (), snddq.size ()); |
1302 | vpn->dns_sndpq.size (), snddq.size (), |
|
|
1303 | rcvpq.size ()); |
1282 | |
1304 | |
1283 | // 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 |
1284 | if (next < NOW + 0.0001) |
1307 | if (next < ev_now () + 0.001) |
1285 | next = NOW + 0.1; |
1308 | next = ev_now () + 0.1; |
1286 | |
1309 | |
1287 | w.start (next); |
1310 | w.start (next - ev_now ()); |
1288 | } |
1311 | } |
1289 | |
1312 | |
1290 | #endif |
1313 | #endif |
1291 | |
1314 | |