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