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