… | |
… | |
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> |
… | |
… | |
43 | |
44 | |
44 | #include "netcompat.h" |
45 | #include "netcompat.h" |
45 | |
46 | |
46 | #include "vpn.h" |
47 | #include "vpn.h" |
47 | |
48 | |
48 | #define MIN_POLL_INTERVAL .2 // how often to poll minimally when the server is having data |
49 | #define MIN_POLL_INTERVAL .02 // how often to poll minimally when the server has data |
49 | #define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data |
50 | #define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data |
50 | #define ACTIVITY_INTERVAL 5. |
51 | #define ACTIVITY_INTERVAL 5. |
51 | |
52 | |
52 | #define INITIAL_TIMEOUT 1. |
53 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
53 | #define INITIAL_SYN_TIMEOUT 2. |
54 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
54 | |
55 | |
55 | #define MIN_SEND_INTERVAL (1./1000.) |
56 | #define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests |
56 | #define MAX_SEND_INTERVAL 0.5 // optimistic? |
57 | #define MAX_SEND_INTERVAL 0.5 // optimistic? |
57 | |
58 | |
58 | #define MAX_OUTSTANDING 400 // max. outstanding requests |
59 | #define MAX_OUTSTANDING 10 // max. outstanding requests |
59 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING |
60 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
60 | #define MAX_RATE 100 // requests/s |
|
|
61 | #define MAX_BACKLOG (100*1024) // size of protocol backlog, must be > MAXSIZE |
61 | #define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
62 | |
62 | |
63 | #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 |
64 | // 240 leaves about 4 bytes of server reply data |
64 | // 240 leaves about 4 bytes of server reply data |
65 | // every two request byte sless give room for one reply byte |
65 | // every two request bytes less give room for one reply byte |
66 | |
66 | |
67 | #define SEQNO_MASK 0xffff |
67 | #define SEQNO_MASK 0x3fff |
68 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
68 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
69 | |
69 | |
70 | #define MAX_LBL_SIZE 63 |
70 | #define MAX_LBL_SIZE 63 |
71 | #define MAX_PKT_SIZE 512 |
71 | #define MAX_PKT_SIZE 512 |
72 | |
72 | |
… | |
… | |
265 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
265 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
266 | |
266 | |
267 | ///////////////////////////////////////////////////////////////////////////// |
267 | ///////////////////////////////////////////////////////////////////////////// |
268 | |
268 | |
269 | #define HDRSIZE 6 |
269 | #define HDRSIZE 6 |
270 | |
270 | |
271 | inline void encode_header (char *data, int clientid, int seqno) |
271 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
272 | { |
272 | { |
273 | u8 hdr[3] = { clientid, seqno >> 8, seqno }; |
273 | seqno &= SEQNO_MASK; |
|
|
274 | |
|
|
275 | u8 hdr[3] = { |
|
|
276 | clientid, |
|
|
277 | (seqno >> 8) | (retry << 6), |
|
|
278 | seqno, |
|
|
279 | }; |
274 | |
280 | |
275 | assert (clientid < 256); |
281 | assert (clientid < 256); |
276 | |
282 | |
277 | cdc26.encode (data, hdr, 3); |
283 | cdc26.encode (data, hdr, 3); |
278 | } |
284 | } |
… | |
… | |
282 | u8 hdr[3]; |
288 | u8 hdr[3]; |
283 | |
289 | |
284 | cdc26.decode (hdr, data, HDRSIZE); |
290 | cdc26.decode (hdr, data, HDRSIZE); |
285 | |
291 | |
286 | clientid = hdr[0]; |
292 | clientid = hdr[0]; |
287 | seqno = (hdr[1] << 8) | hdr[2]; |
293 | seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK; |
288 | } |
294 | } |
289 | |
295 | |
290 | ///////////////////////////////////////////////////////////////////////////// |
296 | ///////////////////////////////////////////////////////////////////////////// |
291 | |
297 | |
292 | struct byte_stream |
298 | struct byte_stream |
… | |
… | |
321 | } |
327 | } |
322 | |
328 | |
323 | void byte_stream::remove (int count) |
329 | void byte_stream::remove (int count) |
324 | { |
330 | { |
325 | if (count > fill) |
331 | if (count > fill) |
326 | abort (); |
332 | assert (count <= fill); |
327 | |
333 | |
328 | memmove (data, data + count, fill -= count); |
334 | memmove (data, data + count, fill -= count); |
329 | } |
335 | } |
330 | |
336 | |
331 | bool byte_stream::put (u8 *data, unsigned int datalen) |
337 | bool byte_stream::put (u8 *data, unsigned int datalen) |
… | |
… | |
351 | return true; |
357 | return true; |
352 | } |
358 | } |
353 | |
359 | |
354 | vpn_packet *byte_stream::get () |
360 | vpn_packet *byte_stream::get () |
355 | { |
361 | { |
|
|
362 | unsigned int len; |
|
|
363 | |
|
|
364 | for (;;) |
|
|
365 | { |
356 | int len = (data [0] << 8) | data [1]; |
366 | len = (data [0] << 8) | data [1]; |
357 | |
367 | |
358 | if (len > MAXSIZE && fill >= 2) |
368 | if (len <= MAXSIZE || fill < 2) |
359 | abort (); // TODO handle this gracefully, connection reset |
369 | break; |
360 | |
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 | |
361 | if (fill < len + 2) |
376 | if (fill < len + 2) |
362 | return 0; |
377 | return 0; |
363 | |
378 | |
364 | vpn_packet *pkt = new vpn_packet; |
379 | vpn_packet *pkt = new vpn_packet; |
365 | |
380 | |
… | |
… | |
394 | struct dns_cfg |
409 | struct dns_cfg |
395 | { |
410 | { |
396 | static int next_uid; |
411 | static int next_uid; |
397 | |
412 | |
398 | u8 id1, id2, id3, id4; |
413 | u8 id1, id2, id3, id4; |
|
|
414 | |
399 | u8 version; |
415 | u8 version; |
|
|
416 | u8 flags; |
400 | u8 rrtype; |
417 | u8 rrtype; |
401 | u8 flags; |
|
|
402 | u8 def_ttl; |
418 | u8 def_ttl; |
403 | u8 rcv_cdc; |
419 | |
404 | u8 snd_cdc; |
|
|
405 | u16 max_size; |
|
|
406 | u16 client; |
420 | u16 client; |
407 | u16 uid; // to make request unique |
421 | u16 uid; // to make request unique |
408 | |
422 | |
409 | 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; |
410 | |
431 | |
411 | void reset (int clientid); |
432 | void reset (int clientid); |
412 | bool valid (); |
433 | bool valid (); |
413 | }; |
434 | }; |
414 | |
435 | |
… | |
… | |
424 | version = 1; |
445 | version = 1; |
425 | |
446 | |
426 | rrtype = RR_TYPE_TXT; |
447 | rrtype = RR_TYPE_TXT; |
427 | flags = 0; |
448 | flags = 0; |
428 | def_ttl = 0; |
449 | def_ttl = 0; |
|
|
450 | seq_cdc = 26; |
|
|
451 | req_cdc = 62; |
429 | rcv_cdc = 0; |
452 | rep_cdc = 0; |
430 | snd_cdc = 62; |
|
|
431 | max_size = ntohs (MAX_PKT_SIZE); |
453 | max_size = ntohs (MAX_PKT_SIZE); |
432 | client = ntohs (clientid); |
454 | client = ntohs (clientid); |
433 | uid = next_uid++; |
455 | uid = next_uid++; |
434 | |
456 | |
435 | memset (reserved, 0, 8); |
457 | r2 = r3 = r4 = 0; |
|
|
458 | r4 = r5 = r6 = r7 = 0; |
436 | } |
459 | } |
437 | |
460 | |
438 | bool dns_cfg::valid () |
461 | bool dns_cfg::valid () |
439 | { |
462 | { |
440 | return id1 == 'G' |
463 | return id1 == 'G' |
441 | && id2 == 'V' |
464 | && id2 == 'V' |
442 | && id3 == 'P' |
465 | && id3 == 'P' |
443 | && id4 == 'E' |
466 | && id4 == 'E' |
|
|
467 | && seq_cdc == 26 |
|
|
468 | && req_cdc == 62 |
|
|
469 | && rep_cdc == 0 |
444 | && version == 1 |
470 | && version == 1 |
445 | && flags == 0 |
|
|
446 | && rcv_cdc == 0 |
|
|
447 | && snd_cdc == 62 |
|
|
448 | && max_size == ntohs (MAX_PKT_SIZE); |
471 | && max_size == ntohs (MAX_PKT_SIZE); |
449 | } |
472 | } |
450 | |
473 | |
451 | struct dns_packet : net_packet |
474 | struct dns_packet : net_packet |
452 | { |
475 | { |
453 | u16 id; |
476 | u16 id; |
454 | 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 |
455 | u16 qdcount, ancount, nscount, arcount; |
478 | u16 qdcount, ancount, nscount, arcount; |
456 | |
479 | |
457 | u8 data[MAXSIZE - 6 * 2]; |
480 | u8 data [MAXSIZE - 6 * 2]; |
458 | |
481 | |
459 | int decode_label (char *data, int size, int &offs); |
482 | int decode_label (char *data, int size, int &offs); |
460 | }; |
483 | }; |
461 | |
484 | |
462 | int dns_packet::decode_label (char *data, int size, int &offs) |
485 | int dns_packet::decode_label (char *data, int size, int &offs) |
… | |
… | |
493 | return data - orig; |
516 | return data - orig; |
494 | } |
517 | } |
495 | |
518 | |
496 | ///////////////////////////////////////////////////////////////////////////// |
519 | ///////////////////////////////////////////////////////////////////////////// |
497 | |
520 | |
498 | struct dns_snd |
|
|
499 | { |
|
|
500 | dns_packet *pkt; |
|
|
501 | tstamp timeout, sent; |
|
|
502 | int retry; |
|
|
503 | struct dns_connection *dns; |
|
|
504 | int seqno; |
|
|
505 | |
|
|
506 | dns_snd (dns_connection *dns); |
|
|
507 | void gen_stream_req (int seqno, byte_stream &stream); |
|
|
508 | void gen_syn_req (const dns_cfg &cfg); |
|
|
509 | }; |
|
|
510 | |
|
|
511 | static u16 dns_id = 12098; // TODO: should be per-vpn |
521 | static u16 dns_id = 0; // TODO: should be per-vpn |
512 | |
522 | |
513 | static u16 next_id () |
523 | static u16 next_id () |
514 | { |
524 | { |
|
|
525 | if (!dns_id) |
|
|
526 | dns_id = time (0); |
|
|
527 | |
515 | // the simplest lsfr with periodicity 65535 i could find |
528 | // the simplest lsfr with periodicity 65535 i could find |
516 | dns_id = (dns_id << 1) |
529 | dns_id = (dns_id << 1) |
517 | | (((dns_id >> 1) |
530 | | (((dns_id >> 1) |
518 | ^ (dns_id >> 2) |
531 | ^ (dns_id >> 2) |
519 | ^ (dns_id >> 4) |
532 | ^ (dns_id >> 4) |
520 | ^ (dns_id >> 15)) & 1); |
533 | ^ (dns_id >> 15)) & 1); |
521 | |
534 | |
522 | return dns_id; |
535 | return dns_id; |
523 | } |
536 | } |
524 | |
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 | |
|
|
567 | struct dns_snd |
|
|
568 | { |
|
|
569 | dns_packet *pkt; |
|
|
570 | tstamp timeout, sent; |
|
|
571 | int retry; |
|
|
572 | struct dns_connection *dns; |
|
|
573 | int seqno; |
|
|
574 | bool stdhdr; |
|
|
575 | |
|
|
576 | void gen_stream_req (int seqno, byte_stream &stream); |
|
|
577 | void gen_syn_req (); |
|
|
578 | |
|
|
579 | dns_snd (dns_connection *dns); |
|
|
580 | ~dns_snd (); |
|
|
581 | }; |
|
|
582 | |
525 | dns_snd::dns_snd (dns_connection *dns) |
583 | dns_snd::dns_snd (dns_connection *dns) |
526 | : dns (dns) |
584 | : dns (dns) |
527 | { |
585 | { |
528 | timeout = 0; |
586 | timeout = 0; |
529 | retry = 0; |
587 | retry = 0; |
530 | seqno = 0; |
588 | seqno = 0; |
|
|
589 | sent = NOW; |
|
|
590 | stdhdr = false; |
531 | |
591 | |
532 | pkt = new dns_packet; |
592 | pkt = new dns_packet; |
533 | |
593 | |
534 | pkt->id = next_id (); |
594 | pkt->id = next_id (); |
|
|
595 | } |
|
|
596 | |
|
|
597 | dns_snd::~dns_snd () |
|
|
598 | { |
|
|
599 | delete pkt; |
535 | } |
600 | } |
536 | |
601 | |
537 | static void append_domain (dns_packet &pkt, int &offs, const char *domain) |
602 | static void append_domain (dns_packet &pkt, int &offs, const char *domain) |
538 | { |
603 | { |
539 | // add tunnel domain |
604 | // add tunnel domain |
… | |
… | |
557 | } |
622 | } |
558 | } |
623 | } |
559 | |
624 | |
560 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
625 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
561 | { |
626 | { |
|
|
627 | stdhdr = true; |
562 | this->seqno = seqno; |
628 | this->seqno = seqno; |
563 | |
629 | |
564 | timeout = NOW + INITIAL_TIMEOUT; |
630 | timeout = NOW + INITIAL_TIMEOUT; |
565 | |
631 | |
566 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
632 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
567 | pkt->qdcount = htons (1); |
633 | pkt->qdcount = htons (1); |
568 | |
634 | |
569 | int offs = 6*2; |
635 | int offs = 6*2; |
570 | int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); |
636 | int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2); |
571 | // 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, |
572 | // 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 |
573 | |
639 | |
574 | char enc[256], *encp = enc; |
640 | char enc[256], *encp = enc; |
575 | encode_header (enc, THISNODE->id, seqno); |
641 | encode_header (enc, THISNODE->id, seqno); |
… | |
… | |
593 | encp += lbllen; |
659 | encp += lbllen; |
594 | |
660 | |
595 | enclen -= lbllen; |
661 | enclen -= lbllen; |
596 | } |
662 | } |
597 | |
663 | |
598 | append_domain (*pkt, offs, THISNODE->domain); |
664 | append_domain (*pkt, offs, dns->c->conf->domain); |
599 | |
665 | |
600 | (*pkt)[offs++] = 0; |
666 | (*pkt)[offs++] = 0; |
601 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
667 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
602 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
668 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
603 | |
669 | |
604 | pkt->len = offs; |
670 | pkt->len = offs; |
605 | } |
671 | } |
606 | |
672 | |
607 | void dns_snd::gen_syn_req (const dns_cfg &cfg) |
673 | void dns_snd::gen_syn_req () |
608 | { |
674 | { |
609 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
675 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
610 | |
676 | |
611 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
677 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
612 | pkt->qdcount = htons (1); |
678 | pkt->qdcount = htons (1); |
613 | |
679 | |
614 | int offs = 6*2; |
680 | int offs = 6 * 2; |
615 | |
681 | |
616 | 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)); |
617 | |
683 | |
618 | assert (elen <= MAX_LBL_SIZE); |
684 | assert (elen <= MAX_LBL_SIZE); |
619 | |
685 | |
620 | (*pkt)[offs] = elen; |
686 | (*pkt)[offs] = elen; |
621 | offs += elen + 1; |
687 | offs += elen + 1; |
622 | append_domain (*pkt, offs, THISNODE->domain); |
688 | append_domain (*pkt, offs, dns->c->conf->domain); |
623 | |
689 | |
624 | (*pkt)[offs++] = 0; |
690 | (*pkt)[offs++] = 0; |
625 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
691 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
626 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
692 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
627 | |
693 | |
… | |
… | |
650 | delete pkt; |
716 | delete pkt; |
651 | } |
717 | } |
652 | |
718 | |
653 | ///////////////////////////////////////////////////////////////////////////// |
719 | ///////////////////////////////////////////////////////////////////////////// |
654 | |
720 | |
655 | struct dns_connection |
|
|
656 | { |
|
|
657 | connection *c; |
|
|
658 | struct vpn *vpn; |
|
|
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 | |
|
|
668 | vector<dns_rcv *> rcvpq; |
|
|
669 | |
|
|
670 | byte_stream rcvdq; int rcvseq; |
|
|
671 | byte_stream snddq; int sndseq; |
|
|
672 | |
|
|
673 | void time_cb (time_watcher &w); time_watcher tw; |
|
|
674 | void receive_rep (dns_rcv *r); |
|
|
675 | |
|
|
676 | dns_connection (connection *c); |
|
|
677 | ~dns_connection (); |
|
|
678 | }; |
|
|
679 | |
|
|
680 | dns_connection::dns_connection (connection *c) |
721 | dns_connection::dns_connection (connection *c) |
681 | : c (c) |
722 | : c (c) |
682 | , rcvdq (MAX_BACKLOG * 2) |
723 | , rcvdq (MAX_BACKLOG * 2) |
683 | , snddq (MAX_BACKLOG * 2) |
724 | , snddq (MAX_BACKLOG * 2) |
684 | , tw (this, &dns_connection::time_cb) |
725 | , tw (this, &dns_connection::time_cb) |
… | |
… | |
689 | |
730 | |
690 | rcvseq = sndseq = 0; |
731 | rcvseq = sndseq = 0; |
691 | |
732 | |
692 | last_sent = last_received = 0; |
733 | last_sent = last_received = 0; |
693 | poll_interval = MIN_POLL_INTERVAL; |
734 | poll_interval = MIN_POLL_INTERVAL; |
694 | send_interval = 0.2; // starting rate |
735 | send_interval = 0.5; // starting rate |
|
|
736 | last_latency = INITIAL_TIMEOUT; |
695 | } |
737 | } |
696 | |
738 | |
697 | dns_connection::~dns_connection () |
739 | dns_connection::~dns_connection () |
698 | { |
740 | { |
699 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
741 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
… | |
… | |
707 | if (r->datalen) |
749 | if (r->datalen) |
708 | { |
750 | { |
709 | last_received = NOW; |
751 | last_received = NOW; |
710 | tw.trigger (); |
752 | tw.trigger (); |
711 | |
753 | |
712 | poll_interval *= 0.99; |
754 | poll_interval = send_interval; |
713 | if (poll_interval > MIN_POLL_INTERVAL) |
|
|
714 | poll_interval = MIN_POLL_INTERVAL; |
|
|
715 | } |
755 | } |
716 | else |
756 | else |
717 | { |
757 | { |
718 | poll_interval *= 1.1; |
758 | poll_interval *= 1.5; |
719 | if (poll_interval > MAX_POLL_INTERVAL) |
759 | if (poll_interval > MAX_POLL_INTERVAL) |
720 | poll_interval = MAX_POLL_INTERVAL; |
760 | poll_interval = MAX_POLL_INTERVAL; |
721 | } |
761 | } |
722 | |
762 | |
723 | rcvpq.push_back (r); |
763 | rcvpq.push_back (r); |
… | |
… | |
741 | } |
781 | } |
742 | |
782 | |
743 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
783 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
744 | |
784 | |
745 | if (!rcvdq.put (r->data, r->datalen)) |
785 | if (!rcvdq.put (r->data, r->datalen)) |
|
|
786 | { |
|
|
787 | slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)"); |
746 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
788 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
|
|
789 | } |
747 | |
790 | |
748 | while (vpn_packet *pkt = rcvdq.get ()) |
791 | while (vpn_packet *pkt = rcvdq.get ()) |
749 | { |
792 | { |
750 | sockinfo si; |
793 | sockinfo si; |
751 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
794 | si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4; |
752 | |
795 | |
753 | vpn->recv_vpn_packet (pkt, si); |
796 | vpn->recv_vpn_packet (pkt, si); |
|
|
797 | |
|
|
798 | delete pkt; |
754 | } |
799 | } |
755 | |
800 | |
756 | // check for further packets |
801 | // check for further packets |
757 | goto redo; |
802 | goto redo; |
758 | } |
803 | } |
… | |
… | |
768 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
813 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
769 | |
814 | |
770 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
815 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
771 | && pkt.qdcount == htons (1)) |
816 | && pkt.qdcount == htons (1)) |
772 | { |
817 | { |
773 | char qname[MAXSIZE]; |
818 | char qname [MAXSIZE]; |
774 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
819 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
775 | |
820 | |
776 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
821 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
777 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
822 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
778 | |
823 | |
779 | pkt.qdcount = htons (1); |
824 | pkt.qdcount = htons (1); |
780 | pkt.ancount = 0; |
825 | pkt.ancount = 0; |
781 | pkt.nscount = 0; // should be self, as other nameservers reply like this |
826 | pkt.nscount = 0; // should be self, as other nameservers reply like this |
782 | pkt.arcount = 0; // a record for self, as other nameservers reply like this |
827 | pkt.arcount = 0; // a record for self, as other nameservers reply like this |
783 | |
828 | |
784 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
829 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL); |
785 | |
830 | |
786 | int dlen = strlen (THISNODE->domain); |
831 | int dlen = strlen (THISNODE->domain); |
787 | |
832 | |
788 | if (qclass == RR_CLASS_IN |
833 | if (qclass == RR_CLASS_IN |
789 | && qlen > dlen + 1 |
834 | && qlen > dlen + 1 |
790 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
835 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
791 | { |
836 | { |
792 | // now generate reply |
837 | // now generate reply |
793 | pkt.ancount = htons (1); // one answer RR |
838 | pkt.ancount = htons (1); // one answer RR |
794 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
839 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
795 | |
840 | |
… | |
… | |
808 | if (0 < client && client <= conns.size ()) |
853 | if (0 < client && client <= conns.size ()) |
809 | { |
854 | { |
810 | connection *c = conns [client - 1]; |
855 | connection *c = conns [client - 1]; |
811 | dns_connection *dns = c->dns; |
856 | dns_connection *dns = c->dns; |
812 | dns_rcv *rcv; |
857 | dns_rcv *rcv; |
|
|
858 | bool in_seq; |
813 | |
859 | |
814 | if (dns) |
860 | if (dns) |
815 | { |
861 | { |
816 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
862 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
817 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
863 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
818 | { |
864 | { |
819 | // already seen that request: simply reply with the cached reply |
865 | // already seen that request: simply reply with the cached reply |
820 | dns_rcv *r = *i; |
866 | dns_rcv *r = *i; |
821 | |
867 | |
822 | printf ("DUPLICATE %d\n", htons (r->pkt->id));//D |
868 | slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno); |
|
|
869 | |
|
|
870 | // refresh header & id, as the retry count could have changed |
|
|
871 | memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE); |
|
|
872 | r->pkt->id = pkt.id; |
823 | |
873 | |
824 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
874 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
825 | pkt.id = r->pkt->id; |
875 | |
826 | goto duplicate_request; |
876 | goto duplicate_request; |
827 | } |
877 | } |
|
|
878 | |
|
|
879 | in_seq = dns->rcvseq == seqno; |
828 | |
880 | |
829 | // new packet, queue |
881 | // new packet, queue |
830 | rcv = new dns_rcv (seqno, data, datalen); |
882 | rcv = new dns_rcv (seqno, data, datalen); |
831 | dns->receive_rep (rcv); |
883 | dns->receive_rep (rcv); |
832 | } |
884 | } |
833 | |
885 | |
|
|
886 | { |
834 | 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 |
835 | |
888 | |
836 | // type |
|
|
837 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
889 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
838 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; |
890 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
839 | |
|
|
840 | // class |
|
|
841 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; |
891 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
842 | |
|
|
843 | // TTL |
|
|
844 | pkt [offs++] = 0; pkt [offs++] = 0; |
892 | pkt [offs++] = 0; pkt [offs++] = 0; |
845 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; |
893 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
846 | |
894 | |
847 | int rdlen_offs = offs += 2; |
895 | int rdlen_offs = offs += 2; |
848 | |
896 | |
849 | 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; |
850 | // 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 |
851 | dlen -= qlen; |
899 | dlen -= qlen; |
852 | |
900 | |
853 | if (dns) |
901 | if (dns) |
854 | { |
902 | { |
|
|
903 | // only put data into in-order sequence packets, if |
|
|
904 | // we receive out-of-order packets we generate empty |
|
|
905 | // replies |
855 | while (dlen > 1 && !dns->snddq.empty ()) |
906 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
856 | { |
907 | { |
857 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
908 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
858 | |
909 | |
859 | if (txtlen > dns->snddq.size ()) |
910 | if (txtlen > dns->snddq.size ()) |
860 | txtlen = dns->snddq.size (); |
911 | txtlen = dns->snddq.size (); |
861 | |
912 | |
862 | pkt[offs++] = txtlen; |
913 | pkt[offs++] = txtlen; |
863 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
914 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
864 | offs += txtlen; |
915 | offs += txtlen; |
865 | dns->snddq.remove (txtlen); |
916 | dns->snddq.remove (txtlen); |
866 | |
917 | |
867 | dlen -= txtlen + 1; |
918 | dlen -= txtlen + 1; |
868 | } |
919 | } |
869 | |
920 | |
870 | // avoid empty TXT rdata |
921 | // avoid empty TXT rdata |
871 | if (offs == rdlen_offs) |
922 | if (offs == rdlen_offs) |
872 | pkt[offs++] = 0; |
923 | pkt[offs++] = 0; |
|
|
924 | |
|
|
925 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
873 | } |
926 | } |
874 | else |
927 | else |
875 | { |
928 | { |
876 | // send RST |
929 | // send RST |
877 | 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; |
878 | pkt [offs++] = CMD_IP_RST; |
931 | pkt [offs++] = CMD_IP_RST; |
879 | } |
932 | } |
880 | |
933 | |
881 | int rdlen = offs - rdlen_offs; |
934 | int rdlen = offs - rdlen_offs; |
882 | |
935 | |
883 | pkt [rdlen_offs - 2] = rdlen >> 8; |
936 | pkt [rdlen_offs - 2] = rdlen >> 8; |
884 | pkt [rdlen_offs - 1] = rdlen; |
937 | pkt [rdlen_offs - 1] = rdlen; |
885 | |
938 | |
886 | if (dns) |
939 | if (dns) |
887 | { |
940 | { |
888 | // now update dns_rcv copy |
941 | // now update dns_rcv copy |
889 | rcv->pkt->len = offs; |
942 | rcv->pkt->len = offs; |
890 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
943 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
891 | } |
944 | } |
|
|
945 | } |
892 | |
946 | |
893 | duplicate_request: ; |
947 | duplicate_request: ; |
894 | } |
948 | } |
895 | else |
949 | else |
896 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
950 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
… | |
… | |
908 | 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 |
909 | pkt [offs++] = 0; pkt [offs++] = 0; |
963 | pkt [offs++] = 0; pkt [offs++] = 0; |
910 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
964 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
911 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
965 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
912 | |
966 | |
913 | slog (L_INFO, _("DNS tunnel: client %d tries to connect"), client); |
967 | slog (L_INFO, _("DNS: client %d connects"), client); |
914 | |
968 | |
915 | 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; |
916 | pkt [offs++] = CMD_IP_REJ; |
970 | pkt [offs++] = CMD_IP_REJ; |
917 | |
971 | |
918 | if (0 < client && client <= conns.size ()) |
972 | if (0 < client && client <= conns.size ()) |
… | |
… | |
949 | i != dns_sndpq.end (); |
1003 | i != dns_sndpq.end (); |
950 | ++i) |
1004 | ++i) |
951 | if ((*i)->pkt->id == pkt.id) |
1005 | if ((*i)->pkt->id == pkt.id) |
952 | { |
1006 | { |
953 | dns_connection *dns = (*i)->dns; |
1007 | dns_connection *dns = (*i)->dns; |
|
|
1008 | connection *c = dns->c; |
954 | int seqno = (*i)->seqno; |
1009 | int seqno = (*i)->seqno; |
955 | u8 data[MAXSIZE], *datap = data; |
1010 | u8 data[MAXSIZE], *datap = data; |
956 | |
1011 | |
957 | if ((*i)->retry) |
1012 | if ((*i)->retry) |
958 | { |
1013 | { |
959 | dns->send_interval *= 1.01; |
1014 | dns->send_interval *= 1.01; |
960 | if (dns->send_interval < MAX_SEND_INTERVAL) |
1015 | if (dns->send_interval > MAX_SEND_INTERVAL) |
961 | dns->send_interval = MAX_SEND_INTERVAL; |
1016 | dns->send_interval = MAX_SEND_INTERVAL; |
962 | } |
1017 | } |
963 | else |
1018 | else |
964 | { |
1019 | { |
|
|
1020 | #if 1 |
965 | dns->send_interval *= 0.99; |
1021 | dns->send_interval *= 0.999; |
|
|
1022 | #endif |
966 | if (dns->send_interval < MIN_SEND_INTERVAL) |
1023 | if (dns->send_interval < MIN_SEND_INTERVAL) |
967 | dns->send_interval = MIN_SEND_INTERVAL; |
1024 | dns->send_interval = MIN_SEND_INTERVAL; |
968 | |
1025 | |
969 | // the latency surely puts an upper bound on |
1026 | // the latency surely puts an upper bound on |
970 | // the minimum send interval |
1027 | // the minimum send interval |
|
|
1028 | double latency = NOW - (*i)->sent; |
|
|
1029 | dns->last_latency = latency; |
|
|
1030 | |
971 | if (dns->send_interval > NOW - (*i)->sent) |
1031 | if (dns->send_interval > latency) |
972 | dns->send_interval = NOW - (*i)->sent; |
1032 | dns->send_interval = latency; |
973 | } |
1033 | } |
974 | |
1034 | |
975 | delete *i; |
1035 | delete *i; |
976 | dns_sndpq.erase (i); |
1036 | dns_sndpq.erase (i); |
977 | |
1037 | |
… | |
… | |
1027 | |
1087 | |
1028 | if (ip [0] == CMD_IP_1 |
1088 | if (ip [0] == CMD_IP_1 |
1029 | && ip [1] == CMD_IP_2 |
1089 | && ip [1] == CMD_IP_2 |
1030 | && ip [2] == CMD_IP_3) |
1090 | && ip [2] == CMD_IP_3) |
1031 | { |
1091 | { |
1032 | slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]); |
1092 | slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]); |
1033 | |
1093 | |
1034 | if (ip [3] == CMD_IP_RST) |
1094 | if (ip [3] == CMD_IP_RST) |
1035 | { |
1095 | { |
1036 | slog (L_DEBUG, _("got tunnel RST request")); |
1096 | slog (L_DEBUG, _("DNS: got tunnel RST request")); |
1037 | |
1097 | |
1038 | connection *c = dns->c; |
|
|
1039 | delete c->dns; c->dns = 0; |
1098 | delete dns; c->dns = 0; |
1040 | |
1099 | |
1041 | return; |
1100 | return; |
1042 | } |
1101 | } |
1043 | else if (ip [3] == CMD_IP_SYN) |
1102 | else if (ip [3] == CMD_IP_SYN) |
|
|
1103 | { |
|
|
1104 | slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); |
1044 | dns->established = true; |
1105 | dns->established = true; |
|
|
1106 | } |
|
|
1107 | else if (ip [3] == CMD_IP_REJ) |
|
|
1108 | { |
|
|
1109 | slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting.")); |
|
|
1110 | abort (); |
|
|
1111 | } |
1045 | else |
1112 | else |
1046 | slog (L_INFO, _("got unknown meta command %02x"), ip [3]); |
1113 | slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); |
1047 | } |
1114 | } |
1048 | else |
1115 | else |
1049 | slog (L_INFO, _("got spurious a record %d.%d.%d.%d"), |
1116 | slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), |
1050 | ip [0], ip [1], ip [2], ip [3]); |
1117 | ip [0], ip [1], ip [2], ip [3]); |
1051 | |
1118 | |
1052 | return; |
1119 | return; |
1053 | } |
1120 | } |
1054 | |
1121 | |
1055 | int client, rseqno; |
1122 | int client, rseqno; |
1056 | decode_header (qname, client, rseqno); |
1123 | decode_header (qname, client, rseqno); |
1057 | |
1124 | |
1058 | if (client != THISNODE->id) |
1125 | if (client != THISNODE->id) |
1059 | { |
1126 | { |
1060 | slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); |
1127 | slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring")); |
1061 | datap = 0; |
1128 | datap = 0; |
1062 | } |
1129 | } |
1063 | else if (rseqno != seqno) |
1130 | else if (rseqno != seqno) |
1064 | { |
1131 | { |
1065 | slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?")); |
1132 | slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?")); |
1066 | datap = 0; |
1133 | datap = 0; |
1067 | } |
1134 | } |
1068 | } |
1135 | } |
1069 | } |
1136 | } |
1070 | |
1137 | |
… | |
… | |
1087 | |
1154 | |
1088 | 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); |
1089 | |
1156 | |
1090 | if (pkt->len > 0) |
1157 | if (pkt->len > 0) |
1091 | { |
1158 | { |
1092 | if (THISNODE->dns_port) |
1159 | if (ntohs (pkt->flags) & FLAG_RESPONSE) |
|
|
1160 | dnsv4_client (*pkt); |
|
|
1161 | else |
1093 | { |
1162 | { |
1094 | dnsv4_server (*pkt); |
1163 | dnsv4_server (*pkt); |
1095 | 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); |
1096 | } |
1165 | } |
1097 | else |
|
|
1098 | dnsv4_client (*pkt); |
|
|
1099 | |
1166 | |
1100 | delete pkt; |
1167 | delete pkt; |
1101 | } |
1168 | } |
1102 | } |
1169 | } |
1103 | } |
1170 | } |
1104 | |
1171 | |
1105 | bool |
1172 | bool |
1106 | 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) |
1107 | { |
1174 | { |
|
|
1175 | int client = ntohs (si.port); |
|
|
1176 | |
|
|
1177 | assert (0 < client && client <= conns.size ()); |
|
|
1178 | |
|
|
1179 | connection *c = conns [client - 1]; |
|
|
1180 | |
1108 | if (!dns) |
1181 | if (!c->dns) |
1109 | dns = new dns_connection (this); |
1182 | c->dns = new dns_connection (c); |
1110 | |
1183 | |
1111 | if (!dns->snddq.put (pkt)) |
1184 | if (!c->dns->snddq.put (pkt)) |
1112 | return false; |
1185 | return false; |
1113 | |
1186 | |
1114 | dns->tw.trigger (); |
1187 | c->dns->tw.trigger (); |
1115 | |
1188 | |
1116 | return true; |
1189 | return true; |
|
|
1190 | } |
|
|
1191 | |
|
|
1192 | void |
|
|
1193 | connection::dnsv4_reset_connection () |
|
|
1194 | { |
|
|
1195 | //delete dns; dns = 0; //TODO |
1117 | } |
1196 | } |
1118 | |
1197 | |
1119 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1198 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
1120 | |
1199 | |
1121 | void |
1200 | void |
… | |
… | |
1140 | if (!send) |
1219 | if (!send) |
1141 | { |
1220 | { |
1142 | send = r; |
1221 | send = r; |
1143 | |
1222 | |
1144 | r->retry++; |
1223 | r->retry++; |
1145 | r->timeout = NOW + r->retry; |
1224 | r->timeout = NOW + (r->retry * last_latency * 8.); |
|
|
1225 | |
|
|
1226 | // the following code changes the query section a bit, forcing |
|
|
1227 | // the forwarder to generate a new request |
|
|
1228 | if (r->stdhdr) |
|
|
1229 | { |
|
|
1230 | //printf ("reencoded header for ID %d retry %d:%d:%d\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry);printf ("reencoded header for ID %d retry %d:%d:%d\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
|
|
1231 | //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
|
|
1232 | } |
1146 | } |
1233 | } |
1147 | } |
1234 | } |
1148 | else if (r->timeout < next) |
1235 | else |
1149 | NEXT (r->timeout); |
1236 | NEXT (r->timeout); |
1150 | } |
1237 | } |
1151 | |
1238 | |
1152 | if (last_sent + send_interval <= NOW) |
1239 | if (last_sent + send_interval <= NOW) |
1153 | { |
1240 | { |
… | |
… | |
1160 | if (vpn->dns_sndpq.empty ()) |
1247 | if (vpn->dns_sndpq.empty ()) |
1161 | { |
1248 | { |
1162 | send = new dns_snd (this); |
1249 | send = new dns_snd (this); |
1163 | |
1250 | |
1164 | cfg.reset (THISNODE->id); |
1251 | cfg.reset (THISNODE->id); |
1165 | send->gen_syn_req (cfg); |
1252 | send->gen_syn_req (); |
1166 | } |
1253 | } |
1167 | } |
1254 | } |
1168 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
1255 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
|
|
1256 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1169 | { |
1257 | { |
|
|
1258 | //printf ("sending data request etc.\n"); //D |
|
|
1259 | if (!snddq.empty ()) |
|
|
1260 | { |
|
|
1261 | poll_interval = send_interval; |
|
|
1262 | NEXT (NOW + send_interval); |
|
|
1263 | } |
|
|
1264 | |
1170 | send = new dns_snd (this); |
1265 | send = new dns_snd (this); |
1171 | send->gen_stream_req (sndseq, snddq); |
1266 | send->gen_stream_req (sndseq, snddq); |
|
|
1267 | send->timeout = NOW + last_latency * 8.; |
1172 | |
1268 | |
1173 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1269 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1174 | } |
1270 | } |
1175 | |
1271 | |
1176 | if (send) |
1272 | if (send) |
1177 | vpn->dns_sndpq.push_back (send); |
1273 | vpn->dns_sndpq.push_back (send); |
1178 | } |
1274 | } |
1179 | |
1275 | |
1180 | if (send) |
1276 | if (send) |
1181 | { |
1277 | { |
1182 | printf ("send pkt\n"); |
|
|
1183 | last_sent = NOW; |
1278 | last_sent = NOW; |
1184 | |
|
|
1185 | if (!send->retry) |
|
|
1186 | send->sent = NOW; |
|
|
1187 | |
|
|
1188 | sendto (vpn->dnsv4_fd, |
1279 | sendto (vpn->dnsv4_fd, |
1189 | send->pkt->at (0), send->pkt->len, 0, |
1280 | send->pkt->at (0), send->pkt->len, 0, |
1190 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1281 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1191 | } |
1282 | } |
1192 | } |
1283 | } |
1193 | else |
1284 | else |
1194 | NEXT (last_sent + send_interval); |
1285 | NEXT (last_sent + send_interval); |
1195 | |
1286 | |
1196 | printf ("pi %f si %f N %f (%d:%d)\n", poll_interval, send_interval, next - NOW, vpn->dns_sndpq.size (), snddq.size ()); |
1287 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)", |
|
|
1288 | poll_interval, send_interval, next - NOW, |
|
|
1289 | vpn->dns_sndpq.size (), snddq.size ()); |
|
|
1290 | |
|
|
1291 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
|
|
1292 | if (next < NOW + 0.0001) |
|
|
1293 | next = NOW + 0.1; |
1197 | |
1294 | |
1198 | w.start (next); |
1295 | w.start (next); |
1199 | } |
1296 | } |
1200 | |
1297 | |
1201 | #endif |
1298 | #endif |