… | |
… | |
25 | |
25 | |
26 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
26 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
27 | // it's a hack, use only in emergency situations please. |
27 | // it's a hack, use only in emergency situations please. |
28 | |
28 | |
29 | #include <cstring> |
29 | #include <cstring> |
|
|
30 | #include <cassert> |
30 | |
31 | |
31 | #include <sys/types.h> |
32 | #include <sys/types.h> |
32 | #include <sys/socket.h> |
33 | #include <sys/socket.h> |
33 | #include <sys/wait.h> |
34 | #include <sys/wait.h> |
34 | #include <sys/uio.h> |
35 | #include <sys/uio.h> |
… | |
… | |
264 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
265 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
265 | |
266 | |
266 | ///////////////////////////////////////////////////////////////////////////// |
267 | ///////////////////////////////////////////////////////////////////////////// |
267 | |
268 | |
268 | #define HDRSIZE 6 |
269 | #define HDRSIZE 6 |
269 | |
270 | |
270 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
271 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
271 | { |
272 | { |
272 | seqno &= SEQNO_MASK; |
273 | seqno &= SEQNO_MASK; |
273 | |
274 | |
274 | u8 hdr[3] = { |
275 | u8 hdr[3] = { |
… | |
… | |
356 | return true; |
357 | return true; |
357 | } |
358 | } |
358 | |
359 | |
359 | vpn_packet *byte_stream::get () |
360 | vpn_packet *byte_stream::get () |
360 | { |
361 | { |
|
|
362 | unsigned int len; |
|
|
363 | |
|
|
364 | for (;;) |
|
|
365 | { |
361 | unsigned int len = (data [0] << 8) | data [1]; |
366 | len = (data [0] << 8) | data [1]; |
362 | |
367 | |
363 | if (len > MAXSIZE && fill >= 2) |
368 | if (len <= MAXSIZE || fill < 2) |
364 | assert (len <= MAXSIZE || fill < 2); // TODO handle this gracefully, connection reset |
369 | break; |
365 | |
370 | |
|
|
371 | // TODO: handle this better than skipping, e.g. by reset |
|
|
372 | slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte...")); |
|
|
373 | remove (1); |
|
|
374 | } |
|
|
375 | |
366 | if (fill < len + 2) |
376 | if (fill < len + 2) |
367 | return 0; |
377 | return 0; |
368 | |
378 | |
369 | vpn_packet *pkt = new vpn_packet; |
379 | vpn_packet *pkt = new vpn_packet; |
370 | |
380 | |
… | |
… | |
399 | struct dns_cfg |
409 | struct dns_cfg |
400 | { |
410 | { |
401 | static int next_uid; |
411 | static int next_uid; |
402 | |
412 | |
403 | u8 id1, id2, id3, id4; |
413 | u8 id1, id2, id3, id4; |
|
|
414 | |
404 | u8 version; |
415 | u8 version; |
|
|
416 | u8 flags; |
405 | u8 rrtype; |
417 | u8 rrtype; |
406 | u8 flags; |
|
|
407 | u8 def_ttl; |
418 | u8 def_ttl; |
408 | u8 rcv_cdc; |
419 | |
409 | u8 snd_cdc; |
|
|
410 | u16 max_size; |
|
|
411 | u16 client; |
420 | u16 client; |
412 | u16 uid; // to make request unique |
421 | u16 uid; // to make request unique |
413 | |
422 | |
414 | u8 reserved[8]; |
423 | u16 max_size; |
|
|
424 | u8 seq_cdc; |
|
|
425 | u8 req_cdc; |
|
|
426 | |
|
|
427 | u8 rep_cdc; |
|
|
428 | u8 r2, r3, r4; |
|
|
429 | |
|
|
430 | u8 r5, r6, r7, r8; |
415 | |
431 | |
416 | void reset (int clientid); |
432 | void reset (int clientid); |
417 | bool valid (); |
433 | bool valid (); |
418 | }; |
434 | }; |
419 | |
435 | |
… | |
… | |
428 | |
444 | |
429 | version = 1; |
445 | version = 1; |
430 | |
446 | |
431 | rrtype = RR_TYPE_TXT; |
447 | rrtype = RR_TYPE_TXT; |
432 | flags = 0; |
448 | flags = 0; |
433 | def_ttl = 1; |
449 | def_ttl = 0; |
|
|
450 | seq_cdc = 26; |
|
|
451 | req_cdc = 62; |
434 | rcv_cdc = 0; |
452 | rep_cdc = 0; |
435 | snd_cdc = 62; |
|
|
436 | max_size = ntohs (MAX_PKT_SIZE); |
453 | max_size = ntohs (MAX_PKT_SIZE); |
437 | client = ntohs (clientid); |
454 | client = ntohs (clientid); |
438 | uid = next_uid++; |
455 | uid = next_uid++; |
439 | |
456 | |
440 | memset (reserved, 0, 8); |
457 | r2 = r3 = r4 = 0; |
|
|
458 | r4 = r5 = r6 = r7 = 0; |
441 | } |
459 | } |
442 | |
460 | |
443 | bool dns_cfg::valid () |
461 | bool dns_cfg::valid () |
444 | { |
462 | { |
445 | return id1 == 'G' |
463 | return id1 == 'G' |
446 | && id2 == 'V' |
464 | && id2 == 'V' |
447 | && id3 == 'P' |
465 | && id3 == 'P' |
448 | && id4 == 'E' |
466 | && id4 == 'E' |
|
|
467 | && seq_cdc == 26 |
|
|
468 | && req_cdc == 62 |
|
|
469 | && rep_cdc == 0 |
449 | && version == 1 |
470 | && version == 1 |
450 | && flags == 0 |
|
|
451 | && rcv_cdc == 0 |
|
|
452 | && snd_cdc == 62 |
|
|
453 | && max_size == ntohs (MAX_PKT_SIZE); |
471 | && max_size == ntohs (MAX_PKT_SIZE); |
454 | } |
472 | } |
455 | |
473 | |
456 | struct dns_packet : net_packet |
474 | struct dns_packet : net_packet |
457 | { |
475 | { |
458 | u16 id; |
476 | u16 id; |
459 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
477 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
460 | u16 qdcount, ancount, nscount, arcount; |
478 | u16 qdcount, ancount, nscount, arcount; |
461 | |
479 | |
462 | u8 data[MAXSIZE - 6 * 2]; |
480 | u8 data [MAXSIZE - 6 * 2]; |
463 | |
481 | |
464 | int decode_label (char *data, int size, int &offs); |
482 | int decode_label (char *data, int size, int &offs); |
465 | }; |
483 | }; |
466 | |
484 | |
467 | int dns_packet::decode_label (char *data, int size, int &offs) |
485 | int dns_packet::decode_label (char *data, int size, int &offs) |
… | |
… | |
498 | return data - orig; |
516 | return data - orig; |
499 | } |
517 | } |
500 | |
518 | |
501 | ///////////////////////////////////////////////////////////////////////////// |
519 | ///////////////////////////////////////////////////////////////////////////// |
502 | |
520 | |
|
|
521 | static u16 dns_id = 0; // TODO: should be per-vpn |
|
|
522 | |
|
|
523 | static u16 next_id () |
|
|
524 | { |
|
|
525 | if (!dns_id) |
|
|
526 | dns_id = time (0); |
|
|
527 | |
|
|
528 | // the simplest lsfr with periodicity 65535 i could find |
|
|
529 | dns_id = (dns_id << 1) |
|
|
530 | | (((dns_id >> 1) |
|
|
531 | ^ (dns_id >> 2) |
|
|
532 | ^ (dns_id >> 4) |
|
|
533 | ^ (dns_id >> 15)) & 1); |
|
|
534 | |
|
|
535 | return dns_id; |
|
|
536 | } |
|
|
537 | |
|
|
538 | struct dns_rcv; |
|
|
539 | struct dns_snd; |
|
|
540 | |
|
|
541 | struct dns_connection |
|
|
542 | { |
|
|
543 | connection *c; |
|
|
544 | struct vpn *vpn; |
|
|
545 | |
|
|
546 | dns_cfg cfg; |
|
|
547 | |
|
|
548 | bool established; |
|
|
549 | |
|
|
550 | tstamp last_received; |
|
|
551 | tstamp last_sent; |
|
|
552 | double last_latency; |
|
|
553 | double poll_interval, send_interval; |
|
|
554 | |
|
|
555 | vector<dns_rcv *> rcvpq; |
|
|
556 | |
|
|
557 | byte_stream rcvdq; int rcvseq; |
|
|
558 | byte_stream snddq; int sndseq; |
|
|
559 | |
|
|
560 | void time_cb (time_watcher &w); time_watcher tw; |
|
|
561 | void receive_rep (dns_rcv *r); |
|
|
562 | |
|
|
563 | dns_connection (connection *c); |
|
|
564 | ~dns_connection (); |
|
|
565 | }; |
|
|
566 | |
503 | struct dns_snd |
567 | struct dns_snd |
504 | { |
568 | { |
505 | dns_packet *pkt; |
569 | dns_packet *pkt; |
506 | tstamp timeout, sent; |
570 | tstamp timeout, sent; |
507 | int retry; |
571 | int retry; |
508 | struct dns_connection *dns; |
572 | struct dns_connection *dns; |
509 | int seqno; |
573 | int seqno; |
510 | bool stdhdr; |
574 | bool stdhdr; |
511 | |
575 | |
512 | void gen_stream_req (int seqno, byte_stream &stream); |
576 | void gen_stream_req (int seqno, byte_stream &stream); |
513 | void gen_syn_req (const dns_cfg &cfg); |
577 | void gen_syn_req (); |
514 | |
578 | |
515 | dns_snd (dns_connection *dns); |
579 | dns_snd (dns_connection *dns); |
516 | ~dns_snd (); |
580 | ~dns_snd (); |
517 | }; |
581 | }; |
518 | |
|
|
519 | static u16 dns_id = 12098; // TODO: should be per-vpn |
|
|
520 | |
|
|
521 | static u16 next_id () |
|
|
522 | { |
|
|
523 | // the simplest lsfr with periodicity 65535 i could find |
|
|
524 | dns_id = (dns_id << 1) |
|
|
525 | | (((dns_id >> 1) |
|
|
526 | ^ (dns_id >> 2) |
|
|
527 | ^ (dns_id >> 4) |
|
|
528 | ^ (dns_id >> 15)) & 1); |
|
|
529 | |
|
|
530 | return dns_id; |
|
|
531 | } |
|
|
532 | |
582 | |
533 | dns_snd::dns_snd (dns_connection *dns) |
583 | dns_snd::dns_snd (dns_connection *dns) |
534 | : dns (dns) |
584 | : dns (dns) |
535 | { |
585 | { |
536 | timeout = 0; |
586 | timeout = 0; |
… | |
… | |
581 | |
631 | |
582 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
632 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
583 | pkt->qdcount = htons (1); |
633 | pkt->qdcount = htons (1); |
584 | |
634 | |
585 | int offs = 6*2; |
635 | int offs = 6*2; |
586 | int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); |
636 | int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2); |
587 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
637 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
588 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
638 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
589 | |
639 | |
590 | char enc[256], *encp = enc; |
640 | char enc[256], *encp = enc; |
591 | encode_header (enc, THISNODE->id, seqno); |
641 | encode_header (enc, THISNODE->id, seqno); |
… | |
… | |
609 | encp += lbllen; |
659 | encp += lbllen; |
610 | |
660 | |
611 | enclen -= lbllen; |
661 | enclen -= lbllen; |
612 | } |
662 | } |
613 | |
663 | |
614 | append_domain (*pkt, offs, THISNODE->domain); |
664 | append_domain (*pkt, offs, dns->c->conf->domain); |
615 | |
665 | |
616 | (*pkt)[offs++] = 0; |
666 | (*pkt)[offs++] = 0; |
617 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
667 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
618 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
668 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
619 | |
669 | |
620 | pkt->len = offs; |
670 | pkt->len = offs; |
621 | } |
671 | } |
622 | |
672 | |
623 | void dns_snd::gen_syn_req (const dns_cfg &cfg) |
673 | void dns_snd::gen_syn_req () |
624 | { |
674 | { |
625 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
675 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
626 | |
676 | |
627 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
677 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
628 | pkt->qdcount = htons (1); |
678 | pkt->qdcount = htons (1); |
629 | |
679 | |
630 | int offs = 6*2; |
680 | int offs = 6 * 2; |
631 | |
681 | |
632 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg)); |
682 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg)); |
633 | |
683 | |
634 | assert (elen <= MAX_LBL_SIZE); |
684 | assert (elen <= MAX_LBL_SIZE); |
635 | |
685 | |
636 | (*pkt)[offs] = elen; |
686 | (*pkt)[offs] = elen; |
637 | offs += elen + 1; |
687 | offs += elen + 1; |
638 | append_domain (*pkt, offs, THISNODE->domain); |
688 | append_domain (*pkt, offs, dns->c->conf->domain); |
639 | |
689 | |
640 | (*pkt)[offs++] = 0; |
690 | (*pkt)[offs++] = 0; |
641 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
691 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
642 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
692 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
643 | |
693 | |
… | |
… | |
666 | delete pkt; |
716 | delete pkt; |
667 | } |
717 | } |
668 | |
718 | |
669 | ///////////////////////////////////////////////////////////////////////////// |
719 | ///////////////////////////////////////////////////////////////////////////// |
670 | |
720 | |
671 | struct dns_connection |
|
|
672 | { |
|
|
673 | connection *c; |
|
|
674 | struct vpn *vpn; |
|
|
675 | |
|
|
676 | dns_cfg cfg; |
|
|
677 | |
|
|
678 | bool established; |
|
|
679 | |
|
|
680 | tstamp last_received; |
|
|
681 | tstamp last_sent; |
|
|
682 | double last_latency; |
|
|
683 | double poll_interval, send_interval; |
|
|
684 | |
|
|
685 | vector<dns_rcv *> rcvpq; |
|
|
686 | |
|
|
687 | byte_stream rcvdq; int rcvseq; |
|
|
688 | byte_stream snddq; int sndseq; |
|
|
689 | |
|
|
690 | void time_cb (time_watcher &w); time_watcher tw; |
|
|
691 | void receive_rep (dns_rcv *r); |
|
|
692 | |
|
|
693 | dns_connection (connection *c); |
|
|
694 | ~dns_connection (); |
|
|
695 | }; |
|
|
696 | |
|
|
697 | dns_connection::dns_connection (connection *c) |
721 | dns_connection::dns_connection (connection *c) |
698 | : c (c) |
722 | : c (c) |
699 | , rcvdq (MAX_BACKLOG * 2) |
723 | , rcvdq (MAX_BACKLOG * 2) |
700 | , snddq (MAX_BACKLOG * 2) |
724 | , snddq (MAX_BACKLOG * 2) |
701 | , tw (this, &dns_connection::time_cb) |
725 | , tw (this, &dns_connection::time_cb) |
… | |
… | |
765 | } |
789 | } |
766 | |
790 | |
767 | while (vpn_packet *pkt = rcvdq.get ()) |
791 | while (vpn_packet *pkt = rcvdq.get ()) |
768 | { |
792 | { |
769 | sockinfo si; |
793 | sockinfo si; |
770 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
794 | si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4; |
771 | |
795 | |
772 | vpn->recv_vpn_packet (pkt, si); |
796 | vpn->recv_vpn_packet (pkt, si); |
773 | |
797 | |
774 | delete pkt; |
798 | delete pkt; |
775 | } |
799 | } |
… | |
… | |
789 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
813 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
790 | |
814 | |
791 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
815 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
792 | && pkt.qdcount == htons (1)) |
816 | && pkt.qdcount == htons (1)) |
793 | { |
817 | { |
794 | char qname[MAXSIZE]; |
818 | char qname [MAXSIZE]; |
795 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
819 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
796 | |
820 | |
797 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
821 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
798 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
822 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
799 | |
823 | |
… | |
… | |
806 | |
830 | |
807 | int dlen = strlen (THISNODE->domain); |
831 | int dlen = strlen (THISNODE->domain); |
808 | |
832 | |
809 | if (qclass == RR_CLASS_IN |
833 | if (qclass == RR_CLASS_IN |
810 | && qlen > dlen + 1 |
834 | && qlen > dlen + 1 |
811 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
835 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
812 | { |
836 | { |
813 | // now generate reply |
837 | // now generate reply |
814 | pkt.ancount = htons (1); // one answer RR |
838 | pkt.ancount = htons (1); // one answer RR |
815 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
839 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
816 | |
840 | |
… | |
… | |
857 | // new packet, queue |
881 | // new packet, queue |
858 | rcv = new dns_rcv (seqno, data, datalen); |
882 | rcv = new dns_rcv (seqno, data, datalen); |
859 | dns->receive_rep (rcv); |
883 | dns->receive_rep (rcv); |
860 | } |
884 | } |
861 | |
885 | |
|
|
886 | { |
862 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
887 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
863 | |
888 | |
864 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
889 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
865 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
890 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
866 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
891 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
867 | pkt [offs++] = 0; pkt [offs++] = 0; |
892 | pkt [offs++] = 0; pkt [offs++] = 0; |
868 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
893 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
869 | |
894 | |
870 | int rdlen_offs = offs += 2; |
895 | int rdlen_offs = offs += 2; |
871 | |
896 | |
872 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
897 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
873 | // bind doesn't compress well, so reduce further by one label length |
898 | // bind doesn't compress well, so reduce further by one label length |
874 | dlen -= qlen; |
899 | dlen -= qlen; |
875 | |
900 | |
876 | if (dns) |
901 | if (dns) |
877 | { |
902 | { |
878 | // only put data into in-order sequence packets, if |
903 | // only put data into in-order sequence packets, if |
879 | // we receive out-of-order packets we generate empty |
904 | // we receive out-of-order packets we generate empty |
880 | // replies |
905 | // replies |
881 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
906 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
882 | { |
907 | { |
883 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
908 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
884 | |
909 | |
885 | if (txtlen > dns->snddq.size ()) |
910 | if (txtlen > dns->snddq.size ()) |
886 | txtlen = dns->snddq.size (); |
911 | txtlen = dns->snddq.size (); |
887 | |
912 | |
888 | pkt[offs++] = txtlen; |
913 | pkt[offs++] = txtlen; |
889 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
914 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
890 | offs += txtlen; |
915 | offs += txtlen; |
891 | dns->snddq.remove (txtlen); |
916 | dns->snddq.remove (txtlen); |
892 | |
917 | |
893 | dlen -= txtlen + 1; |
918 | dlen -= txtlen + 1; |
894 | } |
919 | } |
895 | |
920 | |
896 | // avoid empty TXT rdata |
921 | // avoid empty TXT rdata |
897 | if (offs == rdlen_offs) |
922 | if (offs == rdlen_offs) |
898 | pkt[offs++] = 0; |
923 | pkt[offs++] = 0; |
899 | |
924 | |
900 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
925 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
901 | } |
926 | } |
902 | else |
927 | else |
903 | { |
928 | { |
904 | // send RST |
929 | // send RST |
905 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
930 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
906 | pkt [offs++] = CMD_IP_RST; |
931 | pkt [offs++] = CMD_IP_RST; |
907 | } |
932 | } |
908 | |
933 | |
909 | int rdlen = offs - rdlen_offs; |
934 | int rdlen = offs - rdlen_offs; |
910 | |
935 | |
911 | pkt [rdlen_offs - 2] = rdlen >> 8; |
936 | pkt [rdlen_offs - 2] = rdlen >> 8; |
912 | pkt [rdlen_offs - 1] = rdlen; |
937 | pkt [rdlen_offs - 1] = rdlen; |
913 | |
938 | |
914 | if (dns) |
939 | if (dns) |
915 | { |
940 | { |
916 | // now update dns_rcv copy |
941 | // now update dns_rcv copy |
917 | rcv->pkt->len = offs; |
942 | rcv->pkt->len = offs; |
918 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
943 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
919 | } |
944 | } |
|
|
945 | } |
920 | |
946 | |
921 | duplicate_request: ; |
947 | duplicate_request: ; |
922 | } |
948 | } |
923 | else |
949 | else |
924 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
950 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
… | |
… | |
936 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
962 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
937 | pkt [offs++] = 0; pkt [offs++] = 0; |
963 | pkt [offs++] = 0; pkt [offs++] = 0; |
938 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
964 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
939 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
965 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
940 | |
966 | |
941 | slog (L_INFO, _("DNS: client %d tries to connect"), client); |
967 | slog (L_INFO, _("DNS: client %d connects"), client); |
942 | |
968 | |
943 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
969 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
944 | pkt [offs++] = CMD_IP_REJ; |
970 | pkt [offs++] = CMD_IP_REJ; |
945 | |
971 | |
946 | if (0 < client && client <= conns.size ()) |
972 | if (0 < client && client <= conns.size ()) |
… | |
… | |
1128 | |
1154 | |
1129 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1155 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1130 | |
1156 | |
1131 | if (pkt->len > 0) |
1157 | if (pkt->len > 0) |
1132 | { |
1158 | { |
1133 | if (THISNODE->dns_port) |
1159 | if (ntohs (pkt->flags) & FLAG_RESPONSE) |
|
|
1160 | dnsv4_client (*pkt); |
|
|
1161 | else |
1134 | { |
1162 | { |
1135 | dnsv4_server (*pkt); |
1163 | dnsv4_server (*pkt); |
1136 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1164 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1137 | } |
1165 | } |
1138 | else |
|
|
1139 | dnsv4_client (*pkt); |
|
|
1140 | |
1166 | |
1141 | delete pkt; |
1167 | delete pkt; |
1142 | } |
1168 | } |
1143 | } |
1169 | } |
1144 | } |
1170 | } |
1145 | |
1171 | |
1146 | bool |
1172 | bool |
1147 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1173 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1148 | { |
1174 | { |
|
|
1175 | int client = ntohs (si.port); |
|
|
1176 | |
|
|
1177 | assert (0 < client && client <= conns.size ()); |
|
|
1178 | |
|
|
1179 | connection *c = conns [client - 1]; |
|
|
1180 | |
1149 | if (!dns) |
1181 | if (!c->dns) |
1150 | dns = new dns_connection (this); |
1182 | c->dns = new dns_connection (c); |
1151 | |
1183 | |
1152 | if (!dns->snddq.put (pkt)) |
1184 | if (!c->dns->snddq.put (pkt)) |
1153 | return false; |
1185 | return false; |
1154 | |
1186 | |
1155 | dns->tw.trigger (); |
1187 | c->dns->tw.trigger (); |
1156 | |
1188 | |
1157 | return true; |
1189 | return true; |
1158 | } |
1190 | } |
1159 | |
1191 | |
1160 | void |
1192 | void |
… | |
… | |
1215 | if (vpn->dns_sndpq.empty ()) |
1247 | if (vpn->dns_sndpq.empty ()) |
1216 | { |
1248 | { |
1217 | send = new dns_snd (this); |
1249 | send = new dns_snd (this); |
1218 | |
1250 | |
1219 | cfg.reset (THISNODE->id); |
1251 | cfg.reset (THISNODE->id); |
1220 | send->gen_syn_req (cfg); |
1252 | send->gen_syn_req (); |
1221 | } |
1253 | } |
1222 | } |
1254 | } |
1223 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1255 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1224 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1256 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1225 | { |
1257 | { |
|
|
1258 | //printf ("sending data request etc.\n"); //D |
1226 | if (!snddq.empty ()) |
1259 | if (!snddq.empty ()) |
1227 | { |
1260 | { |
1228 | poll_interval = send_interval; |
1261 | poll_interval = send_interval; |
1229 | NEXT (NOW + send_interval); |
1262 | NEXT (NOW + send_interval); |
1230 | } |
1263 | } |