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