… | |
… | |
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 2. // optimistic? |
60 | #define MAX_SEND_INTERVAL 2. // optimistic? |
58 | |
61 | |
59 | #define LATENCY_FACTOR 0.5 // RTT * LATENCY_FACTOR == sending rate |
|
|
60 | #define MAX_OUTSTANDING 100 // max. outstanding requests |
|
|
61 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
62 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
62 | #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 |
63 | |
64 | |
64 | #define MAX_DOMAIN_SIZE 240 // 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 |
65 | // 240 leaves about 4 bytes of server reply data |
66 | // 240 leaves about 4 bytes of server reply data |
66 | // every two request bytes less give room for one reply byte |
67 | // every request byte less give room for two reply bytes |
67 | |
68 | |
68 | #define SEQNO_MASK 0x3fff |
69 | #define SEQNO_MASK 0x3fff |
69 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
70 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
70 | |
71 | |
71 | #define MAX_LBL_SIZE 63 |
72 | #define MAX_LBL_SIZE 63 |
… | |
… | |
424 | u16 max_size; |
425 | u16 max_size; |
425 | u8 seq_cdc; |
426 | u8 seq_cdc; |
426 | u8 req_cdc; |
427 | u8 req_cdc; |
427 | |
428 | |
428 | u8 rep_cdc; |
429 | u8 rep_cdc; |
|
|
430 | u8 delay; // time in 0.01s units that the server may delay replying packets |
429 | u8 r2, r3, r4; |
431 | u8 r3, r4; |
430 | |
432 | |
431 | u8 r5, r6, r7, r8; |
433 | u8 r5, r6, r7, r8; |
432 | |
434 | |
433 | void reset (int clientid); |
435 | void reset (int clientid); |
434 | bool valid (); |
436 | bool valid (); |
… | |
… | |
452 | req_cdc = 62; |
454 | req_cdc = 62; |
453 | rep_cdc = 0; |
455 | rep_cdc = 0; |
454 | max_size = htons (MAX_PKT_SIZE); |
456 | max_size = htons (MAX_PKT_SIZE); |
455 | client = htons (clientid); |
457 | client = htons (clientid); |
456 | uid = next_uid++; |
458 | uid = next_uid++; |
|
|
459 | delay = 0; |
457 | |
460 | |
458 | r2 = r3 = r4 = 0; |
461 | r3 = r4 = 0; |
459 | r4 = r5 = r6 = r7 = 0; |
462 | r4 = r5 = r6 = r7 = 0; |
460 | } |
463 | } |
461 | |
464 | |
462 | bool dns_cfg::valid () |
465 | bool dns_cfg::valid () |
463 | { |
466 | { |
|
|
467 | // although the protocol itself allows for some configurability, |
|
|
468 | // only the following encoding/decoding settings are implemented. |
464 | return id1 == 'G' |
469 | return id1 == 'G' |
465 | && id2 == 'V' |
470 | && id2 == 'V' |
466 | && id3 == 'P' |
471 | && id3 == 'P' |
467 | && id4 == 'E' |
472 | && id4 == 'E' |
468 | && seq_cdc == 26 |
473 | && seq_cdc == 26 |
… | |
… | |
552 | double min_latency; |
557 | double min_latency; |
553 | double poll_interval, send_interval; |
558 | double poll_interval, send_interval; |
554 | |
559 | |
555 | vector<dns_rcv *> rcvpq; |
560 | vector<dns_rcv *> rcvpq; |
556 | |
561 | |
557 | byte_stream rcvdq; int rcvseq; |
562 | byte_stream rcvdq; int rcvseq; int repseq; |
558 | byte_stream snddq; int sndseq; |
563 | byte_stream snddq; int sndseq; |
559 | |
564 | |
560 | void time_cb (time_watcher &w); time_watcher tw; |
565 | void time_cb (ev::timer &w, int revents); ev::timer tw; |
561 | void receive_rep (dns_rcv *r); |
566 | void receive_rep (dns_rcv *r); |
562 | |
567 | |
563 | dns_connection (connection *c); |
568 | dns_connection (connection *c); |
564 | ~dns_connection (); |
569 | ~dns_connection (); |
565 | }; |
570 | }; |
… | |
… | |
584 | : dns (dns) |
589 | : dns (dns) |
585 | { |
590 | { |
586 | timeout = 0; |
591 | timeout = 0; |
587 | retry = 0; |
592 | retry = 0; |
588 | seqno = 0; |
593 | seqno = 0; |
589 | sent = NOW; |
594 | sent = ev_now (); |
590 | stdhdr = false; |
595 | stdhdr = false; |
591 | |
596 | |
592 | pkt = new dns_packet; |
597 | pkt = new dns_packet; |
593 | |
598 | |
594 | pkt->id = next_id (); |
599 | pkt->id = next_id (); |
… | |
… | |
625 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
630 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
626 | { |
631 | { |
627 | stdhdr = true; |
632 | stdhdr = true; |
628 | this->seqno = seqno; |
633 | this->seqno = seqno; |
629 | |
634 | |
630 | timeout = NOW + INITIAL_TIMEOUT; |
635 | timeout = ev_now () + INITIAL_TIMEOUT; |
631 | |
636 | |
632 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
637 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
633 | pkt->qdcount = htons (1); |
638 | pkt->qdcount = htons (1); |
634 | |
639 | |
635 | int offs = 6*2; |
640 | int offs = 6*2; |
… | |
… | |
670 | pkt->len = offs; |
675 | pkt->len = offs; |
671 | } |
676 | } |
672 | |
677 | |
673 | void dns_snd::gen_syn_req () |
678 | void dns_snd::gen_syn_req () |
674 | { |
679 | { |
675 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
680 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
676 | |
681 | |
677 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
682 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
678 | pkt->qdcount = htons (1); |
683 | pkt->qdcount = htons (1); |
679 | |
684 | |
680 | int offs = 6 * 2; |
685 | int offs = 6 * 2; |
… | |
… | |
719 | ///////////////////////////////////////////////////////////////////////////// |
724 | ///////////////////////////////////////////////////////////////////////////// |
720 | |
725 | |
721 | dns_connection::dns_connection (connection *c) |
726 | dns_connection::dns_connection (connection *c) |
722 | : c (c) |
727 | : c (c) |
723 | , rcvdq (MAX_BACKLOG * 2) |
728 | , rcvdq (MAX_BACKLOG * 2) |
724 | , snddq (MAX_BACKLOG * 2) |
729 | , snddq (MAX_BACKLOG) |
725 | , tw (this, &dns_connection::time_cb) |
730 | , tw (this, &dns_connection::time_cb) |
726 | { |
731 | { |
727 | vpn = c->vpn; |
732 | vpn = c->vpn; |
728 | |
733 | |
729 | established = false; |
734 | established = false; |
730 | |
735 | |
731 | rcvseq = sndseq = 0; |
736 | rcvseq = repseq = sndseq = 0; |
732 | |
737 | |
733 | last_sent = last_received = 0; |
738 | last_sent = last_received = 0; |
734 | poll_interval = MIN_POLL_INTERVAL; |
739 | poll_interval = 0.5; // starting here |
735 | send_interval = 0.5; // starting rate |
740 | send_interval = 0.5; // starting rate |
736 | min_latency = INITIAL_TIMEOUT; |
741 | min_latency = INITIAL_TIMEOUT; |
737 | } |
742 | } |
738 | |
743 | |
739 | dns_connection::~dns_connection () |
744 | dns_connection::~dns_connection () |
… | |
… | |
746 | |
751 | |
747 | void dns_connection::receive_rep (dns_rcv *r) |
752 | void dns_connection::receive_rep (dns_rcv *r) |
748 | { |
753 | { |
749 | if (r->datalen) |
754 | if (r->datalen) |
750 | { |
755 | { |
751 | last_received = NOW; |
756 | last_received = ev_now (); |
752 | tw.trigger (); |
757 | tw (); |
753 | |
758 | |
754 | poll_interval = send_interval; |
759 | poll_interval = send_interval; |
755 | } |
760 | } |
756 | else |
761 | else |
757 | { |
762 | { |
… | |
… | |
767 | |
772 | |
768 | // find next packet |
773 | // find next packet |
769 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
774 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
770 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
775 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
771 | { |
776 | { |
|
|
777 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
772 | // enter the packet into our input stream |
778 | // enter the packet into our input stream |
773 | r = *i; |
779 | r = *i; |
774 | |
780 | |
775 | // remove the oldest packet, look forward, as it's oldest first |
781 | // remove the oldest packet, look forward, as it's oldest first |
776 | 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) |
777 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
783 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
778 | { |
784 | { |
|
|
785 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
779 | delete *j; |
786 | delete *j; |
780 | rcvpq.erase (j); |
787 | rcvpq.erase (j); |
781 | break; |
788 | break; |
782 | } |
789 | } |
783 | |
790 | |
… | |
… | |
790 | } |
797 | } |
791 | |
798 | |
792 | while (vpn_packet *pkt = rcvdq.get ()) |
799 | while (vpn_packet *pkt = rcvdq.get ()) |
793 | { |
800 | { |
794 | sockinfo si; |
801 | sockinfo si; |
795 | 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; |
796 | |
803 | |
797 | vpn->recv_vpn_packet (pkt, si); |
804 | vpn->recv_vpn_packet (pkt, si); |
798 | |
805 | |
799 | delete pkt; |
806 | delete pkt; |
800 | } |
807 | } |
… | |
… | |
854 | if (0 < client && client <= conns.size ()) |
861 | if (0 < client && client <= conns.size ()) |
855 | { |
862 | { |
856 | connection *c = conns [client - 1]; |
863 | connection *c = conns [client - 1]; |
857 | dns_connection *dns = c->dns; |
864 | dns_connection *dns = c->dns; |
858 | dns_rcv *rcv; |
865 | dns_rcv *rcv; |
859 | bool in_seq; |
|
|
860 | |
866 | |
861 | if (dns) |
867 | if (dns) |
862 | { |
868 | { |
863 | 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 (); ) |
864 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
870 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
… | |
… | |
875 | 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); |
876 | |
882 | |
877 | goto duplicate_request; |
883 | goto duplicate_request; |
878 | } |
884 | } |
879 | |
885 | |
880 | in_seq = dns->rcvseq == seqno; |
|
|
881 | |
|
|
882 | // new packet, queue |
886 | // new packet, queue |
883 | rcv = new dns_rcv (seqno, data, datalen); |
887 | rcv = new dns_rcv (seqno, data, datalen); |
884 | dns->receive_rep (rcv); |
888 | dns->receive_rep (rcv); |
885 | } |
889 | } |
886 | |
890 | |
… | |
… | |
903 | dlen -= qlen; |
907 | dlen -= qlen; |
904 | |
908 | |
905 | // only put data into in-order sequence packets, if |
909 | // only put data into in-order sequence packets, if |
906 | // we receive out-of-order packets we generate empty |
910 | // we receive out-of-order packets we generate empty |
907 | // replies |
911 | // replies |
908 | 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) |
909 | { |
914 | { |
|
|
915 | dns->repseq = seqno; |
|
|
916 | |
|
|
917 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
918 | { |
910 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
919 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
911 | |
920 | |
912 | if (txtlen > dns->snddq.size ()) |
921 | if (txtlen > dns->snddq.size ()) |
913 | txtlen = dns->snddq.size (); |
922 | txtlen = dns->snddq.size (); |
914 | |
923 | |
915 | pkt[offs++] = txtlen; |
924 | pkt[offs++] = txtlen; |
916 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
925 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
917 | offs += txtlen; |
926 | offs += txtlen; |
918 | dns->snddq.remove (txtlen); |
927 | dns->snddq.remove (txtlen); |
919 | |
928 | |
920 | dlen -= txtlen + 1; |
929 | dlen -= txtlen + 1; |
|
|
930 | } |
921 | } |
931 | } |
922 | |
932 | |
923 | // avoid empty TXT rdata |
933 | // avoid completely empty TXT rdata |
924 | if (offs == rdlen_offs) |
934 | if (offs == rdlen_offs) |
925 | pkt[offs++] = 0; |
935 | pkt[offs++] = 0; |
926 | |
936 | |
927 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
937 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
928 | } |
938 | } |
… | |
… | |
1008 | { |
1018 | { |
1009 | dns_connection *dns = (*i)->dns; |
1019 | dns_connection *dns = (*i)->dns; |
1010 | connection *c = dns->c; |
1020 | connection *c = dns->c; |
1011 | int seqno = (*i)->seqno; |
1021 | int seqno = (*i)->seqno; |
1012 | u8 data[MAXSIZE], *datap = data; |
1022 | u8 data[MAXSIZE], *datap = data; |
|
|
1023 | //printf ("rcv pkt %x\n", seqno);//D |
1013 | |
1024 | |
1014 | if ((*i)->retry) |
1025 | if ((*i)->retry) |
1015 | { |
1026 | { |
1016 | dns->send_interval *= 1.01; |
1027 | dns->send_interval *= 1.01; |
1017 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1028 | if (dns->send_interval > MAX_SEND_INTERVAL) |
… | |
… | |
1022 | #if 0 |
1033 | #if 0 |
1023 | dns->send_interval *= 0.999; |
1034 | dns->send_interval *= 0.999; |
1024 | #endif |
1035 | #endif |
1025 | // the latency surely puts an upper bound on |
1036 | // the latency surely puts an upper bound on |
1026 | // the minimum send interval |
1037 | // the minimum send interval |
1027 | double latency = NOW - (*i)->sent; |
1038 | double latency = ev_now () - (*i)->sent; |
1028 | |
1039 | |
1029 | if (latency < dns->min_latency) |
1040 | if (latency < dns->min_latency) |
1030 | dns->min_latency = latency; |
1041 | dns->min_latency = latency; |
1031 | |
1042 | |
1032 | if (dns->send_interval > dns->min_latency * LATENCY_FACTOR) |
1043 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
1033 | dns->send_interval = dns->min_latency * LATENCY_FACTOR; |
1044 | dns->send_interval = dns->min_latency * conf.dns_overlap_factor; |
1034 | |
1045 | |
1035 | if (dns->send_interval < MIN_SEND_INTERVAL) |
1046 | if (dns->send_interval < conf.dns_send_interval) |
1036 | dns->send_interval = MIN_SEND_INTERVAL; |
1047 | dns->send_interval = conf.dns_send_interval; |
1037 | } |
1048 | } |
1038 | |
1049 | |
1039 | delete *i; |
1050 | delete *i; |
1040 | dns_sndpq.erase (i); |
1051 | dns_sndpq.erase (i); |
1041 | |
1052 | |
… | |
… | |
1146 | break; |
1157 | break; |
1147 | } |
1158 | } |
1148 | } |
1159 | } |
1149 | |
1160 | |
1150 | void |
1161 | void |
1151 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1162 | vpn::dnsv4_ev (ev::io &w, int revents) |
1152 | { |
1163 | { |
1153 | if (revents & EVENT_READ) |
1164 | if (revents & EV_READ) |
1154 | { |
1165 | { |
1155 | dns_packet *pkt = new dns_packet; |
1166 | dns_packet *pkt = new dns_packet; |
1156 | struct sockaddr_in sa; |
1167 | struct sockaddr_in sa; |
1157 | socklen_t sa_len = sizeof (sa); |
1168 | socklen_t sa_len = sizeof (sa); |
1158 | |
1169 | |
… | |
… | |
1174 | } |
1185 | } |
1175 | |
1186 | |
1176 | bool |
1187 | bool |
1177 | 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) |
1178 | { |
1189 | { |
1179 | int client = ntohs (si.port); |
1190 | int client = ntohl (si.host); |
1180 | |
1191 | |
1181 | assert (0 < client && client <= conns.size ()); |
1192 | assert (0 < client && client <= conns.size ()); |
1182 | |
1193 | |
1183 | connection *c = conns [client - 1]; |
1194 | connection *c = conns [client - 1]; |
1184 | |
1195 | |
1185 | if (!c->dns) |
1196 | if (!c->dns) |
1186 | c->dns = new dns_connection (c); |
1197 | c->dns = new dns_connection (c); |
1187 | |
1198 | |
1188 | if (!c->dns->snddq.put (pkt)) |
1199 | if (c->dns->snddq.put (pkt)) |
1189 | return false; |
|
|
1190 | |
|
|
1191 | c->dns->tw.trigger (); |
1200 | c->dns->tw (); |
1192 | |
1201 | |
|
|
1202 | // always return true even if the buffer overflows |
1193 | return true; |
1203 | return true; |
1194 | } |
1204 | } |
1195 | |
1205 | |
1196 | void |
1206 | void |
1197 | connection::dnsv4_reset_connection () |
1207 | connection::dnsv4_reset_connection () |
… | |
… | |
1200 | } |
1210 | } |
1201 | |
1211 | |
1202 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1212 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1203 | |
1213 | |
1204 | void |
1214 | void |
1205 | dns_connection::time_cb (time_watcher &w) |
1215 | dns_connection::time_cb (ev::timer &w, int revents) |
1206 | { |
1216 | { |
1207 | // servers have to be polled |
1217 | // servers have to be polled |
1208 | if (THISNODE->dns_port) |
1218 | if (THISNODE->dns_port) |
1209 | return; |
1219 | return; |
1210 | |
1220 | |
1211 | // check for timeouts and (re)transmit |
1221 | // check for timeouts and (re)transmit |
1212 | tstamp next = NOW + poll_interval; |
1222 | tstamp next = ev::now () + poll_interval; |
1213 | dns_snd *send = 0; |
1223 | dns_snd *send = 0; |
1214 | |
1224 | |
1215 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1225 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1216 | i != vpn->dns_sndpq.end (); |
1226 | i != vpn->dns_sndpq.end (); |
1217 | ++i) |
1227 | ++i) |
1218 | { |
1228 | { |
1219 | dns_snd *r = *i; |
1229 | dns_snd *r = *i; |
1220 | |
1230 | |
1221 | if (r->timeout <= NOW) |
1231 | if (r->timeout <= ev_now ()) |
1222 | { |
1232 | { |
1223 | if (!send) |
1233 | if (!send) |
1224 | { |
1234 | { |
1225 | send = r; |
1235 | send = r; |
1226 | |
1236 | |
1227 | r->retry++; |
1237 | r->retry++; |
1228 | r->timeout = NOW + (r->retry * min_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 |
1229 | |
1240 | |
1230 | // the following code changes the query section a bit, forcing |
1241 | // the following code changes the query section a bit, forcing |
1231 | // the forwarder to generate a new request |
1242 | // the forwarder to generate a new request |
1232 | if (r->stdhdr) |
1243 | if (r->stdhdr) |
1233 | { |
|
|
1234 | //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
|
|
1235 | //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); |
1236 | } |
|
|
1237 | } |
1245 | } |
1238 | } |
1246 | } |
1239 | else |
1247 | else |
1240 | NEXT (r->timeout); |
1248 | NEXT (r->timeout); |
1241 | } |
1249 | } |
1242 | |
1250 | |
1243 | if (!send) |
1251 | if (!send) |
1244 | { |
1252 | { |
1245 | if (last_sent + send_interval <= NOW) |
1253 | // generate a new packet, if wise |
|
|
1254 | |
|
|
1255 | if (!established) |
1246 | { |
1256 | { |
1247 | // generate a new packet, if wise |
1257 | if (vpn->dns_sndpq.empty ()) |
1248 | |
|
|
1249 | if (!established) |
|
|
1250 | { |
1258 | { |
1251 | if (vpn->dns_sndpq.empty ()) |
|
|
1252 | { |
|
|
1253 | send = new dns_snd (this); |
1259 | send = new dns_snd (this); |
1254 | |
1260 | |
1255 | cfg.reset (THISNODE->id); |
1261 | cfg.reset (THISNODE->id); |
1256 | send->gen_syn_req (); |
1262 | send->gen_syn_req (); |
1257 | } |
|
|
1258 | } |
1263 | } |
1259 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1264 | } |
|
|
1265 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1260 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1266 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
|
|
1267 | { |
|
|
1268 | if (last_sent + send_interval <= ev_now ()) |
1261 | { |
1269 | { |
1262 | //printf ("sending data request etc.\n"); //D |
1270 | //printf ("sending data request etc.\n"); //D |
1263 | if (!snddq.empty () || last_received + 1. > NOW) |
1271 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
1264 | { |
1272 | { |
1265 | poll_interval = send_interval; |
1273 | poll_interval = send_interval; |
1266 | NEXT (NOW + send_interval); |
1274 | NEXT (ev_now () + send_interval); |
1267 | } |
1275 | } |
1268 | |
1276 | |
1269 | send = new dns_snd (this); |
1277 | send = new dns_snd (this); |
1270 | send->gen_stream_req (sndseq, snddq); |
1278 | send->gen_stream_req (sndseq, snddq); |
1271 | send->timeout = NOW + min_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 |
1272 | |
1281 | |
1273 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1282 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1274 | } |
1283 | } |
1275 | |
1284 | else |
1276 | if (send) |
1285 | NEXT (last_sent + send_interval); |
1277 | vpn->dns_sndpq.push_back (send); |
|
|
1278 | } |
1286 | } |
1279 | else |
1287 | |
1280 | NEXT (last_sent + send_interval); |
1288 | if (send) |
|
|
1289 | vpn->dns_sndpq.push_back (send); |
1281 | } |
1290 | } |
1282 | |
1291 | |
1283 | if (send) |
1292 | if (send) |
1284 | { |
1293 | { |
1285 | last_sent = NOW; |
1294 | last_sent = ev_now (); |
1286 | sendto (vpn->dnsv4_fd, |
1295 | sendto (vpn->dnsv4_fd, |
1287 | send->pkt->at (0), send->pkt->len, 0, |
1296 | send->pkt->at (0), send->pkt->len, 0, |
1288 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1297 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1289 | } |
1298 | } |
1290 | |
1299 | |
1291 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1300 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1292 | poll_interval, send_interval, next - NOW, |
1301 | poll_interval, send_interval, next - ev_now (), |
1293 | vpn->dns_sndpq.size (), snddq.size (), |
1302 | vpn->dns_sndpq.size (), snddq.size (), |
1294 | rcvpq.size ()); |
1303 | rcvpq.size ()); |
1295 | |
1304 | |
1296 | // 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 |
1297 | if (next < NOW + 0.0001) |
1307 | if (next < ev_now () + 0.001) |
1298 | next = NOW + 0.1; |
1308 | next = ev_now () + 0.1; |
1299 | |
1309 | |
1300 | w.start (next); |
1310 | w.start (next - ev_now ()); |
1301 | } |
1311 | } |
1302 | |
1312 | |
1303 | #endif |
1313 | #endif |
1304 | |
1314 | |