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.3 by pcg, Tue Mar 1 06:27:20 2005 UTC vs.
Revision 1.48 by pcg, Tue Jul 28 00:42:14 2009 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-2004 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE.
6
5 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify it
6 it under the terms of the GNU General Public License as published by 8 under the terms of the GNU General Public License as published by the
7 the Free Software Foundation; either version 2 of the License, or 9 Free Software Foundation; either version 3 of the License, or (at your
8 (at your option) any later version. 10 option) any later version.
9 11
10 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful, but
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 GNU General Public License for more details. 15 Public License for more details.
14 16
15 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 along
16 along with this program; if not, write to the Free Software 18 with this program; if not, see <http://www.gnu.org/licenses/>.
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19
20 Additional permission under GNU GPL version 3 section 7
21
22 If you modify this Program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a modified
24 version of that library), containing parts covered by the terms of the
25 OpenSSL or SSLeay licenses, the licensors of this Program grant you
26 additional permission to convey the resulting work. Corresponding
27 Source for a non-source form of such a combination shall include the
28 source code for the parts of OpenSSL used as well as that of the
29 covered work.
18*/ 30*/
31
32// TODO: EDNS0 option to increase dns mtu?
33// TODO: re-write dns packet parsing/creation using a safe mem-buffer
34// to ensure no buffer overflows or similar problems.
19 35
20#include "config.h" 36#include "config.h"
21 37
22#if ENABLE_DNS 38#if ENABLE_DNS
23 39
24// dns processing is EXTREMELY ugly. For obvious(?) reasons. 40// dns processing is EXTREMELY ugly. For obvious(?) reasons.
25// it's a hack, use only in emergency situations please. 41// it's a hack, use only in emergency situations please.
26 42
27#include <cstring> 43#include <cstring>
44#include <cassert>
28 45
29#include <sys/types.h> 46#include <sys/types.h>
30#include <sys/socket.h> 47#include <sys/socket.h>
31#include <sys/wait.h> 48#include <sys/wait.h>
32#include <sys/uio.h> 49#include <sys/uio.h>
35#include <unistd.h> 52#include <unistd.h>
36#include <fcntl.h> 53#include <fcntl.h>
37 54
38#include <map> 55#include <map>
39 56
57#include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */
58#include <gmp.h>
59
40#include "netcompat.h" 60#include "netcompat.h"
41 61
42#include "vpn.h" 62#include "vpn.h"
43 63
44#define MIN_RETRY 1. 64#define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data
45#define MAX_RETRY 60. 65#define ACTIVITY_INTERVAL 5.
46 66
47#define MAX_OUTSTANDING 10 // max. outstanding requests 67#define INITIAL_TIMEOUT 0.1 // retry timeouts
68#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn
48 69
49/* 70#define MAX_SEND_INTERVAL 2. // optimistic?
50 71
51protocol, in shorthand :) 72#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog
73#define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE
52 74
53client -> server <req> ANY? 75#define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well
54server -> client <req> TXT <rep> 76// 240 leaves about 4 bytes of server reply data
77// every request byte less give room for two reply bytes
55 78
56<req> is dns64-encoded <client-id:12><recv-seqno:10>[<send-seqno:10><data>] 79#define SEQNO_MASK 0x3fff
57<rep> is dns64-encoded <0:12><recv-seqno:10>[<send-seqno:10><data>] 80#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) )
58
59if <client-id> is zero, the connection will be configured:
60
61<0:12><0:4>client-id:12><default-ttl:8><max-size:16><flags:16>
62
63*/
64 81
65#define MAX_LBL_SIZE 63 82#define MAX_LBL_SIZE 63
66#define MAX_PKT_SIZE 512 83#define MAX_PKT_SIZE 512
67 84
85#define RR_TYPE_A 1
86#define RR_TYPE_NULL 10
68#define RR_TYPE_TXT 16 87#define RR_TYPE_TXT 16
88#define RR_TYPE_AAAA 28
69#define RR_TYPE_ANY 255 89#define RR_TYPE_ANY 255
90
70#define RR_CLASS_IN 1 91#define RR_CLASS_IN 1
71 92
72// the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) 93#define CMD_IP_1 207
73struct dns64 94#define CMD_IP_2 46
74{ 95#define CMD_IP_3 236
75 static const char encode_chars[64 + 1]; 96#define CMD_IP_RST 29
76 static s8 decode_chars[256]; 97#define CMD_IP_SYN 113
98#define CMD_IP_REJ 32
77 99
78 static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } 100// works for cmaps up to 255 (not 256!)
79 static int decode_len (int bytes) { return (bytes * 6) / 8; } 101struct charmap
102{
103 enum { INVALID = (u8)255 };
104
105 char encode [256]; // index => char
106 u8 decode [256]; // char => index
107 unsigned int size;
108
109 charmap (const char *cmap);
110};
111
112charmap::charmap (const char *cmap)
113{
114 char *enc = encode;
115 u8 *dec = decode;
116
117 memset (enc, (char) 0, 256);
118 memset (dec, (char)INVALID, 256);
119
120 for (size = 0; cmap [size]; size++)
121 {
122 enc [size] = cmap [size];
123 dec [(u8)enc [size]] = size;
124 }
125
126 assert (size < 256);
127}
128
129#define MAX_DEC_LEN 500
130#define MAX_ENC_LEN (MAX_DEC_LEN * 2)
131#define MAX_LIMBS ((MAX_DEC_LEN * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS)
132
133// ugly. minimum base is 16(!)
134struct basecoder
135{
136 charmap cmap;
137 unsigned int enc_len [MAX_DEC_LEN];
138 unsigned int dec_len [MAX_ENC_LEN];
139
140 unsigned int encode_len (unsigned int len);
141 unsigned int decode_len (unsigned int len);
142
80 static void encode (char *dst, u8 *src, int len); 143 unsigned int encode (char *dst, u8 *src, unsigned int len);
81 static void decode (u8 *dst, char *src, int len); 144 unsigned int decode (u8 *dst, char *src, unsigned int len);
82 145
83 dns64 (); 146 basecoder (const char *cmap);
84} dns64; 147};
85 148
86const char dns64::encode_chars[64 + 1] = "0123456789-abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 149basecoder::basecoder (const char *cmap)
87s8 dns64::decode_chars[256]; 150: cmap (cmap)
88
89dns64::dns64 ()
90{ 151{
91 for (int i = 0; i < 64; i++) 152 for (unsigned int len = 0; len < MAX_DEC_LEN; ++len)
92 decode_chars [encode_chars [i]] = i + 1; 153 {
93} 154 u8 src [MAX_DEC_LEN];
155 u8 dst [MAX_ENC_LEN];
94 156
95void dns64::encode (char *dst, u8 *src, int len) 157 memset (src, 255, len);
158
159 mp_limb_t m [MAX_LIMBS];
160 mp_size_t n;
161
162 n = mpn_set_str (m, src, len, 256);
163 n = mpn_get_str (dst, this->cmap.size, m, n);
164
165 for (int i = 0; !dst [i]; ++i)
166 n--;
167
168 enc_len [len] = n;
169 dec_len [n] = len;
170 }
171}
172
173unsigned int basecoder::encode_len (unsigned int len)
96{ 174{
97 // slow, but easy to debug 175 return enc_len [len];
98 unsigned int accum, bits = 0; 176}
177
178unsigned int basecoder::decode_len (unsigned int len)
179{
180 while (len && !dec_len [len])
181 --len;
182
183 return dec_len [len];
184}
185
186unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len)
187{
188 if (!len || len > MAX_DEC_LEN)
189 return 0;
190
191 int elen = encode_len (len);
192
193 mp_limb_t m [MAX_LIMBS];
194 mp_size_t n;
195
196 u8 dst_ [MAX_ENC_LEN];
197
198 n = mpn_set_str (m, src, len, 256);
199 n = mpn_get_str (dst_, cmap.size, m, n);
200
201 int plen = elen; // for padding
202
203 while (n < plen)
204 {
205 *dst++ = cmap.encode [0];
206 plen--;
207 }
208
209 for (unsigned int i = n - plen; i < n; ++i)
210 *dst++ = cmap.encode [dst_ [i]];
211
212 return elen;
213}
214
215unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len)
216{
217 if (!len || len > MAX_ENC_LEN)
218 return 0;
219
220 u8 src_ [MAX_ENC_LEN];
221 unsigned int elen = 0;
99 222
100 while (len--) 223 while (len--)
101 { 224 {
102 accum <<= 8; 225 u8 val = cmap.decode [(u8)*src++];
103 accum |= *src++;
104 bits += 8;
105 226
106 while (bits >= 6) 227 if (val != charmap::INVALID)
107 { 228 src_ [elen++] = val;
108 *dst++ = encode_chars [(accum >> (bits - 6)) & 63]; 229 }
109 bits -= 6; 230
110 } 231 int dlen = decode_len (elen);
232
233 mp_limb_t m [MAX_LIMBS];
234 mp_size_t n;
235
236 u8 dst_ [MAX_DEC_LEN];
237
238 n = mpn_set_str (m, src_, elen, cmap.size);
239 n = mpn_get_str (dst_, 256, m, n);
240
241 if (n < dlen)
111 } 242 {
243 memset (dst, 0, dlen - n);
244 memcpy (dst + dlen - n, dst_, n);
245 }
246 else
247 memcpy (dst, dst_ + n - dlen, dlen);
112 248
113 if (bits) 249 return dlen;
114 *dst++ = encode_chars [(accum << (6 - bits)) & 63];
115} 250}
116 251
117void dns64::decode (u8 *dst, char *src, int len) 252#if 0
118{ 253struct test { test (); } test;
119 // slow, but easy to debug
120 unsigned int accum, bits = 0;
121 254
122 while (len--) 255test::test ()
256{
257 basecoder cdc ("0123456789abcdefghijklmnopqrstuvwxyz");
258
259 u8 in[] = "0123456789abcdefghijklmnopqrstuvwxyz";
260 static char enc[200];
261 static u8 dec[200];
262
263 for (int i = 1; i < 20; i++)
123 { 264 {
124 s8 chr = decode_chars [(u8)*src++]; 265 int elen = cdc.encode (enc, in, i);
266 int dlen = cdc.decode (dec, enc, elen);
125 267
268 printf ("%d>%d>%d (%s>%s)\n", i, elen, dlen, enc, dec);
269 }
270 abort ();
271}
272#endif
126 273
127 if (!chr) 274//static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-");
275//static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
276static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
277//static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet
278static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO");
279
280/////////////////////////////////////////////////////////////////////////////
281
282#define HDRSIZE 6
283
284inline void encode_header (char *data, int clientid, int seqno, int retry = 0)
285{
286 seqno &= SEQNO_MASK;
287
288 u8 hdr[3] = {
289 clientid,
290 (seqno >> 8) | (retry << 6),
291 seqno,
292 };
293
294 assert (clientid < 256);
295
296 cdc26.encode (data, hdr, 3);
297}
298
299inline void decode_header (char *data, int &clientid, int &seqno)
300{
301 u8 hdr[3];
302
303 cdc26.decode (hdr, data, HDRSIZE);
304
305 clientid = hdr[0];
306 seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK;
307}
308
309/////////////////////////////////////////////////////////////////////////////
310
311struct byte_stream
312{
313 u8 *data;
314 int maxsize;
315 int fill;
316
317 byte_stream (int maxsize);
318 ~byte_stream ();
319
320 bool empty () { return !fill; }
321 int size () { return fill; }
322
323 bool put (u8 *data, unsigned int datalen);
324 bool put (vpn_packet *pkt);
325 vpn_packet *get ();
326
327 u8 *begin () { return data; }
328 void remove (int count);
329};
330
331byte_stream::byte_stream (int maxsize)
332: maxsize (maxsize), fill (0)
333{
334 data = new u8 [maxsize];
335}
336
337byte_stream::~byte_stream ()
338{
339 delete data;
340}
341
342void byte_stream::remove (int count)
343{
344 if (count > fill)
345 assert (count <= fill);
346
347 memmove (data, data + count, fill -= count);
348}
349
350bool byte_stream::put (u8 *data, unsigned int datalen)
351{
352 if (maxsize - fill < datalen)
353 return false;
354
355 memcpy (this->data + fill, data, datalen); fill += datalen;
356
357 return true;
358}
359
360bool byte_stream::put (vpn_packet *pkt)
361{
362 if (maxsize - fill < pkt->len + 2)
363 return false;
364
365 data [fill++] = pkt->len >> 8;
366 data [fill++] = pkt->len;
367
368 memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len;
369
370 return true;
371}
372
373vpn_packet *byte_stream::get ()
374{
375 unsigned int len;
376
377 for (;;)
378 {
379 len = (data [0] << 8) | data [1];
380
381 if (len <= MAXSIZE || fill < 2)
128 break; 382 break;
129 383
130 accum <<= 6; 384 // TODO: handle this better than skipping, e.g. by reset
131 accum |= chr - 1; 385 slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte..."));
132 bits += 6; 386 remove (1);
133 387 }
134 while (bits >= 8)
135 { 388
136 *dst++ = accum >> (bits - 8); 389 if (fill < len + 2)
137 bits -= 8; 390 return 0;
138 } 391
139 } 392 vpn_packet *pkt = new vpn_packet;
393
394 pkt->len = len;
395 memcpy (pkt->at (0), data + 2, len);
396 remove (len + 2);
397
398 return pkt;
140} 399}
141 400
142///////////////////////////////////////////////////////////////////////////// 401/////////////////////////////////////////////////////////////////////////////
143 402
144#define FLAG_QUERY ( 0 << 15) 403#define FLAG_QUERY ( 0 << 15)
145#define FLAG_RESPONSE ( 1 << 15) 404#define FLAG_RESPONSE ( 1 << 15)
146#define FLAG_OP_MASK (15 << 14) 405#define FLAG_OP_MASK (15 << 11)
147#define FLAG_OP_QUERY ( 0 << 11) 406#define FLAG_OP_QUERY ( 0 << 11)
148#define FLAG_AA ( 1 << 10) 407#define FLAG_AA ( 1 << 10)
149#define FLAG_TC ( 1 << 9) 408#define FLAG_TC ( 1 << 9)
150#define FLAG_RD ( 1 << 8) 409#define FLAG_RD ( 1 << 8)
151#define FLAG_RA ( 1 << 7) 410#define FLAG_RA ( 1 << 7)
411#define FLAG_AUTH ( 1 << 5)
152#define FLAG_RCODE_MASK (15 << 0) 412#define FLAG_RCODE_MASK (15 << 0)
153#define FLAG_RCODE_OK ( 0 << 0) 413#define FLAG_RCODE_OK ( 0 << 0)
154#define FLAG_RCODE_FORMERR ( 1 << 0) 414#define FLAG_RCODE_FORMERR ( 1 << 0)
155#define FLAG_RCODE_SERVFAIL ( 2 << 0) 415#define FLAG_RCODE_SERVFAIL ( 2 << 0)
156#define FLAG_RCODE_NXDOMAIN ( 3 << 0) 416#define FLAG_RCODE_NXDOMAIN ( 3 << 0)
157#define FLAG_RCODE_REFUSED ( 5 << 0) 417#define FLAG_RCODE_REFUSED ( 5 << 0)
158 418
159#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) 419#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD)
160#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD) 420#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA)
421
422struct dns_cfg
423{
424 static int next_uid;
425
426 u8 id1, id2, id3, id4;
427
428 u8 version;
429 u8 flags;
430 u8 rrtype;
431 u8 def_ttl;
432
433 u16 client;
434 u16 uid; // to make request unique
435
436 u16 max_size;
437 u8 seq_cdc;
438 u8 req_cdc;
439
440 u8 rep_cdc;
441 u8 delay; // time in 0.01s units that the server may delay replying packets
442 u8 r3, r4;
443
444 u8 r5, r6, r7, r8;
445
446 void reset (int clientid);
447 bool valid ();
448};
449
450int dns_cfg::next_uid;
451
452void dns_cfg::reset (int clientid)
453{
454 id1 = 'G';
455 id2 = 'V';
456 id3 = 'P';
457 id4 = 'E';
458
459 version = 1;
460
461 rrtype = RR_TYPE_TXT;
462 flags = 0;
463 def_ttl = 0;
464 seq_cdc = 26;
465 req_cdc = 62;
466 rep_cdc = 0;
467 max_size = htons (MAX_PKT_SIZE);
468 client = htons (clientid);
469 uid = next_uid++;
470 delay = 0;
471
472 r3 = r4 = 0;
473 r4 = r5 = r6 = r7 = 0;
474}
475
476bool dns_cfg::valid ()
477{
478 // although the protocol itself allows for some configurability,
479 // only the following encoding/decoding settings are implemented.
480 return id1 == 'G'
481 && id2 == 'V'
482 && id3 == 'P'
483 && id4 == 'E'
484 && seq_cdc == 26
485 && req_cdc == 62
486 && rep_cdc == 0
487 && version == 1;
488}
161 489
162struct dns_packet : net_packet 490struct dns_packet : net_packet
163{ 491{
164 u16 id; 492 u16 id;
165 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 493 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4
166 u16 qdcount, ancount, nscount, arcount; 494 u16 qdcount, ancount, nscount, arcount;
167 495
168 u8 data[MAXSIZE - 6 * 2]; 496 u8 data [MAXSIZE - 6 * 2];
169 497
170 int decode_label (char *data, int size, int &offs); 498 int decode_label (char *data, int size, int &offs);
171}; 499};
172 500
173int dns_packet::decode_label (char *data, int size, int &offs) 501int dns_packet::decode_label (char *data, int size, int &offs)
204 return data - orig; 532 return data - orig;
205} 533}
206 534
207///////////////////////////////////////////////////////////////////////////// 535/////////////////////////////////////////////////////////////////////////////
208 536
537static u16 dns_id = 0; // TODO: should be per-vpn
538
539static u16 next_id ()
540{
541 if (!dns_id)
542 dns_id = time (0);
543
544 // the simplest lsfr with periodicity 65535 i could find
545 dns_id = (dns_id << 1)
546 | (((dns_id >> 1)
547 ^ (dns_id >> 2)
548 ^ (dns_id >> 4)
549 ^ (dns_id >> 15)) & 1);
550
551 return dns_id;
552}
553
209struct dns_req 554struct dns_rcv;
555struct dns_snd;
556
557struct dns_connection
558{
559 connection *c;
560 struct vpn *vpn;
561
562 dns_cfg cfg;
563
564 bool established;
565
566 tstamp last_received;
567 tstamp last_sent;
568 double min_latency;
569 double poll_interval, send_interval;
570
571 vector<dns_rcv *> rcvpq;
572
573 byte_stream rcvdq; int rcvseq; int repseq;
574 byte_stream snddq; int sndseq;
575
576 inline void time_cb (ev::timer &w, int revents); ev::timer tw;
577 void receive_rep (dns_rcv *r);
578
579 dns_connection (connection *c);
580 ~dns_connection ();
581};
582
583struct dns_snd
210{ 584{
211 dns_packet *pkt; 585 dns_packet *pkt;
212 tstamp next; 586 tstamp timeout, sent;
213 int retry; 587 int retry;
588 struct dns_connection *dns;
589 int seqno;
590 bool stdhdr;
214 591
215 dns_req (connection *c, vpn_packet *p, int &offs); 592 void gen_stream_req (int seqno, byte_stream &stream);
593 void gen_syn_req ();
594
595 dns_snd (dns_connection *dns);
596 ~dns_snd ();
216}; 597};
217 598
599dns_snd::dns_snd (dns_connection *dns)
600: dns (dns)
601{
602 timeout = 0;
603 retry = 0;
604 seqno = 0;
605 sent = ev_now ();
606 stdhdr = false;
607
608 pkt = new dns_packet;
609
610 pkt->id = next_id ();
611}
612
613dns_snd::~dns_snd ()
614{
615 delete pkt;
616}
617
618static void append_domain (dns_packet &pkt, int &offs, const char *domain)
619{
620 // add tunnel domain
621 for (;;)
622 {
623 const char *end = strchr (domain, '.');
624
625 if (!end)
626 end = domain + strlen (domain);
627
628 int len = end - domain;
629
630 pkt [offs++] = len;
631 memcpy (pkt.at (offs), domain, len);
632 offs += len;
633
634 if (!*end)
635 break;
636
637 domain = end + 1;
638 }
639}
640
641void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
642{
643 stdhdr = true;
644 this->seqno = seqno;
645
646 timeout = ev_now () + INITIAL_TIMEOUT;
647
648 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
649 pkt->qdcount = htons (1);
650
651 int offs = 6*2;
652 int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2);
653 // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well,
654 // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra
655
656 char enc[256], *encp = enc;
657 encode_header (enc, THISNODE->id, seqno);
658
659 int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE);
660
661 if (datalen > stream.size ())
662 datalen = stream.size ();
663
664 int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE;
665 stream.remove (datalen);
666
667 while (enclen)
668 {
669 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE;
670
671 (*pkt)[offs++] = lbllen;
672 memcpy (pkt->at (offs), encp, lbllen);
673
674 offs += lbllen;
675 encp += lbllen;
676
677 enclen -= lbllen;
678 }
679
680 append_domain (*pkt, offs, dns->c->conf->domain);
681
682 (*pkt)[offs++] = 0;
683 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY;
684 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
685
686 pkt->len = offs;
687}
688
689void dns_snd::gen_syn_req ()
690{
691 timeout = ev_now () + INITIAL_SYN_TIMEOUT;
692
693 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
694 pkt->qdcount = htons (1);
695
696 int offs = 6 * 2;
697
698 int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg));
699
700 assert (elen <= MAX_LBL_SIZE);
701
702 (*pkt)[offs] = elen;
703 offs += elen + 1;
704 append_domain (*pkt, offs, dns->c->conf->domain);
705
706 (*pkt)[offs++] = 0;
707 (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A;
708 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
709
710 pkt->len = offs;
711}
712
218struct dns_rep 713struct dns_rcv
219{ 714{
715 int seqno;
716 dns_packet *pkt; // reply packet
717 u8 data [MAXSIZE]; // actually part of the reply packet...
718 int datalen;
719
720 dns_rcv (int seqno, u8 *data, int datalen);
721 ~dns_rcv ();
220}; 722};
221 723
222static u16 dns_id; // TODO: should be per-vpn 724dns_rcv::dns_rcv (int seqno, u8 *data, int datalen)
223 725: seqno (seqno), pkt (new dns_packet), datalen (datalen)
224dns_req::dns_req (connection *c, vpn_packet *p, int &offs)
225{ 726{
226 next = 0; 727 memcpy (this->data, data, datalen);
227 retry = 0; 728}
228 729
229 pkt = new dns_packet; 730dns_rcv::~dns_rcv ()
230 731{
231 pkt->id = dns_id++; 732 delete pkt;
232 pkt->flags = DEFAULT_CLIENT_FLAGS;
233 pkt->qdcount = htons (1 * 0);
234 pkt->ancount = 0;
235 pkt->nscount = 0;
236 pkt->arcount = 0;
237
238 offs += 10000;
239
240 c->dns_sndq.push_back (this);
241} 733}
242 734
243///////////////////////////////////////////////////////////////////////////// 735/////////////////////////////////////////////////////////////////////////////
244 736
245struct dns_cfg 737dns_connection::dns_connection (connection *c)
738: c (c)
739, rcvdq (MAX_BACKLOG * 2)
740, snddq (MAX_BACKLOG)
246{ 741{
247 u8 id1, id2, id3; 742 tw.set<dns_connection, &dns_connection::time_cb> (this);
248 u8 def_ttl;
249 u8 unused1;
250 u16 max_size;
251 u8 flags1, flags2;
252};
253 743
254// reply to client-poll 744 vpn = c->vpn;
255void dnsv4_poll (dns_packet *pkt, int &offs)
256{
257 int dlen = MAX_PKT_SIZE - offs - 2;
258 745
259 (*pkt) [offs++] = 0; 746 established = false;
260 (*pkt) [offs++] = 5;
261 memcpy (&((*pkt)[offs]), "\01H\02\xff\x00", 5);
262 offs += 5;
263 747
264 pkt->ancount = htons (1); 748 rcvseq = repseq = sndseq = 0;
265 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
266 749
267 printf ("we have room for %d bytes\n", dlen); 750 last_sent = last_received = 0;
751 poll_interval = 0.5; // starting here
752 send_interval = 0.5; // starting rate
753 min_latency = INITIAL_TIMEOUT;
754}
755
756dns_connection::~dns_connection ()
757{
758 for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
759 i != rcvpq.end ();
760 ++i)
761 delete *i;
762}
763
764void dns_connection::receive_rep (dns_rcv *r)
765{
766 if (r->datalen)
767 {
768 last_received = ev_now ();
769 tw ();
770
771 poll_interval = send_interval;
772 }
773 else
774 {
775 poll_interval *= 1.5;
776
777 if (poll_interval > MAX_POLL_INTERVAL)
778 poll_interval = MAX_POLL_INTERVAL;
779 }
780
781 rcvpq.push_back (r);
782
783 redo:
784
785 // find next packet
786 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); )
787 if (SEQNO_EQ (rcvseq, (*i)->seqno))
788 {
789 //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D
790 // enter the packet into our input stream
791 r = *i;
792
793 // remove the oldest packet, look forward, as it's oldest first
794 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j)
795 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW))
796 {
797 //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D
798 delete *j;
799 rcvpq.erase (j);
800 break;
801 }
802
803 rcvseq = (rcvseq + 1) & SEQNO_MASK;
804
805 if (!rcvdq.put (r->data, r->datalen))
806 {
807 slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)");
808 abort (); // MUST never overflow, can be caused by data corruption, TODO
809 }
810
811 while (vpn_packet *pkt = rcvdq.get ())
812 {
813 sockinfo si;
814 si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4;
815
816 vpn->recv_vpn_packet (pkt, si);
817
818 delete pkt;
819 }
820
821 // check for further packets
822 goto redo;
823 }
268} 824}
269 825
270void 826void
827vpn::dnsv4_server (dns_packet &pkt)
828{
829 u16 flags = ntohs (pkt.flags);
830
831 int offs = 6 * 2; // skip header
832
833 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
834
835 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK))
836 && pkt.qdcount == htons (1))
837 {
838 char qname [MAXSIZE];
839 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
840
841 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
842 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
843
844 pkt.qdcount = htons (1);
845 pkt.ancount = 0;
846 pkt.nscount = 0; // should be self, as other nameservers reply like this
847 pkt.arcount = 0; // a record for self, as other nameservers reply like this
848
849 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL);
850
851 int dlen = strlen (THISNODE->domain);
852
853 if (qclass == RR_CLASS_IN
854 && qlen > dlen + 1
855 && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen))
856 {
857 // now generate reply
858 pkt.ancount = htons (1); // one answer RR
859 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
860
861 if ((qtype == RR_TYPE_ANY
862 || qtype == RR_TYPE_TXT
863 || qtype == RR_TYPE_NULL)
864 && qlen > dlen + 1 + HDRSIZE)
865 {
866 // correct class, domain: parse
867 int client, seqno;
868 decode_header (qname, client, seqno);
869
870 u8 data[MAXSIZE];
871 int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE));
872
873 if (0 < client && client <= conns.size ())
874 {
875 connection *c = conns [client - 1];
876 dns_connection *dns = c->dns;
877 dns_rcv *rcv;
878
879 if (dns)
880 {
881 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); )
882 if (SEQNO_EQ ((*i)->seqno, seqno))
883 {
884 // already seen that request: simply reply with the cached reply
885 dns_rcv *r = *i;
886
887 slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno);
888
889 // refresh header & id, as the retry count could have changed
890 memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE);
891 r->pkt->id = pkt.id;
892
893 memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len);
894
895 goto duplicate_request;
896 }
897
898 // new packet, queue
899 rcv = new dns_rcv (seqno, data, datalen);
900 dns->receive_rep (rcv);
901 }
902
903 {
904 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
905
906 int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A;
907 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type
908 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
909 pkt [offs++] = 0; pkt [offs++] = 0;
910 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL
911
912 int rdlen_offs = offs += 2;
913
914 if (dns)
915 {
916 int dlen = ntohs (dns->cfg.max_size) - offs;
917
918 // bind doesn't compress well, so reduce further by one label length
919 dlen -= qlen;
920
921 // only put data into in-order sequence packets, if
922 // we receive out-of-order packets we generate empty
923 // replies
924 //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D
925 if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW)
926 {
927 dns->repseq = seqno;
928
929 while (dlen > 1 && !dns->snddq.empty ())
930 {
931 int txtlen = dlen <= 255 ? dlen - 1 : 255;
932
933 if (txtlen > dns->snddq.size ())
934 txtlen = dns->snddq.size ();
935
936 pkt[offs++] = txtlen;
937 memcpy (pkt.at (offs), dns->snddq.begin (), txtlen);
938 offs += txtlen;
939 dns->snddq.remove (txtlen);
940
941 dlen -= txtlen + 1;
942 }
943 }
944
945 // avoid completely empty TXT rdata
946 if (offs == rdlen_offs)
947 pkt[offs++] = 0;
948
949 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
950 }
951 else
952 {
953 // send RST
954 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
955 pkt [offs++] = CMD_IP_RST;
956 }
957
958 int rdlen = offs - rdlen_offs;
959
960 pkt [rdlen_offs - 2] = rdlen >> 8;
961 pkt [rdlen_offs - 1] = rdlen;
962
963 if (dns)
964 {
965 // now update dns_rcv copy
966 rcv->pkt->len = offs;
967 memcpy (rcv->pkt->at (0), pkt.at (0), offs);
968 }
969 }
970
971 duplicate_request: ;
972 }
973 else
974 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
975 }
976 else if (qtype == RR_TYPE_A
977 && qlen > dlen + 1 + cdc26.encode_len (sizeof (dns_cfg)))
978 {
979 dns_cfg cfg;
980 cdc26.decode ((u8 *)&cfg, qname, cdc26.encode_len (sizeof (dns_cfg)));
981 int client = ntohs (cfg.client);
982
983 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
984
985 pkt [offs++] = RR_TYPE_A >> 8; pkt [offs++] = RR_TYPE_A; // type
986 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
987 pkt [offs++] = 0; pkt [offs++] = 0;
988 pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
989 pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
990
991 slog (L_INFO, _("DNS: client %d connects"), client);
992
993 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
994 pkt [offs++] = CMD_IP_REJ;
995
996 if (0 < client && client <= conns.size ())
997 {
998 connection *c = conns [client - 1];
999
1000 if (cfg.valid ())
1001 {
1002 pkt [offs - 1] = CMD_IP_SYN;
1003
1004 delete c->dns;
1005 c->dns = new dns_connection (c);
1006 c->dns->cfg = cfg;
1007 }
1008 }
1009 }
1010 }
1011
1012 pkt.len = offs;
1013 }
1014}
1015
1016void
1017vpn::dnsv4_client (dns_packet &pkt)
1018{
1019 u16 flags = ntohs (pkt.flags);
1020 int offs = 6 * 2; // skip header
1021
1022 pkt.qdcount = ntohs (pkt.qdcount);
1023 pkt.ancount = ntohs (pkt.ancount);
1024
1025 // go through our request list and find the corresponding request
1026 for (vector<dns_snd *>::iterator i = dns_sndpq.begin ();
1027 i != dns_sndpq.end ();
1028 ++i)
1029 if ((*i)->pkt->id == pkt.id)
1030 {
1031 dns_connection *dns = (*i)->dns;
1032 connection *c = dns->c;
1033 int seqno = (*i)->seqno;
1034 u8 data[MAXSIZE], *datap = data;
1035 //printf ("rcv pkt %x\n", seqno);//D
1036
1037 if ((*i)->retry)
1038 {
1039 dns->send_interval *= 1.01;
1040 if (dns->send_interval > MAX_SEND_INTERVAL)
1041 dns->send_interval = MAX_SEND_INTERVAL;
1042 }
1043 else
1044 {
1045#if 0
1046 dns->send_interval *= 0.999;
1047#endif
1048 // the latency surely puts an upper bound on
1049 // the minimum send interval
1050 double latency = ev_now () - (*i)->sent;
1051
1052 if (latency < dns->min_latency)
1053 dns->min_latency = latency;
1054
1055 if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor)
1056 dns->send_interval = dns->min_latency * conf.dns_overlap_factor;
1057
1058 if (dns->send_interval < conf.dns_send_interval)
1059 dns->send_interval = conf.dns_send_interval;
1060 }
1061
1062 delete *i;
1063 dns_sndpq.erase (i);
1064
1065 if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK))
1066 {
1067 char qname[MAXSIZE];
1068
1069 while (pkt.qdcount-- && offs < MAXSIZE - 4)
1070 {
1071 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
1072 offs += 4; // skip qtype, qclass
1073 }
1074
1075 while (pkt.ancount-- && offs < MAXSIZE - 10 && datap)
1076 {
1077 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
1078
1079 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
1080 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
1081 u32 ttl = pkt [offs++] << 24;
1082 ttl |= pkt [offs++] << 16;
1083 ttl |= pkt [offs++] << 8;
1084 ttl |= pkt [offs++];
1085 u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++];
1086
1087 if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT)
1088 {
1089 if (rdlen <= MAXSIZE - offs)
1090 {
1091 // decode bytes, finally
1092
1093 while (rdlen)
1094 {
1095 int txtlen = pkt [offs++];
1096
1097 assert (txtlen + offs < MAXSIZE - 1);
1098
1099 memcpy (datap, pkt.at (offs), txtlen);
1100 datap += txtlen; offs += txtlen;
1101
1102 rdlen -= txtlen + 1;
1103 }
1104 }
1105 }
1106 else if (qtype == RR_TYPE_A)
1107 {
1108 u8 ip [4];
1109
1110 ip [0] = pkt [offs++];
1111 ip [1] = pkt [offs++];
1112 ip [2] = pkt [offs++];
1113 ip [3] = pkt [offs++];
1114
1115 if (ip [0] == CMD_IP_1
1116 && ip [1] == CMD_IP_2
1117 && ip [2] == CMD_IP_3)
1118 {
1119 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]);
1120
1121 if (ip [3] == CMD_IP_RST)
1122 {
1123 slog (L_DEBUG, _("DNS: got tunnel RST request"));
1124
1125 delete dns; c->dns = 0;
1126
1127 return;
1128 }
1129 else if (ip [3] == CMD_IP_SYN)
1130 {
1131 slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us."));
1132 dns->established = true;
1133 }
1134 else if (ip [3] == CMD_IP_REJ)
1135 {
1136 slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting."));
1137 abort ();
1138 }
1139 else
1140 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]);
1141 }
1142 else
1143 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"),
1144 ip [0], ip [1], ip [2], ip [3]);
1145
1146 return;
1147 }
1148
1149 int client, rseqno;
1150 decode_header (qname, client, rseqno);
1151
1152 if (client != THISNODE->id)
1153 {
1154 slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring"));
1155 datap = 0;
1156 }
1157 else if (rseqno != seqno)
1158 {
1159 slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?"));
1160 datap = 0;
1161 }
1162 }
1163 }
1164
1165 // todo: pkt now used
1166 if (datap)
1167 dns->receive_rep (new dns_rcv (seqno, data, datap - data));
1168
1169 break;
1170 }
1171}
1172
1173void
271vpn::dnsv4_ev (io_watcher &w, short revents) 1174vpn::dnsv4_ev (ev::io &w, int revents)
272{ 1175{
273 if (revents & EVENT_READ) 1176 if (revents & EV_READ)
274 { 1177 {
275 dns_packet *pkt = new dns_packet; 1178 dns_packet *pkt = new dns_packet;
276 struct sockaddr_in sa; 1179 struct sockaddr_in sa;
277 socklen_t sa_len = sizeof (sa); 1180 socklen_t sa_len = sizeof (sa);
278 1181
279 int len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 1182 pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
280 pkt->len = len;
281 1183
282 u16 flags = ntohs (pkt->flags); 1184 if (pkt->len > 0)
283
284 if (THISNODE->dns_port)
285 { 1185 {
286 // server 1186 if (ntohs (pkt->flags) & FLAG_RESPONSE)
287 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 1187 dnsv4_client (*pkt);
288 1188 else
289 int offs = 6 * 2; // skip header
290
291 if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC))
292 && pkt->qdcount == htons (1))
293 { 1189 {
294 char qname[MAXSIZE]; 1190 dnsv4_server (*pkt);
295 int qlen = pkt->decode_label ((char *)qname, MAXSIZE, offs); 1191 sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len);
296
297 printf ("rcvd packet len %d, id%04x flags%04x q%04x a%04x n%04x a%04x <%s>\n", len,
298 pkt->id, flags, pkt->qdcount, pkt->ancount, pkt->nscount, pkt->arcount, qname);//D
299
300 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++];
301 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++];
302
303 pkt->qdcount = htons (1);
304 pkt->ancount = 0;
305 pkt->nscount = 0; // should be self, as other nameservers reply like this
306 pkt->arcount = 0; // a record for self, as other nameservers reply like this
307
308 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN);
309
310 int dlen = strlen (THISNODE->domain);
311
312 if (qclass == RR_CLASS_IN
313 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT)
314 && qlen > dlen + 1
315 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen))
316 {
317 // correct class, now generate reply
318 pkt->ancount = htons (1); // one answer RR
319
320 (*pkt) [offs++] = 0xc0;
321 (*pkt) [offs++] = 6 * 2; // same as in query section
322
323 (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT;
324 (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN;
325
326 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0;
327 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL
328
329 dnsv4_poll (pkt, offs);
330 printf ("correct class\n");
331 }
332 } 1192 }
333 1193
334 sendto (w.fd, &((*pkt)[0]), offs, 0, (sockaddr *)&sa, sa_len); 1194 delete pkt;
1195 }
1196 }
1197}
1198
1199bool
1200vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1201{
1202 int client = ntohl (si.host);
1203
1204 assert (0 < client && client <= conns.size ());
1205
1206 connection *c = conns [client - 1];
1207
1208 if (!c->dns)
1209 c->dns = new dns_connection (c);
1210
1211 if (c->dns->snddq.put (pkt))
1212 c->dns->tw ();
1213
1214 // always return true even if the buffer overflows
1215 return true;
1216}
1217
1218void
1219connection::dnsv4_reset_connection ()
1220{
1221 //delete dns; dns = 0; //TODO
1222}
1223
1224#define NEXT(w) do { if (next > (w)) next = w; } while (0)
1225
1226void
1227dns_connection::time_cb (ev::timer &w, int revents)
1228{
1229 // servers have to be polled
1230 if (THISNODE->dns_port)
1231 return;
1232
1233 // check for timeouts and (re)transmit
1234 tstamp next = ev::now () + poll_interval;
1235 dns_snd *send = 0;
1236
1237 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
1238 i != vpn->dns_sndpq.end ();
1239 ++i)
1240 {
1241 dns_snd *r = *i;
1242
1243 if (r->timeout <= ev_now ())
1244 {
1245 if (!send)
1246 {
1247 send = r;
1248
1249 r->retry++;
1250 r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor);
1251 //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D
1252
1253 // the following code changes the query section a bit, forcing
1254 // the forwarder to generate a new request
1255 if (r->stdhdr)
1256 encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry);
1257 }
335 } 1258 }
336 else 1259 else
1260 NEXT (r->timeout);
1261 }
1262
1263 if (!send)
1264 {
1265 // generate a new packet, if wise
1266
1267 if (!established)
337 { 1268 {
338 // client 1269 if (vpn->dns_sndpq.empty ())
1270 {
1271 send = new dns_snd (this);
1272
1273 cfg.reset (THISNODE->id);
1274 send->gen_syn_req ();
1275 }
339 } 1276 }
340 } 1277 else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding
341} 1278 && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
342
343bool
344connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
345{
346 if (dns_sndq.size () >= MAX_OUTSTANDING)
347 return false;
348
349 // split vpn packet into dns packets, add to sndq
350 for (int offs = 0; offs < pkt->len; )
351 new dns_req (this, pkt, offs);
352
353 // force transmit
354 dnsv4_cb (dnsv4_tw);
355
356 return true;
357}
358
359void
360connection::dnsv4_cb (time_watcher &w)
361{
362 // check for timeouts and (re)transmit
363 tstamp next = NOW + 60;
364
365 slog (L_NOISE, _("dnsv4, %d packets in send queue\n"), dns_sndq.size ());
366
367 if (dns_sndq.empty ())
368 {
369 next = NOW + 1;
370 // push poll packet
371 }
372
373 for (vector<dns_req *>::iterator i = dns_sndq.begin ();
374 i != dns_sndq.end ();
375 ++i)
376 {
377 dns_req *r = *i;
378
379 if (r->next <= NOW)
380 { 1279 {
381 slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); 1280 if (last_sent + send_interval <= ev_now ())
382 r->retry++; 1281 {
383 r->next = NOW + r->retry; 1282 //printf ("sending data request etc.\n"); //D
1283 if (!snddq.empty () || last_received + 1. > ev_now ())
1284 {
1285 poll_interval = send_interval;
1286 NEXT (ev_now () + send_interval);
1287 }
1288
1289 send = new dns_snd (this);
1290 send->gen_stream_req (sndseq, snddq);
1291 send->timeout = ev_now () + min_latency * conf.dns_timeout_factor;
1292 //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D
1293
1294 sndseq = (sndseq + 1) & SEQNO_MASK;
1295 }
1296 else
1297 NEXT (last_sent + send_interval);
384 } 1298 }
385 1299
386 if (r->next < next) 1300 if (send)
387 next = r->next; 1301 vpn->dns_sndpq.push_back (send);
1302 }
1303
1304 if (send)
388 } 1305 {
1306 last_sent = ev_now ();
1307 sendto (vpn->dnsv4_fd,
1308 send->pkt->at (0), send->pkt->len, 0,
1309 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
1310 }
389 1311
390 printf ("%f next\n", next); 1312 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)",
391 w.start (next); 1313 poll_interval, send_interval, next - ev_now (),
1314 vpn->dns_sndpq.size (), snddq.size (),
1315 rcvpq.size ());
1316
1317 // TODO: no idea when this happens, but when next < ev_now (), we have a problem
1318 // doesn't seem to happen anymore
1319 if (next < ev_now () + 0.001)
1320 next = ev_now () + 0.1;
1321
1322 w.start (next - ev_now ());
392} 1323}
393 1324
394#endif 1325#endif
395 1326

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines