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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines