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.5 by pcg, Thu Mar 3 07:24:57 2005 UTC vs.
Revision 1.38 by pcg, Tue Apr 19 04:23:38 2005 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-2005 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
6 it under the terms of the GNU General Public License as published by 8 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 9 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 10 (at your 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,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but 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
13 GNU General Public License for more details. 15 GNU General 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
16 along with this program; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
17 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
18*/ 20*/
19 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
20#include "config.h" 26#include "config.h"
21 27
22#if ENABLE_DNS 28#if ENABLE_DNS
23 29
24// dns processing is EXTREMELY ugly. For obvious(?) reasons. 30// dns processing is EXTREMELY ugly. For obvious(?) reasons.
25// it's a hack, use only in emergency situations please. 31// it's a hack, use only in emergency situations please.
26 32
27#include <cstring> 33#include <cstring>
34#include <cassert>
28 35
29#include <sys/types.h> 36#include <sys/types.h>
30#include <sys/socket.h> 37#include <sys/socket.h>
31#include <sys/wait.h> 38#include <sys/wait.h>
32#include <sys/uio.h> 39#include <sys/uio.h>
35#include <unistd.h> 42#include <unistd.h>
36#include <fcntl.h> 43#include <fcntl.h>
37 44
38#include <map> 45#include <map>
39 46
47#include <gmp.h>
48
40#include "netcompat.h" 49#include "netcompat.h"
41 50
42#include "vpn.h" 51#include "vpn.h"
43 52
44#define MIN_RETRY 1. 53#define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data
45#define MAX_RETRY 60. 54#define ACTIVITY_INTERVAL 5.
46 55
47#define MAX_OUTSTANDING 40 // 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
48#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING 61#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog
49#define MAX_RATE 1000 // requests/s
50#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
51 63
52#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
53// 240 leaves about 4 bytes of server reply data 65// 240 leaves about 4 bytes of server reply data
54// every two request byte sless give room for one reply byte 66// every request byte less give room for two reply bytes
55 67
56#define SEQNO_MASK 0xffff 68#define SEQNO_MASK 0x3fff
57 69#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) )
58/*
59
60protocol, in shorthand :)
61
62client -> server <req> ANY?
63server -> client <req> TXT <rep>
64
65<req> is dns64-encoded <client-id:12><recv-seqno:10>[<send-seqno:10><data>]
66<rep> is dns64-encoded <0:12><recv-seqno:10>[<send-seqno:10><data>]
67
68if <client-id> is zero, the connection will be configured:
69
70<0:12><0:4>client-id:12><default-ttl:8><max-size:16><flags:16>
71
72*/
73 70
74#define MAX_LBL_SIZE 63 71#define MAX_LBL_SIZE 63
75#define MAX_PKT_SIZE 512 72#define MAX_PKT_SIZE 512
76 73
74#define RR_TYPE_A 1
75#define RR_TYPE_NULL 10
77#define RR_TYPE_TXT 16 76#define RR_TYPE_TXT 16
78#define RR_TYPE_ANY 255 77#define RR_TYPE_ANY 255
78
79#define RR_CLASS_IN 1 79#define RR_CLASS_IN 1
80 80
81// the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) 81#define CMD_IP_1 207
82struct dns64 82#define CMD_IP_2 46
83{ 83#define CMD_IP_3 236
84 static const char encode_chars[64 + 1]; 84#define CMD_IP_RST 29
85 static s8 decode_chars[256]; 85#define CMD_IP_SYN 113
86#define CMD_IP_REJ 32
86 87
87 static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } 88// works for cmaps up to 255 (not 256!)
88 static int decode_len (int bytes) { return (bytes * 6) / 8; } 89struct charmap
90{
91 enum { INVALID = (u8)255 };
92
93 char encode [256]; // index => char
94 u8 decode [256]; // char => index
95 unsigned int size;
96
97 charmap (const char *cmap);
98};
99
100charmap::charmap (const char *cmap)
101{
102 char *enc = encode;
103 u8 *dec = decode;
104
105 memset (enc, (char) 0, 256);
106 memset (dec, (char)INVALID, 256);
107
108 for (size = 0; cmap [size]; size++)
109 {
110 enc [size] = cmap [size];
111 dec [(u8)enc [size]] = size;
112 }
113
114 assert (size < 256);
115}
116
117#define MAX_DEC_LEN 500
118#define MAX_ENC_LEN (MAX_DEC_LEN * 2)
119#define MAX_LIMBS ((MAX_DEC_LEN * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS)
120
121// ugly. minimum base is 16(!)
122struct basecoder
123{
124 charmap cmap;
125 unsigned int enc_len [MAX_DEC_LEN];
126 unsigned int dec_len [MAX_ENC_LEN];
127
128 unsigned int encode_len (unsigned int len);
129 unsigned int decode_len (unsigned int len);
130
89 static int encode (char *dst, u8 *src, int len); 131 unsigned int encode (char *dst, u8 *src, unsigned int len);
90 static int decode (u8 *dst, char *src, int len); 132 unsigned int decode (u8 *dst, char *src, unsigned int len);
91 133
92 dns64 (); 134 basecoder (const char *cmap);
93} dns64; 135};
94 136
95const char dns64::encode_chars[64 + 1] = "_4B9dLphHzrqQmGjkTbJt5svlZX8xSaReEYfwKgF1DP2W6NyVOU70IouACcMn3i-"; 137basecoder::basecoder (const char *cmap)
96s8 dns64::decode_chars[256]; 138: cmap (cmap)
97
98dns64::dns64 ()
99{ 139{
100 for (int i = 0; i < 64; i++) 140 for (unsigned int len = 0; len < MAX_DEC_LEN; ++len)
101 decode_chars [encode_chars [i]] = i + 1; 141 {
102} 142 u8 src [MAX_DEC_LEN];
143 u8 dst [MAX_ENC_LEN];
103 144
104int dns64::encode (char *dst, u8 *src, int len) 145 memset (src, 255, len);
146
147 mp_limb_t m [MAX_LIMBS];
148 mp_size_t n;
149
150 n = mpn_set_str (m, src, len, 256);
151 n = mpn_get_str (dst, this->cmap.size, m, n);
152
153 for (int i = 0; !dst [i]; ++i)
154 n--;
155
156 enc_len [len] = n;
157 dec_len [n] = len;
158 }
159}
160
161unsigned int basecoder::encode_len (unsigned int len)
105{ 162{
106 // slow, but easy to debug 163 return enc_len [len];
107 char *beg = dst; 164}
108 unsigned int accum, bits = 0; 165
166unsigned int basecoder::decode_len (unsigned int len)
167{
168 while (len && !dec_len [len])
169 --len;
170
171 return dec_len [len];
172}
173
174unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len)
175{
176 if (!len || len > MAX_DEC_LEN)
177 return 0;
178
179 int elen = encode_len (len);
180
181 mp_limb_t m [MAX_LIMBS];
182 mp_size_t n;
183
184 u8 dst_ [MAX_ENC_LEN];
185
186 n = mpn_set_str (m, src, len, 256);
187 n = mpn_get_str (dst_, cmap.size, m, n);
188
189 int plen = elen; // for padding
190
191 while (n < plen)
192 {
193 *dst++ = cmap.encode [0];
194 plen--;
195 }
196
197 for (unsigned int i = n - plen; i < n; ++i)
198 *dst++ = cmap.encode [dst_ [i]];
199
200 return elen;
201}
202
203unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len)
204{
205 if (!len || len > MAX_ENC_LEN)
206 return 0;
207
208 u8 src_ [MAX_ENC_LEN];
209 unsigned int elen = 0;
109 210
110 while (len--) 211 while (len--)
111 { 212 {
112 accum <<= 8; 213 u8 val = cmap.decode [(u8)*src++];
113 accum |= *src++;
114 bits += 8;
115 214
116 while (bits >= 6) 215 if (val != charmap::INVALID)
117 { 216 src_ [elen++] = val;
118 *dst++ = encode_chars [(accum >> (bits - 6)) & 63]; 217 }
119 bits -= 6; 218
120 } 219 int dlen = decode_len (elen);
220
221 mp_limb_t m [MAX_LIMBS];
222 mp_size_t n;
223
224 u8 dst_ [MAX_DEC_LEN];
225
226 n = mpn_set_str (m, src_, elen, cmap.size);
227 n = mpn_get_str (dst_, 256, m, n);
228
229 if (n < dlen)
121 } 230 {
231 memset (dst, 0, dlen - n);
232 memcpy (dst + dlen - n, dst_, n);
233 }
234 else
235 memcpy (dst, dst_ + n - dlen, dlen);
122 236
123 if (bits) 237 return dlen;
124 *dst++ = encode_chars [(accum << (6 - bits)) & 63];
125
126 return dst - beg;
127} 238}
128 239
129int dns64::decode (u8 *dst, char *src, int len) 240#if 0
130{ 241struct test { test (); } test;
131 // slow, but easy to debug
132 u8 *beg = dst;
133 unsigned int accum, bits = 0;
134 242
135 while (len--) 243test::test ()
244{
245 basecoder cdc ("0123456789abcdefghijklmnopqrstuvwxyz");
246
247 u8 in[] = "0123456789abcdefghijklmnopqrstuvwxyz";
248 static char enc[200];
249 static u8 dec[200];
250
251 for (int i = 1; i < 20; i++)
136 { 252 {
137 s8 chr = decode_chars [(u8)*src++]; 253 int elen = cdc.encode (enc, in, i);
254 int dlen = cdc.decode (dec, enc, elen);
138 255
139 if (!chr) 256 printf ("%d>%d>%d (%s>%s)\n", i, elen, dlen, enc, dec);
140 continue;
141
142 accum <<= 6;
143 accum |= chr - 1;
144 bits += 6;
145
146 while (bits >= 8)
147 {
148 *dst++ = accum >> (bits - 8);
149 bits -= 8;
150 }
151 } 257 }
258 abort ();
259}
260#endif
152 261
153 return dst - beg; 262//static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-");
263//static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
264static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
265//static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet
266static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO");
267
268/////////////////////////////////////////////////////////////////////////////
269
270#define HDRSIZE 6
271
272inline void encode_header (char *data, int clientid, int seqno, int retry = 0)
273{
274 seqno &= SEQNO_MASK;
275
276 u8 hdr[3] = {
277 clientid,
278 (seqno >> 8) | (retry << 6),
279 seqno,
280 };
281
282 assert (clientid < 256);
283
284 cdc26.encode (data, hdr, 3);
285}
286
287inline void decode_header (char *data, int &clientid, int &seqno)
288{
289 u8 hdr[3];
290
291 cdc26.decode (hdr, data, HDRSIZE);
292
293 clientid = hdr[0];
294 seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK;
154} 295}
155 296
156///////////////////////////////////////////////////////////////////////////// 297/////////////////////////////////////////////////////////////////////////////
157 298
158struct byte_stream 299struct byte_stream
187} 328}
188 329
189void byte_stream::remove (int count) 330void byte_stream::remove (int count)
190{ 331{
191 if (count > fill) 332 if (count > fill)
192 abort (); 333 assert (count <= fill);
193 334
194 memmove (data, data + count, fill -= count); 335 memmove (data, data + count, fill -= count);
195} 336}
196 337
197bool byte_stream::put (u8 *data, unsigned int datalen) 338bool byte_stream::put (u8 *data, unsigned int datalen)
210 return false; 351 return false;
211 352
212 data [fill++] = pkt->len >> 8; 353 data [fill++] = pkt->len >> 8;
213 data [fill++] = pkt->len; 354 data [fill++] = pkt->len;
214 355
215 memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len; 356 memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len;
216 357
217 return true; 358 return true;
218} 359}
219 360
220vpn_packet *byte_stream::get () 361vpn_packet *byte_stream::get ()
221{ 362{
363 unsigned int len;
364
365 for (;;)
366 {
222 int len = (data [0] << 8) | data [1]; 367 len = (data [0] << 8) | data [1];
223 368
224 printf ("get len %d, fill %d\n", len, fill);//D
225
226 if (len > MAXSIZE && fill >= 2) 369 if (len <= MAXSIZE || fill < 2)
227 abort (); // TODO handle this gracefully, connection reset 370 break;
228 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
229 if (fill < len + 2) 377 if (fill < len + 2)
230 return 0; 378 return 0;
231 379
232 vpn_packet *pkt = new vpn_packet; 380 vpn_packet *pkt = new vpn_packet;
233 381
234 pkt->len = len; 382 pkt->len = len;
235 memcpy (&((*pkt)[0]), data + 2, len); 383 memcpy (pkt->at (0), data + 2, len);
236 remove (len + 2); 384 remove (len + 2);
237 385
238 return pkt; 386 return pkt;
239} 387}
240 388
241///////////////////////////////////////////////////////////////////////////// 389/////////////////////////////////////////////////////////////////////////////
242 390
243#define FLAG_QUERY ( 0 << 15) 391#define FLAG_QUERY ( 0 << 15)
244#define FLAG_RESPONSE ( 1 << 15) 392#define FLAG_RESPONSE ( 1 << 15)
245#define FLAG_OP_MASK (15 << 14) 393#define FLAG_OP_MASK (15 << 11)
246#define FLAG_OP_QUERY ( 0 << 11) 394#define FLAG_OP_QUERY ( 0 << 11)
247#define FLAG_AA ( 1 << 10) 395#define FLAG_AA ( 1 << 10)
248#define FLAG_TC ( 1 << 9) 396#define FLAG_TC ( 1 << 9)
249#define FLAG_RD ( 1 << 8) 397#define FLAG_RD ( 1 << 8)
250#define FLAG_RA ( 1 << 7) 398#define FLAG_RA ( 1 << 7)
257#define FLAG_RCODE_REFUSED ( 5 << 0) 405#define FLAG_RCODE_REFUSED ( 5 << 0)
258 406
259#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) 407#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD)
260#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)
261 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
262struct dns_packet : net_packet 476struct dns_packet : net_packet
263{ 477{
264 u16 id; 478 u16 id;
265 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
266 u16 qdcount, ancount, nscount, arcount; 480 u16 qdcount, ancount, nscount, arcount;
267 481
268 u8 data[MAXSIZE - 6 * 2]; 482 u8 data [MAXSIZE - 6 * 2];
269 483
270 int decode_label (char *data, int size, int &offs); 484 int decode_label (char *data, int size, int &offs);
271}; 485};
272 486
273int dns_packet::decode_label (char *data, int size, int &offs) 487int dns_packet::decode_label (char *data, int size, int &offs)
304 return data - orig; 518 return data - orig;
305} 519}
306 520
307///////////////////////////////////////////////////////////////////////////// 521/////////////////////////////////////////////////////////////////////////////
308 522
309struct dns_req
310{
311 dns_packet *pkt;
312 tstamp next;
313 int retry;
314 connection *conn;
315 int seqno;
316
317 dns_req (connection *c);
318 void gen_stream_req (int seqno, byte_stream *stream);
319};
320
321static u16 dns_id = 12098; // TODO: should be per-vpn 523static u16 dns_id = 0; // TODO: should be per-vpn
322 524
323static u16 next_id () 525static u16 next_id ()
324{ 526{
527 if (!dns_id)
528 dns_id = time (0);
529
325 // the simplest lsfr with periodicity 65535 i could find 530 // the simplest lsfr with periodicity 65535 i could find
326 dns_id = (dns_id << 1) 531 dns_id = (dns_id << 1)
327 | (((dns_id >> 1) 532 | (((dns_id >> 1)
328 ^ (dns_id >> 2) 533 ^ (dns_id >> 2)
329 ^ (dns_id >> 4) 534 ^ (dns_id >> 4)
330 ^ (dns_id >> 15)) & 1); 535 ^ (dns_id >> 15)) & 1);
331 536
332 return dns_id; 537 return dns_id;
333} 538}
334 539
335dns_req::dns_req (connection *c) 540struct dns_rcv;
336: conn (c) 541struct dns_snd;
542
543struct dns_connection
337{ 544{
338 next = 0; 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
585dns_snd::dns_snd (dns_connection *dns)
586: dns (dns)
587{
588 timeout = 0;
339 retry = 0; 589 retry = 0;
590 seqno = 0;
591 sent = NOW;
592 stdhdr = false;
340 593
341 pkt = new dns_packet; 594 pkt = new dns_packet;
342 595
343 pkt->id = next_id (); 596 pkt->id = next_id ();
344} 597}
345 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
346void dns_req::gen_stream_req (int seqno, byte_stream *stream) 627void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
347{ 628{
629 stdhdr = true;
348 this->seqno = seqno; 630 this->seqno = seqno;
631
632 timeout = NOW + INITIAL_TIMEOUT;
349 633
350 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 634 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
351 pkt->qdcount = htons (1); 635 pkt->qdcount = htons (1);
352 636
353 int offs = 6*2; 637 int offs = 6*2;
354 int dlen = MAX_DOMAIN_SIZE - strlen (THISNODE->domain) - 2; 638 int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2);
355 // 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,
356 // 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
357 641
358 u8 data[256]; //TODO
359
360 data[0] = THISNODE->id; //TODO
361 data[1] = seqno >> 8; //TODO
362 data[2] = seqno; //TODO
363
364 int datalen = dns64::decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE) - 3;
365
366 if (datalen > stream->size ())
367 datalen = stream->size ();
368
369 char enc[256], *encp = enc; 642 char enc[256], *encp = enc;
370 643 encode_header (enc, THISNODE->id, seqno);
371 memcpy (data + 3, stream->begin (), datalen); 644
372 int enclen = dns64::encode (enc, data, datalen + 3); 645 int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE);
646
647 if (datalen > stream.size ())
648 datalen = stream.size ();
649
650 int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE;
373 stream->remove (datalen); 651 stream.remove (datalen);
374 652
375 while (enclen) 653 while (enclen)
376 { 654 {
377 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; 655 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE;
378 656
383 encp += lbllen; 661 encp += lbllen;
384 662
385 enclen -= lbllen; 663 enclen -= lbllen;
386 } 664 }
387 665
388 const char *suffix = THISNODE->domain; 666 append_domain (*pkt, offs, dns->c->conf->domain);
389
390 // add tunnel domain
391 for (;;)
392 {
393 const char *end = strchr (suffix, '.');
394
395 if (!end)
396 end = suffix + strlen (suffix);
397
398 int len = end - suffix;
399
400 (*pkt)[offs++] = len;
401 memcpy (&((*pkt)[offs]), suffix, len);
402 offs += len;
403
404 if (!*end)
405 break;
406
407 suffix = end + 1;
408 }
409 667
410 (*pkt)[offs++] = 0; 668 (*pkt)[offs++] = 0;
411 (*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;
412 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 694 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
413 695
414 pkt->len = offs; 696 pkt->len = offs;
415} 697}
416 698
419 int seqno; 701 int seqno;
420 dns_packet *pkt; // reply packet 702 dns_packet *pkt; // reply packet
421 u8 data [MAXSIZE]; // actually part of the reply packet... 703 u8 data [MAXSIZE]; // actually part of the reply packet...
422 int datalen; 704 int datalen;
423 705
424 dns_rcv (int seqno, dns_packet *req, u8 *data, int datalen); 706 dns_rcv (int seqno, u8 *data, int datalen);
425 ~dns_rcv (); 707 ~dns_rcv ();
426}; 708};
427 709
428dns_rcv::dns_rcv (int seqno, dns_packet *req, u8 *data, int datalen) 710dns_rcv::dns_rcv (int seqno, u8 *data, int datalen)
429: seqno (seqno), pkt (new dns_packet), datalen (datalen) 711: seqno (seqno), pkt (new dns_packet), datalen (datalen)
430{ 712{
431 memcpy (this->data, data, datalen); 713 memcpy (this->data, data, datalen);
432 pkt->len = req->len;
433 memcpy (pkt->at (0), req->at (0), req->len);
434} 714}
435 715
436dns_rcv::~dns_rcv () 716dns_rcv::~dns_rcv ()
437{ 717{
438 delete pkt; 718 delete pkt;
439} 719}
440 720
441///////////////////////////////////////////////////////////////////////////// 721/////////////////////////////////////////////////////////////////////////////
442 722
443struct dns_cfg 723dns_connection::dns_connection (connection *c)
724: c (c)
725, rcvdq (MAX_BACKLOG * 2)
726, snddq (MAX_BACKLOG)
727, tw (this, &dns_connection::time_cb)
444{ 728{
445 u8 id1, id2, id3; 729 vpn = c->vpn;
446 u8 def_ttl;
447 u8 unused1;
448 u16 max_size;
449 u8 flags1, flags2;
450};
451 730
731 established = false;
732
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;
739}
740
741dns_connection::~dns_connection ()
742{
743 for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
744 i != rcvpq.end ();
745 ++i)
746 delete *i;
747}
748
452void connection::dnsv4_receive_rep (struct dns_rcv *r) 749void dns_connection::receive_rep (dns_rcv *r)
453{ 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
454 dns_rcvpq.push_back (r); 766 rcvpq.push_back (r);
455 767
456 redo: 768 redo:
457 769
770 // find next packet
458 for (vector<dns_rcv *>::iterator i = dns_rcvpq.begin (); 771 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); )
459 i != dns_rcvpq.end ();
460 ++i)
461 if (dns_rcvseq == (*i)->seqno) 772 if (SEQNO_EQ (rcvseq, (*i)->seqno))
462 { 773 {
774 // enter the packet into our input stream
463 dns_rcv *r = *i; 775 r = *i;
464 776
777 // remove the oldest packet, look forward, as it's oldest first
778 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j)
779 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW))
780 {
781 delete *j;
782 rcvpq.erase (j);
783 break;
784 }
785
465 dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; 786 rcvseq = (rcvseq + 1) & SEQNO_MASK;
466 787
467 if (!dns_snddq && !dns_rcvdq) 788 if (!rcvdq.put (r->data, r->datalen))
468 { 789 {
469 dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); 790 slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)");
470 dns_snddq = new byte_stream (MAX_BACKLOG); 791 abort (); // MUST never overflow, can be caused by data corruption, TODO
471
472 dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4);
473 } 792 }
474 793
475 if (!dns_rcvdq->put (r->data, r->datalen))
476 abort (); // MUST never overflow, can be caused by data corruption, TODO
477
478 while (vpn_packet *pkt = dns_rcvdq->get ()) 794 while (vpn_packet *pkt = rcvdq.get ())
479 { 795 {
480 sockinfo si; 796 sockinfo si;
481 si.host = 0; si.port = 0; si.prot = PROT_DNSv4; 797 si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4;
482 798
483 vpn->recv_vpn_packet (pkt, si); 799 vpn->recv_vpn_packet (pkt, si);
800
801 delete pkt;
484 } 802 }
485 } 803
486 else if ((u32)dns_rcvseq - MAX_WINDOW - (u32)(*i)->seqno < MAX_WINDOW * 2) 804 // check for further packets
487 {
488 dns_rcvpq.erase (i);
489 goto redo; 805 goto redo;
490 } 806 }
491} 807}
492 808
493dns_packet * 809void
494vpn::dnsv4_server (dns_packet *pkt) 810vpn::dnsv4_server (dns_packet &pkt)
495{ 811{
496 u16 flags = ntohs (pkt->flags); 812 u16 flags = ntohs (pkt.flags);
497 813
498 //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len);
499 int offs = 6 * 2; // skip header 814 int offs = 6 * 2; // skip header
500 815
501 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 816 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
502 817
503 if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) 818 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK))
504 && pkt->qdcount == htons (1)) 819 && pkt.qdcount == htons (1))
505 { 820 {
506 char qname[MAXSIZE]; 821 char qname [MAXSIZE];
507 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 822 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
508 823
509 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; 824 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
510 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; 825 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
511 826
512 pkt->qdcount = htons (1); 827 pkt.qdcount = htons (1);
513 pkt->ancount = 0; 828 pkt.ancount = 0;
514 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
515 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
516 831
517 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); 832 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL);
518 833
519 int dlen = strlen (THISNODE->domain); 834 int dlen = strlen (THISNODE->domain);
520 835
521 if (qclass == RR_CLASS_IN 836 if (qclass == RR_CLASS_IN
522 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT)
523 && qlen > dlen + 1 837 && qlen > dlen + 1
524 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) 838 && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen))
525 { 839 {
526 // correct class, domain: parse 840 // now generate reply
527 u8 data[MAXSIZE]; 841 pkt.ancount = htons (1); // one answer RR
528 int datalen = dns64::decode (data, qname, qlen - dlen - 1); 842 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
529 843
530 int client = data[0]; 844 if ((qtype == RR_TYPE_ANY
531 int seqno = ((data[1] << 8) | data[2]) & SEQNO_MASK; 845 || qtype == RR_TYPE_TXT
532 846 || qtype == RR_TYPE_NULL)
533 if (0 < client && client <= conns.size ()) 847 && qlen > dlen + 1 + HDRSIZE)
534 { 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 {
535 connection *c = conns [client - 1]; 858 connection *c = conns [client - 1];
859 dns_connection *dns = c->dns;
860 dns_rcv *rcv;
536 861
537 for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.begin (); 862 if (dns)
538 i != c->dns_rcvpq.end ();
539 ++i) 863 {
864 for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); )
540 if ((*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
541 { 886 {
542 // already seen that request, just reply with the original reply 887 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
543 dns_rcv *r = *i;
544 888
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
894
895 int rdlen_offs = offs += 2;
896
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
545 offs = r->pkt->len; 949 rcv->pkt->len = offs;
546 memcpy (pkt->at (0), r->pkt->at (0), offs); 950 memcpy (rcv->pkt->at (0), pkt.at (0), offs);
547 goto duplicate_request; 951 }
548 } 952 }
549 953
550 // new packet, queue 954 duplicate_request: ;
551 c->dnsv4_receive_rep (new dns_rcv (seqno, pkt, data + 3, datalen - 3)); 955 }
552 956 else
553 // now generate reply
554 pkt->ancount = htons (1); // one answer RR
555 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);
556 965
557 (*pkt) [offs++] = 0xc0;
558 (*pkt) [offs++] = 6 * 2; // same as in query section 966 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
559 967
560 (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; 968 pkt [offs++] = RR_TYPE_A >> 8; pkt [offs++] = RR_TYPE_A; // type
561 (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; 969 pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
562
563 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; 970 pkt [offs++] = 0; pkt [offs++] = 0;
564 (*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
565 973
566 int dlen = MAX_PKT_SIZE - offs - 2; 974 slog (L_INFO, _("DNS: client %d connects"), client);
567 975
568 // 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;
569 dlen -= qlen; 977 pkt [offs++] = CMD_IP_REJ;
570 978
571 int rdlen_offs = offs += 2; 979 if (0 < client && client <= conns.size ())
572
573 while (c->dns_snddq
574 && !c->dns_snddq->empty ()
575 && dlen > 1)
576 { 980 {
577 int txtlen = dlen <= 255 ? dlen - 1 : 255; 981 connection *c = conns [client - 1];
578 982
579 if (txtlen > c->dns_snddq->size ()) 983 if (cfg.valid ())
580 txtlen = c->dns_snddq->size (); 984 {
985 pkt [offs - 1] = CMD_IP_SYN;
581 986
582 (*pkt)[offs++] = txtlen; 987 delete c->dns;
583 memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen); 988 c->dns = new dns_connection (c);
584 offs += txtlen; 989 c->dns->cfg = cfg;
585 c->dns_snddq->remove (txtlen); 990 }
586
587 dlen -= txtlen + 1;
588 } 991 }
589
590 // avoid empty TXT rdata
591 if (offs == rdlen_offs)
592 (*pkt)[offs++] = 0;
593
594 int rdlen = offs - rdlen_offs;
595
596 (*pkt) [rdlen_offs - 2] = rdlen >> 8;
597 (*pkt) [rdlen_offs - 1] = rdlen;
598
599 duplicate_request: ;
600 } 992 }
601 else
602 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
603 } 993 }
604 }
605 else
606 offs = pkt->len;
607 994
608 pkt->len = offs; 995 pkt.len = offs;
609 return pkt; 996 }
610} 997}
611 998
612void 999void
613vpn::dnsv4_client (dns_packet *pkt) 1000vpn::dnsv4_client (dns_packet &pkt)
614{ 1001{
615 u16 flags = ntohs (pkt->flags); 1002 u16 flags = ntohs (pkt.flags);
616 int offs = 6 * 2; // skip header 1003 int offs = 6 * 2; // skip header
617 1004
618 pkt->qdcount = ntohs (pkt->qdcount); 1005 pkt.qdcount = ntohs (pkt.qdcount);
619 pkt->ancount = ntohs (pkt->ancount); 1006 pkt.ancount = ntohs (pkt.ancount);
620 1007
621 // go through our request list and find the corresponding request 1008 // go through our request list and find the corresponding request
622 for (vector<dns_req *>::iterator i = dns_sndpq.begin (); 1009 for (vector<dns_snd *>::iterator i = dns_sndpq.begin ();
623 i != dns_sndpq.end (); 1010 i != dns_sndpq.end ();
624 ++i) 1011 ++i)
625 if ((*i)->pkt->id == pkt->id) 1012 if ((*i)->pkt->id == pkt.id)
626 { 1013 {
1014 dns_connection *dns = (*i)->dns;
627 connection *c = (*i)->conn; 1015 connection *c = dns->c;
628 int seqno = (*i)->seqno; 1016 int seqno = (*i)->seqno;
629 u8 data[MAXSIZE], *datap = data; 1017 u8 data[MAXSIZE], *datap = data;
630 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
631 delete *i; 1044 delete *i;
632 dns_sndpq.erase (i); 1045 dns_sndpq.erase (i);
633 1046
634 if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) 1047 if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK))
635 { 1048 {
636 char qname[MAXSIZE]; 1049 char qname[MAXSIZE];
637 1050
638 while (pkt->qdcount-- && offs < MAXSIZE - 4) 1051 while (pkt.qdcount-- && offs < MAXSIZE - 4)
639 { 1052 {
640 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 1053 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
641 offs += 4; // skip qtype, qclass 1054 offs += 4; // skip qtype, qclass
642 } 1055 }
643 1056
644 while (pkt->ancount-- && offs < MAXSIZE - 10) 1057 while (pkt.ancount-- && offs < MAXSIZE - 10 && datap)
645 { 1058 {
646 pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 1059 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
647 1060
648 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; 1061 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
649 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; 1062 u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
650 u32 ttl = (*pkt) [offs++] << 24; 1063 u32 ttl = pkt [offs++] << 24;
651 ttl |= (*pkt) [offs++] << 16; 1064 ttl |= pkt [offs++] << 16;
652 ttl |= (*pkt) [offs++] << 8; 1065 ttl |= pkt [offs++] << 8;
653 ttl |= (*pkt) [offs++]; 1066 ttl |= pkt [offs++];
654
655 u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; 1067 u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++];
656 1068
657 if (rdlen <= MAXSIZE - offs) 1069 if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT)
658 { 1070 {
659 // decode bytes, finally 1071 if (rdlen <= MAXSIZE - offs)
660
661 while (rdlen)
662 { 1072 {
1073 // decode bytes, finally
1074
1075 while (rdlen)
1076 {
663 int txtlen = (*pkt) [offs++]; 1077 int txtlen = pkt [offs++];
664 1078
665 assert (txtlen + offs < MAXSIZE - 1); 1079 assert (txtlen + offs < MAXSIZE - 1);
666 1080
667 memcpy (datap, pkt->at (offs), txtlen); 1081 memcpy (datap, pkt.at (offs), txtlen);
668 datap += txtlen; offs += txtlen; 1082 datap += txtlen; offs += txtlen;
669 1083
670 rdlen -= txtlen + 1; 1084 rdlen -= txtlen + 1;
1085 }
671 } 1086 }
672
673 } 1087 }
1088 else if (qtype == RR_TYPE_A)
1089 {
1090 u8 ip [4];
674 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 }
1130
1131 int client, rseqno;
1132 decode_header (qname, client, rseqno);
1133
1134 if (client != THISNODE->id)
1135 {
1136 slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring"));
1137 datap = 0;
1138 }
1139 else if (rseqno != seqno)
1140 {
1141 slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?"));
1142 datap = 0;
1143 }
675 } 1144 }
676 } 1145 }
677 1146
1147 // todo: pkt now used
678 if (datap != data) 1148 if (datap)
679 printf ("%02x %02x %02x %02x\n",
680 data[0],
681 data[1],
682 data[2],
683 data[3]);
684
685 printf ("recv %d,%d\n", pkt->id, seqno, datap - data);//D
686 c->dnsv4_receive_rep (new dns_rcv (seqno, pkt, data, datap - data)); 1149 dns->receive_rep (new dns_rcv (seqno, data, datap - data));
687 1150
688 break; 1151 break;
689 } 1152 }
690
691 delete pkt;
692} 1153}
693 1154
694void 1155void
695vpn::dnsv4_ev (io_watcher &w, short revents) 1156vpn::dnsv4_ev (io_watcher &w, short revents)
696{ 1157{
698 { 1159 {
699 dns_packet *pkt = new dns_packet; 1160 dns_packet *pkt = new dns_packet;
700 struct sockaddr_in sa; 1161 struct sockaddr_in sa;
701 socklen_t sa_len = sizeof (sa); 1162 socklen_t sa_len = sizeof (sa);
702 1163
703 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);
704 1165
705 if (pkt->len > 0) 1166 if (pkt->len > 0)
706 { 1167 {
707 if (pkt->flags & htons (FLAG_TC)) 1168 if (ntohs (pkt->flags) & FLAG_RESPONSE)
1169 dnsv4_client (*pkt);
1170 else
708 { 1171 {
709 slog (L_WARN, _("DNS request/response truncated, check protocol settings.")); 1172 dnsv4_server (*pkt);
710 //TODO connection reset 1173 sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len);
711 } 1174 }
712 1175
713 if (THISNODE->dns_port) 1176 delete pkt;
714 {
715 pkt = dnsv4_server (pkt);
716 sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len);
717 }
718 else
719 dnsv4_client (pkt);
720 } 1177 }
721 } 1178 }
722} 1179}
723 1180
724bool 1181bool
725connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 1182vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
726{ 1183{
727 // never initialized 1184 int client = ntohl (si.host);
728 if (!dns_snddq && !dns_rcvdq)
729 {
730 dns_rcvdq = new byte_stream (MAX_BACKLOG * 2);
731 dns_snddq = new byte_stream (MAX_BACKLOG);
732 1185
733 //dns_rcvseq = dns_sndseq = 0; 1186 assert (0 < client && client <= conns.size ());
734 1187
735 dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); 1188 connection *c = conns [client - 1];
736 } 1189
1190 if (!c->dns)
1191 c->dns = new dns_connection (c);
737 1192
738 if (!dns_snddq->put (pkt)) 1193 if (c->dns->snddq.put (pkt))
739 return false; 1194 c->dns->tw.trigger ();
740 1195
741 // start timer if neccessary 1196 // always return true even if the buffer overflows
742 if (!THISNODE->dns_port && !dnsv4_tw.active)
743 dnsv4_cb (dnsv4_tw);
744
745 return true; 1197 return true;
746} 1198}
747 1199
748void 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)
1207
1208void
749connection::dnsv4_cb (time_watcher &w) 1209dns_connection::time_cb (time_watcher &w)
750{ 1210{
1211 // servers have to be polled
1212 if (THISNODE->dns_port)
1213 return;
1214
751 // check for timeouts and (re)transmit 1215 // check for timeouts and (re)transmit
752 tstamp next = NOW + 60; 1216 tstamp next = NOW + poll_interval;
753 dns_req *send = 0; 1217 dns_snd *send = 0;
754 1218
755 for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); 1219 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
756 i != vpn->dns_sndpq.end (); 1220 i != vpn->dns_sndpq.end ();
757 ++i) 1221 ++i)
758 { 1222 {
759 dns_req *r = *i; 1223 dns_snd *r = *i;
760 1224
761 if (r->next <= NOW) 1225 if (r->timeout <= NOW)
762 { 1226 {
763 if (!send) 1227 if (!send)
764 { 1228 {
765 send = r; 1229 send = r;
766 1230
767 if (r->retry)//D
768 printf ("req %d, retry %d\n", r->pkt->id, r->retry);
769 r->retry++; 1231 r->retry++;
770 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 }
771 } 1241 }
772 } 1242 }
773 1243 else
774 if (r->next < next) 1244 NEXT (r->timeout);
775 next = r->next;
776 } 1245 }
777 1246
778 if (!send 1247 if (!send)
779 && vpn->dns_sndpq.size () < MAX_OUTSTANDING)
780 { 1248 {
1249 // generate a new packet, if wise
1250
1251 if (!established)
1252 {
1253 if (vpn->dns_sndpq.empty ())
1254 {
781 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);
782 send->gen_stream_req (dns_sndseq, dns_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)
783 vpn->dns_sndpq.push_back (send); 1284 vpn->dns_sndpq.push_back (send);
784
785 dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK;
786 } 1285 }
787
788 tstamp min_next = NOW + (1. / (tstamp)MAX_RATE);
789 1286
790 if (send) 1287 if (send)
791 { 1288 {
792 dns_packet *pkt = send->pkt; 1289 last_sent = NOW;
793 1290 sendto (vpn->dnsv4_fd,
794 next = min_next; 1291 send->pkt->at (0), send->pkt->len, 0,
795 1292 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
796 sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ());
797 } 1293 }
798 else if (next < min_next) 1294
799 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;
800 1304
801 w.start (next); 1305 w.start (next);
802} 1306}
803 1307
804#endif 1308#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines