ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_dns.C
(Generate patch)

Comparing gvpe/src/vpn_dns.C (file contents):
Revision 1.2 by pcg, Tue Mar 1 04:38:21 2005 UTC vs.
Revision 1.7 by pcg, Thu Mar 3 16:54:34 2005 UTC

1/* 1/*
2 vpn_dns.C -- handle the dns tunnel part of the protocol. 2 vpn_dns.C -- handle the dns tunnel part of the protocol.
3 Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE.
6
5 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 8 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 9 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 10 (at your option) any later version.
9 11
10 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 15 GNU General Public License for more details.
14 16
15 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/ 20*/
19 21
20#include "config.h" 22#include "config.h"
21 23
39 41
40#include "netcompat.h" 42#include "netcompat.h"
41 43
42#include "vpn.h" 44#include "vpn.h"
43 45
44#if ENABLE_HTTP_PROXY
45# include "conf.h"
46#endif
47
48#define MIN_RETRY 1. 46#define MIN_RETRY 1.
49#define MAX_RETRY 60. 47#define MAX_RETRY 60.
50 48
51#define SERVER conf.dns_port 49#define MAX_OUTSTANDING 40 // max. outstanding requests
50#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING
51#define MAX_RATE 1000 // requests/s
52#define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE
53
54#define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well
55// 240 leaves about 4 bytes of server reply data
56// every two request byte sless give room for one reply byte
57
58// seqno has 12 bits, but the lower bit is always left as zero
59// as bind caches ttl=0 records and we have to generate
60// sequence numbers that always differ case-insensitively
61#define SEQNO_MASK 0x07ff
52 62
53/* 63/*
54 64
55protocol, in shorthand :) 65protocol, in shorthand :)
56 66
79 static const char encode_chars[64 + 1]; 89 static const char encode_chars[64 + 1];
80 static s8 decode_chars[256]; 90 static s8 decode_chars[256];
81 91
82 static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } 92 static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; }
83 static int decode_len (int bytes) { return (bytes * 6) / 8; } 93 static int decode_len (int bytes) { return (bytes * 6) / 8; }
84 static void encode (char *dst, u8 *src, int len); 94 static int encode (char *dst, u8 *src, int len);
85 static void decode (u8 *dst, char *src, int len); 95 static int decode (u8 *dst, char *src, int len);
86 96
87 dns64 (); 97 dns64 ();
88} dns64; 98} dns64;
89 99
90const char dns64::encode_chars[64 + 1] = "0123456789-abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 100// the following sequence has been crafted to
101// a) look somewhat random
102// b) the even (and odd) indices never share the same character as upper/lowercase
103const char dns64::encode_chars[64 + 1] = "_-dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI";
91s8 dns64::decode_chars[256]; 104s8 dns64::decode_chars[256];
92 105
93dns64::dns64 () 106dns64::dns64 ()
94{ 107{
95 for (int i = 0; i < 64; i++) 108 for (int i = 0; i < 64; i++)
96 decode_chars [encode_chars [i]] = i + 1; 109 decode_chars [encode_chars [i]] = i + 1;
97} 110}
98 111
99void dns64::encode (char *dst, u8 *src, int len) 112int dns64::encode (char *dst, u8 *src, int len)
100{ 113{
101 // slow, but easy to debug 114 // slow, but easy to debug
115 char *beg = dst;
102 unsigned int accum, bits = 0; 116 unsigned int accum, bits = 0;
103 117
104 while (len--) 118 while (len--)
105 { 119 {
106 accum <<= 8; 120 accum <<= 8;
114 } 128 }
115 } 129 }
116 130
117 if (bits) 131 if (bits)
118 *dst++ = encode_chars [(accum << (6 - bits)) & 63]; 132 *dst++ = encode_chars [(accum << (6 - bits)) & 63];
119}
120 133
134 return dst - beg;
135}
136
121void dns64::decode (u8 *dst, char *src, int len) 137int dns64::decode (u8 *dst, char *src, int len)
122{ 138{
123 // slow, but easy to debug 139 // slow, but easy to debug
140 u8 *beg = dst;
124 unsigned int accum, bits = 0; 141 unsigned int accum, bits = 0;
125 142
126 while (len--) 143 while (len--)
127 { 144 {
128 s8 chr = decode_chars [(u8)*src++]; 145 s8 chr = decode_chars [(u8)*src++];
129 146
130
131 if (!chr) 147 if (!chr)
132 break; 148 continue;
133 149
134 accum <<= 6; 150 accum <<= 6;
135 accum |= chr - 1; 151 accum |= chr - 1;
136 bits += 6; 152 bits += 6;
137 153
139 { 155 {
140 *dst++ = accum >> (bits - 8); 156 *dst++ = accum >> (bits - 8);
141 bits -= 8; 157 bits -= 8;
142 } 158 }
143 } 159 }
160
161 return dst - beg;
144} 162}
163
164/////////////////////////////////////////////////////////////////////////////
165
166struct byte_stream
167{
168 u8 *data;
169 int maxsize;
170 int fill;
171
172 byte_stream (int maxsize);
173 ~byte_stream ();
174
175 bool empty () { return !fill; }
176 int size () { return fill; }
177
178 bool put (u8 *data, unsigned int datalen);
179 bool put (vpn_packet *pkt);
180 vpn_packet *get ();
181
182 u8 *begin () { return data; }
183 void remove (int count);
184};
185
186byte_stream::byte_stream (int maxsize)
187: maxsize (maxsize), fill (0)
188{
189 data = new u8 [maxsize];
190}
191
192byte_stream::~byte_stream ()
193{
194 delete data;
195}
196
197void byte_stream::remove (int count)
198{
199 if (count > fill)
200 abort ();
201
202 memmove (data, data + count, fill -= count);
203}
204
205bool byte_stream::put (u8 *data, unsigned int datalen)
206{
207 if (maxsize - fill < datalen)
208 return false;
209
210 memcpy (this->data + fill, data, datalen); fill += datalen;
211
212 return true;
213}
214
215bool byte_stream::put (vpn_packet *pkt)
216{
217 if (maxsize - fill < pkt->len + 2)
218 return false;
219
220 data [fill++] = pkt->len >> 8;
221 data [fill++] = pkt->len;
222
223 memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len;
224
225 return true;
226}
227
228vpn_packet *byte_stream::get ()
229{
230 int len = (data [0] << 8) | data [1];
231
232 if (len > MAXSIZE && fill >= 2)
233 abort (); // TODO handle this gracefully, connection reset
234
235 if (fill < len + 2)
236 return 0;
237
238 vpn_packet *pkt = new vpn_packet;
239
240 pkt->len = len;
241 memcpy (&((*pkt)[0]), data + 2, len);
242 remove (len + 2);
243
244 return pkt;
245}
246
247/////////////////////////////////////////////////////////////////////////////
145 248
146#define FLAG_QUERY ( 0 << 15) 249#define FLAG_QUERY ( 0 << 15)
147#define FLAG_RESPONSE ( 1 << 15) 250#define FLAG_RESPONSE ( 1 << 15)
148#define FLAG_OP_MASK (15 << 14) 251#define FLAG_OP_MASK (15 << 14)
149#define FLAG_OP_QUERY ( 0 << 11) 252#define FLAG_OP_QUERY ( 0 << 11)
150#define FLAG_AA ( 1 << 10) 253#define FLAG_AA ( 1 << 10)
151#define FLAG_TC ( 1 << 9) 254#define FLAG_TC ( 1 << 9)
152#define FLAG_RD ( 1 << 8) 255#define FLAG_RD ( 1 << 8)
153#define FLAG_RA ( 1 << 7) 256#define FLAG_RA ( 1 << 7)
257#define FLAG_AUTH ( 1 << 5)
154#define FLAG_RCODE_MASK (15 << 0) 258#define FLAG_RCODE_MASK (15 << 0)
155#define FLAG_RCODE_OK ( 0 << 0) 259#define FLAG_RCODE_OK ( 0 << 0)
156#define FLAG_RCODE_FORMERR ( 1 << 0) 260#define FLAG_RCODE_FORMERR ( 1 << 0)
157#define FLAG_RCODE_SERVFAIL ( 2 << 0) 261#define FLAG_RCODE_SERVFAIL ( 2 << 0)
158#define FLAG_RCODE_NXDOMAIN ( 3 << 0) 262#define FLAG_RCODE_NXDOMAIN ( 3 << 0)
159#define FLAG_RCODE_REFUSED ( 5 << 0) 263#define FLAG_RCODE_REFUSED ( 5 << 0)
160 264
161#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) 265#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD)
162#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD) 266#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA)
163 267
164struct dns_packet : net_packet 268struct dns_packet : net_packet
165{ 269{
166 u16 id; 270 u16 id;
167 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 271 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4
204 } 308 }
205 309
206 return data - orig; 310 return data - orig;
207} 311}
208 312
313/////////////////////////////////////////////////////////////////////////////
314
315struct dns_req
316{
317 dns_packet *pkt;
318 tstamp next;
319 int retry;
320 connection *conn;
321 int seqno;
322
323 dns_req (connection *c);
324 void gen_stream_req (int seqno, byte_stream *stream);
325};
326
327static u16 dns_id = 12098; // TODO: should be per-vpn
328
329static u16 next_id ()
330{
331 // the simplest lsfr with periodicity 65535 i could find
332 dns_id = (dns_id << 1)
333 | (((dns_id >> 1)
334 ^ (dns_id >> 2)
335 ^ (dns_id >> 4)
336 ^ (dns_id >> 15)) & 1);
337
338 return dns_id;
339}
340
341dns_req::dns_req (connection *c)
342: conn (c)
343{
344 next = 0;
345 retry = 0;
346
347 pkt = new dns_packet;
348
349 pkt->id = next_id ();
350}
351
352void dns_req::gen_stream_req (int seqno, byte_stream *stream)
353{
354 this->seqno = seqno;
355
356 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
357 pkt->qdcount = htons (1);
358
359 int offs = 6*2;
360 int dlen = MAX_DOMAIN_SIZE - strlen (THISNODE->domain) - 2;
361 // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well,
362 // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra
363
364 u8 data[256]; //TODO
365
366 data[0] = THISNODE->id; //TODO
367 data[1] = seqno >> 7; //TODO
368 data[2] = seqno << 1; //TODO
369
370 int datalen = dns64::decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE) - 3;
371
372 if (datalen > stream->size ())
373 datalen = stream->size ();
374
375 char enc[256], *encp = enc;
376
377 memcpy (data + 3, stream->begin (), datalen);
378 int enclen = dns64::encode (enc, data, datalen + 3);
379 stream->remove (datalen);
380
381 while (enclen)
382 {
383 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE;
384
385 (*pkt)[offs++] = lbllen;
386 memcpy (pkt->at (offs), encp, lbllen);
387
388 offs += lbllen;
389 encp += lbllen;
390
391 enclen -= lbllen;
392 }
393
394 const char *suffix = THISNODE->domain;
395
396 // add tunnel domain
397 for (;;)
398 {
399 const char *end = strchr (suffix, '.');
400
401 if (!end)
402 end = suffix + strlen (suffix);
403
404 int len = end - suffix;
405
406 (*pkt)[offs++] = len;
407 memcpy (&((*pkt)[offs]), suffix, len);
408 offs += len;
409
410 if (!*end)
411 break;
412
413 suffix = end + 1;
414 }
415
416 (*pkt)[offs++] = 0;
417 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY;
418 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
419
420 pkt->len = offs;
421}
422
423struct dns_rcv
424{
425 int seqno;
426 dns_packet *pkt; // reply packet
427 u8 data [MAXSIZE]; // actually part of the reply packet...
428 int datalen;
429
430 dns_rcv (int seqno, u8 *data, int datalen);
431 ~dns_rcv ();
432};
433
434dns_rcv::dns_rcv (int seqno, u8 *data, int datalen)
435: seqno (seqno), pkt (new dns_packet), datalen (datalen)
436{
437 memcpy (this->data, data, datalen);
438}
439
440dns_rcv::~dns_rcv ()
441{
442 delete pkt;
443}
444
445/////////////////////////////////////////////////////////////////////////////
446
209struct dns_cfg 447struct dns_cfg
210{ 448{
211 u8 id1, id2, id3; 449 u8 id1, id2, id3;
212 u8 def_ttl; 450 u8 def_ttl;
213 u8 unused1; 451 u8 unused1;
214 u16 max_size; 452 u16 max_size;
215 u8 flags1, flags2; 453 u8 flags1, flags2;
216}; 454};
217 455
218// reply to client-poll 456void connection::dnsv4_receive_rep (struct dns_rcv *r)
219void dnsv4_poll (dns_packet *pkt, int &offs)
220{ 457{
221 int dlen = MAX_PKT_SIZE - offs - 2; 458 dns_rcvpq.push_back (r);
222 459
223 (*pkt) [offs++] = 0; 460 redo:
224 (*pkt) [offs++] = 5;
225 memcpy (&((*pkt)[offs]), "\01H\02\xff\x00", 5);
226 offs += 5;
227 461
228 pkt->ancount = htons (1); 462 for (vector<dns_rcv *>::iterator i = dns_rcvpq.begin ();
463 i != dns_rcvpq.end ();
464 ++i)
465 if (dns_rcvseq == (*i)->seqno)
466 {
467 dns_rcv *r = *i;
468
469 dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK;
470
471 if (!dns_snddq && !dns_rcvdq)
472 {
473 dns_rcvdq = new byte_stream (MAX_BACKLOG * 2);
474 dns_snddq = new byte_stream (MAX_BACKLOG);
475
476 dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4);
477 }
478
479 if (!dns_rcvdq->put (r->data, r->datalen))
480 abort (); // MUST never overflow, can be caused by data corruption, TODO
481
482 while (vpn_packet *pkt = dns_rcvdq->get ())
483 {
484 sockinfo si;
485 si.host = 0; si.port = 0; si.prot = PROT_DNSv4;
486
487 vpn->recv_vpn_packet (pkt, si);
488 }
489 }
490 else if ((u32)(*i)->seqno - (u32)dns_rcvseq + MAX_WINDOW > MAX_WINDOW * 2)
491 {
492 //D
493 //abort();
494 printf ("%d erasing %d (%d)\n", THISNODE->id, (u32)(*i)->seqno, dns_rcvseq);
495 dns_rcvpq.erase (i);
496 goto redo;
497 }
498}
499
500dns_packet *
501vpn::dnsv4_server (dns_packet *pkt)
502{
503 u16 flags = ntohs (pkt->flags);
504
505 //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len);
506 int offs = 6 * 2; // skip header
507
229 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); 508 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
230 509
231 printf ("we have room for %d bytes\n", dlen); 510 if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC))
511 && pkt->qdcount == htons (1))
512 {
513 char qname[MAXSIZE];
514 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs);
515
516 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++];
517 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++];
518
519 pkt->qdcount = htons (1);
520 pkt->ancount = 0;
521 pkt->nscount = 0; // should be self, as other nameservers reply like this
522 pkt->arcount = 0; // a record for self, as other nameservers reply like this
523
524 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN);
525
526 int dlen = strlen (THISNODE->domain);
527
528 if (qclass == RR_CLASS_IN
529 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT)
530 && qlen > dlen + 1
531 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen))
532 {
533 // correct class, domain: parse
534 u8 data[MAXSIZE];
535 int datalen = dns64::decode (data, qname, qlen - dlen - 1);
536
537 int client = data[0];
538 int seqno = ((data[1] << 7) | (data[2] >> 1)) & SEQNO_MASK;
539
540 if (0 < client && client <= conns.size ())
541 {
542 connection *c = conns [client - 1];
543
544 redo:
545
546 for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.begin ();
547 i != c->dns_rcvpq.end ();
548 ++i)
549 if ((*i)->seqno == seqno)
550 {
551 // already seen that request: simply reply with the cached reply
552 dns_rcv *r = *i;
553
554 printf ("DUPLICATE %d\n", htons (r->pkt->id));//D
555
556 offs = r->pkt->len;
557 memcpy (pkt->at (0), r->pkt->at (0), offs);
558 goto duplicate_request;
559 }
560
561 // new packet, queue
562 dns_rcv *rcv = new dns_rcv (seqno, data + 3, datalen - 3);
563 c->dnsv4_receive_rep (rcv);
564
565 // now generate reply
566 pkt->ancount = htons (1); // one answer RR
567 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
568
569 (*pkt) [offs++] = 0xc0;
570 (*pkt) [offs++] = 6 * 2; // same as in query section
571
572 (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT;
573 (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN;
574
575 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0;
576 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL
577
578 int dlen = MAX_PKT_SIZE - offs - 2;
579
580 // bind doesn't compress well, so reduce further by one label length
581 dlen -= qlen;
582
583 int rdlen_offs = offs += 2;
584
585 while (c->dns_snddq
586 && !c->dns_snddq->empty ()
587 && dlen > 1)
588 {
589 int txtlen = dlen <= 255 ? dlen - 1 : 255;
590
591 if (txtlen > c->dns_snddq->size ())
592 txtlen = c->dns_snddq->size ();
593
594 (*pkt)[offs++] = txtlen;
595 memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen);
596 offs += txtlen;
597 c->dns_snddq->remove (txtlen);
598
599 dlen -= txtlen + 1;
600 }
601
602 // avoid empty TXT rdata
603 if (offs == rdlen_offs)
604 (*pkt)[offs++] = 0;
605
606 int rdlen = offs - rdlen_offs;
607
608 (*pkt) [rdlen_offs - 2] = rdlen >> 8;
609 (*pkt) [rdlen_offs - 1] = rdlen;
610
611 // now update dns_rcv copy
612 rcv->pkt->len = offs;
613 memcpy (rcv->pkt->at (0), pkt->at (0), offs);
614
615 duplicate_request: ;
616 }
617 else
618 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
619 }
620
621 pkt->len = offs;
622 }
623
624 return pkt;
625}
626
627void
628vpn::dnsv4_client (dns_packet *pkt)
629{
630 u16 flags = ntohs (pkt->flags);
631 int offs = 6 * 2; // skip header
632
633 pkt->qdcount = ntohs (pkt->qdcount);
634 pkt->ancount = ntohs (pkt->ancount);
635
636 // go through our request list and find the corresponding request
637 for (vector<dns_req *>::iterator i = dns_sndpq.begin ();
638 i != dns_sndpq.end ();
639 ++i)
640 if ((*i)->pkt->id == pkt->id)
641 {
642 connection *c = (*i)->conn;
643 int seqno = (*i)->seqno;
644 u8 data[MAXSIZE], *datap = data;
645
646 delete *i;
647 dns_sndpq.erase (i);
648
649 if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC))
650 {
651 char qname[MAXSIZE];
652
653 while (pkt->qdcount-- && offs < MAXSIZE - 4)
654 {
655 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs);
656 offs += 4; // skip qtype, qclass
657 }
658
659 while (pkt->ancount-- && offs < MAXSIZE - 10)
660 {
661 pkt->decode_label ((char *)qname, MAXSIZE - offs, offs);
662
663 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++];
664 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++];
665 u32 ttl = (*pkt) [offs++] << 24;
666 ttl |= (*pkt) [offs++] << 16;
667 ttl |= (*pkt) [offs++] << 8;
668 ttl |= (*pkt) [offs++];
669
670 u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++];
671
672 if (rdlen <= MAXSIZE - offs)
673 {
674 // decode bytes, finally
675
676 while (rdlen)
677 {
678 int txtlen = (*pkt) [offs++];
679
680 assert (txtlen + offs < MAXSIZE - 1);
681
682 memcpy (datap, pkt->at (offs), txtlen);
683 datap += txtlen; offs += txtlen;
684
685 rdlen -= txtlen + 1;
686 }
687
688 }
689
690 }
691 }
692
693 // todo: pkt now used
694 c->dnsv4_receive_rep (new dns_rcv (seqno, data, datap - data));
695
696 break;
697 }
698
699 delete pkt;
232} 700}
233 701
234void 702void
235vpn::dnsv4_ev (io_watcher &w, short revents) 703vpn::dnsv4_ev (io_watcher &w, short revents)
236{ 704{
238 { 706 {
239 dns_packet *pkt = new dns_packet; 707 dns_packet *pkt = new dns_packet;
240 struct sockaddr_in sa; 708 struct sockaddr_in sa;
241 socklen_t sa_len = sizeof (sa); 709 socklen_t sa_len = sizeof (sa);
242 710
243 int len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 711 pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
244 pkt->len = len;
245 712
246 u16 flags = ntohs (pkt->flags); 713 if (pkt->len > 0)
247
248 if (THISNODE->dns_port)
249 { 714 {
250 // server 715 if (pkt->flags & htons (FLAG_TC))
251 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
252
253 int offs = 6 * 2; // skip header
254
255 if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC))
256 && pkt->qdcount == htons (1))
257 { 716 {
258 char qname[MAXSIZE]; 717 slog (L_WARN, _("DNS request/response truncated, check protocol settings."));
259 int qlen = pkt->decode_label ((char *)qname, MAXSIZE, offs); 718 //TODO connection reset
260
261 printf ("rcvd packet len %d, id%04x flags%04x q%04x a%04x n%04x a%04x <%s>\n", len,
262 pkt->id, flags, pkt->qdcount, pkt->ancount, pkt->nscount, pkt->arcount, qname);//D
263
264 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++];
265 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++];
266
267 pkt->qdcount = htons (1);
268 pkt->ancount = 0;
269 pkt->nscount = 0; // should be self, as other nameservers reply like this
270 pkt->arcount = 0; // a record for self, as other nameservers reply like this
271
272 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN);
273
274 int dlen = strlen (THISNODE->domain);
275
276 if (qclass == RR_CLASS_IN
277 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT)
278 && qlen > dlen + 1
279 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen))
280 {
281 // correct class, now generate reply
282 pkt->ancount = htons (1); // one answer RR
283
284 (*pkt) [offs++] = 0xc0;
285 (*pkt) [offs++] = 6 * 2; // same as in query section
286
287 (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT;
288 (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN;
289
290 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0;
291 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL
292
293 dnsv4_poll (pkt, offs);
294 printf ("correct class\n");
295 }
296 } 719 }
297 720
721 if (THISNODE->dns_port)
722 {
723 pkt = dnsv4_server (pkt);
298 sendto (w.fd, &((*pkt)[0]), offs, 0, (sockaddr *)&sa, sa_len); 724 sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len);
725 }
726 else
727 dnsv4_client (pkt);
299 } 728 }
300 else 729 }
730}
731
732bool
733connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
734{
735 // never initialized
736 if (!dns_snddq && !dns_rcvdq)
737 {
738 dns_rcvdq = new byte_stream (MAX_BACKLOG * 2);
739 dns_snddq = new byte_stream (MAX_BACKLOG);
740
741 //dns_rcvseq = dns_sndseq = 0;
742
743 dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4);
744 }
745
746 if (!dns_snddq->put (pkt))
747 return false;
748
749 // start timer if neccessary
750 if (!THISNODE->dns_port && !dnsv4_tw.active)
751 dnsv4_cb (dnsv4_tw);
752
753 return true;
754}
755
756void
757connection::dnsv4_cb (time_watcher &w)
758{
759 // check for timeouts and (re)transmit
760 tstamp next = NOW + 60;
761 dns_req *send = 0;
762
763 for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin ();
764 i != vpn->dns_sndpq.end ();
765 ++i)
766 {
767 dns_req *r = *i;
768
769 if (r->next <= NOW)
301 { 770 {
302 // client 771 if (!send)
772 {
773 send = r;
774
775 if (r->retry)//D
776 printf ("req %d, retry %d\n", r->pkt->id, r->retry);
777 r->retry++;
778 r->next = NOW + r->retry;
779 }
303 } 780 }
781
782 if (r->next < next)
783 next = r->next;
784 }
785
786 if (!send
787 && vpn->dns_sndpq.size () < MAX_OUTSTANDING)
304 } 788 {
305} 789 send = new dns_req (this);
790 send->gen_stream_req (dns_sndseq, dns_snddq);
791 vpn->dns_sndpq.push_back (send);
306 792
307bool 793 dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK;
308vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 794 }
309{ 795
310 //return i->send_packet (pkt, tos); 796 tstamp min_next = NOW + (1. / (tstamp)MAX_RATE);
797
798 if (send)
799 {
800 dns_packet *pkt = send->pkt;
801
802 next = min_next;
803
804 sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ());
805 }
806 else if (next < min_next)
807 next = min_next;
808
809 w.start (next);
311} 810}
312 811
313#endif 812#endif
314 813

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines