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.3 by pcg, Tue Mar 1 06:27:20 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
42#include "vpn.h" 44#include "vpn.h"
43 45
44#define MIN_RETRY 1. 46#define MIN_RETRY 1.
45#define MAX_RETRY 60. 47#define MAX_RETRY 60.
46 48
47#define MAX_OUTSTANDING 10 // max. outstanding requests 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
48 62
49/* 63/*
50 64
51protocol, in shorthand :) 65protocol, in shorthand :)
52 66
75 static const char encode_chars[64 + 1]; 89 static const char encode_chars[64 + 1];
76 static s8 decode_chars[256]; 90 static s8 decode_chars[256];
77 91
78 static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } 92 static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; }
79 static int decode_len (int bytes) { return (bytes * 6) / 8; } 93 static int decode_len (int bytes) { return (bytes * 6) / 8; }
80 static void encode (char *dst, u8 *src, int len); 94 static int encode (char *dst, u8 *src, int len);
81 static void decode (u8 *dst, char *src, int len); 95 static int decode (u8 *dst, char *src, int len);
82 96
83 dns64 (); 97 dns64 ();
84} dns64; 98} dns64;
85 99
86const 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";
87s8 dns64::decode_chars[256]; 104s8 dns64::decode_chars[256];
88 105
89dns64::dns64 () 106dns64::dns64 ()
90{ 107{
91 for (int i = 0; i < 64; i++) 108 for (int i = 0; i < 64; i++)
92 decode_chars [encode_chars [i]] = i + 1; 109 decode_chars [encode_chars [i]] = i + 1;
93} 110}
94 111
95void dns64::encode (char *dst, u8 *src, int len) 112int dns64::encode (char *dst, u8 *src, int len)
96{ 113{
97 // slow, but easy to debug 114 // slow, but easy to debug
115 char *beg = dst;
98 unsigned int accum, bits = 0; 116 unsigned int accum, bits = 0;
99 117
100 while (len--) 118 while (len--)
101 { 119 {
102 accum <<= 8; 120 accum <<= 8;
110 } 128 }
111 } 129 }
112 130
113 if (bits) 131 if (bits)
114 *dst++ = encode_chars [(accum << (6 - bits)) & 63]; 132 *dst++ = encode_chars [(accum << (6 - bits)) & 63];
115}
116 133
134 return dst - beg;
135}
136
117void dns64::decode (u8 *dst, char *src, int len) 137int dns64::decode (u8 *dst, char *src, int len)
118{ 138{
119 // slow, but easy to debug 139 // slow, but easy to debug
140 u8 *beg = dst;
120 unsigned int accum, bits = 0; 141 unsigned int accum, bits = 0;
121 142
122 while (len--) 143 while (len--)
123 { 144 {
124 s8 chr = decode_chars [(u8)*src++]; 145 s8 chr = decode_chars [(u8)*src++];
125 146
126
127 if (!chr) 147 if (!chr)
128 break; 148 continue;
129 149
130 accum <<= 6; 150 accum <<= 6;
131 accum |= chr - 1; 151 accum |= chr - 1;
132 bits += 6; 152 bits += 6;
133 153
135 { 155 {
136 *dst++ = accum >> (bits - 8); 156 *dst++ = accum >> (bits - 8);
137 bits -= 8; 157 bits -= 8;
138 } 158 }
139 } 159 }
160
161 return dst - beg;
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;
140} 245}
141 246
142///////////////////////////////////////////////////////////////////////////// 247/////////////////////////////////////////////////////////////////////////////
143 248
144#define FLAG_QUERY ( 0 << 15) 249#define FLAG_QUERY ( 0 << 15)
147#define FLAG_OP_QUERY ( 0 << 11) 252#define FLAG_OP_QUERY ( 0 << 11)
148#define FLAG_AA ( 1 << 10) 253#define FLAG_AA ( 1 << 10)
149#define FLAG_TC ( 1 << 9) 254#define FLAG_TC ( 1 << 9)
150#define FLAG_RD ( 1 << 8) 255#define FLAG_RD ( 1 << 8)
151#define FLAG_RA ( 1 << 7) 256#define FLAG_RA ( 1 << 7)
257#define FLAG_AUTH ( 1 << 5)
152#define FLAG_RCODE_MASK (15 << 0) 258#define FLAG_RCODE_MASK (15 << 0)
153#define FLAG_RCODE_OK ( 0 << 0) 259#define FLAG_RCODE_OK ( 0 << 0)
154#define FLAG_RCODE_FORMERR ( 1 << 0) 260#define FLAG_RCODE_FORMERR ( 1 << 0)
155#define FLAG_RCODE_SERVFAIL ( 2 << 0) 261#define FLAG_RCODE_SERVFAIL ( 2 << 0)
156#define FLAG_RCODE_NXDOMAIN ( 3 << 0) 262#define FLAG_RCODE_NXDOMAIN ( 3 << 0)
157#define FLAG_RCODE_REFUSED ( 5 << 0) 263#define FLAG_RCODE_REFUSED ( 5 << 0)
158 264
159#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) 265#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD)
160#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)
161 267
162struct dns_packet : net_packet 268struct dns_packet : net_packet
163{ 269{
164 u16 id; 270 u16 id;
165 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
209struct dns_req 315struct dns_req
210{ 316{
211 dns_packet *pkt; 317 dns_packet *pkt;
212 tstamp next; 318 tstamp next;
213 int retry; 319 int retry;
320 connection *conn;
321 int seqno;
214 322
215 dns_req (connection *c, vpn_packet *p, int &offs); 323 dns_req (connection *c);
324 void gen_stream_req (int seqno, byte_stream *stream);
216}; 325};
217 326
218struct dns_rep
219{
220};
221
222static u16 dns_id; // TODO: should be per-vpn 327static u16 dns_id = 12098; // TODO: should be per-vpn
223 328
224dns_req::dns_req (connection *c, vpn_packet *p, int &offs) 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)
225{ 343{
226 next = 0; 344 next = 0;
227 retry = 0; 345 retry = 0;
228 346
229 pkt = new dns_packet; 347 pkt = new dns_packet;
230 348
231 pkt->id = dns_id++; 349 pkt->id = next_id ();
350}
351
352void dns_req::gen_stream_req (int seqno, byte_stream *stream)
353{
354 this->seqno = seqno;
355
232 pkt->flags = DEFAULT_CLIENT_FLAGS; 356 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
233 pkt->qdcount = htons (1 * 0); 357 pkt->qdcount = htons (1);
234 pkt->ancount = 0;
235 pkt->nscount = 0;
236 pkt->arcount = 0;
237 358
238 offs += 10000; 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
239 363
240 c->dns_sndq.push_back (this); 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;
241} 443}
242 444
243///////////////////////////////////////////////////////////////////////////// 445/////////////////////////////////////////////////////////////////////////////
244 446
245struct dns_cfg 447struct dns_cfg
249 u8 unused1; 451 u8 unused1;
250 u16 max_size; 452 u16 max_size;
251 u8 flags1, flags2; 453 u8 flags1, flags2;
252}; 454};
253 455
254// reply to client-poll 456void connection::dnsv4_receive_rep (struct dns_rcv *r)
255void dnsv4_poll (dns_packet *pkt, int &offs)
256{ 457{
257 int dlen = MAX_PKT_SIZE - offs - 2; 458 dns_rcvpq.push_back (r);
258 459
259 (*pkt) [offs++] = 0; 460 redo:
260 (*pkt) [offs++] = 5;
261 memcpy (&((*pkt)[offs]), "\01H\02\xff\x00", 5);
262 offs += 5;
263 461
264 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
265 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); 508 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
266 509
267 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;
268} 700}
269 701
270void 702void
271vpn::dnsv4_ev (io_watcher &w, short revents) 703vpn::dnsv4_ev (io_watcher &w, short revents)
272{ 704{
274 { 706 {
275 dns_packet *pkt = new dns_packet; 707 dns_packet *pkt = new dns_packet;
276 struct sockaddr_in sa; 708 struct sockaddr_in sa;
277 socklen_t sa_len = sizeof (sa); 709 socklen_t sa_len = sizeof (sa);
278 710
279 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);
280 pkt->len = len;
281 712
282 u16 flags = ntohs (pkt->flags); 713 if (pkt->len > 0)
283
284 if (THISNODE->dns_port)
285 { 714 {
286 // server 715 if (pkt->flags & htons (FLAG_TC))
287 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
288
289 int offs = 6 * 2; // skip header
290
291 if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC))
292 && pkt->qdcount == htons (1))
293 { 716 {
294 char qname[MAXSIZE]; 717 slog (L_WARN, _("DNS request/response truncated, check protocol settings."));
295 int qlen = pkt->decode_label ((char *)qname, MAXSIZE, offs); 718 //TODO connection reset
296
297 printf ("rcvd packet len %d, id%04x flags%04x q%04x a%04x n%04x a%04x <%s>\n", len,
298 pkt->id, flags, pkt->qdcount, pkt->ancount, pkt->nscount, pkt->arcount, qname);//D
299
300 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++];
301 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++];
302
303 pkt->qdcount = htons (1);
304 pkt->ancount = 0;
305 pkt->nscount = 0; // should be self, as other nameservers reply like this
306 pkt->arcount = 0; // a record for self, as other nameservers reply like this
307
308 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN);
309
310 int dlen = strlen (THISNODE->domain);
311
312 if (qclass == RR_CLASS_IN
313 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT)
314 && qlen > dlen + 1
315 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen))
316 {
317 // correct class, now generate reply
318 pkt->ancount = htons (1); // one answer RR
319
320 (*pkt) [offs++] = 0xc0;
321 (*pkt) [offs++] = 6 * 2; // same as in query section
322
323 (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT;
324 (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN;
325
326 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0;
327 (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL
328
329 dnsv4_poll (pkt, offs);
330 printf ("correct class\n");
331 }
332 } 719 }
333 720
721 if (THISNODE->dns_port)
722 {
723 pkt = dnsv4_server (pkt);
334 sendto (w.fd, &((*pkt)[0]), offs, 0, (sockaddr *)&sa, sa_len); 724 sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len);
335 } 725 }
336 else 726 else
337 { 727 dnsv4_client (pkt);
338 // client
339 } 728 }
340 } 729 }
341} 730}
342 731
343bool 732bool
344connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 733connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
345{ 734{
346 if (dns_sndq.size () >= MAX_OUTSTANDING) 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))
347 return false; 747 return false;
348 748
349 // split vpn packet into dns packets, add to sndq 749 // start timer if neccessary
350 for (int offs = 0; offs < pkt->len; ) 750 if (!THISNODE->dns_port && !dnsv4_tw.active)
351 new dns_req (this, pkt, offs);
352
353 // force transmit
354 dnsv4_cb (dnsv4_tw); 751 dnsv4_cb (dnsv4_tw);
355 752
356 return true; 753 return true;
357} 754}
358 755
359void 756void
360connection::dnsv4_cb (time_watcher &w) 757connection::dnsv4_cb (time_watcher &w)
361{ 758{
362 // check for timeouts and (re)transmit 759 // check for timeouts and (re)transmit
363 tstamp next = NOW + 60; 760 tstamp next = NOW + 60;
761 dns_req *send = 0;
364 762
365 slog (L_NOISE, _("dnsv4, %d packets in send queue\n"), dns_sndq.size ());
366
367 if (dns_sndq.empty ())
368 {
369 next = NOW + 1;
370 // push poll packet
371 }
372
373 for (vector<dns_req *>::iterator i = dns_sndq.begin (); 763 for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin ();
374 i != dns_sndq.end (); 764 i != vpn->dns_sndpq.end ();
375 ++i) 765 ++i)
376 { 766 {
377 dns_req *r = *i; 767 dns_req *r = *i;
378 768
379 if (r->next <= NOW) 769 if (r->next <= NOW)
380 { 770 {
381 slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); 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);
382 r->retry++; 777 r->retry++;
383 r->next = NOW + r->retry; 778 r->next = NOW + r->retry;
779 }
384 } 780 }
385 781
386 if (r->next < next) 782 if (r->next < next)
387 next = r->next; 783 next = r->next;
388 } 784 }
389 785
390 printf ("%f next\n", next); 786 if (!send
787 && vpn->dns_sndpq.size () < MAX_OUTSTANDING)
788 {
789 send = new dns_req (this);
790 send->gen_stream_req (dns_sndseq, dns_snddq);
791 vpn->dns_sndpq.push_back (send);
792
793 dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK;
794 }
795
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
391 w.start (next); 809 w.start (next);
392} 810}
393 811
394#endif 812#endif
395 813

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines