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