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.38 by pcg, Tue Apr 19 04:23:38 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines