… | |
… | |
43 | |
43 | |
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_POLL_INTERVAL .2 // how often to poll minimally when the server is having data |
49 | #define MAX_RETRY 6. |
49 | #define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data |
|
|
50 | #define ACTIVITY_INTERVAL 5. |
|
|
51 | |
|
|
52 | #define INITIAL_TIMEOUT 1. |
|
|
53 | #define INITIAL_SYN_TIMEOUT 2. |
|
|
54 | |
|
|
55 | #define MIN_SEND_INTERVAL (1./1000.) |
|
|
56 | #define MAX_SEND_INTERVAL 0.5 // optimistic? |
50 | |
57 | |
51 | #define MAX_OUTSTANDING 400 // max. outstanding requests |
58 | #define MAX_OUTSTANDING 400 // max. outstanding requests |
52 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING |
59 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING |
53 | #define MAX_RATE 100 // requests/s |
60 | #define MAX_RATE 100 // requests/s |
54 | #define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE |
61 | #define MAX_BACKLOG (100*1024) // size of protocol backlog, must be > MAXSIZE |
55 | |
62 | |
56 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
63 | #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 |
64 | // 240 leaves about 4 bytes of server reply data |
58 | // every two request byte sless give room for one reply byte |
65 | // every two request byte sless give room for one reply byte |
59 | |
66 | |
… | |
… | |
61 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
68 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
62 | |
69 | |
63 | #define MAX_LBL_SIZE 63 |
70 | #define MAX_LBL_SIZE 63 |
64 | #define MAX_PKT_SIZE 512 |
71 | #define MAX_PKT_SIZE 512 |
65 | |
72 | |
|
|
73 | #define RR_TYPE_A 1 |
|
|
74 | #define RR_TYPE_NULL 10 |
66 | #define RR_TYPE_TXT 16 |
75 | #define RR_TYPE_TXT 16 |
67 | #define RR_TYPE_ANY 255 |
76 | #define RR_TYPE_ANY 255 |
|
|
77 | |
68 | #define RR_CLASS_IN 1 |
78 | #define RR_CLASS_IN 1 |
|
|
79 | |
|
|
80 | #define CMD_IP_1 207 |
|
|
81 | #define CMD_IP_2 46 |
|
|
82 | #define CMD_IP_3 236 |
|
|
83 | #define CMD_IP_RST 29 |
|
|
84 | #define CMD_IP_SYN 113 |
|
|
85 | #define CMD_IP_REJ 32 |
69 | |
86 | |
70 | // works for cmaps up to 255 (not 256!) |
87 | // works for cmaps up to 255 (not 256!) |
71 | struct charmap |
88 | struct charmap |
72 | { |
89 | { |
73 | enum { INVALID = (u8)255 }; |
90 | enum { INVALID = (u8)255 }; |
… | |
… | |
153 | return dec_len [len]; |
170 | return dec_len [len]; |
154 | } |
171 | } |
155 | |
172 | |
156 | unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
173 | unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
157 | { |
174 | { |
158 | if (!len) |
175 | if (!len || len > MAX_DEC_LEN) |
159 | return 0; |
176 | return 0; |
160 | |
177 | |
161 | int elen = encode_len (len); |
178 | int elen = encode_len (len); |
162 | |
179 | |
163 | mp_limb_t m [MAX_LIMBS]; |
180 | mp_limb_t m [MAX_LIMBS]; |
… | |
… | |
182 | return elen; |
199 | return elen; |
183 | } |
200 | } |
184 | |
201 | |
185 | unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
202 | unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
186 | { |
203 | { |
187 | if (!len) |
204 | if (!len || len > MAX_ENC_LEN) |
188 | return 0; |
205 | return 0; |
189 | |
206 | |
190 | u8 src_ [MAX_ENC_LEN]; |
207 | u8 src_ [MAX_ENC_LEN]; |
191 | unsigned int elen = 0; |
208 | unsigned int elen = 0; |
192 | |
209 | |
… | |
… | |
239 | } |
256 | } |
240 | abort (); |
257 | abort (); |
241 | } |
258 | } |
242 | #endif |
259 | #endif |
243 | |
260 | |
244 | // the following sequence has been crafted to |
261 | //static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-"); |
245 | // a) look somewhat random |
|
|
246 | // b) the even (and odd) indices never share the same character as upper/lowercase |
|
|
247 | // the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) |
|
|
248 | // the other sequences are obviously derived |
|
|
249 | //static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
262 | //static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
250 | static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
263 | static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
251 | //static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet |
264 | //static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet |
252 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
265 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
253 | |
266 | |
… | |
… | |
331 | return false; |
344 | return false; |
332 | |
345 | |
333 | data [fill++] = pkt->len >> 8; |
346 | data [fill++] = pkt->len >> 8; |
334 | data [fill++] = pkt->len; |
347 | data [fill++] = pkt->len; |
335 | |
348 | |
336 | memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len; |
349 | memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len; |
337 | |
350 | |
338 | return true; |
351 | return true; |
339 | } |
352 | } |
340 | |
353 | |
341 | vpn_packet *byte_stream::get () |
354 | vpn_packet *byte_stream::get () |
… | |
… | |
349 | return 0; |
362 | return 0; |
350 | |
363 | |
351 | vpn_packet *pkt = new vpn_packet; |
364 | vpn_packet *pkt = new vpn_packet; |
352 | |
365 | |
353 | pkt->len = len; |
366 | pkt->len = len; |
354 | memcpy (&((*pkt)[0]), data + 2, len); |
367 | memcpy (pkt->at (0), data + 2, len); |
355 | remove (len + 2); |
368 | remove (len + 2); |
356 | |
369 | |
357 | return pkt; |
370 | return pkt; |
358 | } |
371 | } |
359 | |
372 | |
… | |
… | |
376 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
389 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
377 | |
390 | |
378 | #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
391 | #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
379 | #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
392 | #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
380 | |
393 | |
|
|
394 | struct dns_cfg |
|
|
395 | { |
|
|
396 | static int next_uid; |
|
|
397 | |
|
|
398 | u8 id1, id2, id3, id4; |
|
|
399 | u8 version; |
|
|
400 | u8 rrtype; |
|
|
401 | u8 flags; |
|
|
402 | u8 def_ttl; |
|
|
403 | u8 rcv_cdc; |
|
|
404 | u8 snd_cdc; |
|
|
405 | u16 max_size; |
|
|
406 | u16 client; |
|
|
407 | u16 uid; // to make request unique |
|
|
408 | |
|
|
409 | u8 reserved[8]; |
|
|
410 | |
|
|
411 | void reset (int clientid); |
|
|
412 | bool valid (); |
|
|
413 | }; |
|
|
414 | |
|
|
415 | int dns_cfg::next_uid; |
|
|
416 | |
|
|
417 | void dns_cfg::reset (int clientid) |
|
|
418 | { |
|
|
419 | id1 = 'G'; |
|
|
420 | id2 = 'V'; |
|
|
421 | id3 = 'P'; |
|
|
422 | id4 = 'E'; |
|
|
423 | |
|
|
424 | version = 1; |
|
|
425 | |
|
|
426 | rrtype = RR_TYPE_TXT; |
|
|
427 | flags = 0; |
|
|
428 | def_ttl = 0; |
|
|
429 | rcv_cdc = 0; |
|
|
430 | snd_cdc = 62; |
|
|
431 | max_size = ntohs (MAX_PKT_SIZE); |
|
|
432 | client = ntohs (clientid); |
|
|
433 | uid = next_uid++; |
|
|
434 | |
|
|
435 | memset (reserved, 0, 8); |
|
|
436 | } |
|
|
437 | |
|
|
438 | bool dns_cfg::valid () |
|
|
439 | { |
|
|
440 | return id1 == 'G' |
|
|
441 | && id2 == 'V' |
|
|
442 | && id3 == 'P' |
|
|
443 | && id4 == 'E' |
|
|
444 | && version == 1 |
|
|
445 | && flags == 0 |
|
|
446 | && rcv_cdc == 0 |
|
|
447 | && snd_cdc == 62 |
|
|
448 | && max_size == ntohs (MAX_PKT_SIZE); |
|
|
449 | } |
|
|
450 | |
381 | struct dns_packet : net_packet |
451 | struct dns_packet : net_packet |
382 | { |
452 | { |
383 | u16 id; |
453 | u16 id; |
384 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
454 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
385 | u16 qdcount, ancount, nscount, arcount; |
455 | u16 qdcount, ancount, nscount, arcount; |
… | |
… | |
423 | return data - orig; |
493 | return data - orig; |
424 | } |
494 | } |
425 | |
495 | |
426 | ///////////////////////////////////////////////////////////////////////////// |
496 | ///////////////////////////////////////////////////////////////////////////// |
427 | |
497 | |
428 | struct dns_req |
498 | struct dns_snd |
429 | { |
499 | { |
430 | dns_packet *pkt; |
500 | dns_packet *pkt; |
431 | tstamp next; |
501 | tstamp timeout, sent; |
432 | int retry; |
502 | int retry; |
433 | struct dns_connection *dns; |
503 | struct dns_connection *dns; |
434 | int seqno; |
504 | int seqno; |
435 | |
505 | |
436 | dns_req (dns_connection *dns); |
506 | dns_snd (dns_connection *dns); |
437 | void gen_stream_req (int seqno, byte_stream &stream); |
507 | void gen_stream_req (int seqno, byte_stream &stream); |
|
|
508 | void gen_syn_req (const dns_cfg &cfg); |
438 | }; |
509 | }; |
439 | |
510 | |
440 | static u16 dns_id = 12098; // TODO: should be per-vpn |
511 | static u16 dns_id = 12098; // TODO: should be per-vpn |
441 | |
512 | |
442 | static u16 next_id () |
513 | static u16 next_id () |
… | |
… | |
449 | ^ (dns_id >> 15)) & 1); |
520 | ^ (dns_id >> 15)) & 1); |
450 | |
521 | |
451 | return dns_id; |
522 | return dns_id; |
452 | } |
523 | } |
453 | |
524 | |
454 | dns_req::dns_req (dns_connection *dns) |
525 | dns_snd::dns_snd (dns_connection *dns) |
455 | : dns (dns) |
526 | : dns (dns) |
456 | { |
527 | { |
457 | next = 0; |
528 | timeout = 0; |
458 | retry = 0; |
529 | retry = 0; |
|
|
530 | seqno = 0; |
459 | |
531 | |
460 | pkt = new dns_packet; |
532 | pkt = new dns_packet; |
461 | |
533 | |
462 | pkt->id = next_id (); |
534 | pkt->id = next_id (); |
463 | } |
535 | } |
464 | |
536 | |
|
|
537 | static void append_domain (dns_packet &pkt, int &offs, const char *domain) |
|
|
538 | { |
|
|
539 | // add tunnel domain |
|
|
540 | for (;;) |
|
|
541 | { |
|
|
542 | const char *end = strchr (domain, '.'); |
|
|
543 | |
|
|
544 | if (!end) |
|
|
545 | end = domain + strlen (domain); |
|
|
546 | |
|
|
547 | int len = end - domain; |
|
|
548 | |
|
|
549 | pkt [offs++] = len; |
|
|
550 | memcpy (pkt.at (offs), domain, len); |
|
|
551 | offs += len; |
|
|
552 | |
|
|
553 | if (!*end) |
|
|
554 | break; |
|
|
555 | |
|
|
556 | domain = end + 1; |
|
|
557 | } |
|
|
558 | } |
|
|
559 | |
465 | void dns_req::gen_stream_req (int seqno, byte_stream &stream) |
560 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
466 | { |
561 | { |
467 | this->seqno = seqno; |
562 | this->seqno = seqno; |
|
|
563 | |
|
|
564 | timeout = NOW + INITIAL_TIMEOUT; |
468 | |
565 | |
469 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
566 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
470 | pkt->qdcount = htons (1); |
567 | pkt->qdcount = htons (1); |
471 | |
568 | |
472 | int offs = 6*2; |
569 | int offs = 6*2; |
… | |
… | |
496 | encp += lbllen; |
593 | encp += lbllen; |
497 | |
594 | |
498 | enclen -= lbllen; |
595 | enclen -= lbllen; |
499 | } |
596 | } |
500 | |
597 | |
501 | const char *suffix = THISNODE->domain; |
598 | append_domain (*pkt, offs, THISNODE->domain); |
502 | |
|
|
503 | // add tunnel domain |
|
|
504 | for (;;) |
|
|
505 | { |
|
|
506 | const char *end = strchr (suffix, '.'); |
|
|
507 | |
|
|
508 | if (!end) |
|
|
509 | end = suffix + strlen (suffix); |
|
|
510 | |
|
|
511 | int len = end - suffix; |
|
|
512 | |
|
|
513 | (*pkt)[offs++] = len; |
|
|
514 | memcpy (&((*pkt)[offs]), suffix, len); |
|
|
515 | offs += len; |
|
|
516 | |
|
|
517 | if (!*end) |
|
|
518 | break; |
|
|
519 | |
|
|
520 | suffix = end + 1; |
|
|
521 | } |
|
|
522 | |
599 | |
523 | (*pkt)[offs++] = 0; |
600 | (*pkt)[offs++] = 0; |
524 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
601 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
|
|
602 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
|
|
603 | |
|
|
604 | pkt->len = offs; |
|
|
605 | } |
|
|
606 | |
|
|
607 | void dns_snd::gen_syn_req (const dns_cfg &cfg) |
|
|
608 | { |
|
|
609 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
|
|
610 | |
|
|
611 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
|
|
612 | pkt->qdcount = htons (1); |
|
|
613 | |
|
|
614 | int offs = 6*2; |
|
|
615 | |
|
|
616 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg)); |
|
|
617 | |
|
|
618 | assert (elen <= MAX_LBL_SIZE); |
|
|
619 | |
|
|
620 | (*pkt)[offs] = elen; |
|
|
621 | offs += elen + 1; |
|
|
622 | append_domain (*pkt, offs, THISNODE->domain); |
|
|
623 | |
|
|
624 | (*pkt)[offs++] = 0; |
|
|
625 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
525 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
626 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
526 | |
627 | |
527 | pkt->len = offs; |
628 | pkt->len = offs; |
528 | } |
629 | } |
529 | |
630 | |
… | |
… | |
554 | struct dns_connection |
655 | struct dns_connection |
555 | { |
656 | { |
556 | connection *c; |
657 | connection *c; |
557 | struct vpn *vpn; |
658 | struct vpn *vpn; |
558 | |
659 | |
|
|
660 | dns_cfg cfg; |
|
|
661 | |
|
|
662 | bool established; |
|
|
663 | |
|
|
664 | tstamp last_received; |
|
|
665 | tstamp last_sent; |
|
|
666 | double poll_interval, send_interval; |
|
|
667 | |
559 | vector<dns_rcv *> rcvpq; |
668 | vector<dns_rcv *> rcvpq; |
560 | |
669 | |
561 | int rcvseq; |
|
|
562 | int sndseq; |
|
|
563 | |
|
|
564 | byte_stream rcvdq; |
670 | byte_stream rcvdq; int rcvseq; |
565 | byte_stream snddq; |
671 | byte_stream snddq; int sndseq; |
566 | |
672 | |
567 | void time_cb (time_watcher &w); time_watcher tw; |
673 | void time_cb (time_watcher &w); time_watcher tw; |
568 | void receive_rep (dns_rcv *r); |
674 | void receive_rep (dns_rcv *r); |
569 | |
675 | |
570 | dns_connection (connection *c); |
676 | dns_connection (connection *c); |
… | |
… | |
577 | , snddq (MAX_BACKLOG * 2) |
683 | , snddq (MAX_BACKLOG * 2) |
578 | , tw (this, &dns_connection::time_cb) |
684 | , tw (this, &dns_connection::time_cb) |
579 | { |
685 | { |
580 | vpn = c->vpn; |
686 | vpn = c->vpn; |
581 | |
687 | |
|
|
688 | established = false; |
|
|
689 | |
582 | rcvseq = sndseq = 0; |
690 | rcvseq = sndseq = 0; |
|
|
691 | |
|
|
692 | last_sent = last_received = 0; |
|
|
693 | poll_interval = MIN_POLL_INTERVAL; |
|
|
694 | send_interval = 0.2; // starting rate |
583 | } |
695 | } |
584 | |
696 | |
585 | dns_connection::~dns_connection () |
697 | dns_connection::~dns_connection () |
586 | { |
698 | { |
587 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
699 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
588 | i != rcvpq.end (); |
700 | i != rcvpq.end (); |
589 | ++i) |
701 | ++i) |
590 | delete *i; |
702 | delete *i; |
591 | } |
703 | } |
592 | |
704 | |
593 | struct dns_cfg |
|
|
594 | { |
|
|
595 | u8 id1, id2, id3; |
|
|
596 | u8 def_ttl; |
|
|
597 | u8 unused1; |
|
|
598 | u16 max_size; |
|
|
599 | u8 flags1, flags2; |
|
|
600 | }; |
|
|
601 | |
|
|
602 | void dns_connection::receive_rep (dns_rcv *r) |
705 | void dns_connection::receive_rep (dns_rcv *r) |
603 | { |
706 | { |
|
|
707 | if (r->datalen) |
|
|
708 | { |
|
|
709 | last_received = NOW; |
|
|
710 | tw.trigger (); |
|
|
711 | |
|
|
712 | poll_interval *= 0.99; |
|
|
713 | if (poll_interval > MIN_POLL_INTERVAL) |
|
|
714 | poll_interval = MIN_POLL_INTERVAL; |
|
|
715 | } |
|
|
716 | else |
|
|
717 | { |
|
|
718 | poll_interval *= 1.1; |
|
|
719 | if (poll_interval > MAX_POLL_INTERVAL) |
|
|
720 | poll_interval = MAX_POLL_INTERVAL; |
|
|
721 | } |
|
|
722 | |
604 | rcvpq.push_back (r); |
723 | rcvpq.push_back (r); |
605 | |
724 | |
606 | redo: |
725 | redo: |
607 | |
726 | |
608 | // find next packet |
727 | // find next packet |
… | |
… | |
637 | // check for further packets |
756 | // check for further packets |
638 | goto redo; |
757 | goto redo; |
639 | } |
758 | } |
640 | } |
759 | } |
641 | |
760 | |
642 | dns_packet * |
761 | void |
643 | vpn::dnsv4_server (dns_packet *pkt) |
762 | vpn::dnsv4_server (dns_packet &pkt) |
644 | { |
763 | { |
645 | u16 flags = ntohs (pkt->flags); |
764 | u16 flags = ntohs (pkt.flags); |
646 | |
765 | |
647 | //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
|
|
648 | int offs = 6 * 2; // skip header |
766 | int offs = 6 * 2; // skip header |
649 | |
767 | |
650 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
768 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
651 | |
769 | |
652 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
770 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
653 | && pkt->qdcount == htons (1)) |
771 | && pkt.qdcount == htons (1)) |
654 | { |
772 | { |
655 | char qname[MAXSIZE]; |
773 | char qname[MAXSIZE]; |
656 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
774 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
657 | |
775 | |
658 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
776 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
659 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
777 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
660 | |
778 | |
661 | pkt->qdcount = htons (1); |
779 | pkt.qdcount = htons (1); |
662 | pkt->ancount = 0; |
780 | pkt.ancount = 0; |
663 | pkt->nscount = 0; // should be self, as other nameservers reply like this |
781 | pkt.nscount = 0; // should be self, as other nameservers reply like this |
664 | pkt->arcount = 0; // a record for self, as other nameservers reply like this |
782 | pkt.arcount = 0; // a record for self, as other nameservers reply like this |
665 | |
783 | |
666 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
784 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
667 | |
785 | |
668 | int dlen = strlen (THISNODE->domain); |
786 | int dlen = strlen (THISNODE->domain); |
669 | |
787 | |
670 | if (qclass == RR_CLASS_IN |
788 | if (qclass == RR_CLASS_IN |
671 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
|
|
672 | && qlen > dlen + 1 + HDRSIZE |
789 | && qlen > dlen + 1 |
673 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
790 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
674 | { |
791 | { |
675 | // correct class, domain: parse |
792 | // now generate reply |
676 | int client, seqno; |
793 | pkt.ancount = htons (1); // one answer RR |
677 | decode_header (qname, client, seqno); |
794 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
678 | |
795 | |
679 | u8 data[MAXSIZE]; |
796 | if ((qtype == RR_TYPE_ANY |
680 | int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
797 | || qtype == RR_TYPE_TXT |
681 | |
798 | || qtype == RR_TYPE_NULL) |
682 | if (0 < client && client <= conns.size ()) |
799 | && qlen > dlen + 1 + HDRSIZE) |
683 | { |
800 | { |
684 | connection *c = conns [client - 1]; |
801 | // correct class, domain: parse |
|
|
802 | int client, seqno; |
|
|
803 | decode_header (qname, client, seqno); |
685 | |
804 | |
686 | if (!c->dns) |
805 | u8 data[MAXSIZE]; |
687 | c->dns = new dns_connection (c); |
806 | int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
688 | |
807 | |
689 | dns_connection *dns = c->dns; |
808 | if (0 < client && client <= conns.size ()) |
690 | |
|
|
691 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
|
|
692 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
|
|
693 | { |
|
|
694 | // already seen that request: simply reply with the cached reply |
|
|
695 | dns_rcv *r = *i; |
|
|
696 | |
|
|
697 | printf ("DUPLICATE %d\n", htons (r->pkt->id));//D |
|
|
698 | |
|
|
699 | memcpy (pkt->at (0), r->pkt->at (0), offs = r->pkt->len); |
|
|
700 | pkt->id = r->pkt->id; |
|
|
701 | goto duplicate_request; |
|
|
702 | } |
|
|
703 | |
|
|
704 | // new packet, queue |
|
|
705 | dns_rcv *rcv = new dns_rcv (seqno, data, datalen); |
|
|
706 | dns->receive_rep (rcv); |
|
|
707 | |
|
|
708 | // now generate reply |
|
|
709 | pkt->ancount = htons (1); // one answer RR |
|
|
710 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
|
|
711 | |
|
|
712 | (*pkt) [offs++] = 0xc0; |
|
|
713 | (*pkt) [offs++] = 6 * 2; // same as in query section |
|
|
714 | |
|
|
715 | (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; |
|
|
716 | (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; |
|
|
717 | |
|
|
718 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; |
|
|
719 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL |
|
|
720 | |
|
|
721 | int dlen = MAX_PKT_SIZE - offs - 2; |
|
|
722 | |
|
|
723 | // bind doesn't compress well, so reduce further by one label length |
|
|
724 | dlen -= qlen; |
|
|
725 | |
|
|
726 | int rdlen_offs = offs += 2; |
|
|
727 | |
|
|
728 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
729 | { |
809 | { |
|
|
810 | connection *c = conns [client - 1]; |
|
|
811 | dns_connection *dns = c->dns; |
|
|
812 | dns_rcv *rcv; |
|
|
813 | |
|
|
814 | if (dns) |
|
|
815 | { |
|
|
816 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
|
|
817 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
|
|
818 | { |
|
|
819 | // already seen that request: simply reply with the cached reply |
|
|
820 | dns_rcv *r = *i; |
|
|
821 | |
|
|
822 | printf ("DUPLICATE %d\n", htons (r->pkt->id));//D |
|
|
823 | |
|
|
824 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
|
|
825 | pkt.id = r->pkt->id; |
|
|
826 | goto duplicate_request; |
|
|
827 | } |
|
|
828 | |
|
|
829 | // new packet, queue |
|
|
830 | rcv = new dns_rcv (seqno, data, datalen); |
|
|
831 | dns->receive_rep (rcv); |
|
|
832 | } |
|
|
833 | |
|
|
834 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
|
|
835 | |
|
|
836 | // type |
|
|
837 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
|
|
838 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; |
|
|
839 | |
|
|
840 | // class |
|
|
841 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; |
|
|
842 | |
|
|
843 | // TTL |
|
|
844 | pkt [offs++] = 0; pkt [offs++] = 0; |
|
|
845 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; |
|
|
846 | |
|
|
847 | int rdlen_offs = offs += 2; |
|
|
848 | |
|
|
849 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
|
|
850 | // bind doesn't compress well, so reduce further by one label length |
|
|
851 | dlen -= qlen; |
|
|
852 | |
|
|
853 | if (dns) |
|
|
854 | { |
|
|
855 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
856 | { |
730 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
857 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
731 | |
858 | |
732 | if (txtlen > dns->snddq.size ()) |
859 | if (txtlen > dns->snddq.size ()) |
733 | txtlen = dns->snddq.size (); |
860 | txtlen = dns->snddq.size (); |
734 | |
861 | |
735 | (*pkt)[offs++] = txtlen; |
862 | pkt[offs++] = txtlen; |
736 | memcpy (pkt->at (offs), dns->snddq.begin (), txtlen); |
863 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
737 | offs += txtlen; |
864 | offs += txtlen; |
738 | dns->snddq.remove (txtlen); |
865 | dns->snddq.remove (txtlen); |
739 | |
866 | |
740 | dlen -= txtlen + 1; |
867 | dlen -= txtlen + 1; |
|
|
868 | } |
|
|
869 | |
|
|
870 | // avoid empty TXT rdata |
|
|
871 | if (offs == rdlen_offs) |
|
|
872 | pkt[offs++] = 0; |
|
|
873 | } |
|
|
874 | else |
|
|
875 | { |
|
|
876 | // send RST |
|
|
877 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
|
|
878 | pkt [offs++] = CMD_IP_RST; |
|
|
879 | } |
|
|
880 | |
|
|
881 | int rdlen = offs - rdlen_offs; |
|
|
882 | |
|
|
883 | pkt [rdlen_offs - 2] = rdlen >> 8; |
|
|
884 | pkt [rdlen_offs - 1] = rdlen; |
|
|
885 | |
|
|
886 | if (dns) |
|
|
887 | { |
|
|
888 | // now update dns_rcv copy |
|
|
889 | rcv->pkt->len = offs; |
|
|
890 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
|
|
891 | } |
|
|
892 | |
|
|
893 | duplicate_request: ; |
741 | } |
894 | } |
742 | |
895 | else |
743 | // avoid empty TXT rdata |
896 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
744 | if (offs == rdlen_offs) |
|
|
745 | (*pkt)[offs++] = 0; |
|
|
746 | |
|
|
747 | int rdlen = offs - rdlen_offs; |
|
|
748 | |
|
|
749 | (*pkt) [rdlen_offs - 2] = rdlen >> 8; |
|
|
750 | (*pkt) [rdlen_offs - 1] = rdlen; |
|
|
751 | |
|
|
752 | // now update dns_rcv copy |
|
|
753 | rcv->pkt->len = offs; |
|
|
754 | memcpy (rcv->pkt->at (0), pkt->at (0), offs); |
|
|
755 | |
|
|
756 | duplicate_request: ; |
|
|
757 | } |
897 | } |
758 | else |
898 | else if (qtype == RR_TYPE_A |
759 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
899 | && qlen > dlen + 1 + cdc26.encode_len (sizeof (dns_cfg))) |
|
|
900 | { |
|
|
901 | dns_cfg cfg; |
|
|
902 | cdc26.decode ((u8 *)&cfg, qname, cdc26.encode_len (sizeof (dns_cfg))); |
|
|
903 | int client = ntohs (cfg.client); |
|
|
904 | |
|
|
905 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
|
|
906 | |
|
|
907 | pkt [offs++] = RR_TYPE_A >> 8; pkt [offs++] = RR_TYPE_A; // type |
|
|
908 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
|
|
909 | pkt [offs++] = 0; pkt [offs++] = 0; |
|
|
910 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
|
|
911 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
|
|
912 | |
|
|
913 | slog (L_INFO, _("DNS tunnel: client %d tries to connect"), client); |
|
|
914 | |
|
|
915 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
|
|
916 | pkt [offs++] = CMD_IP_REJ; |
|
|
917 | |
|
|
918 | if (0 < client && client <= conns.size ()) |
|
|
919 | { |
|
|
920 | connection *c = conns [client - 1]; |
|
|
921 | |
|
|
922 | if (cfg.valid ()) |
|
|
923 | { |
|
|
924 | pkt [offs - 1] = CMD_IP_SYN; |
|
|
925 | |
|
|
926 | delete c->dns; |
|
|
927 | c->dns = new dns_connection (c); |
|
|
928 | c->dns->cfg = cfg; |
|
|
929 | } |
|
|
930 | } |
|
|
931 | } |
760 | } |
932 | } |
761 | |
933 | |
762 | pkt->len = offs; |
934 | pkt.len = offs; |
763 | } |
935 | } |
764 | |
|
|
765 | return pkt; |
|
|
766 | } |
936 | } |
767 | |
937 | |
768 | void |
938 | void |
769 | vpn::dnsv4_client (dns_packet *pkt) |
939 | vpn::dnsv4_client (dns_packet &pkt) |
770 | { |
940 | { |
771 | u16 flags = ntohs (pkt->flags); |
941 | u16 flags = ntohs (pkt.flags); |
772 | int offs = 6 * 2; // skip header |
942 | int offs = 6 * 2; // skip header |
773 | |
943 | |
774 | pkt->qdcount = ntohs (pkt->qdcount); |
944 | pkt.qdcount = ntohs (pkt.qdcount); |
775 | pkt->ancount = ntohs (pkt->ancount); |
945 | pkt.ancount = ntohs (pkt.ancount); |
776 | |
946 | |
777 | // go through our request list and find the corresponding request |
947 | // go through our request list and find the corresponding request |
778 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
948 | for (vector<dns_snd *>::iterator i = dns_sndpq.begin (); |
779 | i != dns_sndpq.end (); |
949 | i != dns_sndpq.end (); |
780 | ++i) |
950 | ++i) |
781 | if ((*i)->pkt->id == pkt->id) |
951 | if ((*i)->pkt->id == pkt.id) |
782 | { |
952 | { |
783 | dns_connection *dns = (*i)->dns; |
953 | dns_connection *dns = (*i)->dns; |
784 | int seqno = (*i)->seqno; |
954 | int seqno = (*i)->seqno; |
785 | u8 data[MAXSIZE], *datap = data; |
955 | u8 data[MAXSIZE], *datap = data; |
786 | |
956 | |
|
|
957 | if ((*i)->retry) |
|
|
958 | { |
|
|
959 | dns->send_interval *= 1.01; |
|
|
960 | if (dns->send_interval < MAX_SEND_INTERVAL) |
|
|
961 | dns->send_interval = MAX_SEND_INTERVAL; |
|
|
962 | } |
|
|
963 | else |
|
|
964 | { |
|
|
965 | dns->send_interval *= 0.99; |
|
|
966 | if (dns->send_interval < MIN_SEND_INTERVAL) |
|
|
967 | dns->send_interval = MIN_SEND_INTERVAL; |
|
|
968 | |
|
|
969 | // the latency surely puts an upper bound on |
|
|
970 | // the minimum send interval |
|
|
971 | if (dns->send_interval > NOW - (*i)->sent) |
|
|
972 | dns->send_interval = NOW - (*i)->sent; |
|
|
973 | } |
|
|
974 | |
787 | delete *i; |
975 | delete *i; |
788 | dns_sndpq.erase (i); |
976 | dns_sndpq.erase (i); |
789 | |
977 | |
790 | if (flags & FLAG_RESPONSE && !(flags & (FLAG_OP_MASK | FLAG_TC))) |
978 | if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK)) |
791 | { |
979 | { |
792 | char qname[MAXSIZE]; |
980 | char qname[MAXSIZE]; |
793 | |
981 | |
794 | while (pkt->qdcount-- && offs < MAXSIZE - 4) |
982 | while (pkt.qdcount-- && offs < MAXSIZE - 4) |
795 | { |
983 | { |
796 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
984 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
797 | offs += 4; // skip qtype, qclass |
985 | offs += 4; // skip qtype, qclass |
798 | } |
986 | } |
799 | |
987 | |
800 | while (pkt->ancount-- && offs < MAXSIZE - 10 && datap) |
988 | while (pkt.ancount-- && offs < MAXSIZE - 10 && datap) |
801 | { |
989 | { |
802 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
990 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
803 | |
991 | |
804 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
992 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
805 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
993 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
806 | u32 ttl = (*pkt) [offs++] << 24; |
994 | u32 ttl = pkt [offs++] << 24; |
807 | ttl |= (*pkt) [offs++] << 16; |
995 | ttl |= pkt [offs++] << 16; |
808 | ttl |= (*pkt) [offs++] << 8; |
996 | ttl |= pkt [offs++] << 8; |
809 | ttl |= (*pkt) [offs++]; |
997 | ttl |= pkt [offs++]; |
810 | |
|
|
811 | u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
998 | u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++]; |
812 | |
999 | |
813 | if (rdlen <= MAXSIZE - offs) |
1000 | if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT) |
814 | { |
1001 | { |
815 | // decode bytes, finally |
1002 | if (rdlen <= MAXSIZE - offs) |
816 | |
|
|
817 | while (rdlen) |
|
|
818 | { |
1003 | { |
|
|
1004 | // decode bytes, finally |
|
|
1005 | |
|
|
1006 | while (rdlen) |
|
|
1007 | { |
819 | int txtlen = (*pkt) [offs++]; |
1008 | int txtlen = pkt [offs++]; |
820 | |
1009 | |
821 | assert (txtlen + offs < MAXSIZE - 1); |
1010 | assert (txtlen + offs < MAXSIZE - 1); |
822 | |
1011 | |
823 | memcpy (datap, pkt->at (offs), txtlen); |
1012 | memcpy (datap, pkt.at (offs), txtlen); |
824 | datap += txtlen; offs += txtlen; |
1013 | datap += txtlen; offs += txtlen; |
825 | |
1014 | |
826 | rdlen -= txtlen + 1; |
1015 | rdlen -= txtlen + 1; |
|
|
1016 | } |
827 | } |
1017 | } |
|
|
1018 | } |
|
|
1019 | else if (qtype == RR_TYPE_A) |
|
|
1020 | { |
|
|
1021 | u8 ip [4]; |
|
|
1022 | |
|
|
1023 | ip [0] = pkt [offs++]; |
|
|
1024 | ip [1] = pkt [offs++]; |
|
|
1025 | ip [2] = pkt [offs++]; |
|
|
1026 | ip [3] = pkt [offs++]; |
|
|
1027 | |
|
|
1028 | if (ip [0] == CMD_IP_1 |
|
|
1029 | && ip [1] == CMD_IP_2 |
|
|
1030 | && ip [2] == CMD_IP_3) |
|
|
1031 | { |
|
|
1032 | slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]); |
|
|
1033 | |
|
|
1034 | if (ip [3] == CMD_IP_RST) |
|
|
1035 | { |
|
|
1036 | slog (L_DEBUG, _("got tunnel RST request")); |
|
|
1037 | |
|
|
1038 | connection *c = dns->c; |
|
|
1039 | delete c->dns; c->dns = 0; |
|
|
1040 | |
|
|
1041 | return; |
|
|
1042 | } |
|
|
1043 | else if (ip [3] == CMD_IP_SYN) |
|
|
1044 | dns->established = true; |
|
|
1045 | else |
|
|
1046 | slog (L_INFO, _("got unknown meta command %02x"), ip [3]); |
|
|
1047 | } |
|
|
1048 | else |
|
|
1049 | slog (L_INFO, _("got spurious a record %d.%d.%d.%d"), |
|
|
1050 | ip [0], ip [1], ip [2], ip [3]); |
|
|
1051 | |
|
|
1052 | return; |
828 | } |
1053 | } |
829 | |
1054 | |
830 | int client, rseqno; |
1055 | int client, rseqno; |
831 | decode_header (qname, client, rseqno); |
1056 | decode_header (qname, client, rseqno); |
832 | |
1057 | |
… | |
… | |
847 | if (datap) |
1072 | if (datap) |
848 | dns->receive_rep (new dns_rcv (seqno, data, datap - data)); |
1073 | dns->receive_rep (new dns_rcv (seqno, data, datap - data)); |
849 | |
1074 | |
850 | break; |
1075 | break; |
851 | } |
1076 | } |
852 | |
|
|
853 | delete pkt; |
|
|
854 | } |
1077 | } |
855 | |
1078 | |
856 | void |
1079 | void |
857 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1080 | vpn::dnsv4_ev (io_watcher &w, short revents) |
858 | { |
1081 | { |
… | |
… | |
860 | { |
1083 | { |
861 | dns_packet *pkt = new dns_packet; |
1084 | dns_packet *pkt = new dns_packet; |
862 | struct sockaddr_in sa; |
1085 | struct sockaddr_in sa; |
863 | socklen_t sa_len = sizeof (sa); |
1086 | socklen_t sa_len = sizeof (sa); |
864 | |
1087 | |
865 | pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1088 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
866 | |
1089 | |
867 | if (pkt->len > 0) |
1090 | if (pkt->len > 0) |
868 | { |
1091 | { |
869 | if (pkt->flags & htons (FLAG_TC)) |
|
|
870 | { |
|
|
871 | slog (L_WARN, _("DNS request/response truncated, check protocol settings.")); |
|
|
872 | //TODO connection reset |
|
|
873 | } |
|
|
874 | |
|
|
875 | if (THISNODE->dns_port) |
1092 | if (THISNODE->dns_port) |
876 | { |
1093 | { |
877 | pkt = dnsv4_server (pkt); |
1094 | dnsv4_server (*pkt); |
878 | sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1095 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
879 | } |
1096 | } |
880 | else |
1097 | else |
881 | dnsv4_client (pkt); |
1098 | dnsv4_client (*pkt); |
|
|
1099 | |
|
|
1100 | delete pkt; |
882 | } |
1101 | } |
883 | } |
1102 | } |
884 | } |
1103 | } |
885 | |
1104 | |
886 | bool |
1105 | bool |
… | |
… | |
890 | dns = new dns_connection (this); |
1109 | dns = new dns_connection (this); |
891 | |
1110 | |
892 | if (!dns->snddq.put (pkt)) |
1111 | if (!dns->snddq.put (pkt)) |
893 | return false; |
1112 | return false; |
894 | |
1113 | |
895 | // start timer if neccessary |
|
|
896 | if (!THISNODE->dns_port && !dns->tw.active) |
|
|
897 | dns->tw.trigger (); |
1114 | dns->tw.trigger (); |
898 | |
1115 | |
899 | return true; |
1116 | return true; |
900 | } |
1117 | } |
|
|
1118 | |
|
|
1119 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
901 | |
1120 | |
902 | void |
1121 | void |
903 | dns_connection::time_cb (time_watcher &w) |
1122 | dns_connection::time_cb (time_watcher &w) |
904 | { |
1123 | { |
|
|
1124 | // servers have to be polled |
|
|
1125 | if (THISNODE->dns_port) |
|
|
1126 | return; |
|
|
1127 | |
905 | // check for timeouts and (re)transmit |
1128 | // check for timeouts and (re)transmit |
906 | tstamp next = NOW + 60; |
1129 | tstamp next = NOW + poll_interval; |
907 | dns_req *send = 0; |
1130 | dns_snd *send = 0; |
908 | |
1131 | |
909 | for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); |
1132 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
910 | i != vpn->dns_sndpq.end (); |
1133 | i != vpn->dns_sndpq.end (); |
911 | ++i) |
1134 | ++i) |
912 | { |
1135 | { |
913 | dns_req *r = *i; |
1136 | dns_snd *r = *i; |
914 | |
1137 | |
915 | if (r->next <= NOW) |
1138 | if (r->timeout <= NOW) |
916 | { |
1139 | { |
917 | if (!send) |
1140 | if (!send) |
918 | { |
1141 | { |
919 | send = r; |
1142 | send = r; |
920 | |
1143 | |
921 | if (r->retry)//D |
|
|
922 | printf ("req %d:%d, retry %d\n", r->seqno, r->pkt->id, r->retry); |
|
|
923 | r->retry++; |
1144 | r->retry++; |
924 | r->next = NOW + r->retry; |
1145 | r->timeout = NOW + r->retry; |
925 | } |
1146 | } |
926 | } |
1147 | } |
927 | |
|
|
928 | if (r->next < next) |
1148 | else if (r->timeout < next) |
929 | next = r->next; |
1149 | NEXT (r->timeout); |
|
|
1150 | } |
|
|
1151 | |
|
|
1152 | if (last_sent + send_interval <= NOW) |
930 | } |
1153 | { |
931 | |
|
|
932 | if (!send |
1154 | if (!send) |
|
|
1155 | { |
|
|
1156 | // generate a new packet, if wise |
|
|
1157 | |
|
|
1158 | if (!established) |
|
|
1159 | { |
|
|
1160 | if (vpn->dns_sndpq.empty ()) |
|
|
1161 | { |
|
|
1162 | send = new dns_snd (this); |
|
|
1163 | |
|
|
1164 | cfg.reset (THISNODE->id); |
|
|
1165 | send->gen_syn_req (cfg); |
|
|
1166 | } |
|
|
1167 | } |
933 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
1168 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
934 | { |
1169 | { |
935 | send = new dns_req (this); |
1170 | send = new dns_snd (this); |
936 | send->gen_stream_req (sndseq, snddq); |
1171 | send->gen_stream_req (sndseq, snddq); |
|
|
1172 | |
|
|
1173 | sndseq = (sndseq + 1) & SEQNO_MASK; |
|
|
1174 | } |
|
|
1175 | |
|
|
1176 | if (send) |
937 | vpn->dns_sndpq.push_back (send); |
1177 | vpn->dns_sndpq.push_back (send); |
|
|
1178 | } |
938 | |
1179 | |
939 | sndseq = (sndseq + 1) & SEQNO_MASK; |
|
|
940 | } |
|
|
941 | |
|
|
942 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
|
|
943 | |
|
|
944 | if (send) |
1180 | if (send) |
945 | { |
1181 | { |
946 | dns_packet *pkt = send->pkt; |
1182 | printf ("send pkt\n"); |
|
|
1183 | last_sent = NOW; |
947 | |
1184 | |
948 | next = min_next; |
1185 | if (!send->retry) |
|
|
1186 | send->sent = NOW; |
949 | |
1187 | |
950 | sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, |
1188 | sendto (vpn->dnsv4_fd, |
|
|
1189 | send->pkt->at (0), send->pkt->len, 0, |
951 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1190 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
|
|
1191 | } |
952 | } |
1192 | } |
953 | else if (next < min_next) |
1193 | else |
954 | next = min_next; |
1194 | NEXT (last_sent + send_interval); |
|
|
1195 | |
|
|
1196 | printf ("pi %f si %f N %f (%d:%d)\n", poll_interval, send_interval, next - NOW, vpn->dns_sndpq.size (), snddq.size ()); |
955 | |
1197 | |
956 | w.start (next); |
1198 | w.start (next); |
957 | } |
1199 | } |
958 | |
1200 | |
959 | #endif |
1201 | #endif |