1 | /* |
1 | /* |
2 | vpn_dns.C -- handle the dns tunnel part of the protocol. |
2 | vpn_dns.C -- handle the dns tunnel part of the protocol. |
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | This file is part of GVPE. |
5 | This file is part of GVPE. |
6 | |
6 | |
7 | GVPE is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify it |
8 | it under the terms of the GNU General Public License as published by |
8 | under the terms of the GNU General Public License as published by the |
9 | the Free Software Foundation; either version 2 of the License, or |
9 | Free Software Foundation; either version 3 of the License, or (at your |
10 | (at your option) any later version. |
10 | option) any later version. |
11 | |
11 | |
12 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, but |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
15 | GNU General Public License for more details. |
15 | 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 along |
18 | along with gvpe; if not, write to the Free Software |
18 | with this program; if not, see <http://www.gnu.org/licenses/>. |
19 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | |
|
|
20 | Additional permission under GNU GPL version 3 section 7 |
|
|
21 | |
|
|
22 | If you modify this Program, or any covered work, by linking or |
|
|
23 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
24 | version of that library), containing parts covered by the terms of the |
|
|
25 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
26 | additional permission to convey the resulting work. Corresponding |
|
|
27 | Source for a non-source form of such a combination shall include the |
|
|
28 | source code for the parts of OpenSSL used as well as that of the |
|
|
29 | covered work. |
20 | */ |
30 | */ |
|
|
31 | |
|
|
32 | // TODO: EDNS0 option to increase dns mtu? |
|
|
33 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
|
|
34 | // to ensure no buffer overflows or similar problems. |
21 | |
35 | |
22 | #include "config.h" |
36 | #include "config.h" |
23 | |
37 | |
24 | #if ENABLE_DNS |
38 | #if ENABLE_DNS |
25 | |
39 | |
… | |
… | |
38 | #include <unistd.h> |
52 | #include <unistd.h> |
39 | #include <fcntl.h> |
53 | #include <fcntl.h> |
40 | |
54 | |
41 | #include <map> |
55 | #include <map> |
42 | |
56 | |
|
|
57 | #include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */ |
43 | #include <gmp.h> |
58 | #include <gmp.h> |
44 | |
59 | |
45 | #include "netcompat.h" |
60 | #include "netcompat.h" |
46 | |
61 | |
47 | #include "vpn.h" |
62 | #include "vpn.h" |
48 | |
63 | |
49 | #define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data |
64 | #define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data |
50 | #define ACTIVITY_INTERVAL 5. |
65 | #define ACTIVITY_INTERVAL 5. |
51 | |
66 | |
52 | #define TIMEOUT_FACTOR 4. |
|
|
53 | |
|
|
54 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
67 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
55 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
68 | #define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn |
56 | |
69 | |
57 | #define MIN_SEND_INTERVAL 0.001 // wait at least this time between sending requests |
|
|
58 | #define MAX_SEND_INTERVAL 2. // optimistic? |
70 | #define MAX_SEND_INTERVAL 2. // optimistic? |
59 | |
71 | |
60 | #define LATENCY_FACTOR 0.5 // RTT * LATENCY_FACTOR == sending rate |
|
|
61 | #define MAX_OUTSTANDING 100 // max. outstanding requests |
|
|
62 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
72 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
63 | #define MAX_BACKLOG (32*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
73 | #define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
64 | |
74 | |
65 | #define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well |
75 | #define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well |
66 | // 240 leaves about 4 bytes of server reply data |
76 | // 240 leaves about 4 bytes of server reply data |
67 | // every two request bytes less give room for one reply byte |
77 | // every request byte less give room for two reply bytes |
68 | |
78 | |
69 | #define SEQNO_MASK 0x3fff |
79 | #define SEQNO_MASK 0x3fff |
70 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
80 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
71 | |
81 | |
72 | #define MAX_LBL_SIZE 63 |
82 | #define MAX_LBL_SIZE 63 |
73 | #define MAX_PKT_SIZE 512 |
83 | #define MAX_PKT_SIZE 512 |
74 | |
84 | |
75 | #define RR_TYPE_A 1 |
85 | #define RR_TYPE_A 1 |
76 | #define RR_TYPE_NULL 10 |
86 | #define RR_TYPE_NULL 10 |
77 | #define RR_TYPE_TXT 16 |
87 | #define RR_TYPE_TXT 16 |
|
|
88 | #define RR_TYPE_AAAA 28 |
78 | #define RR_TYPE_ANY 255 |
89 | #define RR_TYPE_ANY 255 |
79 | |
90 | |
80 | #define RR_CLASS_IN 1 |
91 | #define RR_CLASS_IN 1 |
81 | |
92 | |
82 | #define CMD_IP_1 207 |
93 | #define CMD_IP_1 207 |
… | |
… | |
157 | enc_len [len] = n; |
168 | enc_len [len] = n; |
158 | dec_len [n] = len; |
169 | dec_len [n] = len; |
159 | } |
170 | } |
160 | } |
171 | } |
161 | |
172 | |
|
|
173 | unsigned int |
162 | unsigned int basecoder::encode_len (unsigned int len) |
174 | basecoder::encode_len (unsigned int len) |
163 | { |
175 | { |
164 | return enc_len [len]; |
176 | return enc_len [len]; |
165 | } |
177 | } |
166 | |
178 | |
|
|
179 | unsigned int |
167 | unsigned int basecoder::decode_len (unsigned int len) |
180 | basecoder::decode_len (unsigned int len) |
168 | { |
181 | { |
169 | while (len && !dec_len [len]) |
182 | while (len && !dec_len [len]) |
170 | --len; |
183 | --len; |
171 | |
184 | |
172 | return dec_len [len]; |
185 | return dec_len [len]; |
173 | } |
186 | } |
174 | |
187 | |
|
|
188 | unsigned int |
175 | unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
189 | basecoder::encode (char *dst, u8 *src, unsigned int len) |
176 | { |
190 | { |
177 | if (!len || len > MAX_DEC_LEN) |
191 | if (!len || len > MAX_DEC_LEN) |
178 | return 0; |
192 | return 0; |
179 | |
193 | |
180 | int elen = encode_len (len); |
194 | int elen = encode_len (len); |
… | |
… | |
199 | *dst++ = cmap.encode [dst_ [i]]; |
213 | *dst++ = cmap.encode [dst_ [i]]; |
200 | |
214 | |
201 | return elen; |
215 | return elen; |
202 | } |
216 | } |
203 | |
217 | |
|
|
218 | unsigned int |
204 | unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
219 | basecoder::decode (u8 *dst, char *src, unsigned int len) |
205 | { |
220 | { |
206 | if (!len || len > MAX_ENC_LEN) |
221 | if (!len || len > MAX_ENC_LEN) |
207 | return 0; |
222 | return 0; |
208 | |
223 | |
209 | u8 src_ [MAX_ENC_LEN]; |
224 | u8 src_ [MAX_ENC_LEN]; |
… | |
… | |
268 | |
283 | |
269 | ///////////////////////////////////////////////////////////////////////////// |
284 | ///////////////////////////////////////////////////////////////////////////// |
270 | |
285 | |
271 | #define HDRSIZE 6 |
286 | #define HDRSIZE 6 |
272 | |
287 | |
|
|
288 | inline void |
273 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
289 | encode_header (char *data, int clientid, int seqno, int retry = 0) |
274 | { |
290 | { |
275 | seqno &= SEQNO_MASK; |
291 | seqno &= SEQNO_MASK; |
276 | |
292 | |
277 | u8 hdr[3] = { |
293 | u8 hdr[3] = { |
278 | clientid, |
294 | clientid, |
… | |
… | |
283 | assert (clientid < 256); |
299 | assert (clientid < 256); |
284 | |
300 | |
285 | cdc26.encode (data, hdr, 3); |
301 | cdc26.encode (data, hdr, 3); |
286 | } |
302 | } |
287 | |
303 | |
|
|
304 | inline void |
288 | inline void decode_header (char *data, int &clientid, int &seqno) |
305 | decode_header (char *data, int &clientid, int &seqno) |
289 | { |
306 | { |
290 | u8 hdr[3]; |
307 | u8 hdr[3]; |
291 | |
308 | |
292 | cdc26.decode (hdr, data, HDRSIZE); |
309 | cdc26.decode (hdr, data, HDRSIZE); |
293 | |
310 | |
… | |
… | |
326 | byte_stream::~byte_stream () |
343 | byte_stream::~byte_stream () |
327 | { |
344 | { |
328 | delete data; |
345 | delete data; |
329 | } |
346 | } |
330 | |
347 | |
|
|
348 | void |
331 | void byte_stream::remove (int count) |
349 | byte_stream::remove (int count) |
332 | { |
350 | { |
333 | if (count > fill) |
351 | if (count > fill) |
334 | assert (count <= fill); |
352 | assert (count <= fill); |
335 | |
353 | |
336 | memmove (data, data + count, fill -= count); |
354 | memmove (data, data + count, fill -= count); |
337 | } |
355 | } |
338 | |
356 | |
|
|
357 | bool |
339 | bool byte_stream::put (u8 *data, unsigned int datalen) |
358 | byte_stream::put (u8 *data, unsigned int datalen) |
340 | { |
359 | { |
341 | if (maxsize - fill < datalen) |
360 | if (maxsize - fill < datalen) |
342 | return false; |
361 | return false; |
343 | |
362 | |
344 | memcpy (this->data + fill, data, datalen); fill += datalen; |
363 | memcpy (this->data + fill, data, datalen); fill += datalen; |
345 | |
364 | |
346 | return true; |
365 | return true; |
347 | } |
366 | } |
348 | |
367 | |
|
|
368 | bool |
349 | bool byte_stream::put (vpn_packet *pkt) |
369 | byte_stream::put (vpn_packet *pkt) |
350 | { |
370 | { |
351 | if (maxsize - fill < pkt->len + 2) |
371 | if (maxsize - fill < pkt->len + 2) |
352 | return false; |
372 | return false; |
353 | |
373 | |
354 | data [fill++] = pkt->len >> 8; |
374 | data [fill++] = pkt->len >> 8; |
… | |
… | |
436 | bool valid (); |
456 | bool valid (); |
437 | }; |
457 | }; |
438 | |
458 | |
439 | int dns_cfg::next_uid; |
459 | int dns_cfg::next_uid; |
440 | |
460 | |
|
|
461 | void |
441 | void dns_cfg::reset (int clientid) |
462 | dns_cfg::reset (int clientid) |
442 | { |
463 | { |
443 | id1 = 'G'; |
464 | id1 = 'G'; |
444 | id2 = 'V'; |
465 | id2 = 'V'; |
445 | id3 = 'P'; |
466 | id3 = 'P'; |
446 | id4 = 'E'; |
467 | id4 = 'E'; |
… | |
… | |
460 | |
481 | |
461 | r3 = r4 = 0; |
482 | r3 = r4 = 0; |
462 | r4 = r5 = r6 = r7 = 0; |
483 | r4 = r5 = r6 = r7 = 0; |
463 | } |
484 | } |
464 | |
485 | |
|
|
486 | bool |
465 | bool dns_cfg::valid () |
487 | dns_cfg::valid () |
466 | { |
488 | { |
|
|
489 | // although the protocol itself allows for some configurability, |
|
|
490 | // only the following encoding/decoding settings are implemented. |
467 | return id1 == 'G' |
491 | return id1 == 'G' |
468 | && id2 == 'V' |
492 | && id2 == 'V' |
469 | && id3 == 'P' |
493 | && id3 == 'P' |
470 | && id4 == 'E' |
494 | && id4 == 'E' |
471 | && seq_cdc == 26 |
495 | && seq_cdc == 26 |
… | |
… | |
483 | u8 data [MAXSIZE - 6 * 2]; |
507 | u8 data [MAXSIZE - 6 * 2]; |
484 | |
508 | |
485 | int decode_label (char *data, int size, int &offs); |
509 | int decode_label (char *data, int size, int &offs); |
486 | }; |
510 | }; |
487 | |
511 | |
|
|
512 | int |
488 | int dns_packet::decode_label (char *data, int size, int &offs) |
513 | dns_packet::decode_label (char *data, int size, int &offs) |
489 | { |
514 | { |
490 | char *orig = data; |
515 | char *orig = data; |
491 | |
516 | |
492 | memset (data, 0, size); |
517 | memset (data, 0, size); |
493 | |
518 | |
… | |
… | |
519 | return data - orig; |
544 | return data - orig; |
520 | } |
545 | } |
521 | |
546 | |
522 | ///////////////////////////////////////////////////////////////////////////// |
547 | ///////////////////////////////////////////////////////////////////////////// |
523 | |
548 | |
|
|
549 | static |
|
|
550 | u16 next_id () |
|
|
551 | { |
524 | static u16 dns_id = 0; // TODO: should be per-vpn |
552 | static u16 dns_id = 0; // TODO: should be per-vpn |
525 | |
553 | |
526 | static u16 next_id () |
|
|
527 | { |
|
|
528 | if (!dns_id) |
554 | if (!dns_id) |
529 | dns_id = time (0); |
555 | dns_id = time (0); |
530 | |
556 | |
531 | // the simplest lsfr with periodicity 65535 i could find |
557 | // the simplest lsfr with periodicity 65535 i could find |
532 | dns_id = (dns_id << 1) |
558 | dns_id = (dns_id << 1) |
… | |
… | |
558 | vector<dns_rcv *> rcvpq; |
584 | vector<dns_rcv *> rcvpq; |
559 | |
585 | |
560 | byte_stream rcvdq; int rcvseq; int repseq; |
586 | byte_stream rcvdq; int rcvseq; int repseq; |
561 | byte_stream snddq; int sndseq; |
587 | byte_stream snddq; int sndseq; |
562 | |
588 | |
563 | void time_cb (time_watcher &w); time_watcher tw; |
589 | inline void time_cb (ev::timer &w, int revents); ev::timer tw; |
564 | void receive_rep (dns_rcv *r); |
590 | void receive_rep (dns_rcv *r); |
565 | |
591 | |
566 | dns_connection (connection *c); |
592 | dns_connection (connection *c); |
567 | ~dns_connection (); |
593 | ~dns_connection (); |
568 | }; |
594 | }; |
… | |
… | |
587 | : dns (dns) |
613 | : dns (dns) |
588 | { |
614 | { |
589 | timeout = 0; |
615 | timeout = 0; |
590 | retry = 0; |
616 | retry = 0; |
591 | seqno = 0; |
617 | seqno = 0; |
592 | sent = NOW; |
618 | sent = ev_now (); |
593 | stdhdr = false; |
619 | stdhdr = false; |
594 | |
620 | |
595 | pkt = new dns_packet; |
621 | pkt = new dns_packet; |
596 | |
622 | |
597 | pkt->id = next_id (); |
623 | pkt->id = next_id (); |
… | |
… | |
600 | dns_snd::~dns_snd () |
626 | dns_snd::~dns_snd () |
601 | { |
627 | { |
602 | delete pkt; |
628 | delete pkt; |
603 | } |
629 | } |
604 | |
630 | |
|
|
631 | static void |
605 | static void append_domain (dns_packet &pkt, int &offs, const char *domain) |
632 | append_domain (dns_packet &pkt, int &offs, const char *domain) |
606 | { |
633 | { |
607 | // add tunnel domain |
634 | // add tunnel domain |
608 | for (;;) |
635 | for (;;) |
609 | { |
636 | { |
610 | const char *end = strchr (domain, '.'); |
637 | const char *end = strchr (domain, '.'); |
… | |
… | |
623 | |
650 | |
624 | domain = end + 1; |
651 | domain = end + 1; |
625 | } |
652 | } |
626 | } |
653 | } |
627 | |
654 | |
|
|
655 | void |
628 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
656 | dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
629 | { |
657 | { |
630 | stdhdr = true; |
658 | stdhdr = true; |
631 | this->seqno = seqno; |
659 | this->seqno = seqno; |
632 | |
660 | |
633 | timeout = NOW + INITIAL_TIMEOUT; |
661 | timeout = ev_now () + INITIAL_TIMEOUT; |
634 | |
662 | |
635 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
663 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
636 | pkt->qdcount = htons (1); |
664 | pkt->qdcount = htons (1); |
637 | |
665 | |
638 | int offs = 6*2; |
666 | int offs = 6*2; |
… | |
… | |
671 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
699 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
672 | |
700 | |
673 | pkt->len = offs; |
701 | pkt->len = offs; |
674 | } |
702 | } |
675 | |
703 | |
|
|
704 | void |
676 | void dns_snd::gen_syn_req () |
705 | dns_snd::gen_syn_req () |
677 | { |
706 | { |
678 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
707 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
679 | |
708 | |
680 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
709 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
681 | pkt->qdcount = htons (1); |
710 | pkt->qdcount = htons (1); |
682 | |
711 | |
683 | int offs = 6 * 2; |
712 | int offs = 6 * 2; |
… | |
… | |
723 | |
752 | |
724 | dns_connection::dns_connection (connection *c) |
753 | dns_connection::dns_connection (connection *c) |
725 | : c (c) |
754 | : c (c) |
726 | , rcvdq (MAX_BACKLOG * 2) |
755 | , rcvdq (MAX_BACKLOG * 2) |
727 | , snddq (MAX_BACKLOG) |
756 | , snddq (MAX_BACKLOG) |
728 | , tw (this, &dns_connection::time_cb) |
|
|
729 | { |
757 | { |
|
|
758 | tw.set<dns_connection, &dns_connection::time_cb> (this); |
|
|
759 | |
730 | vpn = c->vpn; |
760 | vpn = c->vpn; |
731 | |
761 | |
732 | established = false; |
762 | established = false; |
733 | |
763 | |
734 | rcvseq = repseq = sndseq = 0; |
764 | rcvseq = repseq = sndseq = 0; |
… | |
… | |
745 | i != rcvpq.end (); |
775 | i != rcvpq.end (); |
746 | ++i) |
776 | ++i) |
747 | delete *i; |
777 | delete *i; |
748 | } |
778 | } |
749 | |
779 | |
|
|
780 | void |
750 | void dns_connection::receive_rep (dns_rcv *r) |
781 | dns_connection::receive_rep (dns_rcv *r) |
751 | { |
782 | { |
752 | if (r->datalen) |
783 | if (r->datalen) |
753 | { |
784 | { |
754 | last_received = NOW; |
785 | last_received = ev_now (); |
755 | tw.trigger (); |
786 | tw (); |
756 | |
787 | |
757 | poll_interval = send_interval; |
788 | poll_interval = send_interval; |
758 | } |
789 | } |
759 | else |
790 | else |
760 | { |
791 | { |
… | |
… | |
770 | |
801 | |
771 | // find next packet |
802 | // find next packet |
772 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
803 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
773 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
804 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
774 | { |
805 | { |
|
|
806 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
775 | // enter the packet into our input stream |
807 | // enter the packet into our input stream |
776 | r = *i; |
808 | r = *i; |
777 | |
809 | |
778 | // remove the oldest packet, look forward, as it's oldest first |
810 | // remove the oldest packet, look forward, as it's oldest first |
779 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
811 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
780 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
812 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
781 | { |
813 | { |
|
|
814 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
782 | delete *j; |
815 | delete *j; |
783 | rcvpq.erase (j); |
816 | rcvpq.erase (j); |
784 | break; |
817 | break; |
785 | } |
818 | } |
786 | |
819 | |
… | |
… | |
793 | } |
826 | } |
794 | |
827 | |
795 | while (vpn_packet *pkt = rcvdq.get ()) |
828 | while (vpn_packet *pkt = rcvdq.get ()) |
796 | { |
829 | { |
797 | sockinfo si; |
830 | sockinfo si; |
798 | si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4; |
831 | si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; |
799 | |
832 | |
800 | vpn->recv_vpn_packet (pkt, si); |
833 | vpn->recv_vpn_packet (pkt, si); |
801 | |
834 | |
802 | delete pkt; |
835 | delete pkt; |
803 | } |
836 | } |
… | |
… | |
1014 | { |
1047 | { |
1015 | dns_connection *dns = (*i)->dns; |
1048 | dns_connection *dns = (*i)->dns; |
1016 | connection *c = dns->c; |
1049 | connection *c = dns->c; |
1017 | int seqno = (*i)->seqno; |
1050 | int seqno = (*i)->seqno; |
1018 | u8 data[MAXSIZE], *datap = data; |
1051 | u8 data[MAXSIZE], *datap = data; |
|
|
1052 | //printf ("rcv pkt %x\n", seqno);//D |
1019 | |
1053 | |
1020 | if ((*i)->retry) |
1054 | if ((*i)->retry) |
1021 | { |
1055 | { |
1022 | dns->send_interval *= 1.01; |
1056 | dns->send_interval *= 1.01; |
1023 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1057 | if (dns->send_interval > MAX_SEND_INTERVAL) |
… | |
… | |
1028 | #if 0 |
1062 | #if 0 |
1029 | dns->send_interval *= 0.999; |
1063 | dns->send_interval *= 0.999; |
1030 | #endif |
1064 | #endif |
1031 | // the latency surely puts an upper bound on |
1065 | // the latency surely puts an upper bound on |
1032 | // the minimum send interval |
1066 | // the minimum send interval |
1033 | double latency = NOW - (*i)->sent; |
1067 | double latency = ev_now () - (*i)->sent; |
1034 | |
1068 | |
1035 | if (latency < dns->min_latency) |
1069 | if (latency < dns->min_latency) |
1036 | dns->min_latency = latency; |
1070 | dns->min_latency = latency; |
1037 | |
1071 | |
1038 | if (dns->send_interval > dns->min_latency * LATENCY_FACTOR) |
1072 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
1039 | dns->send_interval = dns->min_latency * LATENCY_FACTOR; |
1073 | dns->send_interval = dns->min_latency * conf.dns_overlap_factor; |
1040 | |
1074 | |
1041 | if (dns->send_interval < MIN_SEND_INTERVAL) |
1075 | if (dns->send_interval < conf.dns_send_interval) |
1042 | dns->send_interval = MIN_SEND_INTERVAL; |
1076 | dns->send_interval = conf.dns_send_interval; |
1043 | } |
1077 | } |
1044 | |
1078 | |
1045 | delete *i; |
1079 | delete *i; |
1046 | dns_sndpq.erase (i); |
1080 | dns_sndpq.erase (i); |
1047 | |
1081 | |
… | |
… | |
1152 | break; |
1186 | break; |
1153 | } |
1187 | } |
1154 | } |
1188 | } |
1155 | |
1189 | |
1156 | void |
1190 | void |
1157 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1191 | vpn::dnsv4_ev (ev::io &w, int revents) |
1158 | { |
1192 | { |
1159 | if (revents & EVENT_READ) |
1193 | if (revents & EV_READ) |
1160 | { |
1194 | { |
1161 | dns_packet *pkt = new dns_packet; |
1195 | dns_packet *pkt = new dns_packet; |
1162 | struct sockaddr_in sa; |
1196 | struct sockaddr_in sa; |
1163 | socklen_t sa_len = sizeof (sa); |
1197 | socklen_t sa_len = sizeof (sa); |
1164 | |
1198 | |
… | |
… | |
1180 | } |
1214 | } |
1181 | |
1215 | |
1182 | bool |
1216 | bool |
1183 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1217 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1184 | { |
1218 | { |
1185 | int client = ntohs (si.port); |
1219 | int client = ntohl (si.host); |
1186 | |
1220 | |
1187 | assert (0 < client && client <= conns.size ()); |
1221 | assert (0 < client && client <= conns.size ()); |
1188 | |
1222 | |
1189 | connection *c = conns [client - 1]; |
1223 | connection *c = conns [client - 1]; |
1190 | |
1224 | |
1191 | if (!c->dns) |
1225 | if (!c->dns) |
1192 | c->dns = new dns_connection (c); |
1226 | c->dns = new dns_connection (c); |
1193 | |
1227 | |
1194 | if (c->dns->snddq.put (pkt)) |
1228 | if (c->dns->snddq.put (pkt)) |
1195 | c->dns->tw.trigger (); |
1229 | c->dns->tw (); |
1196 | |
1230 | |
1197 | // always return true even if the buffer overflows |
1231 | // always return true even if the buffer overflows |
1198 | return true; |
1232 | return true; |
1199 | } |
1233 | } |
1200 | |
1234 | |
… | |
… | |
1205 | } |
1239 | } |
1206 | |
1240 | |
1207 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1241 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1208 | |
1242 | |
1209 | void |
1243 | void |
1210 | dns_connection::time_cb (time_watcher &w) |
1244 | dns_connection::time_cb (ev::timer &w, int revents) |
1211 | { |
1245 | { |
1212 | // servers have to be polled |
1246 | // servers have to be polled |
1213 | if (THISNODE->dns_port) |
1247 | if (THISNODE->dns_port) |
1214 | return; |
1248 | return; |
1215 | |
1249 | |
1216 | // check for timeouts and (re)transmit |
1250 | // check for timeouts and (re)transmit |
1217 | tstamp next = NOW + poll_interval; |
1251 | tstamp next = ev::now () + poll_interval; |
1218 | dns_snd *send = 0; |
1252 | dns_snd *send = 0; |
1219 | |
1253 | |
1220 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1254 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1221 | i != vpn->dns_sndpq.end (); |
1255 | i != vpn->dns_sndpq.end (); |
1222 | ++i) |
1256 | ++i) |
1223 | { |
1257 | { |
1224 | dns_snd *r = *i; |
1258 | dns_snd *r = *i; |
1225 | |
1259 | |
1226 | if (r->timeout <= NOW) |
1260 | if (r->timeout <= ev_now ()) |
1227 | { |
1261 | { |
1228 | if (!send) |
1262 | if (!send) |
1229 | { |
1263 | { |
1230 | send = r; |
1264 | send = r; |
1231 | |
1265 | |
1232 | r->retry++; |
1266 | r->retry++; |
1233 | r->timeout = NOW + (r->retry * min_latency * TIMEOUT_FACTOR); |
1267 | r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); |
|
|
1268 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D |
1234 | |
1269 | |
1235 | // the following code changes the query section a bit, forcing |
1270 | // the following code changes the query section a bit, forcing |
1236 | // the forwarder to generate a new request |
1271 | // the forwarder to generate a new request |
1237 | if (r->stdhdr) |
1272 | if (r->stdhdr) |
1238 | { |
|
|
1239 | //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
|
|
1240 | //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1273 | encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1241 | } |
|
|
1242 | } |
1274 | } |
1243 | } |
1275 | } |
1244 | else |
1276 | else |
1245 | NEXT (r->timeout); |
1277 | NEXT (r->timeout); |
1246 | } |
1278 | } |
… | |
… | |
1257 | |
1289 | |
1258 | cfg.reset (THISNODE->id); |
1290 | cfg.reset (THISNODE->id); |
1259 | send->gen_syn_req (); |
1291 | send->gen_syn_req (); |
1260 | } |
1292 | } |
1261 | } |
1293 | } |
1262 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1294 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1263 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1295 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1264 | { |
1296 | { |
1265 | if (last_sent + send_interval <= NOW) |
1297 | if (last_sent + send_interval <= ev_now ()) |
1266 | { |
1298 | { |
1267 | //printf ("sending data request etc.\n"); //D |
1299 | //printf ("sending data request etc.\n"); //D |
1268 | if (!snddq.empty () || last_received + 1. > NOW) |
1300 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
1269 | { |
1301 | { |
1270 | poll_interval = send_interval; |
1302 | poll_interval = send_interval; |
1271 | NEXT (NOW + send_interval); |
1303 | NEXT (ev_now () + send_interval); |
1272 | } |
1304 | } |
1273 | |
1305 | |
1274 | send = new dns_snd (this); |
1306 | send = new dns_snd (this); |
1275 | send->gen_stream_req (sndseq, snddq); |
1307 | send->gen_stream_req (sndseq, snddq); |
1276 | send->timeout = NOW + min_latency * TIMEOUT_FACTOR; |
1308 | send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; |
|
|
1309 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D |
1277 | |
1310 | |
1278 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1311 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1279 | } |
1312 | } |
1280 | else |
1313 | else |
1281 | NEXT (last_sent + send_interval); |
1314 | NEXT (last_sent + send_interval); |
… | |
… | |
1285 | vpn->dns_sndpq.push_back (send); |
1318 | vpn->dns_sndpq.push_back (send); |
1286 | } |
1319 | } |
1287 | |
1320 | |
1288 | if (send) |
1321 | if (send) |
1289 | { |
1322 | { |
1290 | last_sent = NOW; |
1323 | last_sent = ev_now (); |
1291 | if (rand () & 15 != 0)//D |
|
|
1292 | sendto (vpn->dnsv4_fd, |
1324 | sendto (vpn->dnsv4_fd, |
1293 | send->pkt->at (0), send->pkt->len, 0, |
1325 | send->pkt->at (0), send->pkt->len, 0, |
1294 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1326 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1295 | } |
1327 | } |
1296 | |
1328 | |
1297 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1329 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1298 | poll_interval, send_interval, next - NOW, |
1330 | poll_interval, send_interval, next - ev_now (), |
1299 | vpn->dns_sndpq.size (), snddq.size (), |
1331 | vpn->dns_sndpq.size (), snddq.size (), |
1300 | rcvpq.size ()); |
1332 | rcvpq.size ()); |
1301 | |
1333 | |
1302 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
1334 | // TODO: no idea when this happens, but when next < ev_now (), we have a problem |
|
|
1335 | // doesn't seem to happen anymore |
1303 | if (next < NOW + 0.001) |
1336 | if (next < ev_now () + 0.001) |
1304 | next = NOW + 0.1; |
1337 | next = ev_now () + 0.1; |
1305 | |
1338 | |
1306 | w.start (next); |
1339 | w.start (next - ev_now ()); |
1307 | } |
1340 | } |
1308 | |
1341 | |
1309 | #endif |
1342 | #endif |
1310 | |
1343 | |