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.47 by pcg, Thu Aug 7 17:54:27 2008 UTC vs.
Revision 1.51 by root, Sun Mar 6 19:40:28 2011 UTC

1/* 1/*
2 vpn_dns.C -- handle the dns tunnel part of the protocol. 2 vpn_dns.C -- handle the dns tunnel part of the protocol.
3 Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2011 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify it 7 GVPE is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the 8 under the terms of the GNU General Public License as published by the
59 59
60#include "netcompat.h" 60#include "netcompat.h"
61 61
62#include "vpn.h" 62#include "vpn.h"
63 63
64#define MIN_POLL_INTERVAL 0.1 // poll at most this often when no data received
64#define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data 65#define MAX_POLL_INTERVAL 1. // how often to poll minimally when the server has no data
65#define ACTIVITY_INTERVAL 5.
66 66
67#define INITIAL_TIMEOUT 0.1 // retry timeouts 67#define INITIAL_TIMEOUT 0.1 // retry timeouts
68#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn 68#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn
69 69
70#define MAX_SEND_INTERVAL 2. // optimistic? 70#define MAX_SEND_INTERVAL 2. // optimistic?
83#define MAX_PKT_SIZE 512 83#define MAX_PKT_SIZE 512
84 84
85#define RR_TYPE_A 1 85#define RR_TYPE_A 1
86#define RR_TYPE_NULL 10 86#define RR_TYPE_NULL 10
87#define RR_TYPE_TXT 16 87#define RR_TYPE_TXT 16
88#define RR_TYPE_AAAA 28
88#define RR_TYPE_ANY 255 89#define RR_TYPE_ANY 255
89 90
90#define RR_CLASS_IN 1 91#define RR_CLASS_IN 1
91 92
92#define CMD_IP_1 207 93#define CMD_IP_1 207
93#define CMD_IP_2 46 94#define CMD_IP_2 46
94#define CMD_IP_3 236 95#define CMD_IP_3 236
95#define CMD_IP_RST 29 96
96#define CMD_IP_SYN 113 97#define CMD_IP_RST 29 // some error, reset and retry
97#define CMD_IP_REJ 32 98#define CMD_IP_REJ 32 // do not want you
99#define CMD_IP_SYN 113 // connection established
100#define CMD_IP_CSE 213 // connection established, but likely case mismatch
101
102static bool
103is_uc (char c)
104{
105 return 'A' <= c && c <= 'Z';
106}
107
108static bool
109is_lc (char c)
110{
111 return 'a' <= c && c <= 'z';
112}
98 113
99// works for cmaps up to 255 (not 256!) 114// works for cmaps up to 255 (not 256!)
100struct charmap 115struct charmap
101{ 116{
102 enum { INVALID = (u8)255 }; 117 enum { INVALID = (u8)255 };
116 memset (enc, (char) 0, 256); 131 memset (enc, (char) 0, 256);
117 memset (dec, (char)INVALID, 256); 132 memset (dec, (char)INVALID, 256);
118 133
119 for (size = 0; cmap [size]; size++) 134 for (size = 0; cmap [size]; size++)
120 { 135 {
136 char c = cmap [size];
137
121 enc [size] = cmap [size]; 138 enc [size] = c;
122 dec [(u8)enc [size]] = size; 139 dec [(u8)c] = size;
140
141 // allow lowercase/uppercase aliases if possible
142 if (is_uc (c) && dec [c + ('a' - 'A')] == INVALID) dec [c + ('a' - 'A')] = size;
143 if (is_lc (c) && dec [c - ('a' - 'A')] == INVALID) dec [c - ('a' - 'A')] = size;
123 } 144 }
124 145
125 assert (size < 256); 146 assert (size < 256);
126} 147}
127 148
134{ 155{
135 charmap cmap; 156 charmap cmap;
136 unsigned int enc_len [MAX_DEC_LEN]; 157 unsigned int enc_len [MAX_DEC_LEN];
137 unsigned int dec_len [MAX_ENC_LEN]; 158 unsigned int dec_len [MAX_ENC_LEN];
138 159
139 unsigned int encode_len (unsigned int len); 160 unsigned int encode_len (unsigned int len) const;
140 unsigned int decode_len (unsigned int len); 161 unsigned int decode_len (unsigned int len) const;
141 162
142 unsigned int encode (char *dst, u8 *src, unsigned int len); 163 unsigned int encode (char *dst, u8 *src, unsigned int len) const;
143 unsigned int decode (u8 *dst, char *src, unsigned int len); 164 unsigned int decode (u8 *dst, char *src, unsigned int len) const;
144 165
145 basecoder (const char *cmap); 166 basecoder (const char *cmap);
146}; 167};
147 168
148basecoder::basecoder (const char *cmap) 169basecoder::basecoder (const char *cmap)
167 enc_len [len] = n; 188 enc_len [len] = n;
168 dec_len [n] = len; 189 dec_len [n] = len;
169 } 190 }
170} 191}
171 192
193unsigned int
172unsigned int basecoder::encode_len (unsigned int len) 194basecoder::encode_len (unsigned int len) const
173{ 195{
174 return enc_len [len]; 196 return enc_len [len];
175} 197}
176 198
199unsigned int
177unsigned int basecoder::decode_len (unsigned int len) 200basecoder::decode_len (unsigned int len) const
178{ 201{
179 while (len && !dec_len [len]) 202 while (len && !dec_len [len])
180 --len; 203 --len;
181 204
182 return dec_len [len]; 205 return dec_len [len];
183} 206}
184 207
208unsigned int
185unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) 209basecoder::encode (char *dst, u8 *src, unsigned int len) const
186{ 210{
187 if (!len || len > MAX_DEC_LEN) 211 if (!len || len > MAX_DEC_LEN)
188 return 0; 212 return 0;
189 213
190 int elen = encode_len (len); 214 int elen = encode_len (len);
209 *dst++ = cmap.encode [dst_ [i]]; 233 *dst++ = cmap.encode [dst_ [i]];
210 234
211 return elen; 235 return elen;
212} 236}
213 237
238unsigned int
214unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) 239basecoder::decode (u8 *dst, char *src, unsigned int len) const
215{ 240{
216 if (!len || len > MAX_ENC_LEN) 241 if (!len || len > MAX_ENC_LEN)
217 return 0; 242 return 0;
218 243
219 u8 src_ [MAX_ENC_LEN]; 244 u8 src_ [MAX_ENC_LEN];
268 } 293 }
269 abort (); 294 abort ();
270} 295}
271#endif 296#endif
272 297
273//static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-");
274//static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
275static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); 298static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); // a-zA-Z0-9
276//static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet 299static basecoder cdc36 ("dPhZr06QmJkB34tSvL81xAeF92wGyO57uCnI"); // a-z0-9 for case-changers
277static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); 300static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyOuCnI"); // a-z
278 301
279///////////////////////////////////////////////////////////////////////////// 302/////////////////////////////////////////////////////////////////////////////
280 303
281#define HDRSIZE 6 304#define HDRSIZE 5
282 305
306inline void
283inline void encode_header (char *data, int clientid, int seqno, int retry = 0) 307encode_header (char *data, int clientid, int seqno, int retry = 0)
284{ 308{
309 assert (clientid < 256);
310
285 seqno &= SEQNO_MASK; 311 seqno &= SEQNO_MASK;
286 312
287 u8 hdr[3] = { 313 u8 hdr[3] = {
314 seqno,
315 (seqno >> 8) | (retry << 6),
288 clientid, 316 clientid,
289 (seqno >> 8) | (retry << 6),
290 seqno,
291 }; 317 };
292 318
293 assert (clientid < 256);
294
295 cdc26.encode (data, hdr, 3); 319 cdc36.encode (data, hdr, 3);
296} 320}
297 321
322inline void
298inline void decode_header (char *data, int &clientid, int &seqno) 323decode_header (char *data, int &clientid, int &seqno)
299{ 324{
300 u8 hdr[3]; 325 u8 hdr[3];
301 326
302 cdc26.decode (hdr, data, HDRSIZE); 327 cdc36.decode (hdr, data, HDRSIZE);
303 328
304 clientid = hdr[0]; 329 clientid = hdr[2];
305 seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK; 330 seqno = ((hdr[1] << 8) | hdr[0]) & SEQNO_MASK;
306} 331}
307 332
308///////////////////////////////////////////////////////////////////////////// 333/////////////////////////////////////////////////////////////////////////////
309 334
310struct byte_stream 335struct byte_stream
336byte_stream::~byte_stream () 361byte_stream::~byte_stream ()
337{ 362{
338 delete data; 363 delete data;
339} 364}
340 365
366void
341void byte_stream::remove (int count) 367byte_stream::remove (int count)
342{ 368{
343 if (count > fill) 369 if (count > fill)
344 assert (count <= fill); 370 assert (count <= fill);
345 371
346 memmove (data, data + count, fill -= count); 372 memmove (data, data + count, fill -= count);
347} 373}
348 374
375bool
349bool byte_stream::put (u8 *data, unsigned int datalen) 376byte_stream::put (u8 *data, unsigned int datalen)
350{ 377{
351 if (maxsize - fill < datalen) 378 if (maxsize - fill < datalen)
352 return false; 379 return false;
353 380
354 memcpy (this->data + fill, data, datalen); fill += datalen; 381 memcpy (this->data + fill, data, datalen); fill += datalen;
355 382
356 return true; 383 return true;
357} 384}
358 385
386bool
359bool byte_stream::put (vpn_packet *pkt) 387byte_stream::put (vpn_packet *pkt)
360{ 388{
361 if (maxsize - fill < pkt->len + 2) 389 if (maxsize - fill < pkt->len + 2)
362 return false; 390 return false;
363 391
364 data [fill++] = pkt->len >> 8; 392 data [fill++] = pkt->len >> 8;
420 448
421struct dns_cfg 449struct dns_cfg
422{ 450{
423 static int next_uid; 451 static int next_uid;
424 452
425 u8 id1, id2, id3, id4; 453 u8 chksum;
454 u8 rrtype;
455 u16 uid; // to make request unique
426 456
427 u8 version; 457 u8 version;
428 u8 flags; 458 u8 flags;
429 u8 rrtype; 459 u16 max_size;
460
461 u8 id1, id2, id3, id4;
462
463 u16 client;
430 u8 def_ttl; 464 u8 def_ttl;
465 u8 r0;
431 466
432 u16 client; 467 u8 syn_cdc; // cdc en/decoder for syn (A?) requests
433 u16 uid; // to make request unique 468 u8 hdr_cdc; // cdc en/decoder for regular request headers
469 u8 req_cdc; // cdc en/decoder for regular (ANY?) request data
470 u8 rep_cdc; // cdc en/decoder for regular (TXT) replies, 0 == 8 bit encoding
434 471
435 u16 max_size;
436 u8 seq_cdc;
437 u8 req_cdc;
438
439 u8 rep_cdc;
440 u8 delay; // time in 0.01s units that the server may delay replying packets
441 u8 r3, r4;
442
443 u8 r5, r6, r7, r8; 472 u8 r1, r2, r3, r4;
444 473
445 void reset (int clientid); 474 void reset (int clientid);
446 bool valid (); 475 bool valid ();
476 u8 get_chksum ();
447}; 477};
448 478
449int dns_cfg::next_uid; 479int dns_cfg::next_uid;
450 480
481void
451void dns_cfg::reset (int clientid) 482dns_cfg::reset (int clientid)
452{ 483{
484 // this ID must result in some mixed-case characters in cdc26-encoding
453 id1 = 'G'; 485 id1 = 'G';
454 id2 = 'V'; 486 id2 = 'V';
455 id3 = 'P'; 487 id3 = 'P';
456 id4 = 'E'; 488 id4 = 'E';
457 489
458 version = 1; 490 version = 2;
459 491
460 rrtype = RR_TYPE_TXT; 492 rrtype = RR_TYPE_TXT;
461 flags = 0; 493 flags = 0;
462 def_ttl = 0; 494 def_ttl = 0;
463 seq_cdc = 26; 495 syn_cdc = 26;
464 req_cdc = 62; 496 hdr_cdc = 36;
497 req_cdc = conf.dns_case_preserving ? 62 : 36;
465 rep_cdc = 0; 498 rep_cdc = 0;
466 max_size = htons (MAX_PKT_SIZE); 499 max_size = htons (MAX_PKT_SIZE);
467 client = htons (clientid); 500 client = htons (clientid);
468 uid = next_uid++; 501 uid = ++next_uid;
469 delay = 0;
470 502
471 r3 = r4 = 0; 503 r0 = r1 = r2 = r3 = r4 = 0;
472 r4 = r5 = r6 = r7 = 0;
473}
474 504
505 chksum = get_chksum ();
506}
507
508// simple but not trivial chksum
509u8
510dns_cfg::get_chksum ()
511{
512 unsigned int sum = 0xff00; // only 16 bits required
513
514 u8 old_chksum = chksum;
515 chksum = 0;
516
517 for (unsigned int i = 0; i < sizeof (*this); ++i)
518 sum += ((u8 *)this)[i] * (i + 1);
519
520 chksum = old_chksum;
521
522 return sum + (sum >> 8);
523}
524
525bool
475bool dns_cfg::valid () 526dns_cfg::valid ()
476{ 527{
477 // although the protocol itself allows for some configurability, 528 // although the protocol itself allows for some configurability,
478 // only the following encoding/decoding settings are implemented. 529 // only the following encoding/decoding settings are implemented.
479 return id1 == 'G' 530 return id1 == 'G'
480 && id2 == 'V' 531 && id2 == 'V'
481 && id3 == 'P' 532 && id3 == 'P'
482 && id4 == 'E' 533 && id4 == 'E'
534 && version == 2
483 && seq_cdc == 26 535 && syn_cdc == 26
484 && req_cdc == 62 536 && hdr_cdc == 36
537 && (req_cdc == 36 || req_cdc == 62)
485 && rep_cdc == 0 538 && rep_cdc == 0
486 && version == 1; 539 && chksum == get_chksum ();
487} 540}
488 541
489struct dns_packet : net_packet 542struct dns_packet : net_packet
490{ 543{
491 u16 id; 544 u16 id;
495 u8 data [MAXSIZE - 6 * 2]; 548 u8 data [MAXSIZE - 6 * 2];
496 549
497 int decode_label (char *data, int size, int &offs); 550 int decode_label (char *data, int size, int &offs);
498}; 551};
499 552
553int
500int dns_packet::decode_label (char *data, int size, int &offs) 554dns_packet::decode_label (char *data, int size, int &offs)
501{ 555{
502 char *orig = data; 556 char *orig = data;
503 557
504 memset (data, 0, size); 558 memset (data, 0, size);
505 559
531 return data - orig; 585 return data - orig;
532} 586}
533 587
534///////////////////////////////////////////////////////////////////////////// 588/////////////////////////////////////////////////////////////////////////////
535 589
590static
591u16 next_id ()
592{
536static u16 dns_id = 0; // TODO: should be per-vpn 593 static u16 dns_id = 0; // TODO: should be per-vpn
537 594
538static u16 next_id ()
539{
540 if (!dns_id) 595 if (!dns_id)
541 dns_id = time (0); 596 dns_id = time (0);
542 597
543 // the simplest lsfr with periodicity 65535 i could find 598 // the simplest lsfr with periodicity 65535 i could find
544 dns_id = (dns_id << 1) 599 dns_id = (dns_id << 1)
559 struct vpn *vpn; 614 struct vpn *vpn;
560 615
561 dns_cfg cfg; 616 dns_cfg cfg;
562 617
563 bool established; 618 bool established;
619 const basecoder *cdc;
564 620
565 tstamp last_received; 621 tstamp last_received;
566 tstamp last_sent; 622 tstamp last_sent;
567 double min_latency; 623 double min_latency;
568 double poll_interval, send_interval; 624 double poll_interval, send_interval;
572 byte_stream rcvdq; int rcvseq; int repseq; 628 byte_stream rcvdq; int rcvseq; int repseq;
573 byte_stream snddq; int sndseq; 629 byte_stream snddq; int sndseq;
574 630
575 inline void time_cb (ev::timer &w, int revents); ev::timer tw; 631 inline void time_cb (ev::timer &w, int revents); ev::timer tw;
576 void receive_rep (dns_rcv *r); 632 void receive_rep (dns_rcv *r);
633
634 void reset (); // quite like tcp RST
635 void set_cfg (); // to be called after any cfg changes
577 636
578 dns_connection (connection *c); 637 dns_connection (connection *c);
579 ~dns_connection (); 638 ~dns_connection ();
580}; 639};
581 640
612dns_snd::~dns_snd () 671dns_snd::~dns_snd ()
613{ 672{
614 delete pkt; 673 delete pkt;
615} 674}
616 675
676static void
617static void append_domain (dns_packet &pkt, int &offs, const char *domain) 677append_domain (dns_packet &pkt, int &offs, const char *domain)
618{ 678{
619 // add tunnel domain 679 // add tunnel domain
620 for (;;) 680 for (;;)
621 { 681 {
622 const char *end = strchr (domain, '.'); 682 const char *end = strchr (domain, '.');
635 695
636 domain = end + 1; 696 domain = end + 1;
637 } 697 }
638} 698}
639 699
700void
640void dns_snd::gen_stream_req (int seqno, byte_stream &stream) 701dns_snd::gen_stream_req (int seqno, byte_stream &stream)
641{ 702{
642 stdhdr = true; 703 stdhdr = true;
643 this->seqno = seqno; 704 this->seqno = seqno;
644 705
645 timeout = ev_now () + INITIAL_TIMEOUT; 706 timeout = ev_now () + INITIAL_TIMEOUT;
653 // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra 714 // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra
654 715
655 char enc[256], *encp = enc; 716 char enc[256], *encp = enc;
656 encode_header (enc, THISNODE->id, seqno); 717 encode_header (enc, THISNODE->id, seqno);
657 718
658 int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); 719 int datalen = dns->cdc->decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE);
659 720
660 if (datalen > stream.size ()) 721 if (datalen > stream.size ())
661 datalen = stream.size (); 722 datalen = stream.size ();
662 723
663 int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE; 724 int enclen = dns->cdc->encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE;
664 stream.remove (datalen); 725 stream.remove (datalen);
665 726
666 while (enclen) 727 while (enclen)
667 { 728 {
668 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; 729 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE;
683 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 744 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
684 745
685 pkt->len = offs; 746 pkt->len = offs;
686} 747}
687 748
749void
688void dns_snd::gen_syn_req () 750dns_snd::gen_syn_req ()
689{ 751{
690 timeout = ev_now () + INITIAL_SYN_TIMEOUT; 752 timeout = ev_now () + INITIAL_SYN_TIMEOUT;
691 753
692 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 754 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
693 pkt->qdcount = htons (1); 755 pkt->qdcount = htons (1);
740{ 802{
741 tw.set<dns_connection, &dns_connection::time_cb> (this); 803 tw.set<dns_connection, &dns_connection::time_cb> (this);
742 804
743 vpn = c->vpn; 805 vpn = c->vpn;
744 806
807 reset ();
808}
809
810dns_connection::~dns_connection ()
811{
812 reset ();
813}
814
815void
816dns_connection::reset ()
817{
818 while (!rcvpq.empty ())
819 {
820 delete rcvpq.back ();
821 rcvpq.pop_back ();
822 }
823
824 for (int i = vpn->dns_sndpq.size (); i--; )
825 if (vpn->dns_sndpq [i]->dns == this)
826 {
827 vpn->dns_sndpq [i] = vpn->dns_sndpq.back ();
828 vpn->dns_sndpq.pop_back ();
829 }
830
745 established = false; 831 established = false;
746 832
747 rcvseq = repseq = sndseq = 0; 833 rcvseq = repseq = sndseq = 0;
748 834
749 last_sent = last_received = 0; 835 last_sent = last_received = 0;
750 poll_interval = 0.5; // starting here 836 poll_interval = 0.5; // starting here
751 send_interval = 0.5; // starting rate 837 send_interval = 0.5; // starting rate
752 min_latency = INITIAL_TIMEOUT; 838 min_latency = INITIAL_TIMEOUT;
753} 839}
754 840
755dns_connection::~dns_connection () 841void
842dns_connection::set_cfg ()
756{ 843{
757 for (vector<dns_rcv *>::iterator i = rcvpq.begin (); 844 cdc = cfg.req_cdc == 36 ? &cdc36 : &cdc62;
758 i != rcvpq.end ();
759 ++i)
760 delete *i;
761} 845}
762 846
847void
763void dns_connection::receive_rep (dns_rcv *r) 848dns_connection::receive_rep (dns_rcv *r)
764{ 849{
765 if (r->datalen) 850 if (r->datalen)
766 { 851 {
767 last_received = ev_now (); 852 last_received = ev_now ();
768 tw (); 853 tw ();
801 886
802 rcvseq = (rcvseq + 1) & SEQNO_MASK; 887 rcvseq = (rcvseq + 1) & SEQNO_MASK;
803 888
804 if (!rcvdq.put (r->data, r->datalen)) 889 if (!rcvdq.put (r->data, r->datalen))
805 { 890 {
891 // MUST never overflow, can be caused by data corruption, TODO
806 slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)"); 892 slog (L_CRIT, "DNS: !rcvdq.put (r->data, r->datalen)");
807 abort (); // MUST never overflow, can be caused by data corruption, TODO 893 reset ();
894 return;
808 } 895 }
809 896
810 while (vpn_packet *pkt = rcvdq.get ()) 897 while (vpn_packet *pkt = rcvdq.get ())
811 { 898 {
812 sockinfo si; 899 sockinfo si;
813 si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; 900 si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4;
814 901
815 vpn->recv_vpn_packet (pkt, si); 902 vpn->recv_vpn_packet (pkt, si);
816
817 delete pkt; 903 delete pkt;
818 } 904 }
819 905
820 // check for further packets 906 // check for further packets
821 goto redo; 907 goto redo;
864 { 950 {
865 // correct class, domain: parse 951 // correct class, domain: parse
866 int client, seqno; 952 int client, seqno;
867 decode_header (qname, client, seqno); 953 decode_header (qname, client, seqno);
868 954
869 u8 data[MAXSIZE];
870 int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE));
871
872 if (0 < client && client <= conns.size ()) 955 if (0 < client && client <= conns.size ())
873 { 956 {
874 connection *c = conns [client - 1]; 957 connection *c = conns [client - 1];
875 dns_connection *dns = c->dns; 958 dns_connection *dns = c->dns;
876 dns_rcv *rcv; 959 dns_rcv *rcv;
877 960
878 if (dns) 961 if (dns)
879 { 962 {
963 u8 data[MAXSIZE];
964 int datalen = dns->cdc->decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE));
965
880 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) 966 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); )
881 if (SEQNO_EQ ((*i)->seqno, seqno)) 967 if (SEQNO_EQ ((*i)->seqno, seqno))
882 { 968 {
883 // already seen that request: simply reply with the cached reply 969 // already seen that request: simply reply with the cached reply
884 dns_rcv *r = *i; 970 dns_rcv *r = *i;
985 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class 1071 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
986 pkt [offs++] = 0; pkt [offs++] = 0; 1072 pkt [offs++] = 0; pkt [offs++] = 0;
987 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL 1073 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
988 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength 1074 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
989 1075
990 slog (L_INFO, _("DNS: client %d connects"), client);
991
992 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; 1076 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
993 pkt [offs++] = CMD_IP_REJ; 1077 pkt [offs++] = CMD_IP_REJ;
994 1078
995 if (0 < client && client <= conns.size ()) 1079 if (0 < client && client <= conns.size ())
996 { 1080 {
997 connection *c = conns [client - 1]; 1081 connection *c = conns [client - 1];
998 1082
999 if (cfg.valid ()) 1083 if (cfg.valid ())
1000 { 1084 {
1001 pkt [offs - 1] = CMD_IP_SYN; 1085 slog (L_INFO, _("DNS: client %d connects (version %d, req_cdc %d)"), client, cfg.version, cfg.req_cdc);
1086
1087 // check for any encoding mismatches - hints at a case problem
1088 char qname2 [MAX_ENC_LEN];
1089 cdc26.encode (qname2, (u8 *)&cfg, sizeof (dns_cfg));
1002 1090
1003 delete c->dns; 1091 delete c->dns;
1092
1093 pkt [offs - 1] = memcmp (qname, qname2, cdc26.encode_len (sizeof (dns_cfg)))
1094 ? CMD_IP_CSE : CMD_IP_SYN;
1095
1004 c->dns = new dns_connection (c); 1096 c->dns = new dns_connection (c);
1005 c->dns->cfg = cfg; 1097 c->dns->cfg = cfg;
1098 c->dns->set_cfg ();
1006 } 1099 }
1007 } 1100 }
1008 } 1101 }
1009 } 1102 }
1010 1103
1030 dns_connection *dns = (*i)->dns; 1123 dns_connection *dns = (*i)->dns;
1031 connection *c = dns->c; 1124 connection *c = dns->c;
1032 int seqno = (*i)->seqno; 1125 int seqno = (*i)->seqno;
1033 u8 data[MAXSIZE], *datap = data; 1126 u8 data[MAXSIZE], *datap = data;
1034 //printf ("rcv pkt %x\n", seqno);//D 1127 //printf ("rcv pkt %x\n", seqno);//D
1128 bool back_off = (*i)->retry;
1035 1129
1036 if ((*i)->retry) 1130 if (back_off)
1037 { 1131 {
1038 dns->send_interval *= 1.01; 1132 dns->send_interval *= 1.01;
1039 if (dns->send_interval > MAX_SEND_INTERVAL) 1133 if (dns->send_interval > MAX_SEND_INTERVAL)
1040 dns->send_interval = MAX_SEND_INTERVAL; 1134 dns->send_interval = MAX_SEND_INTERVAL;
1041 } 1135 }
1081 ttl |= pkt [offs++] << 16; 1175 ttl |= pkt [offs++] << 16;
1082 ttl |= pkt [offs++] << 8; 1176 ttl |= pkt [offs++] << 8;
1083 ttl |= pkt [offs++]; 1177 ttl |= pkt [offs++];
1084 u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++]; 1178 u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++];
1085 1179
1086 if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT) 1180 if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT || qtype == dns->cfg.rrtype)
1087 { 1181 {
1088 if (rdlen <= MAXSIZE - offs) 1182 if (rdlen <= MAXSIZE - offs)
1089 { 1183 {
1090 // decode bytes, finally 1184 // decode bytes, finally
1091 1185
1117 { 1211 {
1118 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]); 1212 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]);
1119 1213
1120 if (ip [3] == CMD_IP_RST) 1214 if (ip [3] == CMD_IP_RST)
1121 { 1215 {
1122 slog (L_DEBUG, _("DNS: got tunnel RST request")); 1216 slog (L_DEBUG, _("DNS: got tunnel RST request."));
1123 1217
1124 delete dns; c->dns = 0; 1218 dns->reset ();
1125
1126 return; 1219 return;
1127 } 1220 }
1128 else if (ip [3] == CMD_IP_SYN) 1221 else if (ip [3] == CMD_IP_SYN)
1129 { 1222 {
1130 slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); 1223 slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us."));
1131 dns->established = true; 1224 dns->established = true;
1132 } 1225 }
1226 else if (ip [3] == CMD_IP_CSE)
1227 {
1228 if (conf.dns_case_preserving)
1229 {
1230 slog (L_INFO, _("DNS: got tunnel CSE reply, globally downgrading to case-insensitive protocol."));
1231 conf.dns_case_preserving = false;
1232 dns->reset ();
1233 return;
1234 }
1235 else
1236 {
1237 slog (L_DEBUG, _("DNS: got tunnel CSE reply, server likes us."));
1238 dns->established = true;
1239 }
1240 }
1133 else if (ip [3] == CMD_IP_REJ) 1241 else if (ip [3] == CMD_IP_REJ)
1134 { 1242 {
1135 slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting.")); 1243 slog (L_ERR, _("DNS: got tunnel REJ reply, server does not like us."));
1136 abort (); 1244 dns->tw.start (60.);
1137 } 1245 }
1138 else 1246 else
1247 {
1139 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); 1248 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]);
1249 dns->tw.start (60.);
1250 }
1140 } 1251 }
1141 else 1252 else
1142 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), 1253 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"),
1143 ip [0], ip [1], ip [2], ip [3]); 1254 ip [0], ip [1], ip [2], ip [3]);
1144 1255
1162 } 1273 }
1163 1274
1164 // todo: pkt now used 1275 // todo: pkt now used
1165 if (datap) 1276 if (datap)
1166 dns->receive_rep (new dns_rcv (seqno, data, datap - data)); 1277 dns->receive_rep (new dns_rcv (seqno, data, datap - data));
1278 else if (dns_sndpq.empty ()) // no data received, and nothing to send - idle
1279 {
1280 dns->send_interval *= 1.1;
1281
1282 if (dns->send_interval < MIN_POLL_INTERVAL)
1283 dns->send_interval = MIN_POLL_INTERVAL;
1284
1285 if (dns->send_interval > MAX_POLL_INTERVAL && !back_off)
1286 dns->send_interval = MAX_POLL_INTERVAL;
1287 }
1167 1288
1168 break; 1289 break;
1169 } 1290 }
1170} 1291}
1171 1292
1212 1333
1213 // always return true even if the buffer overflows 1334 // always return true even if the buffer overflows
1214 return true; 1335 return true;
1215} 1336}
1216 1337
1217void
1218connection::dnsv4_reset_connection ()
1219{
1220 //delete dns; dns = 0; //TODO
1221}
1222
1223#define NEXT(w) do { if (next > (w)) next = w; } while (0) 1338#define NEXT(w) do { if (next > (w)) next = w; } while (0)
1224 1339
1225void 1340void
1226dns_connection::time_cb (ev::timer &w, int revents) 1341dns_connection::time_cb (ev::timer &w, int revents)
1227{ 1342{
1268 if (vpn->dns_sndpq.empty ()) 1383 if (vpn->dns_sndpq.empty ())
1269 { 1384 {
1270 send = new dns_snd (this); 1385 send = new dns_snd (this);
1271 1386
1272 cfg.reset (THISNODE->id); 1387 cfg.reset (THISNODE->id);
1388 set_cfg ();
1273 send->gen_syn_req (); 1389 send->gen_syn_req ();
1274 } 1390 }
1275 } 1391 }
1276 else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding 1392 else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding
1277 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) 1393 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines