ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_dns.C
(Generate patch)

Comparing gvpe/src/vpn_dns.C (file contents):
Revision 1.9 by pcg, Fri Mar 4 04:52:38 2005 UTC vs.
Revision 1.26 by pcg, Tue Mar 8 17:25:27 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines