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