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