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. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 | */ |
21 | |
31 | |
22 | // TODO: EDNS0 option to increase dns mtu? |
32 | // TODO: EDNS0 option to increase dns mtu? |
23 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
33 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
24 | // to ensure no buffer overflows or similar problems. |
34 | // to ensure no buffer overflows or similar problems. |
… | |
… | |
42 | #include <unistd.h> |
52 | #include <unistd.h> |
43 | #include <fcntl.h> |
53 | #include <fcntl.h> |
44 | |
54 | |
45 | #include <map> |
55 | #include <map> |
46 | |
56 | |
|
|
57 | #include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */ |
47 | #include <gmp.h> |
58 | #include <gmp.h> |
48 | |
59 | |
49 | #include "netcompat.h" |
60 | #include "netcompat.h" |
50 | |
61 | |
51 | #include "vpn.h" |
62 | #include "vpn.h" |
… | |
… | |
72 | #define MAX_PKT_SIZE 512 |
83 | #define MAX_PKT_SIZE 512 |
73 | |
84 | |
74 | #define RR_TYPE_A 1 |
85 | #define RR_TYPE_A 1 |
75 | #define RR_TYPE_NULL 10 |
86 | #define RR_TYPE_NULL 10 |
76 | #define RR_TYPE_TXT 16 |
87 | #define RR_TYPE_TXT 16 |
|
|
88 | #define RR_TYPE_AAAA 28 |
77 | #define RR_TYPE_ANY 255 |
89 | #define RR_TYPE_ANY 255 |
78 | |
90 | |
79 | #define RR_CLASS_IN 1 |
91 | #define RR_CLASS_IN 1 |
80 | |
92 | |
81 | #define CMD_IP_1 207 |
93 | #define CMD_IP_1 207 |
… | |
… | |
105 | memset (enc, (char) 0, 256); |
117 | memset (enc, (char) 0, 256); |
106 | memset (dec, (char)INVALID, 256); |
118 | memset (dec, (char)INVALID, 256); |
107 | |
119 | |
108 | for (size = 0; cmap [size]; size++) |
120 | for (size = 0; cmap [size]; size++) |
109 | { |
121 | { |
|
|
122 | char c = cmap [size]; |
|
|
123 | |
110 | enc [size] = cmap [size]; |
124 | enc [size] = c; |
111 | dec [(u8)enc [size]] = size; |
125 | dec [(u8)c] = size; |
|
|
126 | |
|
|
127 | // allow lowercase/uppercase aliases if possible |
|
|
128 | if (c >= 'A' && c <= 'Z' && dec [c + ('a' - 'A')] == INVALID) dec [c + ('a' - 'A')] = size; |
|
|
129 | if (c >= 'a' && c <= 'z' && dec [c - ('a' - 'A')] == INVALID) dec [c - ('a' - 'A')] = size; |
112 | } |
130 | } |
113 | |
131 | |
114 | assert (size < 256); |
132 | assert (size < 256); |
115 | } |
133 | } |
116 | |
134 | |
… | |
… | |
156 | enc_len [len] = n; |
174 | enc_len [len] = n; |
157 | dec_len [n] = len; |
175 | dec_len [n] = len; |
158 | } |
176 | } |
159 | } |
177 | } |
160 | |
178 | |
|
|
179 | unsigned int |
161 | unsigned int basecoder::encode_len (unsigned int len) |
180 | basecoder::encode_len (unsigned int len) |
162 | { |
181 | { |
163 | return enc_len [len]; |
182 | return enc_len [len]; |
164 | } |
183 | } |
165 | |
184 | |
|
|
185 | unsigned int |
166 | unsigned int basecoder::decode_len (unsigned int len) |
186 | basecoder::decode_len (unsigned int len) |
167 | { |
187 | { |
168 | while (len && !dec_len [len]) |
188 | while (len && !dec_len [len]) |
169 | --len; |
189 | --len; |
170 | |
190 | |
171 | return dec_len [len]; |
191 | return dec_len [len]; |
172 | } |
192 | } |
173 | |
193 | |
|
|
194 | unsigned int |
174 | unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
195 | basecoder::encode (char *dst, u8 *src, unsigned int len) |
175 | { |
196 | { |
176 | if (!len || len > MAX_DEC_LEN) |
197 | if (!len || len > MAX_DEC_LEN) |
177 | return 0; |
198 | return 0; |
178 | |
199 | |
179 | int elen = encode_len (len); |
200 | int elen = encode_len (len); |
… | |
… | |
198 | *dst++ = cmap.encode [dst_ [i]]; |
219 | *dst++ = cmap.encode [dst_ [i]]; |
199 | |
220 | |
200 | return elen; |
221 | return elen; |
201 | } |
222 | } |
202 | |
223 | |
|
|
224 | unsigned int |
203 | unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
225 | basecoder::decode (u8 *dst, char *src, unsigned int len) |
204 | { |
226 | { |
205 | if (!len || len > MAX_ENC_LEN) |
227 | if (!len || len > MAX_ENC_LEN) |
206 | return 0; |
228 | return 0; |
207 | |
229 | |
208 | u8 src_ [MAX_ENC_LEN]; |
230 | u8 src_ [MAX_ENC_LEN]; |
… | |
… | |
267 | |
289 | |
268 | ///////////////////////////////////////////////////////////////////////////// |
290 | ///////////////////////////////////////////////////////////////////////////// |
269 | |
291 | |
270 | #define HDRSIZE 6 |
292 | #define HDRSIZE 6 |
271 | |
293 | |
|
|
294 | inline void |
272 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
295 | encode_header (char *data, int clientid, int seqno, int retry = 0) |
273 | { |
296 | { |
274 | seqno &= SEQNO_MASK; |
297 | seqno &= SEQNO_MASK; |
275 | |
298 | |
276 | u8 hdr[3] = { |
299 | u8 hdr[3] = { |
277 | clientid, |
300 | clientid, |
… | |
… | |
282 | assert (clientid < 256); |
305 | assert (clientid < 256); |
283 | |
306 | |
284 | cdc26.encode (data, hdr, 3); |
307 | cdc26.encode (data, hdr, 3); |
285 | } |
308 | } |
286 | |
309 | |
|
|
310 | inline void |
287 | inline void decode_header (char *data, int &clientid, int &seqno) |
311 | decode_header (char *data, int &clientid, int &seqno) |
288 | { |
312 | { |
289 | u8 hdr[3]; |
313 | u8 hdr[3]; |
290 | |
314 | |
291 | cdc26.decode (hdr, data, HDRSIZE); |
315 | cdc26.decode (hdr, data, HDRSIZE); |
292 | |
316 | |
… | |
… | |
325 | byte_stream::~byte_stream () |
349 | byte_stream::~byte_stream () |
326 | { |
350 | { |
327 | delete data; |
351 | delete data; |
328 | } |
352 | } |
329 | |
353 | |
|
|
354 | void |
330 | void byte_stream::remove (int count) |
355 | byte_stream::remove (int count) |
331 | { |
356 | { |
332 | if (count > fill) |
357 | if (count > fill) |
333 | assert (count <= fill); |
358 | assert (count <= fill); |
334 | |
359 | |
335 | memmove (data, data + count, fill -= count); |
360 | memmove (data, data + count, fill -= count); |
336 | } |
361 | } |
337 | |
362 | |
|
|
363 | bool |
338 | bool byte_stream::put (u8 *data, unsigned int datalen) |
364 | byte_stream::put (u8 *data, unsigned int datalen) |
339 | { |
365 | { |
340 | if (maxsize - fill < datalen) |
366 | if (maxsize - fill < datalen) |
341 | return false; |
367 | return false; |
342 | |
368 | |
343 | memcpy (this->data + fill, data, datalen); fill += datalen; |
369 | memcpy (this->data + fill, data, datalen); fill += datalen; |
344 | |
370 | |
345 | return true; |
371 | return true; |
346 | } |
372 | } |
347 | |
373 | |
|
|
374 | bool |
348 | bool byte_stream::put (vpn_packet *pkt) |
375 | byte_stream::put (vpn_packet *pkt) |
349 | { |
376 | { |
350 | if (maxsize - fill < pkt->len + 2) |
377 | if (maxsize - fill < pkt->len + 2) |
351 | return false; |
378 | return false; |
352 | |
379 | |
353 | data [fill++] = pkt->len >> 8; |
380 | data [fill++] = pkt->len >> 8; |
… | |
… | |
435 | bool valid (); |
462 | bool valid (); |
436 | }; |
463 | }; |
437 | |
464 | |
438 | int dns_cfg::next_uid; |
465 | int dns_cfg::next_uid; |
439 | |
466 | |
|
|
467 | void |
440 | void dns_cfg::reset (int clientid) |
468 | dns_cfg::reset (int clientid) |
441 | { |
469 | { |
442 | id1 = 'G'; |
470 | id1 = 'G'; |
443 | id2 = 'V'; |
471 | id2 = 'V'; |
444 | id3 = 'P'; |
472 | id3 = 'P'; |
445 | id4 = 'E'; |
473 | id4 = 'E'; |
… | |
… | |
459 | |
487 | |
460 | r3 = r4 = 0; |
488 | r3 = r4 = 0; |
461 | r4 = r5 = r6 = r7 = 0; |
489 | r4 = r5 = r6 = r7 = 0; |
462 | } |
490 | } |
463 | |
491 | |
|
|
492 | bool |
464 | bool dns_cfg::valid () |
493 | dns_cfg::valid () |
465 | { |
494 | { |
|
|
495 | // although the protocol itself allows for some configurability, |
|
|
496 | // only the following encoding/decoding settings are implemented. |
466 | return id1 == 'G' |
497 | return id1 == 'G' |
467 | && id2 == 'V' |
498 | && id2 == 'V' |
468 | && id3 == 'P' |
499 | && id3 == 'P' |
469 | && id4 == 'E' |
500 | && id4 == 'E' |
470 | && seq_cdc == 26 |
501 | && seq_cdc == 26 |
… | |
… | |
482 | u8 data [MAXSIZE - 6 * 2]; |
513 | u8 data [MAXSIZE - 6 * 2]; |
483 | |
514 | |
484 | int decode_label (char *data, int size, int &offs); |
515 | int decode_label (char *data, int size, int &offs); |
485 | }; |
516 | }; |
486 | |
517 | |
|
|
518 | int |
487 | int dns_packet::decode_label (char *data, int size, int &offs) |
519 | dns_packet::decode_label (char *data, int size, int &offs) |
488 | { |
520 | { |
489 | char *orig = data; |
521 | char *orig = data; |
490 | |
522 | |
491 | memset (data, 0, size); |
523 | memset (data, 0, size); |
492 | |
524 | |
… | |
… | |
518 | return data - orig; |
550 | return data - orig; |
519 | } |
551 | } |
520 | |
552 | |
521 | ///////////////////////////////////////////////////////////////////////////// |
553 | ///////////////////////////////////////////////////////////////////////////// |
522 | |
554 | |
|
|
555 | static |
|
|
556 | u16 next_id () |
|
|
557 | { |
523 | static u16 dns_id = 0; // TODO: should be per-vpn |
558 | static u16 dns_id = 0; // TODO: should be per-vpn |
524 | |
559 | |
525 | static u16 next_id () |
|
|
526 | { |
|
|
527 | if (!dns_id) |
560 | if (!dns_id) |
528 | dns_id = time (0); |
561 | dns_id = time (0); |
529 | |
562 | |
530 | // the simplest lsfr with periodicity 65535 i could find |
563 | // the simplest lsfr with periodicity 65535 i could find |
531 | dns_id = (dns_id << 1) |
564 | dns_id = (dns_id << 1) |
… | |
… | |
557 | vector<dns_rcv *> rcvpq; |
590 | vector<dns_rcv *> rcvpq; |
558 | |
591 | |
559 | byte_stream rcvdq; int rcvseq; int repseq; |
592 | byte_stream rcvdq; int rcvseq; int repseq; |
560 | byte_stream snddq; int sndseq; |
593 | byte_stream snddq; int sndseq; |
561 | |
594 | |
562 | void time_cb (time_watcher &w); time_watcher tw; |
595 | inline void time_cb (ev::timer &w, int revents); ev::timer tw; |
563 | void receive_rep (dns_rcv *r); |
596 | void receive_rep (dns_rcv *r); |
564 | |
597 | |
565 | dns_connection (connection *c); |
598 | dns_connection (connection *c); |
566 | ~dns_connection (); |
599 | ~dns_connection (); |
567 | }; |
600 | }; |
… | |
… | |
586 | : dns (dns) |
619 | : dns (dns) |
587 | { |
620 | { |
588 | timeout = 0; |
621 | timeout = 0; |
589 | retry = 0; |
622 | retry = 0; |
590 | seqno = 0; |
623 | seqno = 0; |
591 | sent = NOW; |
624 | sent = ev_now (); |
592 | stdhdr = false; |
625 | stdhdr = false; |
593 | |
626 | |
594 | pkt = new dns_packet; |
627 | pkt = new dns_packet; |
595 | |
628 | |
596 | pkt->id = next_id (); |
629 | pkt->id = next_id (); |
… | |
… | |
599 | dns_snd::~dns_snd () |
632 | dns_snd::~dns_snd () |
600 | { |
633 | { |
601 | delete pkt; |
634 | delete pkt; |
602 | } |
635 | } |
603 | |
636 | |
|
|
637 | static void |
604 | static void append_domain (dns_packet &pkt, int &offs, const char *domain) |
638 | append_domain (dns_packet &pkt, int &offs, const char *domain) |
605 | { |
639 | { |
606 | // add tunnel domain |
640 | // add tunnel domain |
607 | for (;;) |
641 | for (;;) |
608 | { |
642 | { |
609 | const char *end = strchr (domain, '.'); |
643 | const char *end = strchr (domain, '.'); |
… | |
… | |
622 | |
656 | |
623 | domain = end + 1; |
657 | domain = end + 1; |
624 | } |
658 | } |
625 | } |
659 | } |
626 | |
660 | |
|
|
661 | void |
627 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
662 | dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
628 | { |
663 | { |
629 | stdhdr = true; |
664 | stdhdr = true; |
630 | this->seqno = seqno; |
665 | this->seqno = seqno; |
631 | |
666 | |
632 | timeout = NOW + INITIAL_TIMEOUT; |
667 | timeout = ev_now () + INITIAL_TIMEOUT; |
633 | |
668 | |
634 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
669 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
635 | pkt->qdcount = htons (1); |
670 | pkt->qdcount = htons (1); |
636 | |
671 | |
637 | int offs = 6*2; |
672 | int offs = 6*2; |
… | |
… | |
670 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
705 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
671 | |
706 | |
672 | pkt->len = offs; |
707 | pkt->len = offs; |
673 | } |
708 | } |
674 | |
709 | |
|
|
710 | void |
675 | void dns_snd::gen_syn_req () |
711 | dns_snd::gen_syn_req () |
676 | { |
712 | { |
677 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
713 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
678 | |
714 | |
679 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
715 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
680 | pkt->qdcount = htons (1); |
716 | pkt->qdcount = htons (1); |
681 | |
717 | |
682 | int offs = 6 * 2; |
718 | int offs = 6 * 2; |
… | |
… | |
722 | |
758 | |
723 | dns_connection::dns_connection (connection *c) |
759 | dns_connection::dns_connection (connection *c) |
724 | : c (c) |
760 | : c (c) |
725 | , rcvdq (MAX_BACKLOG * 2) |
761 | , rcvdq (MAX_BACKLOG * 2) |
726 | , snddq (MAX_BACKLOG) |
762 | , snddq (MAX_BACKLOG) |
727 | , tw (this, &dns_connection::time_cb) |
|
|
728 | { |
763 | { |
|
|
764 | tw.set<dns_connection, &dns_connection::time_cb> (this); |
|
|
765 | |
729 | vpn = c->vpn; |
766 | vpn = c->vpn; |
730 | |
767 | |
731 | established = false; |
768 | established = false; |
732 | |
769 | |
733 | rcvseq = repseq = sndseq = 0; |
770 | rcvseq = repseq = sndseq = 0; |
… | |
… | |
744 | i != rcvpq.end (); |
781 | i != rcvpq.end (); |
745 | ++i) |
782 | ++i) |
746 | delete *i; |
783 | delete *i; |
747 | } |
784 | } |
748 | |
785 | |
|
|
786 | void |
749 | void dns_connection::receive_rep (dns_rcv *r) |
787 | dns_connection::receive_rep (dns_rcv *r) |
750 | { |
788 | { |
751 | if (r->datalen) |
789 | if (r->datalen) |
752 | { |
790 | { |
753 | last_received = NOW; |
791 | last_received = ev_now (); |
754 | tw.trigger (); |
792 | tw (); |
755 | |
793 | |
756 | poll_interval = send_interval; |
794 | poll_interval = send_interval; |
757 | } |
795 | } |
758 | else |
796 | else |
759 | { |
797 | { |
… | |
… | |
769 | |
807 | |
770 | // find next packet |
808 | // find next packet |
771 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
809 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
772 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
810 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
773 | { |
811 | { |
|
|
812 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
774 | // enter the packet into our input stream |
813 | // enter the packet into our input stream |
775 | r = *i; |
814 | r = *i; |
776 | |
815 | |
777 | // remove the oldest packet, look forward, as it's oldest first |
816 | // remove the oldest packet, look forward, as it's oldest first |
778 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
817 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
779 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
818 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
780 | { |
819 | { |
|
|
820 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
781 | delete *j; |
821 | delete *j; |
782 | rcvpq.erase (j); |
822 | rcvpq.erase (j); |
783 | break; |
823 | break; |
784 | } |
824 | } |
785 | |
825 | |
786 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
826 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
787 | |
827 | |
788 | if (!rcvdq.put (r->data, r->datalen)) |
828 | if (!rcvdq.put (r->data, r->datalen)) |
789 | { |
829 | { |
|
|
830 | // MUST never overflow, can be caused by data corruption, TODO |
790 | slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)"); |
831 | slog (L_CRIT, "DNS: !rcvdq.put (r->data, r->datalen)"); |
791 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
832 | c->dnsv4_reset_connection (); |
|
|
833 | return; |
792 | } |
834 | } |
793 | |
835 | |
794 | while (vpn_packet *pkt = rcvdq.get ()) |
836 | while (vpn_packet *pkt = rcvdq.get ()) |
795 | { |
837 | { |
796 | sockinfo si; |
838 | sockinfo si; |
797 | si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; |
839 | si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; |
798 | |
840 | |
799 | vpn->recv_vpn_packet (pkt, si); |
841 | vpn->recv_vpn_packet (pkt, si); |
800 | |
|
|
801 | delete pkt; |
842 | delete pkt; |
802 | } |
843 | } |
803 | |
844 | |
804 | // check for further packets |
845 | // check for further packets |
805 | goto redo; |
846 | goto redo; |
… | |
… | |
1013 | { |
1054 | { |
1014 | dns_connection *dns = (*i)->dns; |
1055 | dns_connection *dns = (*i)->dns; |
1015 | connection *c = dns->c; |
1056 | connection *c = dns->c; |
1016 | int seqno = (*i)->seqno; |
1057 | int seqno = (*i)->seqno; |
1017 | u8 data[MAXSIZE], *datap = data; |
1058 | u8 data[MAXSIZE], *datap = data; |
|
|
1059 | //printf ("rcv pkt %x\n", seqno);//D |
1018 | |
1060 | |
1019 | if ((*i)->retry) |
1061 | if ((*i)->retry) |
1020 | { |
1062 | { |
1021 | dns->send_interval *= 1.01; |
1063 | dns->send_interval *= 1.01; |
1022 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1064 | if (dns->send_interval > MAX_SEND_INTERVAL) |
… | |
… | |
1027 | #if 0 |
1069 | #if 0 |
1028 | dns->send_interval *= 0.999; |
1070 | dns->send_interval *= 0.999; |
1029 | #endif |
1071 | #endif |
1030 | // the latency surely puts an upper bound on |
1072 | // the latency surely puts an upper bound on |
1031 | // the minimum send interval |
1073 | // the minimum send interval |
1032 | double latency = NOW - (*i)->sent; |
1074 | double latency = ev_now () - (*i)->sent; |
1033 | |
1075 | |
1034 | if (latency < dns->min_latency) |
1076 | if (latency < dns->min_latency) |
1035 | dns->min_latency = latency; |
1077 | dns->min_latency = latency; |
1036 | |
1078 | |
1037 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
1079 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
… | |
… | |
1102 | |
1144 | |
1103 | if (ip [3] == CMD_IP_RST) |
1145 | if (ip [3] == CMD_IP_RST) |
1104 | { |
1146 | { |
1105 | slog (L_DEBUG, _("DNS: got tunnel RST request")); |
1147 | slog (L_DEBUG, _("DNS: got tunnel RST request")); |
1106 | |
1148 | |
1107 | delete dns; c->dns = 0; |
1149 | c->dnsv4_reset_connection (); |
1108 | |
|
|
1109 | return; |
|
|
1110 | } |
1150 | } |
1111 | else if (ip [3] == CMD_IP_SYN) |
1151 | else if (ip [3] == CMD_IP_SYN) |
1112 | { |
1152 | { |
1113 | slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); |
1153 | slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); |
1114 | dns->established = true; |
1154 | dns->established = true; |
1115 | } |
1155 | } |
1116 | else if (ip [3] == CMD_IP_REJ) |
1156 | else if (ip [3] == CMD_IP_REJ) |
1117 | { |
|
|
1118 | slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting.")); |
1157 | slog (L_ERR, _("DNS: got tunnel REJ reply, server does not like us.")); |
1119 | abort (); |
|
|
1120 | } |
|
|
1121 | else |
1158 | else |
1122 | slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); |
1159 | slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); |
1123 | } |
1160 | } |
1124 | else |
1161 | else |
1125 | slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), |
1162 | slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), |
… | |
… | |
1151 | break; |
1188 | break; |
1152 | } |
1189 | } |
1153 | } |
1190 | } |
1154 | |
1191 | |
1155 | void |
1192 | void |
1156 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1193 | vpn::dnsv4_ev (ev::io &w, int revents) |
1157 | { |
1194 | { |
1158 | if (revents & EVENT_READ) |
1195 | if (revents & EV_READ) |
1159 | { |
1196 | { |
1160 | dns_packet *pkt = new dns_packet; |
1197 | dns_packet *pkt = new dns_packet; |
1161 | struct sockaddr_in sa; |
1198 | struct sockaddr_in sa; |
1162 | socklen_t sa_len = sizeof (sa); |
1199 | socklen_t sa_len = sizeof (sa); |
1163 | |
1200 | |
… | |
… | |
1189 | |
1226 | |
1190 | if (!c->dns) |
1227 | if (!c->dns) |
1191 | c->dns = new dns_connection (c); |
1228 | c->dns = new dns_connection (c); |
1192 | |
1229 | |
1193 | if (c->dns->snddq.put (pkt)) |
1230 | if (c->dns->snddq.put (pkt)) |
1194 | c->dns->tw.trigger (); |
1231 | c->dns->tw (); |
1195 | |
1232 | |
1196 | // always return true even if the buffer overflows |
1233 | // always return true even if the buffer overflows |
1197 | return true; |
1234 | return true; |
1198 | } |
1235 | } |
1199 | |
1236 | |
… | |
… | |
1204 | } |
1241 | } |
1205 | |
1242 | |
1206 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1243 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1207 | |
1244 | |
1208 | void |
1245 | void |
1209 | dns_connection::time_cb (time_watcher &w) |
1246 | dns_connection::time_cb (ev::timer &w, int revents) |
1210 | { |
1247 | { |
1211 | // servers have to be polled |
1248 | // servers have to be polled |
1212 | if (THISNODE->dns_port) |
1249 | if (THISNODE->dns_port) |
1213 | return; |
1250 | return; |
1214 | |
1251 | |
1215 | // check for timeouts and (re)transmit |
1252 | // check for timeouts and (re)transmit |
1216 | tstamp next = NOW + poll_interval; |
1253 | tstamp next = ev::now () + poll_interval; |
1217 | dns_snd *send = 0; |
1254 | dns_snd *send = 0; |
1218 | |
1255 | |
1219 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1256 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1220 | i != vpn->dns_sndpq.end (); |
1257 | i != vpn->dns_sndpq.end (); |
1221 | ++i) |
1258 | ++i) |
1222 | { |
1259 | { |
1223 | dns_snd *r = *i; |
1260 | dns_snd *r = *i; |
1224 | |
1261 | |
1225 | if (r->timeout <= NOW) |
1262 | if (r->timeout <= ev_now ()) |
1226 | { |
1263 | { |
1227 | if (!send) |
1264 | if (!send) |
1228 | { |
1265 | { |
1229 | send = r; |
1266 | send = r; |
1230 | |
1267 | |
1231 | r->retry++; |
1268 | r->retry++; |
1232 | r->timeout = NOW + (r->retry * min_latency * conf.dns_timeout_factor); |
1269 | r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); |
|
|
1270 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D |
1233 | |
1271 | |
1234 | // the following code changes the query section a bit, forcing |
1272 | // the following code changes the query section a bit, forcing |
1235 | // the forwarder to generate a new request |
1273 | // the forwarder to generate a new request |
1236 | if (r->stdhdr) |
1274 | if (r->stdhdr) |
1237 | { |
|
|
1238 | //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
|
|
1239 | //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1275 | encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1240 | } |
|
|
1241 | } |
1276 | } |
1242 | } |
1277 | } |
1243 | else |
1278 | else |
1244 | NEXT (r->timeout); |
1279 | NEXT (r->timeout); |
1245 | } |
1280 | } |
… | |
… | |
1259 | } |
1294 | } |
1260 | } |
1295 | } |
1261 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1296 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1262 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1297 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1263 | { |
1298 | { |
1264 | if (last_sent + send_interval <= NOW) |
1299 | if (last_sent + send_interval <= ev_now ()) |
1265 | { |
1300 | { |
1266 | //printf ("sending data request etc.\n"); //D |
1301 | //printf ("sending data request etc.\n"); //D |
1267 | if (!snddq.empty () || last_received + 1. > NOW) |
1302 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
1268 | { |
1303 | { |
1269 | poll_interval = send_interval; |
1304 | poll_interval = send_interval; |
1270 | NEXT (NOW + send_interval); |
1305 | NEXT (ev_now () + send_interval); |
1271 | } |
1306 | } |
1272 | |
1307 | |
1273 | send = new dns_snd (this); |
1308 | send = new dns_snd (this); |
1274 | send->gen_stream_req (sndseq, snddq); |
1309 | send->gen_stream_req (sndseq, snddq); |
1275 | send->timeout = NOW + min_latency * conf.dns_timeout_factor; |
1310 | send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; |
|
|
1311 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D |
1276 | |
1312 | |
1277 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1313 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1278 | } |
1314 | } |
1279 | else |
1315 | else |
1280 | NEXT (last_sent + send_interval); |
1316 | NEXT (last_sent + send_interval); |
… | |
… | |
1284 | vpn->dns_sndpq.push_back (send); |
1320 | vpn->dns_sndpq.push_back (send); |
1285 | } |
1321 | } |
1286 | |
1322 | |
1287 | if (send) |
1323 | if (send) |
1288 | { |
1324 | { |
1289 | last_sent = NOW; |
1325 | last_sent = ev_now (); |
1290 | sendto (vpn->dnsv4_fd, |
1326 | sendto (vpn->dnsv4_fd, |
1291 | send->pkt->at (0), send->pkt->len, 0, |
1327 | send->pkt->at (0), send->pkt->len, 0, |
1292 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1328 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1293 | } |
1329 | } |
1294 | |
1330 | |
1295 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1331 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1296 | poll_interval, send_interval, next - NOW, |
1332 | poll_interval, send_interval, next - ev_now (), |
1297 | vpn->dns_sndpq.size (), snddq.size (), |
1333 | vpn->dns_sndpq.size (), snddq.size (), |
1298 | rcvpq.size ()); |
1334 | rcvpq.size ()); |
1299 | |
1335 | |
1300 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
1336 | // TODO: no idea when this happens, but when next < ev_now (), we have a problem |
1301 | // doesn't seem to happen anymore |
1337 | // doesn't seem to happen anymore |
1302 | if (next < NOW + 0.001) |
1338 | if (next < ev_now () + 0.001) |
1303 | next = NOW + 0.1; |
1339 | next = ev_now () + 0.1; |
1304 | |
1340 | |
1305 | w.start (next); |
1341 | w.start (next - ev_now ()); |
1306 | } |
1342 | } |
1307 | |
1343 | |
1308 | #endif |
1344 | #endif |
1309 | |
1345 | |