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.40 by pcg, Sat Jul 9 20:29:29 2005 UTC

14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 GNU General Public License for more details.
16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with gvpe; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/ 20*/
21
22// TODO: EDNS0 option to increase dns mtu?
23// TODO: re-write dns packet parsing/creation using a safe mem-buffer
24// to ensure no buffer overflows or similar problems.
21 25
22#include "config.h" 26#include "config.h"
23 27
24#if ENABLE_DNS 28#if ENABLE_DNS
25 29
26// dns processing is EXTREMELY ugly. For obvious(?) reasons. 30// dns processing is EXTREMELY ugly. For obvious(?) reasons.
27// it's a hack, use only in emergency situations please. 31// it's a hack, use only in emergency situations please.
28 32
29#include <cstring> 33#include <cstring>
34#include <cassert>
30 35
31#include <sys/types.h> 36#include <sys/types.h>
32#include <sys/socket.h> 37#include <sys/socket.h>
33#include <sys/wait.h> 38#include <sys/wait.h>
34#include <sys/uio.h> 39#include <sys/uio.h>
43 48
44#include "netcompat.h" 49#include "netcompat.h"
45 50
46#include "vpn.h" 51#include "vpn.h"
47 52
48#define MIN_RETRY 1. 53#define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data
49#define MAX_RETRY 6. 54#define ACTIVITY_INTERVAL 5.
50 55
51#define MAX_OUTSTANDING 400 // max. outstanding requests 56#define INITIAL_TIMEOUT 0.1 // retry timeouts
57#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn
58
59#define MAX_SEND_INTERVAL 2. // optimistic?
60
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 (64*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 240 // 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 request byte less give room for two reply bytes
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 delay; // time in 0.01s units that the server may delay replying packets
430 u8 r3, r4;
431
432 u8 r5, r6, r7, r8;
433
434 void reset (int clientid);
435 bool valid ();
436};
437
438int dns_cfg::next_uid;
439
440void dns_cfg::reset (int clientid)
441{
442 id1 = 'G';
443 id2 = 'V';
444 id3 = 'P';
445 id4 = 'E';
446
447 version = 1;
448
449 rrtype = RR_TYPE_TXT;
450 flags = 0;
451 def_ttl = 0;
452 seq_cdc = 26;
453 req_cdc = 62;
454 rep_cdc = 0;
455 max_size = htons (MAX_PKT_SIZE);
456 client = htons (clientid);
457 uid = next_uid++;
458 delay = 0;
459
460 r3 = r4 = 0;
461 r4 = r5 = r6 = r7 = 0;
462}
463
464bool dns_cfg::valid ()
465{
466 // although the protocol itself allows for some configurability,
467 // only the following encoding/decoding settings are implemented.
468 return id1 == 'G'
469 && id2 == 'V'
470 && id3 == 'P'
471 && id4 == 'E'
472 && seq_cdc == 26
473 && req_cdc == 62
474 && rep_cdc == 0
475 && version == 1;
476}
477
381struct dns_packet : net_packet 478struct dns_packet : net_packet
382{ 479{
383 u16 id; 480 u16 id;
384 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 481 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4
385 u16 qdcount, ancount, nscount, arcount; 482 u16 qdcount, ancount, nscount, arcount;
386 483
387 u8 data[MAXSIZE - 6 * 2]; 484 u8 data [MAXSIZE - 6 * 2];
388 485
389 int decode_label (char *data, int size, int &offs); 486 int decode_label (char *data, int size, int &offs);
390}; 487};
391 488
392int dns_packet::decode_label (char *data, int size, int &offs) 489int dns_packet::decode_label (char *data, int size, int &offs)
423 return data - orig; 520 return data - orig;
424} 521}
425 522
426///////////////////////////////////////////////////////////////////////////// 523/////////////////////////////////////////////////////////////////////////////
427 524
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 525static u16 dns_id = 0; // TODO: should be per-vpn
441 526
442static u16 next_id () 527static u16 next_id ()
443{ 528{
529 if (!dns_id)
530 dns_id = time (0);
531
444 // the simplest lsfr with periodicity 65535 i could find 532 // the simplest lsfr with periodicity 65535 i could find
445 dns_id = (dns_id << 1) 533 dns_id = (dns_id << 1)
446 | (((dns_id >> 1) 534 | (((dns_id >> 1)
447 ^ (dns_id >> 2) 535 ^ (dns_id >> 2)
448 ^ (dns_id >> 4) 536 ^ (dns_id >> 4)
449 ^ (dns_id >> 15)) & 1); 537 ^ (dns_id >> 15)) & 1);
450 538
451 return dns_id; 539 return dns_id;
452} 540}
453 541
542struct dns_rcv;
543struct dns_snd;
544
545struct dns_connection
546{
547 connection *c;
548 struct vpn *vpn;
549
550 dns_cfg cfg;
551
552 bool established;
553
554 tstamp last_received;
555 tstamp last_sent;
556 double min_latency;
557 double poll_interval, send_interval;
558
559 vector<dns_rcv *> rcvpq;
560
561 byte_stream rcvdq; int rcvseq; int repseq;
562 byte_stream snddq; int sndseq;
563
564 void time_cb (time_watcher &w); time_watcher tw;
565 void receive_rep (dns_rcv *r);
566
567 dns_connection (connection *c);
568 ~dns_connection ();
569};
570
571struct dns_snd
572{
573 dns_packet *pkt;
574 tstamp timeout, sent;
575 int retry;
576 struct dns_connection *dns;
577 int seqno;
578 bool stdhdr;
579
580 void gen_stream_req (int seqno, byte_stream &stream);
581 void gen_syn_req ();
582
583 dns_snd (dns_connection *dns);
584 ~dns_snd ();
585};
586
454dns_req::dns_req (dns_connection *dns) 587dns_snd::dns_snd (dns_connection *dns)
455: dns (dns) 588: dns (dns)
456{ 589{
457 next = 0; 590 timeout = 0;
458 retry = 0; 591 retry = 0;
592 seqno = 0;
593 sent = NOW;
594 stdhdr = false;
459 595
460 pkt = new dns_packet; 596 pkt = new dns_packet;
461 597
462 pkt->id = next_id (); 598 pkt->id = next_id ();
463} 599}
464 600
601dns_snd::~dns_snd ()
602{
603 delete pkt;
604}
605
606static void append_domain (dns_packet &pkt, int &offs, const char *domain)
607{
608 // add tunnel domain
609 for (;;)
610 {
611 const char *end = strchr (domain, '.');
612
613 if (!end)
614 end = domain + strlen (domain);
615
616 int len = end - domain;
617
618 pkt [offs++] = len;
619 memcpy (pkt.at (offs), domain, len);
620 offs += len;
621
622 if (!*end)
623 break;
624
625 domain = end + 1;
626 }
627}
628
465void dns_req::gen_stream_req (int seqno, byte_stream &stream) 629void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
466{ 630{
631 stdhdr = true;
467 this->seqno = seqno; 632 this->seqno = seqno;
633
634 timeout = NOW + INITIAL_TIMEOUT;
468 635
469 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 636 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
470 pkt->qdcount = htons (1); 637 pkt->qdcount = htons (1);
471 638
472 int offs = 6*2; 639 int offs = 6*2;
473 int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); 640 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, 641 // 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 642 // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra
476 643
477 char enc[256], *encp = enc; 644 char enc[256], *encp = enc;
478 encode_header (enc, THISNODE->id, seqno); 645 encode_header (enc, THISNODE->id, seqno);
496 encp += lbllen; 663 encp += lbllen;
497 664
498 enclen -= lbllen; 665 enclen -= lbllen;
499 } 666 }
500 667
501 const char *suffix = THISNODE->domain; 668 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 669
523 (*pkt)[offs++] = 0; 670 (*pkt)[offs++] = 0;
524 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; 671 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY;
672 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
673
674 pkt->len = offs;
675}
676
677void dns_snd::gen_syn_req ()
678{
679 timeout = NOW + INITIAL_SYN_TIMEOUT;
680
681 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
682 pkt->qdcount = htons (1);
683
684 int offs = 6 * 2;
685
686 int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg));
687
688 assert (elen <= MAX_LBL_SIZE);
689
690 (*pkt)[offs] = elen;
691 offs += elen + 1;
692 append_domain (*pkt, offs, dns->c->conf->domain);
693
694 (*pkt)[offs++] = 0;
695 (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A;
525 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 696 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
526 697
527 pkt->len = offs; 698 pkt->len = offs;
528} 699}
529 700
549 delete pkt; 720 delete pkt;
550} 721}
551 722
552///////////////////////////////////////////////////////////////////////////// 723/////////////////////////////////////////////////////////////////////////////
553 724
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) 725dns_connection::dns_connection (connection *c)
575: c (c) 726: c (c)
576, rcvdq (MAX_BACKLOG * 2) 727, rcvdq (MAX_BACKLOG * 2)
577, snddq (MAX_BACKLOG * 2) 728, snddq (MAX_BACKLOG)
578, tw (this, &dns_connection::time_cb) 729, tw (this, &dns_connection::time_cb)
579{ 730{
580 vpn = c->vpn; 731 vpn = c->vpn;
581 732
733 established = false;
734
582 rcvseq = sndseq = 0; 735 rcvseq = repseq = sndseq = 0;
736
737 last_sent = last_received = 0;
738 poll_interval = 0.5; // starting here
739 send_interval = 0.5; // starting rate
740 min_latency = INITIAL_TIMEOUT;
583} 741}
584 742
585dns_connection::~dns_connection () 743dns_connection::~dns_connection ()
586{ 744{
587 for (vector<dns_rcv *>::iterator i = rcvpq.begin (); 745 for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
588 i != rcvpq.end (); 746 i != rcvpq.end ();
589 ++i) 747 ++i)
590 delete *i; 748 delete *i;
591} 749}
592 750
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) 751void dns_connection::receive_rep (dns_rcv *r)
603{ 752{
753 if (r->datalen)
754 {
755 last_received = NOW;
756 tw.trigger ();
757
758 poll_interval = send_interval;
759 }
760 else
761 {
762 poll_interval *= 1.5;
763
764 if (poll_interval > MAX_POLL_INTERVAL)
765 poll_interval = MAX_POLL_INTERVAL;
766 }
767
604 rcvpq.push_back (r); 768 rcvpq.push_back (r);
605 769
606 redo: 770 redo:
607 771
608 // find next packet 772 // find next packet
622 } 786 }
623 787
624 rcvseq = (rcvseq + 1) & SEQNO_MASK; 788 rcvseq = (rcvseq + 1) & SEQNO_MASK;
625 789
626 if (!rcvdq.put (r->data, r->datalen)) 790 if (!rcvdq.put (r->data, r->datalen))
791 {
792 slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)");
627 abort (); // MUST never overflow, can be caused by data corruption, TODO 793 abort (); // MUST never overflow, can be caused by data corruption, TODO
794 }
628 795
629 while (vpn_packet *pkt = rcvdq.get ()) 796 while (vpn_packet *pkt = rcvdq.get ())
630 { 797 {
631 sockinfo si; 798 sockinfo si;
632 si.host = 0; si.port = 0; si.prot = PROT_DNSv4; 799 si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4;
633 800
634 vpn->recv_vpn_packet (pkt, si); 801 vpn->recv_vpn_packet (pkt, si);
802
803 delete pkt;
635 } 804 }
636 805
637 // check for further packets 806 // check for further packets
638 goto redo; 807 goto redo;
639 } 808 }
640} 809}
641 810
642dns_packet * 811void
643vpn::dnsv4_server (dns_packet *pkt) 812vpn::dnsv4_server (dns_packet &pkt)
644{ 813{
645 u16 flags = ntohs (pkt->flags); 814 u16 flags = ntohs (pkt.flags);
646 815
647 //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len);
648 int offs = 6 * 2; // skip header 816 int offs = 6 * 2; // skip header
649 817
650 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 818 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
651 819
652 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) 820 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK))
653 && pkt->qdcount == htons (1)) 821 && pkt.qdcount == htons (1))
654 { 822 {
655 char qname[MAXSIZE]; 823 char qname [MAXSIZE];
656 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 824 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
657 825
658 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; 826 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
659 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; 827 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
660 828
661 pkt->qdcount = htons (1); 829 pkt.qdcount = htons (1);
662 pkt->ancount = 0; 830 pkt.ancount = 0;
663 pkt->nscount = 0; // should be self, as other nameservers reply like this 831 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 832 pkt.arcount = 0; // a record for self, as other nameservers reply like this
665 833
666 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); 834 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL);
667 835
668 int dlen = strlen (THISNODE->domain); 836 int dlen = strlen (THISNODE->domain);
669 837
670 if (qclass == RR_CLASS_IN 838 if (qclass == RR_CLASS_IN
671 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT)
672 && qlen > dlen + 1 + HDRSIZE 839 && qlen > dlen + 1
673 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) 840 && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen))
674 { 841 {
675 // correct class, domain: parse 842 // now generate reply
676 int client, seqno; 843 pkt.ancount = htons (1); // one answer RR
677 decode_header (qname, client, seqno); 844 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
678 845
679 u8 data[MAXSIZE]; 846 if ((qtype == RR_TYPE_ANY
680 int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); 847 || qtype == RR_TYPE_TXT
681 848 || qtype == RR_TYPE_NULL)
682 if (0 < client && client <= conns.size ()) 849 && qlen > dlen + 1 + HDRSIZE)
683 { 850 {
851 // correct class, domain: parse
852 int client, seqno;
853 decode_header (qname, client, seqno);
854
855 u8 data[MAXSIZE];
856 int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE));
857
858 if (0 < client && client <= conns.size ())
859 {
684 connection *c = conns [client - 1]; 860 connection *c = conns [client - 1];
685
686 if (!c->dns)
687 c->dns = new dns_connection (c);
688
689 dns_connection *dns = c->dns; 861 dns_connection *dns = c->dns;
862 dns_rcv *rcv;
690 863
864 if (dns)
865 {
691 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) 866 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); )
692 if (SEQNO_EQ ((*i)->seqno, seqno)) 867 if (SEQNO_EQ ((*i)->seqno, seqno))
868 {
869 // already seen that request: simply reply with the cached reply
870 dns_rcv *r = *i;
871
872 slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno);
873
874 // refresh header & id, as the retry count could have changed
875 memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE);
876 r->pkt->id = pkt.id;
877
878 memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len);
879
880 goto duplicate_request;
881 }
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 if (dns)
900 {
901 int dlen = ntohs (dns->cfg.max_size) - offs;
902
903 // bind doesn't compress well, so reduce further by one label length
904 dlen -= qlen;
905
906 // only put data into in-order sequence packets, if
907 // we receive out-of-order packets we generate empty
908 // replies
909 //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D
910 if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW)
911 {
912 dns->repseq = seqno;
913
914 while (dlen > 1 && !dns->snddq.empty ())
915 {
916 int txtlen = dlen <= 255 ? dlen - 1 : 255;
917
918 if (txtlen > dns->snddq.size ())
919 txtlen = dns->snddq.size ();
920
921 pkt[offs++] = txtlen;
922 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen);
923 offs += txtlen;
924 dns->snddq.remove (txtlen);
925
926 dlen -= txtlen + 1;
927 }
928 }
929
930 // avoid completely empty TXT rdata
931 if (offs == rdlen_offs)
932 pkt[offs++] = 0;
933
934 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
935 }
936 else
937 {
938 // send RST
939 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
940 pkt [offs++] = CMD_IP_RST;
941 }
942
943 int rdlen = offs - rdlen_offs;
944
945 pkt [rdlen_offs - 2] = rdlen >> 8;
946 pkt [rdlen_offs - 1] = rdlen;
947
948 if (dns)
949 {
950 // now update dns_rcv copy
951 rcv->pkt->len = offs;
952 memcpy (rcv->pkt->at (0), pkt.at (0), offs);
953 }
702 } 954 }
703 955
704 // new packet, queue 956 duplicate_request: ;
705 dns_rcv *rcv = new dns_rcv (seqno, data, datalen); 957 }
706 dns->receive_rep (rcv); 958 else
707
708 // now generate reply
709 pkt->ancount = htons (1); // one answer RR
710 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); 959 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
960 }
961 else if (qtype == RR_TYPE_A
962 && qlen > dlen + 1 + cdc26.encode_len (sizeof (dns_cfg)))
963 {
964 dns_cfg cfg;
965 cdc26.decode ((u8 *)&cfg, qname, cdc26.encode_len (sizeof (dns_cfg)));
966 int client = ntohs (cfg.client);
711 967
712 (*pkt) [offs++] = 0xc0;
713 (*pkt) [offs++] = 6 * 2; // same as in query section 968 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
714 969
715 (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; 970 pkt [offs++] = RR_TYPE_A >> 8; pkt [offs++] = RR_TYPE_A; // type
716 (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; 971 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
717
718 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; 972 pkt [offs++] = 0; pkt [offs++] = 0;
719 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL 973 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
974 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
720 975
721 int dlen = MAX_PKT_SIZE - offs - 2; 976 slog (L_INFO, _("DNS: client %d connects"), client);
722 977
723 // bind doesn't compress well, so reduce further by one label length 978 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
724 dlen -= qlen; 979 pkt [offs++] = CMD_IP_REJ;
725 980
726 int rdlen_offs = offs += 2; 981 if (0 < client && client <= conns.size ())
727
728 while (dlen > 1 && !dns->snddq.empty ())
729 { 982 {
730 int txtlen = dlen <= 255 ? dlen - 1 : 255; 983 connection *c = conns [client - 1];
731 984
732 if (txtlen > dns->snddq.size ()) 985 if (cfg.valid ())
733 txtlen = dns->snddq.size (); 986 {
987 pkt [offs - 1] = CMD_IP_SYN;
734 988
735 (*pkt)[offs++] = txtlen; 989 delete c->dns;
736 memcpy (pkt->at (offs), dns->snddq.begin (), txtlen); 990 c->dns = new dns_connection (c);
737 offs += txtlen; 991 c->dns->cfg = cfg;
738 dns->snddq.remove (txtlen); 992 }
739
740 dlen -= txtlen + 1;
741 } 993 }
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 } 994 }
758 else
759 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
760 } 995 }
761 996
762 pkt->len = offs; 997 pkt.len = offs;
763 } 998 }
764
765 return pkt;
766} 999}
767 1000
768void 1001void
769vpn::dnsv4_client (dns_packet *pkt) 1002vpn::dnsv4_client (dns_packet &pkt)
770{ 1003{
771 u16 flags = ntohs (pkt->flags); 1004 u16 flags = ntohs (pkt.flags);
772 int offs = 6 * 2; // skip header 1005 int offs = 6 * 2; // skip header
773 1006
774 pkt->qdcount = ntohs (pkt->qdcount); 1007 pkt.qdcount = ntohs (pkt.qdcount);
775 pkt->ancount = ntohs (pkt->ancount); 1008 pkt.ancount = ntohs (pkt.ancount);
776 1009
777 // go through our request list and find the corresponding request 1010 // go through our request list and find the corresponding request
778 for (vector<dns_req *>::iterator i = dns_sndpq.begin (); 1011 for (vector<dns_snd *>::iterator i = dns_sndpq.begin ();
779 i != dns_sndpq.end (); 1012 i != dns_sndpq.end ();
780 ++i) 1013 ++i)
781 if ((*i)->pkt->id == pkt->id) 1014 if ((*i)->pkt->id == pkt.id)
782 { 1015 {
783 dns_connection *dns = (*i)->dns; 1016 dns_connection *dns = (*i)->dns;
1017 connection *c = dns->c;
784 int seqno = (*i)->seqno; 1018 int seqno = (*i)->seqno;
785 u8 data[MAXSIZE], *datap = data; 1019 u8 data[MAXSIZE], *datap = data;
786 1020
1021 if ((*i)->retry)
1022 {
1023 dns->send_interval *= 1.01;
1024 if (dns->send_interval > MAX_SEND_INTERVAL)
1025 dns->send_interval = MAX_SEND_INTERVAL;
1026 }
1027 else
1028 {
1029#if 0
1030 dns->send_interval *= 0.999;
1031#endif
1032 // the latency surely puts an upper bound on
1033 // the minimum send interval
1034 double latency = NOW - (*i)->sent;
1035
1036 if (latency < dns->min_latency)
1037 dns->min_latency = latency;
1038
1039 if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor)
1040 dns->send_interval = dns->min_latency * conf.dns_overlap_factor;
1041
1042 if (dns->send_interval < conf.dns_send_interval)
1043 dns->send_interval = conf.dns_send_interval;
1044 }
1045
787 delete *i; 1046 delete *i;
788 dns_sndpq.erase (i); 1047 dns_sndpq.erase (i);
789 1048
790 if (flags & FLAG_RESPONSE && !(flags & (FLAG_OP_MASK | FLAG_TC))) 1049 if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK))
791 { 1050 {
792 char qname[MAXSIZE]; 1051 char qname[MAXSIZE];
793 1052
794 while (pkt->qdcount-- && offs < MAXSIZE - 4) 1053 while (pkt.qdcount-- && offs < MAXSIZE - 4)
795 { 1054 {
796 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 1055 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
797 offs += 4; // skip qtype, qclass 1056 offs += 4; // skip qtype, qclass
798 } 1057 }
799 1058
800 while (pkt->ancount-- && offs < MAXSIZE - 10 && datap) 1059 while (pkt.ancount-- && offs < MAXSIZE - 10 && datap)
801 { 1060 {
802 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 1061 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
803 1062
804 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; 1063 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
805 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; 1064 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
806 u32 ttl = (*pkt) [offs++] << 24; 1065 u32 ttl = pkt [offs++] << 24;
807 ttl |= (*pkt) [offs++] << 16; 1066 ttl |= pkt [offs++] << 16;
808 ttl |= (*pkt) [offs++] << 8; 1067 ttl |= pkt [offs++] << 8;
809 ttl |= (*pkt) [offs++]; 1068 ttl |= pkt [offs++];
810
811 u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; 1069 u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++];
812 1070
813 if (rdlen <= MAXSIZE - offs) 1071 if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT)
814 { 1072 {
815 // decode bytes, finally 1073 if (rdlen <= MAXSIZE - offs)
816
817 while (rdlen)
818 { 1074 {
1075 // decode bytes, finally
1076
1077 while (rdlen)
1078 {
819 int txtlen = (*pkt) [offs++]; 1079 int txtlen = pkt [offs++];
820 1080
821 assert (txtlen + offs < MAXSIZE - 1); 1081 assert (txtlen + offs < MAXSIZE - 1);
822 1082
823 memcpy (datap, pkt->at (offs), txtlen); 1083 memcpy (datap, pkt.at (offs), txtlen);
824 datap += txtlen; offs += txtlen; 1084 datap += txtlen; offs += txtlen;
825 1085
826 rdlen -= txtlen + 1; 1086 rdlen -= txtlen + 1;
1087 }
827 } 1088 }
828 } 1089 }
1090 else if (qtype == RR_TYPE_A)
1091 {
1092 u8 ip [4];
1093
1094 ip [0] = pkt [offs++];
1095 ip [1] = pkt [offs++];
1096 ip [2] = pkt [offs++];
1097 ip [3] = pkt [offs++];
1098
1099 if (ip [0] == CMD_IP_1
1100 && ip [1] == CMD_IP_2
1101 && ip [2] == CMD_IP_3)
1102 {
1103 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]);
1104
1105 if (ip [3] == CMD_IP_RST)
1106 {
1107 slog (L_DEBUG, _("DNS: got tunnel RST request"));
1108
1109 delete dns; c->dns = 0;
1110
1111 return;
1112 }
1113 else if (ip [3] == CMD_IP_SYN)
1114 {
1115 slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us."));
1116 dns->established = true;
1117 }
1118 else if (ip [3] == CMD_IP_REJ)
1119 {
1120 slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting."));
1121 abort ();
1122 }
1123 else
1124 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]);
1125 }
1126 else
1127 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"),
1128 ip [0], ip [1], ip [2], ip [3]);
1129
1130 return;
1131 }
829 1132
830 int client, rseqno; 1133 int client, rseqno;
831 decode_header (qname, client, rseqno); 1134 decode_header (qname, client, rseqno);
832 1135
833 if (client != THISNODE->id) 1136 if (client != THISNODE->id)
834 { 1137 {
835 slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); 1138 slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring"));
836 datap = 0; 1139 datap = 0;
837 } 1140 }
838 else if (rseqno != seqno) 1141 else if (rseqno != seqno)
839 { 1142 {
840 slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?")); 1143 slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?"));
841 datap = 0; 1144 datap = 0;
842 } 1145 }
843 } 1146 }
844 } 1147 }
845 1148
847 if (datap) 1150 if (datap)
848 dns->receive_rep (new dns_rcv (seqno, data, datap - data)); 1151 dns->receive_rep (new dns_rcv (seqno, data, datap - data));
849 1152
850 break; 1153 break;
851 } 1154 }
852
853 delete pkt;
854} 1155}
855 1156
856void 1157void
857vpn::dnsv4_ev (io_watcher &w, short revents) 1158vpn::dnsv4_ev (io_watcher &w, short revents)
858{ 1159{
860 { 1161 {
861 dns_packet *pkt = new dns_packet; 1162 dns_packet *pkt = new dns_packet;
862 struct sockaddr_in sa; 1163 struct sockaddr_in sa;
863 socklen_t sa_len = sizeof (sa); 1164 socklen_t sa_len = sizeof (sa);
864 1165
865 pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 1166 pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
866 1167
867 if (pkt->len > 0) 1168 if (pkt->len > 0)
868 { 1169 {
869 if (pkt->flags & htons (FLAG_TC)) 1170 if (ntohs (pkt->flags) & FLAG_RESPONSE)
1171 dnsv4_client (*pkt);
1172 else
870 { 1173 {
871 slog (L_WARN, _("DNS request/response truncated, check protocol settings.")); 1174 dnsv4_server (*pkt);
872 //TODO connection reset 1175 sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len);
873 } 1176 }
874 1177
875 if (THISNODE->dns_port) 1178 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 } 1179 }
883 } 1180 }
884} 1181}
885 1182
886bool 1183bool
887connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1184vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
888{ 1185{
1186 int client = ntohl (si.host);
1187
1188 assert (0 < client && client <= conns.size ());
1189
1190 connection *c = conns [client - 1];
1191
889 if (!dns) 1192 if (!c->dns)
890 dns = new dns_connection (this); 1193 c->dns = new dns_connection (c);
891 1194
892 if (!dns->snddq.put (pkt)) 1195 if (c->dns->snddq.put (pkt))
893 return false;
894
895 // start timer if neccessary
896 if (!THISNODE->dns_port && !dns->tw.active)
897 dns->tw.trigger (); 1196 c->dns->tw.trigger ();
898 1197
1198 // always return true even if the buffer overflows
899 return true; 1199 return true;
900} 1200}
1201
1202void
1203connection::dnsv4_reset_connection ()
1204{
1205 //delete dns; dns = 0; //TODO
1206}
1207
1208#define NEXT(w) do { if (next > (w)) next = w; } while (0)
901 1209
902void 1210void
903dns_connection::time_cb (time_watcher &w) 1211dns_connection::time_cb (time_watcher &w)
904{ 1212{
1213 // servers have to be polled
1214 if (THISNODE->dns_port)
1215 return;
1216
905 // check for timeouts and (re)transmit 1217 // check for timeouts and (re)transmit
906 tstamp next = NOW + 60; 1218 tstamp next = NOW + poll_interval;
907 dns_req *send = 0; 1219 dns_snd *send = 0;
908 1220
909 for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); 1221 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
910 i != vpn->dns_sndpq.end (); 1222 i != vpn->dns_sndpq.end ();
911 ++i) 1223 ++i)
912 { 1224 {
913 dns_req *r = *i; 1225 dns_snd *r = *i;
914 1226
915 if (r->next <= NOW) 1227 if (r->timeout <= NOW)
916 { 1228 {
917 if (!send) 1229 if (!send)
918 { 1230 {
919 send = r; 1231 send = r;
920 1232
921 if (r->retry)//D
922 printf ("req %d:%d, retry %d\n", r->seqno, r->pkt->id, r->retry);
923 r->retry++; 1233 r->retry++;
924 r->next = NOW + r->retry; 1234 r->timeout = NOW + (r->retry * min_latency * conf.dns_timeout_factor);
1235
1236 // the following code changes the query section a bit, forcing
1237 // the forwarder to generate a new request
1238 if (r->stdhdr)
1239 {
1240 //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry);
1241 //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry);
1242 }
925 } 1243 }
926 } 1244 }
927 1245 else
928 if (r->next < next) 1246 NEXT (r->timeout);
929 next = r->next;
930 } 1247 }
931 1248
932 if (!send 1249 if (!send)
933 && vpn->dns_sndpq.size () < MAX_OUTSTANDING)
934 { 1250 {
1251 // generate a new packet, if wise
1252
1253 if (!established)
1254 {
1255 if (vpn->dns_sndpq.empty ())
1256 {
935 send = new dns_req (this); 1257 send = new dns_snd (this);
1258
1259 cfg.reset (THISNODE->id);
1260 send->gen_syn_req ();
1261 }
1262 }
1263 else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding
1264 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
1265 {
1266 if (last_sent + send_interval <= NOW)
1267 {
1268 //printf ("sending data request etc.\n"); //D
1269 if (!snddq.empty () || last_received + 1. > NOW)
1270 {
1271 poll_interval = send_interval;
1272 NEXT (NOW + send_interval);
1273 }
1274
1275 send = new dns_snd (this);
936 send->gen_stream_req (sndseq, snddq); 1276 send->gen_stream_req (sndseq, snddq);
1277 send->timeout = NOW + min_latency * conf.dns_timeout_factor;
1278
1279 sndseq = (sndseq + 1) & SEQNO_MASK;
1280 }
1281 else
1282 NEXT (last_sent + send_interval);
1283 }
1284
1285 if (send)
937 vpn->dns_sndpq.push_back (send); 1286 vpn->dns_sndpq.push_back (send);
938
939 sndseq = (sndseq + 1) & SEQNO_MASK;
940 } 1287 }
941
942 tstamp min_next = NOW + (1. / (tstamp)MAX_RATE);
943 1288
944 if (send) 1289 if (send)
945 { 1290 {
946 dns_packet *pkt = send->pkt; 1291 last_sent = NOW;
947 1292 sendto (vpn->dnsv4_fd,
948 next = min_next; 1293 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 ()); 1294 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
952 } 1295 }
953 else if (next < min_next) 1296
954 next = min_next; 1297 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)",
1298 poll_interval, send_interval, next - NOW,
1299 vpn->dns_sndpq.size (), snddq.size (),
1300 rcvpq.size ());
1301
1302 // TODO: no idea when this happens, but when next < NOW, we have a problem
1303 // doesn't seem to happen anymore
1304 if (next < NOW + 0.001)
1305 next = NOW + 0.1;
955 1306
956 w.start (next); 1307 w.start (next);
957} 1308}
958 1309
959#endif 1310#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines