… | |
… | |
44 | #include "netcompat.h" |
44 | #include "netcompat.h" |
45 | |
45 | |
46 | #include "vpn.h" |
46 | #include "vpn.h" |
47 | |
47 | |
48 | #define MIN_RETRY 1. |
48 | #define MIN_RETRY 1. |
49 | #define MAX_RETRY 60. |
49 | #define MAX_RETRY 6. |
50 | |
50 | |
51 | #define MAX_OUTSTANDING 400 // max. outstanding requests |
51 | #define MAX_OUTSTANDING 400 // max. outstanding requests |
52 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING |
52 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING |
53 | #define MAX_RATE 10000 // requests/s |
53 | #define MAX_RATE 100 // requests/s |
54 | #define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE |
54 | #define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE |
55 | |
55 | |
56 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
56 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
57 | // 240 leaves about 4 bytes of server reply data |
57 | // 240 leaves about 4 bytes of server reply data |
58 | // every two request byte sless give room for one reply byte |
58 | // every two request byte sless give room for one reply byte |
59 | |
59 | |
60 | // seqno has 12 bits (3 bytes a 4 bits in the header) |
|
|
61 | #define SEQNO_MASK 0x7fff |
60 | #define SEQNO_MASK 0xffff |
62 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
61 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
63 | |
62 | |
64 | #define MAX_LBL_SIZE 63 |
63 | #define MAX_LBL_SIZE 63 |
65 | #define MAX_PKT_SIZE 512 |
64 | #define MAX_PKT_SIZE 512 |
66 | |
65 | |
… | |
… | |
269 | { |
268 | { |
270 | u8 hdr[3]; |
269 | u8 hdr[3]; |
271 | |
270 | |
272 | cdc26.decode (hdr, data, HDRSIZE); |
271 | cdc26.decode (hdr, data, HDRSIZE); |
273 | |
272 | |
274 | printf ("DEC %02x %02x %02x %02x\n", hdr[0], hdr[1], hdr[2], hdr[3]); |
|
|
275 | |
|
|
276 | clientid = hdr[0]; |
273 | clientid = hdr[0]; |
277 | seqno = (hdr[1] << 8) | hdr[2]; |
274 | seqno = (hdr[1] << 8) | hdr[2]; |
278 | } |
275 | } |
279 | |
276 | |
280 | ///////////////////////////////////////////////////////////////////////////// |
277 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
362 | |
359 | |
363 | ///////////////////////////////////////////////////////////////////////////// |
360 | ///////////////////////////////////////////////////////////////////////////// |
364 | |
361 | |
365 | #define FLAG_QUERY ( 0 << 15) |
362 | #define FLAG_QUERY ( 0 << 15) |
366 | #define FLAG_RESPONSE ( 1 << 15) |
363 | #define FLAG_RESPONSE ( 1 << 15) |
367 | #define FLAG_OP_MASK (15 << 14) |
364 | #define FLAG_OP_MASK (15 << 11) |
368 | #define FLAG_OP_QUERY ( 0 << 11) |
365 | #define FLAG_OP_QUERY ( 0 << 11) |
369 | #define FLAG_AA ( 1 << 10) |
366 | #define FLAG_AA ( 1 << 10) |
370 | #define FLAG_TC ( 1 << 9) |
367 | #define FLAG_TC ( 1 << 9) |
371 | #define FLAG_RD ( 1 << 8) |
368 | #define FLAG_RD ( 1 << 8) |
372 | #define FLAG_RA ( 1 << 7) |
369 | #define FLAG_RA ( 1 << 7) |
… | |
… | |
431 | struct dns_req |
428 | struct dns_req |
432 | { |
429 | { |
433 | dns_packet *pkt; |
430 | dns_packet *pkt; |
434 | tstamp next; |
431 | tstamp next; |
435 | int retry; |
432 | int retry; |
436 | connection *conn; |
433 | struct dns_connection *dns; |
437 | int seqno; |
434 | int seqno; |
438 | |
435 | |
439 | dns_req (connection *c); |
436 | dns_req (dns_connection *dns); |
440 | void gen_stream_req (int seqno, byte_stream *stream); |
437 | void gen_stream_req (int seqno, byte_stream &stream); |
441 | }; |
438 | }; |
442 | |
439 | |
443 | static u16 dns_id = 12098; // TODO: should be per-vpn |
440 | static u16 dns_id = 12098; // TODO: should be per-vpn |
444 | |
441 | |
445 | static u16 next_id () |
442 | static u16 next_id () |
… | |
… | |
452 | ^ (dns_id >> 15)) & 1); |
449 | ^ (dns_id >> 15)) & 1); |
453 | |
450 | |
454 | return dns_id; |
451 | return dns_id; |
455 | } |
452 | } |
456 | |
453 | |
457 | dns_req::dns_req (connection *c) |
454 | dns_req::dns_req (dns_connection *dns) |
458 | : conn (c) |
455 | : dns (dns) |
459 | { |
456 | { |
460 | next = 0; |
457 | next = 0; |
461 | retry = 0; |
458 | retry = 0; |
462 | |
459 | |
463 | pkt = new dns_packet; |
460 | pkt = new dns_packet; |
464 | |
461 | |
465 | pkt->id = next_id (); |
462 | pkt->id = next_id (); |
466 | } |
463 | } |
467 | |
464 | |
468 | void dns_req::gen_stream_req (int seqno, byte_stream *stream) |
465 | void dns_req::gen_stream_req (int seqno, byte_stream &stream) |
469 | { |
466 | { |
470 | this->seqno = seqno; |
467 | this->seqno = seqno; |
471 | |
468 | |
472 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
469 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
473 | pkt->qdcount = htons (1); |
470 | pkt->qdcount = htons (1); |
… | |
… | |
480 | char enc[256], *encp = enc; |
477 | char enc[256], *encp = enc; |
481 | encode_header (enc, THISNODE->id, seqno); |
478 | encode_header (enc, THISNODE->id, seqno); |
482 | |
479 | |
483 | int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); |
480 | int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); |
484 | |
481 | |
485 | if (datalen > stream->size ()) |
482 | if (datalen > stream.size ()) |
486 | datalen = stream->size (); |
483 | datalen = stream.size (); |
487 | |
484 | |
488 | int enclen = cdc62.encode (enc + HDRSIZE, stream->begin (), datalen) + HDRSIZE; |
485 | int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE; |
489 | |
|
|
490 | printf ("cdc62.encode %d->%d:%02x %02x %02x %02x\n", datalen, enclen, |
|
|
491 | stream->begin ()[0], |
|
|
492 | stream->begin ()[1], |
|
|
493 | stream->begin ()[2], |
|
|
494 | stream->begin ()[3]); |
|
|
495 | |
|
|
496 | stream->remove (datalen); |
486 | stream.remove (datalen); |
497 | |
487 | |
498 | while (enclen) |
488 | while (enclen) |
499 | { |
489 | { |
500 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
490 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
501 | |
491 | |
… | |
… | |
558 | { |
548 | { |
559 | delete pkt; |
549 | delete pkt; |
560 | } |
550 | } |
561 | |
551 | |
562 | ///////////////////////////////////////////////////////////////////////////// |
552 | ///////////////////////////////////////////////////////////////////////////// |
|
|
553 | |
|
|
554 | struct dns_connection |
|
|
555 | { |
|
|
556 | connection *c; |
|
|
557 | struct vpn *vpn; |
|
|
558 | |
|
|
559 | vector<dns_rcv *> rcvpq; |
|
|
560 | |
|
|
561 | int rcvseq; |
|
|
562 | int sndseq; |
|
|
563 | |
|
|
564 | byte_stream rcvdq; |
|
|
565 | byte_stream snddq; |
|
|
566 | |
|
|
567 | void time_cb (time_watcher &w); time_watcher tw; |
|
|
568 | void receive_rep (dns_rcv *r); |
|
|
569 | |
|
|
570 | dns_connection (connection *c); |
|
|
571 | ~dns_connection (); |
|
|
572 | }; |
|
|
573 | |
|
|
574 | dns_connection::dns_connection (connection *c) |
|
|
575 | : c (c) |
|
|
576 | , rcvdq (MAX_BACKLOG * 2) |
|
|
577 | , snddq (MAX_BACKLOG * 2) |
|
|
578 | , tw (this, &dns_connection::time_cb) |
|
|
579 | { |
|
|
580 | vpn = c->vpn; |
|
|
581 | |
|
|
582 | rcvseq = sndseq = 0; |
|
|
583 | } |
|
|
584 | |
|
|
585 | dns_connection::~dns_connection () |
|
|
586 | { |
|
|
587 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
|
|
588 | i != rcvpq.end (); |
|
|
589 | ++i) |
|
|
590 | delete *i; |
|
|
591 | } |
563 | |
592 | |
564 | struct dns_cfg |
593 | struct dns_cfg |
565 | { |
594 | { |
566 | u8 id1, id2, id3; |
595 | u8 id1, id2, id3; |
567 | u8 def_ttl; |
596 | u8 def_ttl; |
568 | u8 unused1; |
597 | u8 unused1; |
569 | u16 max_size; |
598 | u16 max_size; |
570 | u8 flags1, flags2; |
599 | u8 flags1, flags2; |
571 | }; |
600 | }; |
572 | |
601 | |
573 | void connection::dnsv4_receive_rep (struct dns_rcv *r) |
602 | void dns_connection::receive_rep (dns_rcv *r) |
574 | { |
603 | { |
575 | dns_rcvpq.push_back (r); |
604 | rcvpq.push_back (r); |
576 | |
605 | |
577 | printf ("%d got inketc %d (%02x %02x %02x %02x)\n", THISNODE->id, r->seqno |
|
|
578 | ,r->data[0] |
|
|
579 | ,r->data[1] |
|
|
580 | ,r->data[2] |
|
|
581 | ,r->data[3] |
|
|
582 | ); |
|
|
583 | redo: |
606 | redo: |
584 | |
607 | |
585 | // find next packet |
608 | // find next packet |
586 | for (vector<dns_rcv *>::iterator i = dns_rcvpq.end (); i-- != dns_rcvpq.begin (); ) |
609 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
587 | if (SEQNO_EQ (dns_rcvseq, (*i)->seqno)) |
610 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
588 | { |
611 | { |
589 | // enter the packet into our input stream |
612 | // enter the packet into our input stream |
590 | r = *i; |
613 | r = *i; |
591 | |
614 | |
592 | printf ("%d checking for older packet %d\n", THISNODE->id, dns_rcvseq); |
|
|
593 | // remove the oldest packet, look forward, as it's oldest first |
615 | // remove the oldest packet, look forward, as it's oldest first |
594 | for (vector<dns_rcv *>::iterator j = dns_rcvpq.begin (); j != dns_rcvpq.end (); ++j) |
616 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
595 | if (SEQNO_EQ ((*j)->seqno, dns_rcvseq - MAX_WINDOW)) |
617 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
596 | { |
618 | { |
597 | printf ("%d removing %d\n", THISNODE->id, (*j)->seqno); |
|
|
598 | delete *j; |
619 | delete *j; |
599 | dns_rcvpq.erase (j); |
620 | rcvpq.erase (j); |
600 | break; |
621 | break; |
601 | } |
622 | } |
602 | |
623 | |
603 | dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; |
624 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
604 | |
625 | |
605 | if (!dns_snddq && !dns_rcvdq) |
|
|
606 | { |
|
|
607 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
|
|
608 | dns_snddq = new byte_stream (MAX_BACKLOG); |
|
|
609 | |
|
|
610 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
|
|
611 | } |
|
|
612 | |
|
|
613 | if (!dns_rcvdq->put (r->data, r->datalen)) |
626 | if (!rcvdq.put (r->data, r->datalen)) |
614 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
627 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
615 | |
628 | |
616 | while (vpn_packet *pkt = dns_rcvdq->get ()) |
629 | while (vpn_packet *pkt = rcvdq.get ()) |
617 | { |
630 | { |
618 | sockinfo si; |
631 | sockinfo si; |
619 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
632 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
620 | |
633 | |
621 | vpn->recv_vpn_packet (pkt, si); |
634 | vpn->recv_vpn_packet (pkt, si); |
… | |
… | |
634 | //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
647 | //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
635 | int offs = 6 * 2; // skip header |
648 | int offs = 6 * 2; // skip header |
636 | |
649 | |
637 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
650 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
638 | |
651 | |
639 | if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
652 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
640 | && pkt->qdcount == htons (1)) |
653 | && pkt->qdcount == htons (1)) |
641 | { |
654 | { |
642 | char qname[MAXSIZE]; |
655 | char qname[MAXSIZE]; |
643 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
656 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
644 | |
657 | |
… | |
… | |
664 | decode_header (qname, client, seqno); |
677 | decode_header (qname, client, seqno); |
665 | |
678 | |
666 | u8 data[MAXSIZE]; |
679 | u8 data[MAXSIZE]; |
667 | int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
680 | int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
668 | |
681 | |
669 | printf ("cdc62.decode %d(%d): %02x %02x %02x %02x\n", |
|
|
670 | qlen - (dlen + 1 + HDRSIZE), datalen |
|
|
671 | ,data[0] |
|
|
672 | ,data[1] |
|
|
673 | ,data[2] |
|
|
674 | ,data[3]); |
|
|
675 | |
|
|
676 | printf ("SRV got %d <%.*s>\n", seqno, qlen, qname + HDRSIZE);//D |
|
|
677 | printf ("SRV got %d <%.*s>\n", seqno, qlen - (dlen + 1 + HDRSIZE), qname + HDRSIZE);//D |
|
|
678 | |
|
|
679 | if (0 < client && client <= conns.size ()) |
682 | if (0 < client && client <= conns.size ()) |
680 | { |
683 | { |
681 | connection *c = conns [client - 1]; |
684 | connection *c = conns [client - 1]; |
682 | |
685 | |
|
|
686 | if (!c->dns) |
|
|
687 | c->dns = new dns_connection (c); |
|
|
688 | |
|
|
689 | dns_connection *dns = c->dns; |
|
|
690 | |
683 | for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.end (); i-- != c->dns_rcvpq.begin (); ) |
691 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
684 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
692 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
685 | { |
693 | { |
686 | // already seen that request: simply reply with the cached reply |
694 | // already seen that request: simply reply with the cached reply |
687 | dns_rcv *r = *i; |
695 | dns_rcv *r = *i; |
688 | |
696 | |
… | |
… | |
693 | goto duplicate_request; |
701 | goto duplicate_request; |
694 | } |
702 | } |
695 | |
703 | |
696 | // new packet, queue |
704 | // new packet, queue |
697 | dns_rcv *rcv = new dns_rcv (seqno, data, datalen); |
705 | dns_rcv *rcv = new dns_rcv (seqno, data, datalen); |
698 | c->dnsv4_receive_rep (rcv); |
706 | dns->receive_rep (rcv); |
699 | |
707 | |
700 | // now generate reply |
708 | // now generate reply |
701 | pkt->ancount = htons (1); // one answer RR |
709 | pkt->ancount = htons (1); // one answer RR |
702 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
710 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
703 | |
711 | |
… | |
… | |
715 | // bind doesn't compress well, so reduce further by one label length |
723 | // bind doesn't compress well, so reduce further by one label length |
716 | dlen -= qlen; |
724 | dlen -= qlen; |
717 | |
725 | |
718 | int rdlen_offs = offs += 2; |
726 | int rdlen_offs = offs += 2; |
719 | |
727 | |
720 | while (c->dns_snddq |
728 | while (dlen > 1 && !dns->snddq.empty ()) |
721 | && !c->dns_snddq->empty () |
|
|
722 | && dlen > 1) |
|
|
723 | { |
729 | { |
724 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
730 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
725 | |
731 | |
726 | if (txtlen > c->dns_snddq->size ()) |
732 | if (txtlen > dns->snddq.size ()) |
727 | txtlen = c->dns_snddq->size (); |
733 | txtlen = dns->snddq.size (); |
728 | |
734 | |
729 | (*pkt)[offs++] = txtlen; |
735 | (*pkt)[offs++] = txtlen; |
730 | memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen); |
736 | memcpy (pkt->at (offs), dns->snddq.begin (), txtlen); |
731 | offs += txtlen; |
737 | offs += txtlen; |
732 | c->dns_snddq->remove (txtlen); |
738 | dns->snddq.remove (txtlen); |
733 | |
739 | |
734 | dlen -= txtlen + 1; |
740 | dlen -= txtlen + 1; |
735 | } |
741 | } |
736 | |
742 | |
737 | // avoid empty TXT rdata |
743 | // avoid empty TXT rdata |
… | |
… | |
772 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
778 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
773 | i != dns_sndpq.end (); |
779 | i != dns_sndpq.end (); |
774 | ++i) |
780 | ++i) |
775 | if ((*i)->pkt->id == pkt->id) |
781 | if ((*i)->pkt->id == pkt->id) |
776 | { |
782 | { |
777 | connection *c = (*i)->conn; |
783 | dns_connection *dns = (*i)->dns; |
778 | int seqno = (*i)->seqno; |
784 | int seqno = (*i)->seqno; |
779 | u8 data[MAXSIZE], *datap = data; |
785 | u8 data[MAXSIZE], *datap = data; |
780 | |
786 | |
781 | delete *i; |
787 | delete *i; |
782 | dns_sndpq.erase (i); |
788 | dns_sndpq.erase (i); |
783 | |
789 | |
784 | if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
790 | if (flags & FLAG_RESPONSE && !(flags & (FLAG_OP_MASK | FLAG_TC))) |
785 | { |
791 | { |
786 | char qname[MAXSIZE]; |
792 | char qname[MAXSIZE]; |
787 | |
793 | |
788 | while (pkt->qdcount-- && offs < MAXSIZE - 4) |
794 | while (pkt->qdcount-- && offs < MAXSIZE - 4) |
789 | { |
795 | { |
790 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
796 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
791 | offs += 4; // skip qtype, qclass |
797 | offs += 4; // skip qtype, qclass |
792 | } |
798 | } |
793 | |
799 | |
794 | while (pkt->ancount-- && offs < MAXSIZE - 10) |
800 | while (pkt->ancount-- && offs < MAXSIZE - 10 && datap) |
795 | { |
801 | { |
796 | int qlen = //D |
|
|
797 | pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
802 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
798 | |
|
|
799 | printf ("got reply to <%.*s>\n", qlen, qname);//D |
|
|
800 | |
803 | |
801 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
804 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
802 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
805 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
803 | u32 ttl = (*pkt) [offs++] << 24; |
806 | u32 ttl = (*pkt) [offs++] << 24; |
804 | ttl |= (*pkt) [offs++] << 16; |
807 | ttl |= (*pkt) [offs++] << 16; |
… | |
… | |
820 | memcpy (datap, pkt->at (offs), txtlen); |
823 | memcpy (datap, pkt->at (offs), txtlen); |
821 | datap += txtlen; offs += txtlen; |
824 | datap += txtlen; offs += txtlen; |
822 | |
825 | |
823 | rdlen -= txtlen + 1; |
826 | rdlen -= txtlen + 1; |
824 | } |
827 | } |
825 | |
|
|
826 | } |
828 | } |
827 | |
829 | |
|
|
830 | int client, rseqno; |
|
|
831 | decode_header (qname, client, rseqno); |
|
|
832 | |
|
|
833 | if (client != THISNODE->id) |
|
|
834 | { |
|
|
835 | slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); |
|
|
836 | datap = 0; |
|
|
837 | } |
|
|
838 | else if (rseqno != seqno) |
|
|
839 | { |
|
|
840 | slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?")); |
|
|
841 | datap = 0; |
|
|
842 | } |
828 | } |
843 | } |
829 | } |
844 | } |
830 | |
845 | |
831 | // todo: pkt now used |
846 | // todo: pkt now used |
|
|
847 | if (datap) |
832 | c->dnsv4_receive_rep (new dns_rcv (seqno, data, datap - data)); |
848 | dns->receive_rep (new dns_rcv (seqno, data, datap - data)); |
833 | |
849 | |
834 | break; |
850 | break; |
835 | } |
851 | } |
836 | |
852 | |
837 | delete pkt; |
853 | delete pkt; |
… | |
… | |
868 | } |
884 | } |
869 | |
885 | |
870 | bool |
886 | bool |
871 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
887 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
872 | { |
888 | { |
873 | // never initialized |
889 | if (!dns) |
874 | if (!dns_snddq && !dns_rcvdq) |
890 | dns = new dns_connection (this); |
875 | { |
|
|
876 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
|
|
877 | dns_snddq = new byte_stream (MAX_BACKLOG); |
|
|
878 | |
|
|
879 | //dns_rcvseq = dns_sndseq = 0; |
|
|
880 | |
|
|
881 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
|
|
882 | } |
|
|
883 | |
891 | |
884 | if (!dns_snddq->put (pkt)) |
892 | if (!dns->snddq.put (pkt)) |
885 | return false; |
893 | return false; |
886 | |
894 | |
887 | // start timer if neccessary |
895 | // start timer if neccessary |
888 | if (!THISNODE->dns_port && !dnsv4_tw.active) |
896 | if (!THISNODE->dns_port && !dns->tw.active) |
889 | dnsv4_cb (dnsv4_tw); |
897 | dns->tw.trigger (); |
890 | |
898 | |
891 | return true; |
899 | return true; |
892 | } |
900 | } |
893 | |
901 | |
894 | void |
902 | void |
895 | connection::dnsv4_cb (time_watcher &w) |
903 | dns_connection::time_cb (time_watcher &w) |
896 | { |
904 | { |
897 | // check for timeouts and (re)transmit |
905 | // check for timeouts and (re)transmit |
898 | tstamp next = NOW + 60; |
906 | tstamp next = NOW + 60; |
899 | dns_req *send = 0; |
907 | dns_req *send = 0; |
900 | |
908 | |
… | |
… | |
923 | |
931 | |
924 | if (!send |
932 | if (!send |
925 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
933 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
926 | { |
934 | { |
927 | send = new dns_req (this); |
935 | send = new dns_req (this); |
928 | send->gen_stream_req (dns_sndseq, dns_snddq); |
936 | send->gen_stream_req (sndseq, snddq); |
929 | vpn->dns_sndpq.push_back (send); |
937 | vpn->dns_sndpq.push_back (send); |
930 | |
938 | |
931 | dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK; |
939 | sndseq = (sndseq + 1) & SEQNO_MASK; |
932 | } |
940 | } |
933 | |
941 | |
934 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
942 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
935 | |
943 | |
936 | if (send) |
944 | if (send) |
937 | { |
945 | { |
938 | dns_packet *pkt = send->pkt; |
946 | dns_packet *pkt = send->pkt; |
939 | |
947 | |
940 | next = min_next; |
948 | next = min_next; |
941 | |
949 | |
942 | sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ()); |
950 | sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, |
|
|
951 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
943 | } |
952 | } |
944 | else if (next < min_next) |
953 | else if (next < min_next) |
945 | next = min_next; |
954 | next = min_next; |
946 | |
955 | |
947 | w.start (next); |
956 | w.start (next); |