… | |
… | |
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 | |
26 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
30 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
27 | // it's a hack, use only in emergency situations please. |
31 | // it's a hack, use only in emergency situations please. |
28 | |
32 | |
29 | #include <cstring> |
33 | #include <cstring> |
|
|
34 | #include <cassert> |
30 | |
35 | |
31 | #include <sys/types.h> |
36 | #include <sys/types.h> |
32 | #include <sys/socket.h> |
37 | #include <sys/socket.h> |
33 | #include <sys/wait.h> |
38 | #include <sys/wait.h> |
34 | #include <sys/uio.h> |
39 | #include <sys/uio.h> |
… | |
… | |
37 | #include <unistd.h> |
42 | #include <unistd.h> |
38 | #include <fcntl.h> |
43 | #include <fcntl.h> |
39 | |
44 | |
40 | #include <map> |
45 | #include <map> |
41 | |
46 | |
|
|
47 | #include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */ |
42 | #include <gmp.h> |
48 | #include <gmp.h> |
43 | |
49 | |
44 | #include "netcompat.h" |
50 | #include "netcompat.h" |
45 | |
51 | |
46 | #include "vpn.h" |
52 | #include "vpn.h" |
47 | |
53 | |
48 | #define MIN_POLL_INTERVAL .02 // how often to poll minimally when the server has data |
|
|
49 | #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 |
50 | #define ACTIVITY_INTERVAL 5. |
55 | #define ACTIVITY_INTERVAL 5. |
51 | |
56 | |
52 | #define INITIAL_TIMEOUT 1. // retry timeouts |
57 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
53 | #define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn |
58 | #define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn |
54 | |
59 | |
55 | #define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests |
|
|
56 | #define MAX_SEND_INTERVAL 0.5 // optimistic? |
60 | #define MAX_SEND_INTERVAL 2. // optimistic? |
57 | |
61 | |
58 | #define MAX_OUTSTANDING 40 // max. outstanding requests |
|
|
59 | #define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
62 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
60 | #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 |
61 | |
64 | |
62 | #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 |
63 | // 240 leaves about 4 bytes of server reply data |
66 | // 240 leaves about 4 bytes of server reply data |
64 | // every two request bytes less give room for one reply byte |
67 | // every request byte less give room for two reply bytes |
65 | |
68 | |
66 | #define SEQNO_MASK 0xffff |
69 | #define SEQNO_MASK 0x3fff |
67 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
70 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
68 | |
71 | |
69 | #define MAX_LBL_SIZE 63 |
72 | #define MAX_LBL_SIZE 63 |
70 | #define MAX_PKT_SIZE 512 |
73 | #define MAX_PKT_SIZE 512 |
71 | |
74 | |
… | |
… | |
264 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
267 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
265 | |
268 | |
266 | ///////////////////////////////////////////////////////////////////////////// |
269 | ///////////////////////////////////////////////////////////////////////////// |
267 | |
270 | |
268 | #define HDRSIZE 6 |
271 | #define HDRSIZE 6 |
269 | |
272 | |
270 | inline void encode_header (char *data, int clientid, int seqno) |
273 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
271 | { |
274 | { |
272 | u8 hdr[3] = { clientid, seqno >> 8, seqno }; |
275 | seqno &= SEQNO_MASK; |
|
|
276 | |
|
|
277 | u8 hdr[3] = { |
|
|
278 | clientid, |
|
|
279 | (seqno >> 8) | (retry << 6), |
|
|
280 | seqno, |
|
|
281 | }; |
273 | |
282 | |
274 | assert (clientid < 256); |
283 | assert (clientid < 256); |
275 | |
284 | |
276 | cdc26.encode (data, hdr, 3); |
285 | cdc26.encode (data, hdr, 3); |
277 | } |
286 | } |
… | |
… | |
281 | u8 hdr[3]; |
290 | u8 hdr[3]; |
282 | |
291 | |
283 | cdc26.decode (hdr, data, HDRSIZE); |
292 | cdc26.decode (hdr, data, HDRSIZE); |
284 | |
293 | |
285 | clientid = hdr[0]; |
294 | clientid = hdr[0]; |
286 | seqno = (hdr[1] << 8) | hdr[2]; |
295 | seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK; |
287 | } |
296 | } |
288 | |
297 | |
289 | ///////////////////////////////////////////////////////////////////////////// |
298 | ///////////////////////////////////////////////////////////////////////////// |
290 | |
299 | |
291 | struct byte_stream |
300 | struct byte_stream |
… | |
… | |
320 | } |
329 | } |
321 | |
330 | |
322 | void byte_stream::remove (int count) |
331 | void byte_stream::remove (int count) |
323 | { |
332 | { |
324 | if (count > fill) |
333 | if (count > fill) |
325 | abort (); |
334 | assert (count <= fill); |
326 | |
335 | |
327 | memmove (data, data + count, fill -= count); |
336 | memmove (data, data + count, fill -= count); |
328 | } |
337 | } |
329 | |
338 | |
330 | bool byte_stream::put (u8 *data, unsigned int datalen) |
339 | bool byte_stream::put (u8 *data, unsigned int datalen) |
… | |
… | |
350 | return true; |
359 | return true; |
351 | } |
360 | } |
352 | |
361 | |
353 | vpn_packet *byte_stream::get () |
362 | vpn_packet *byte_stream::get () |
354 | { |
363 | { |
|
|
364 | unsigned int len; |
|
|
365 | |
|
|
366 | for (;;) |
|
|
367 | { |
355 | unsigned int len = (data [0] << 8) | data [1]; |
368 | len = (data [0] << 8) | data [1]; |
356 | |
369 | |
357 | if (len > MAXSIZE && fill >= 2) |
370 | if (len <= MAXSIZE || fill < 2) |
358 | abort (); // TODO handle this gracefully, connection reset |
371 | break; |
359 | |
372 | |
|
|
373 | // TODO: handle this better than skipping, e.g. by reset |
|
|
374 | slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte...")); |
|
|
375 | remove (1); |
|
|
376 | } |
|
|
377 | |
360 | if (fill < len + 2) |
378 | if (fill < len + 2) |
361 | return 0; |
379 | return 0; |
362 | |
380 | |
363 | vpn_packet *pkt = new vpn_packet; |
381 | vpn_packet *pkt = new vpn_packet; |
364 | |
382 | |
… | |
… | |
393 | struct dns_cfg |
411 | struct dns_cfg |
394 | { |
412 | { |
395 | static int next_uid; |
413 | static int next_uid; |
396 | |
414 | |
397 | u8 id1, id2, id3, id4; |
415 | u8 id1, id2, id3, id4; |
|
|
416 | |
398 | u8 version; |
417 | u8 version; |
|
|
418 | u8 flags; |
399 | u8 rrtype; |
419 | u8 rrtype; |
400 | u8 flags; |
|
|
401 | u8 def_ttl; |
420 | u8 def_ttl; |
402 | u8 rcv_cdc; |
421 | |
403 | u8 snd_cdc; |
|
|
404 | u16 max_size; |
|
|
405 | u16 client; |
422 | u16 client; |
406 | u16 uid; // to make request unique |
423 | u16 uid; // to make request unique |
407 | |
424 | |
408 | 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; |
409 | |
434 | |
410 | void reset (int clientid); |
435 | void reset (int clientid); |
411 | bool valid (); |
436 | bool valid (); |
412 | }; |
437 | }; |
413 | |
438 | |
… | |
… | |
423 | version = 1; |
448 | version = 1; |
424 | |
449 | |
425 | rrtype = RR_TYPE_TXT; |
450 | rrtype = RR_TYPE_TXT; |
426 | flags = 0; |
451 | flags = 0; |
427 | def_ttl = 0; |
452 | def_ttl = 0; |
|
|
453 | seq_cdc = 26; |
|
|
454 | req_cdc = 62; |
428 | rcv_cdc = 0; |
455 | rep_cdc = 0; |
429 | snd_cdc = 62; |
|
|
430 | max_size = ntohs (MAX_PKT_SIZE); |
456 | max_size = htons (MAX_PKT_SIZE); |
431 | client = ntohs (clientid); |
457 | client = htons (clientid); |
432 | uid = next_uid++; |
458 | uid = next_uid++; |
|
|
459 | delay = 0; |
433 | |
460 | |
434 | memset (reserved, 0, 8); |
461 | r3 = r4 = 0; |
|
|
462 | r4 = r5 = r6 = r7 = 0; |
435 | } |
463 | } |
436 | |
464 | |
437 | bool dns_cfg::valid () |
465 | bool dns_cfg::valid () |
438 | { |
466 | { |
|
|
467 | // although the protocol itself allows for some configurability, |
|
|
468 | // only the following encoding/decoding settings are implemented. |
439 | return id1 == 'G' |
469 | return id1 == 'G' |
440 | && id2 == 'V' |
470 | && id2 == 'V' |
441 | && id3 == 'P' |
471 | && id3 == 'P' |
442 | && id4 == 'E' |
472 | && id4 == 'E' |
|
|
473 | && seq_cdc == 26 |
|
|
474 | && req_cdc == 62 |
|
|
475 | && rep_cdc == 0 |
443 | && version == 1 |
476 | && version == 1; |
444 | && flags == 0 |
|
|
445 | && rcv_cdc == 0 |
|
|
446 | && snd_cdc == 62 |
|
|
447 | && max_size == ntohs (MAX_PKT_SIZE); |
|
|
448 | } |
477 | } |
449 | |
478 | |
450 | struct dns_packet : net_packet |
479 | struct dns_packet : net_packet |
451 | { |
480 | { |
452 | u16 id; |
481 | u16 id; |
453 | 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 |
454 | u16 qdcount, ancount, nscount, arcount; |
483 | u16 qdcount, ancount, nscount, arcount; |
455 | |
484 | |
456 | u8 data[MAXSIZE - 6 * 2]; |
485 | u8 data [MAXSIZE - 6 * 2]; |
457 | |
486 | |
458 | int decode_label (char *data, int size, int &offs); |
487 | int decode_label (char *data, int size, int &offs); |
459 | }; |
488 | }; |
460 | |
489 | |
461 | int dns_packet::decode_label (char *data, int size, int &offs) |
490 | int dns_packet::decode_label (char *data, int size, int &offs) |
… | |
… | |
492 | return data - orig; |
521 | return data - orig; |
493 | } |
522 | } |
494 | |
523 | |
495 | ///////////////////////////////////////////////////////////////////////////// |
524 | ///////////////////////////////////////////////////////////////////////////// |
496 | |
525 | |
497 | struct dns_snd |
|
|
498 | { |
|
|
499 | dns_packet *pkt; |
|
|
500 | tstamp timeout, sent; |
|
|
501 | int retry; |
|
|
502 | struct dns_connection *dns; |
|
|
503 | int seqno; |
|
|
504 | |
|
|
505 | void gen_stream_req (int seqno, byte_stream &stream); |
|
|
506 | void gen_syn_req (const dns_cfg &cfg); |
|
|
507 | |
|
|
508 | dns_snd (dns_connection *dns); |
|
|
509 | ~dns_snd (); |
|
|
510 | }; |
|
|
511 | |
|
|
512 | static u16 dns_id = 12098; // TODO: should be per-vpn |
526 | static u16 dns_id = 0; // TODO: should be per-vpn |
513 | |
527 | |
514 | static u16 next_id () |
528 | static u16 next_id () |
515 | { |
529 | { |
|
|
530 | if (!dns_id) |
|
|
531 | dns_id = time (0); |
|
|
532 | |
516 | // the simplest lsfr with periodicity 65535 i could find |
533 | // the simplest lsfr with periodicity 65535 i could find |
517 | dns_id = (dns_id << 1) |
534 | dns_id = (dns_id << 1) |
518 | | (((dns_id >> 1) |
535 | | (((dns_id >> 1) |
519 | ^ (dns_id >> 2) |
536 | ^ (dns_id >> 2) |
520 | ^ (dns_id >> 4) |
537 | ^ (dns_id >> 4) |
521 | ^ (dns_id >> 15)) & 1); |
538 | ^ (dns_id >> 15)) & 1); |
522 | |
539 | |
523 | return dns_id; |
540 | return dns_id; |
524 | } |
541 | } |
525 | |
542 | |
|
|
543 | struct dns_rcv; |
|
|
544 | struct dns_snd; |
|
|
545 | |
|
|
546 | struct dns_connection |
|
|
547 | { |
|
|
548 | connection *c; |
|
|
549 | struct vpn *vpn; |
|
|
550 | |
|
|
551 | dns_cfg cfg; |
|
|
552 | |
|
|
553 | bool established; |
|
|
554 | |
|
|
555 | tstamp last_received; |
|
|
556 | tstamp last_sent; |
|
|
557 | double min_latency; |
|
|
558 | double poll_interval, send_interval; |
|
|
559 | |
|
|
560 | vector<dns_rcv *> rcvpq; |
|
|
561 | |
|
|
562 | byte_stream rcvdq; int rcvseq; int repseq; |
|
|
563 | byte_stream snddq; int sndseq; |
|
|
564 | |
|
|
565 | inline void time_cb (ev::timer &w, int revents); ev::timer tw; |
|
|
566 | void receive_rep (dns_rcv *r); |
|
|
567 | |
|
|
568 | dns_connection (connection *c); |
|
|
569 | ~dns_connection (); |
|
|
570 | }; |
|
|
571 | |
|
|
572 | struct dns_snd |
|
|
573 | { |
|
|
574 | dns_packet *pkt; |
|
|
575 | tstamp timeout, sent; |
|
|
576 | int retry; |
|
|
577 | struct dns_connection *dns; |
|
|
578 | int seqno; |
|
|
579 | bool stdhdr; |
|
|
580 | |
|
|
581 | void gen_stream_req (int seqno, byte_stream &stream); |
|
|
582 | void gen_syn_req (); |
|
|
583 | |
|
|
584 | dns_snd (dns_connection *dns); |
|
|
585 | ~dns_snd (); |
|
|
586 | }; |
|
|
587 | |
526 | dns_snd::dns_snd (dns_connection *dns) |
588 | dns_snd::dns_snd (dns_connection *dns) |
527 | : dns (dns) |
589 | : dns (dns) |
528 | { |
590 | { |
529 | timeout = 0; |
591 | timeout = 0; |
530 | retry = 0; |
592 | retry = 0; |
531 | seqno = 0; |
593 | seqno = 0; |
532 | sent = NOW; |
594 | sent = ev_now (); |
|
|
595 | stdhdr = false; |
533 | |
596 | |
534 | pkt = new dns_packet; |
597 | pkt = new dns_packet; |
535 | |
598 | |
536 | pkt->id = next_id (); |
599 | pkt->id = next_id (); |
537 | } |
600 | } |
… | |
… | |
564 | } |
627 | } |
565 | } |
628 | } |
566 | |
629 | |
567 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
630 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
568 | { |
631 | { |
|
|
632 | stdhdr = true; |
569 | this->seqno = seqno; |
633 | this->seqno = seqno; |
570 | |
634 | |
571 | timeout = NOW + INITIAL_TIMEOUT; |
635 | timeout = ev_now () + INITIAL_TIMEOUT; |
572 | |
636 | |
573 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
637 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
574 | pkt->qdcount = htons (1); |
638 | pkt->qdcount = htons (1); |
575 | |
639 | |
576 | int offs = 6*2; |
640 | int offs = 6*2; |
577 | int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); |
641 | int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2); |
578 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
642 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
579 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
643 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
580 | |
644 | |
581 | char enc[256], *encp = enc; |
645 | char enc[256], *encp = enc; |
582 | encode_header (enc, THISNODE->id, seqno); |
646 | encode_header (enc, THISNODE->id, seqno); |
… | |
… | |
600 | encp += lbllen; |
664 | encp += lbllen; |
601 | |
665 | |
602 | enclen -= lbllen; |
666 | enclen -= lbllen; |
603 | } |
667 | } |
604 | |
668 | |
605 | append_domain (*pkt, offs, THISNODE->domain); |
669 | append_domain (*pkt, offs, dns->c->conf->domain); |
606 | |
670 | |
607 | (*pkt)[offs++] = 0; |
671 | (*pkt)[offs++] = 0; |
608 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
672 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
609 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
673 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
610 | |
674 | |
611 | pkt->len = offs; |
675 | pkt->len = offs; |
612 | } |
676 | } |
613 | |
677 | |
614 | void dns_snd::gen_syn_req (const dns_cfg &cfg) |
678 | void dns_snd::gen_syn_req () |
615 | { |
679 | { |
616 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
680 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
617 | |
681 | |
618 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
682 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
619 | pkt->qdcount = htons (1); |
683 | pkt->qdcount = htons (1); |
620 | |
684 | |
621 | int offs = 6*2; |
685 | int offs = 6 * 2; |
622 | |
686 | |
623 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg)); |
687 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg)); |
624 | |
688 | |
625 | assert (elen <= MAX_LBL_SIZE); |
689 | assert (elen <= MAX_LBL_SIZE); |
626 | |
690 | |
627 | (*pkt)[offs] = elen; |
691 | (*pkt)[offs] = elen; |
628 | offs += elen + 1; |
692 | offs += elen + 1; |
629 | append_domain (*pkt, offs, THISNODE->domain); |
693 | append_domain (*pkt, offs, dns->c->conf->domain); |
630 | |
694 | |
631 | (*pkt)[offs++] = 0; |
695 | (*pkt)[offs++] = 0; |
632 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
696 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
633 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
697 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
634 | |
698 | |
… | |
… | |
657 | delete pkt; |
721 | delete pkt; |
658 | } |
722 | } |
659 | |
723 | |
660 | ///////////////////////////////////////////////////////////////////////////// |
724 | ///////////////////////////////////////////////////////////////////////////// |
661 | |
725 | |
662 | struct dns_connection |
|
|
663 | { |
|
|
664 | connection *c; |
|
|
665 | struct vpn *vpn; |
|
|
666 | |
|
|
667 | dns_cfg cfg; |
|
|
668 | |
|
|
669 | bool established; |
|
|
670 | |
|
|
671 | tstamp last_received; |
|
|
672 | tstamp last_sent; |
|
|
673 | double poll_interval, send_interval; |
|
|
674 | |
|
|
675 | vector<dns_rcv *> rcvpq; |
|
|
676 | |
|
|
677 | byte_stream rcvdq; int rcvseq; |
|
|
678 | byte_stream snddq; int sndseq; |
|
|
679 | |
|
|
680 | void time_cb (time_watcher &w); time_watcher tw; |
|
|
681 | void receive_rep (dns_rcv *r); |
|
|
682 | |
|
|
683 | dns_connection (connection *c); |
|
|
684 | ~dns_connection (); |
|
|
685 | }; |
|
|
686 | |
|
|
687 | dns_connection::dns_connection (connection *c) |
726 | dns_connection::dns_connection (connection *c) |
688 | : c (c) |
727 | : c (c) |
689 | , rcvdq (MAX_BACKLOG * 2) |
728 | , rcvdq (MAX_BACKLOG * 2) |
690 | , snddq (MAX_BACKLOG * 2) |
729 | , snddq (MAX_BACKLOG) |
691 | , tw (this, &dns_connection::time_cb) |
|
|
692 | { |
730 | { |
|
|
731 | tw.set<dns_connection, &dns_connection::time_cb> (this); |
|
|
732 | |
693 | vpn = c->vpn; |
733 | vpn = c->vpn; |
694 | |
734 | |
695 | established = false; |
735 | established = false; |
696 | |
736 | |
697 | rcvseq = sndseq = 0; |
737 | rcvseq = repseq = sndseq = 0; |
698 | |
738 | |
699 | last_sent = last_received = 0; |
739 | last_sent = last_received = 0; |
700 | poll_interval = MIN_POLL_INTERVAL; |
740 | poll_interval = 0.5; // starting here |
701 | send_interval = 0.2; // starting rate |
741 | send_interval = 0.5; // starting rate |
|
|
742 | min_latency = INITIAL_TIMEOUT; |
702 | } |
743 | } |
703 | |
744 | |
704 | dns_connection::~dns_connection () |
745 | dns_connection::~dns_connection () |
705 | { |
746 | { |
706 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
747 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
… | |
… | |
711 | |
752 | |
712 | void dns_connection::receive_rep (dns_rcv *r) |
753 | void dns_connection::receive_rep (dns_rcv *r) |
713 | { |
754 | { |
714 | if (r->datalen) |
755 | if (r->datalen) |
715 | { |
756 | { |
716 | last_received = NOW; |
757 | last_received = ev_now (); |
717 | tw.trigger (); |
758 | tw (); |
718 | |
759 | |
719 | poll_interval = send_interval; |
760 | poll_interval = send_interval; |
720 | } |
761 | } |
721 | else |
762 | else |
722 | { |
763 | { |
723 | poll_interval *= 1.1; |
764 | poll_interval *= 1.5; |
|
|
765 | |
724 | if (poll_interval > MAX_POLL_INTERVAL) |
766 | if (poll_interval > MAX_POLL_INTERVAL) |
725 | poll_interval = MAX_POLL_INTERVAL; |
767 | poll_interval = MAX_POLL_INTERVAL; |
726 | } |
768 | } |
727 | |
769 | |
728 | rcvpq.push_back (r); |
770 | rcvpq.push_back (r); |
… | |
… | |
731 | |
773 | |
732 | // find next packet |
774 | // find next packet |
733 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
775 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
734 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
776 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
735 | { |
777 | { |
|
|
778 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
736 | // enter the packet into our input stream |
779 | // enter the packet into our input stream |
737 | r = *i; |
780 | r = *i; |
738 | |
781 | |
739 | // remove the oldest packet, look forward, as it's oldest first |
782 | // remove the oldest packet, look forward, as it's oldest first |
740 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
783 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
741 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
784 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
742 | { |
785 | { |
|
|
786 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
743 | delete *j; |
787 | delete *j; |
744 | rcvpq.erase (j); |
788 | rcvpq.erase (j); |
745 | break; |
789 | break; |
746 | } |
790 | } |
747 | |
791 | |
748 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
792 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
749 | |
793 | |
750 | if (!rcvdq.put (r->data, r->datalen)) |
794 | if (!rcvdq.put (r->data, r->datalen)) |
|
|
795 | { |
|
|
796 | slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)"); |
751 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
797 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
|
|
798 | } |
752 | |
799 | |
753 | while (vpn_packet *pkt = rcvdq.get ()) |
800 | while (vpn_packet *pkt = rcvdq.get ()) |
754 | { |
801 | { |
755 | sockinfo si; |
802 | sockinfo si; |
756 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
803 | si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; |
757 | |
804 | |
758 | vpn->recv_vpn_packet (pkt, si); |
805 | vpn->recv_vpn_packet (pkt, si); |
759 | |
806 | |
760 | delete pkt; |
807 | delete pkt; |
761 | } |
808 | } |
… | |
… | |
775 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
822 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
776 | |
823 | |
777 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
824 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
778 | && pkt.qdcount == htons (1)) |
825 | && pkt.qdcount == htons (1)) |
779 | { |
826 | { |
780 | char qname[MAXSIZE]; |
827 | char qname [MAXSIZE]; |
781 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
828 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
782 | |
829 | |
783 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
830 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
784 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
831 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
785 | |
832 | |
786 | pkt.qdcount = htons (1); |
833 | pkt.qdcount = htons (1); |
787 | pkt.ancount = 0; |
834 | pkt.ancount = 0; |
788 | pkt.nscount = 0; // should be self, as other nameservers reply like this |
835 | pkt.nscount = 0; // should be self, as other nameservers reply like this |
789 | pkt.arcount = 0; // a record for self, as other nameservers reply like this |
836 | pkt.arcount = 0; // a record for self, as other nameservers reply like this |
790 | |
837 | |
791 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
838 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL); |
792 | |
839 | |
793 | int dlen = strlen (THISNODE->domain); |
840 | int dlen = strlen (THISNODE->domain); |
794 | |
841 | |
795 | if (qclass == RR_CLASS_IN |
842 | if (qclass == RR_CLASS_IN |
796 | && qlen > dlen + 1 |
843 | && qlen > dlen + 1 |
797 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
844 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
798 | { |
845 | { |
799 | // now generate reply |
846 | // now generate reply |
800 | pkt.ancount = htons (1); // one answer RR |
847 | pkt.ancount = htons (1); // one answer RR |
801 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
848 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
802 | |
849 | |
… | |
… | |
824 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
871 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
825 | { |
872 | { |
826 | // already seen that request: simply reply with the cached reply |
873 | // already seen that request: simply reply with the cached reply |
827 | dns_rcv *r = *i; |
874 | dns_rcv *r = *i; |
828 | |
875 | |
829 | slog (L_DEBUG, "DUPLICATE %d\n", htons (r->pkt->id)); |
876 | slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno); |
|
|
877 | |
|
|
878 | // refresh header & id, as the retry count could have changed |
|
|
879 | memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE); |
|
|
880 | r->pkt->id = pkt.id; |
830 | |
881 | |
831 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
882 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
832 | pkt.id = r->pkt->id; |
883 | |
833 | goto duplicate_request; |
884 | goto duplicate_request; |
834 | } |
885 | } |
835 | |
886 | |
836 | // new packet, queue |
887 | // new packet, queue |
837 | rcv = new dns_rcv (seqno, data, datalen); |
888 | rcv = new dns_rcv (seqno, data, datalen); |
838 | dns->receive_rep (rcv); |
889 | dns->receive_rep (rcv); |
839 | } |
890 | } |
840 | |
891 | |
|
|
892 | { |
841 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
893 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
842 | |
894 | |
843 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
895 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
844 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
896 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
845 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
897 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
846 | pkt [offs++] = 0; pkt [offs++] = 0; |
898 | pkt [offs++] = 0; pkt [offs++] = 0; |
847 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
899 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
848 | |
900 | |
849 | int rdlen_offs = offs += 2; |
901 | int rdlen_offs = offs += 2; |
850 | |
902 | |
|
|
903 | if (dns) |
|
|
904 | { |
851 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
905 | int dlen = ntohs (dns->cfg.max_size) - offs; |
|
|
906 | |
852 | // bind doesn't compress well, so reduce further by one label length |
907 | // bind doesn't compress well, so reduce further by one label length |
853 | dlen -= qlen; |
908 | dlen -= qlen; |
854 | |
909 | |
855 | if (dns) |
910 | // only put data into in-order sequence packets, if |
856 | { |
911 | // we receive out-of-order packets we generate empty |
857 | while (dlen > 1 && !dns->snddq.empty ()) |
912 | // replies |
|
|
913 | //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D |
|
|
914 | if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW) |
858 | { |
915 | { |
|
|
916 | dns->repseq = seqno; |
|
|
917 | |
|
|
918 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
919 | { |
859 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
920 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
860 | |
921 | |
861 | if (txtlen > dns->snddq.size ()) |
922 | if (txtlen > dns->snddq.size ()) |
862 | txtlen = dns->snddq.size (); |
923 | txtlen = dns->snddq.size (); |
863 | |
924 | |
864 | pkt[offs++] = txtlen; |
925 | pkt[offs++] = txtlen; |
865 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
926 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
866 | offs += txtlen; |
927 | offs += txtlen; |
867 | dns->snddq.remove (txtlen); |
928 | dns->snddq.remove (txtlen); |
868 | |
929 | |
869 | dlen -= txtlen + 1; |
930 | dlen -= txtlen + 1; |
|
|
931 | } |
870 | } |
932 | } |
871 | |
933 | |
872 | // avoid empty TXT rdata |
934 | // avoid completely empty TXT rdata |
873 | if (offs == rdlen_offs) |
935 | if (offs == rdlen_offs) |
874 | pkt[offs++] = 0; |
936 | pkt[offs++] = 0; |
875 | |
937 | |
876 | slog (L_NOISE, "snddq %d", dns->snddq.size ()); |
938 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
877 | } |
939 | } |
878 | else |
940 | else |
879 | { |
941 | { |
880 | // send RST |
942 | // send RST |
881 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
943 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
882 | pkt [offs++] = CMD_IP_RST; |
944 | pkt [offs++] = CMD_IP_RST; |
883 | } |
945 | } |
884 | |
946 | |
885 | int rdlen = offs - rdlen_offs; |
947 | int rdlen = offs - rdlen_offs; |
886 | |
948 | |
887 | pkt [rdlen_offs - 2] = rdlen >> 8; |
949 | pkt [rdlen_offs - 2] = rdlen >> 8; |
888 | pkt [rdlen_offs - 1] = rdlen; |
950 | pkt [rdlen_offs - 1] = rdlen; |
889 | |
951 | |
890 | if (dns) |
952 | if (dns) |
891 | { |
953 | { |
892 | // now update dns_rcv copy |
954 | // now update dns_rcv copy |
893 | rcv->pkt->len = offs; |
955 | rcv->pkt->len = offs; |
894 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
956 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
895 | } |
957 | } |
|
|
958 | } |
896 | |
959 | |
897 | duplicate_request: ; |
960 | duplicate_request: ; |
898 | } |
961 | } |
899 | else |
962 | else |
900 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
963 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
… | |
… | |
912 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
975 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
913 | pkt [offs++] = 0; pkt [offs++] = 0; |
976 | pkt [offs++] = 0; pkt [offs++] = 0; |
914 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
977 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
915 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
978 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
916 | |
979 | |
917 | slog (L_INFO, _("DNS tunnel: client %d tries to connect"), client); |
980 | slog (L_INFO, _("DNS: client %d connects"), client); |
918 | |
981 | |
919 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
982 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
920 | pkt [offs++] = CMD_IP_REJ; |
983 | pkt [offs++] = CMD_IP_REJ; |
921 | |
984 | |
922 | if (0 < client && client <= conns.size ()) |
985 | if (0 < client && client <= conns.size ()) |
… | |
… | |
956 | { |
1019 | { |
957 | dns_connection *dns = (*i)->dns; |
1020 | dns_connection *dns = (*i)->dns; |
958 | connection *c = dns->c; |
1021 | connection *c = dns->c; |
959 | int seqno = (*i)->seqno; |
1022 | int seqno = (*i)->seqno; |
960 | u8 data[MAXSIZE], *datap = data; |
1023 | u8 data[MAXSIZE], *datap = data; |
|
|
1024 | //printf ("rcv pkt %x\n", seqno);//D |
961 | |
1025 | |
962 | if ((*i)->retry) |
1026 | if ((*i)->retry) |
963 | { |
1027 | { |
964 | dns->send_interval *= 1.001; |
1028 | dns->send_interval *= 1.01; |
965 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1029 | if (dns->send_interval > MAX_SEND_INTERVAL) |
966 | dns->send_interval = MAX_SEND_INTERVAL; |
1030 | dns->send_interval = MAX_SEND_INTERVAL; |
967 | } |
1031 | } |
968 | else |
1032 | else |
969 | { |
1033 | { |
970 | #if 1 |
1034 | #if 0 |
971 | dns->send_interval *= 0.9999; |
1035 | dns->send_interval *= 0.999; |
972 | #endif |
1036 | #endif |
973 | if (dns->send_interval < MIN_SEND_INTERVAL) |
|
|
974 | dns->send_interval = MIN_SEND_INTERVAL; |
|
|
975 | |
|
|
976 | // the latency surely puts an upper bound on |
1037 | // the latency surely puts an upper bound on |
977 | // the minimum send interval |
1038 | // the minimum send interval |
978 | double latency = NOW - (*i)->sent; |
1039 | double latency = ev_now () - (*i)->sent; |
979 | |
1040 | |
980 | if (dns->send_interval > latency) |
1041 | if (latency < dns->min_latency) |
981 | dns->send_interval = latency; |
1042 | dns->min_latency = latency; |
|
|
1043 | |
|
|
1044 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
|
|
1045 | dns->send_interval = dns->min_latency * conf.dns_overlap_factor; |
|
|
1046 | |
|
|
1047 | if (dns->send_interval < conf.dns_send_interval) |
|
|
1048 | dns->send_interval = conf.dns_send_interval; |
982 | } |
1049 | } |
983 | |
1050 | |
984 | delete *i; |
1051 | delete *i; |
985 | dns_sndpq.erase (i); |
1052 | dns_sndpq.erase (i); |
986 | |
1053 | |
… | |
… | |
1036 | |
1103 | |
1037 | if (ip [0] == CMD_IP_1 |
1104 | if (ip [0] == CMD_IP_1 |
1038 | && ip [1] == CMD_IP_2 |
1105 | && ip [1] == CMD_IP_2 |
1039 | && ip [2] == CMD_IP_3) |
1106 | && ip [2] == CMD_IP_3) |
1040 | { |
1107 | { |
1041 | slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]); |
1108 | slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]); |
1042 | |
1109 | |
1043 | if (ip [3] == CMD_IP_RST) |
1110 | if (ip [3] == CMD_IP_RST) |
1044 | { |
1111 | { |
1045 | slog (L_DEBUG, _("got tunnel RST request")); |
1112 | slog (L_DEBUG, _("DNS: got tunnel RST request")); |
1046 | |
1113 | |
1047 | delete dns; c->dns = 0; |
1114 | delete dns; c->dns = 0; |
1048 | |
1115 | |
1049 | return; |
1116 | return; |
1050 | } |
1117 | } |
1051 | else if (ip [3] == CMD_IP_SYN) |
1118 | else if (ip [3] == CMD_IP_SYN) |
1052 | { |
1119 | { |
1053 | slog (L_DEBUG, _("got tunnel SYN reply, server likes us.")); |
1120 | slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); |
1054 | dns->established = true; |
1121 | dns->established = true; |
1055 | } |
1122 | } |
1056 | else if (ip [3] == CMD_IP_REJ) |
1123 | else if (ip [3] == CMD_IP_REJ) |
1057 | { |
1124 | { |
1058 | slog (L_DEBUG, _("got tunnel REJ reply, server does not like us, aborting.")); |
1125 | slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting.")); |
1059 | abort (); |
1126 | abort (); |
1060 | } |
1127 | } |
1061 | else |
1128 | else |
1062 | slog (L_INFO, _("got unknown meta command %02x"), ip [3]); |
1129 | slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); |
1063 | } |
1130 | } |
1064 | else |
1131 | else |
1065 | slog (L_INFO, _("got spurious a record %d.%d.%d.%d"), |
1132 | slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), |
1066 | ip [0], ip [1], ip [2], ip [3]); |
1133 | ip [0], ip [1], ip [2], ip [3]); |
1067 | |
1134 | |
1068 | return; |
1135 | return; |
1069 | } |
1136 | } |
1070 | |
1137 | |
1071 | int client, rseqno; |
1138 | int client, rseqno; |
1072 | decode_header (qname, client, rseqno); |
1139 | decode_header (qname, client, rseqno); |
1073 | |
1140 | |
1074 | if (client != THISNODE->id) |
1141 | if (client != THISNODE->id) |
1075 | { |
1142 | { |
1076 | slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); |
1143 | slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring")); |
1077 | datap = 0; |
1144 | datap = 0; |
1078 | } |
1145 | } |
1079 | else if (rseqno != seqno) |
1146 | else if (rseqno != seqno) |
1080 | { |
1147 | { |
1081 | slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?")); |
1148 | slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?")); |
1082 | datap = 0; |
1149 | datap = 0; |
1083 | } |
1150 | } |
1084 | } |
1151 | } |
1085 | } |
1152 | } |
1086 | |
1153 | |
… | |
… | |
1091 | break; |
1158 | break; |
1092 | } |
1159 | } |
1093 | } |
1160 | } |
1094 | |
1161 | |
1095 | void |
1162 | void |
1096 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1163 | vpn::dnsv4_ev (ev::io &w, int revents) |
1097 | { |
1164 | { |
1098 | if (revents & EVENT_READ) |
1165 | if (revents & EV_READ) |
1099 | { |
1166 | { |
1100 | dns_packet *pkt = new dns_packet; |
1167 | dns_packet *pkt = new dns_packet; |
1101 | struct sockaddr_in sa; |
1168 | struct sockaddr_in sa; |
1102 | socklen_t sa_len = sizeof (sa); |
1169 | socklen_t sa_len = sizeof (sa); |
1103 | |
1170 | |
1104 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1171 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1105 | |
1172 | |
1106 | if (pkt->len > 0) |
1173 | if (pkt->len > 0) |
1107 | { |
1174 | { |
1108 | if (THISNODE->dns_port) |
1175 | if (ntohs (pkt->flags) & FLAG_RESPONSE) |
|
|
1176 | dnsv4_client (*pkt); |
|
|
1177 | else |
1109 | { |
1178 | { |
1110 | dnsv4_server (*pkt); |
1179 | dnsv4_server (*pkt); |
1111 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1180 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1112 | } |
1181 | } |
1113 | else |
|
|
1114 | dnsv4_client (*pkt); |
|
|
1115 | |
1182 | |
1116 | delete pkt; |
1183 | delete pkt; |
1117 | } |
1184 | } |
1118 | } |
1185 | } |
1119 | } |
1186 | } |
1120 | |
1187 | |
1121 | bool |
1188 | bool |
1122 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1189 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1123 | { |
1190 | { |
|
|
1191 | int client = ntohl (si.host); |
|
|
1192 | |
|
|
1193 | assert (0 < client && client <= conns.size ()); |
|
|
1194 | |
|
|
1195 | connection *c = conns [client - 1]; |
|
|
1196 | |
1124 | if (!dns) |
1197 | if (!c->dns) |
1125 | dns = new dns_connection (this); |
1198 | c->dns = new dns_connection (c); |
1126 | |
1199 | |
1127 | if (!dns->snddq.put (pkt)) |
1200 | if (c->dns->snddq.put (pkt)) |
1128 | return false; |
1201 | c->dns->tw (); |
1129 | |
1202 | |
1130 | dns->tw.trigger (); |
1203 | // always return true even if the buffer overflows |
1131 | |
|
|
1132 | return true; |
1204 | return true; |
1133 | } |
1205 | } |
1134 | |
1206 | |
1135 | void |
1207 | void |
1136 | connection::dnsv4_reset_connection () |
1208 | connection::dnsv4_reset_connection () |
… | |
… | |
1139 | } |
1211 | } |
1140 | |
1212 | |
1141 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1213 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1142 | |
1214 | |
1143 | void |
1215 | void |
1144 | dns_connection::time_cb (time_watcher &w) |
1216 | dns_connection::time_cb (ev::timer &w, int revents) |
1145 | { |
1217 | { |
1146 | // servers have to be polled |
1218 | // servers have to be polled |
1147 | if (THISNODE->dns_port) |
1219 | if (THISNODE->dns_port) |
1148 | return; |
1220 | return; |
1149 | |
1221 | |
1150 | // check for timeouts and (re)transmit |
1222 | // check for timeouts and (re)transmit |
1151 | tstamp next = NOW + poll_interval; |
1223 | tstamp next = ev::now () + poll_interval; |
1152 | dns_snd *send = 0; |
1224 | dns_snd *send = 0; |
1153 | |
1225 | |
1154 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1226 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1155 | i != vpn->dns_sndpq.end (); |
1227 | i != vpn->dns_sndpq.end (); |
1156 | ++i) |
1228 | ++i) |
1157 | { |
1229 | { |
1158 | dns_snd *r = *i; |
1230 | dns_snd *r = *i; |
1159 | |
1231 | |
1160 | if (r->timeout <= NOW) |
1232 | if (r->timeout <= ev_now ()) |
1161 | { |
1233 | { |
1162 | if (!send) |
1234 | if (!send) |
1163 | { |
1235 | { |
1164 | send = r; |
1236 | send = r; |
1165 | |
1237 | |
1166 | r->retry++; |
1238 | r->retry++; |
1167 | r->timeout = NOW + r->retry; |
1239 | r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); |
|
|
1240 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D |
|
|
1241 | |
|
|
1242 | // the following code changes the query section a bit, forcing |
|
|
1243 | // the forwarder to generate a new request |
|
|
1244 | if (r->stdhdr) |
|
|
1245 | encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1168 | } |
1246 | } |
1169 | } |
1247 | } |
1170 | else |
1248 | else |
1171 | NEXT (r->timeout); |
1249 | NEXT (r->timeout); |
1172 | } |
1250 | } |
1173 | |
1251 | |
1174 | if (last_sent + send_interval <= NOW) |
|
|
1175 | { |
|
|
1176 | if (!send) |
1252 | if (!send) |
|
|
1253 | { |
|
|
1254 | // generate a new packet, if wise |
|
|
1255 | |
|
|
1256 | if (!established) |
1177 | { |
1257 | { |
1178 | // generate a new packet, if wise |
|
|
1179 | |
|
|
1180 | if (!established) |
|
|
1181 | { |
|
|
1182 | if (vpn->dns_sndpq.empty ()) |
1258 | if (vpn->dns_sndpq.empty ()) |
1183 | { |
|
|
1184 | send = new dns_snd (this); |
|
|
1185 | |
|
|
1186 | cfg.reset (THISNODE->id); |
|
|
1187 | send->gen_syn_req (cfg); |
|
|
1188 | } |
|
|
1189 | } |
|
|
1190 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
|
|
1191 | { |
1259 | { |
1192 | send = new dns_snd (this); |
1260 | send = new dns_snd (this); |
|
|
1261 | |
|
|
1262 | cfg.reset (THISNODE->id); |
|
|
1263 | send->gen_syn_req (); |
|
|
1264 | } |
|
|
1265 | } |
|
|
1266 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
|
|
1267 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
|
|
1268 | { |
|
|
1269 | if (last_sent + send_interval <= ev_now ()) |
|
|
1270 | { |
|
|
1271 | //printf ("sending data request etc.\n"); //D |
|
|
1272 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
|
|
1273 | { |
|
|
1274 | poll_interval = send_interval; |
|
|
1275 | NEXT (ev_now () + send_interval); |
|
|
1276 | } |
|
|
1277 | |
|
|
1278 | send = new dns_snd (this); |
1193 | send->gen_stream_req (sndseq, snddq); |
1279 | send->gen_stream_req (sndseq, snddq); |
|
|
1280 | send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; |
|
|
1281 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D |
1194 | |
1282 | |
1195 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1283 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1196 | } |
1284 | } |
1197 | |
1285 | else |
1198 | if (send) |
1286 | NEXT (last_sent + send_interval); |
1199 | vpn->dns_sndpq.push_back (send); |
|
|
1200 | } |
1287 | } |
1201 | |
1288 | |
1202 | if (send) |
1289 | if (send) |
1203 | { |
1290 | vpn->dns_sndpq.push_back (send); |
1204 | last_sent = NOW; |
1291 | } |
|
|
1292 | |
|
|
1293 | if (send) |
|
|
1294 | { |
|
|
1295 | last_sent = ev_now (); |
1205 | sendto (vpn->dnsv4_fd, |
1296 | sendto (vpn->dnsv4_fd, |
1206 | send->pkt->at (0), send->pkt->len, 0, |
1297 | send->pkt->at (0), send->pkt->len, 0, |
1207 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1298 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1208 | } |
|
|
1209 | } |
1299 | } |
1210 | else |
|
|
1211 | NEXT (last_sent + send_interval); |
|
|
1212 | |
1300 | |
1213 | slog (L_NOISE, "pi %f si %f N %f (%d:%d)", |
1301 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1214 | poll_interval, send_interval, next - NOW, |
1302 | poll_interval, send_interval, next - ev_now (), |
1215 | vpn->dns_sndpq.size (), snddq.size ()); |
1303 | vpn->dns_sndpq.size (), snddq.size (), |
|
|
1304 | rcvpq.size ()); |
1216 | |
1305 | |
1217 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
1306 | // TODO: no idea when this happens, but when next < ev_now (), we have a problem |
|
|
1307 | // doesn't seem to happen anymore |
1218 | if (next < NOW + 0.0001) |
1308 | if (next < ev_now () + 0.001) |
1219 | next = NOW + 0.1; |
1309 | next = ev_now () + 0.1; |
1220 | |
1310 | |
1221 | w.start (next); |
1311 | w.start (next - ev_now ()); |
1222 | } |
1312 | } |
1223 | |
1313 | |
1224 | #endif |
1314 | #endif |
1225 | |
1315 | |