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.46 by pcg, Thu Dec 6 00:35:29 2007 UTC vs.
Revision 1.49 by root, Tue Feb 8 23:11:36 2011 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-2005 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify it
8 it under the terms of the GNU General Public License as published by 8 under the terms of the GNU General Public License as published by the
9 the Free Software Foundation; either version 2 of the License, or 9 Free Software Foundation; either version 3 of the License, or (at your
10 (at your option) any later version. 10 option) any later version.
11 11
12 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, but
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 GNU General Public License for more details. 15 Public License for more details.
16 16
17 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 along
18 along with gvpe; if not, write to the Free Software 18 with this program; if not, see <http://www.gnu.org/licenses/>.
19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19
20 Additional permission under GNU GPL version 3 section 7
21
22 If you modify this Program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a modified
24 version of that library), containing parts covered by the terms of the
25 OpenSSL or SSLeay licenses, the licensors of this Program grant you
26 additional permission to convey the resulting work. Corresponding
27 Source for a non-source form of such a combination shall include the
28 source code for the parts of OpenSSL used as well as that of the
29 covered work.
20*/ 30*/
21 31
22// TODO: EDNS0 option to increase dns mtu? 32// TODO: EDNS0 option to increase dns mtu?
23// TODO: re-write dns packet parsing/creation using a safe mem-buffer 33// TODO: re-write dns packet parsing/creation using a safe mem-buffer
24// to ensure no buffer overflows or similar problems. 34// to ensure no buffer overflows or similar problems.
73#define MAX_PKT_SIZE 512 83#define MAX_PKT_SIZE 512
74 84
75#define RR_TYPE_A 1 85#define RR_TYPE_A 1
76#define RR_TYPE_NULL 10 86#define RR_TYPE_NULL 10
77#define RR_TYPE_TXT 16 87#define RR_TYPE_TXT 16
88#define RR_TYPE_AAAA 28
78#define RR_TYPE_ANY 255 89#define RR_TYPE_ANY 255
79 90
80#define RR_CLASS_IN 1 91#define RR_CLASS_IN 1
81 92
82#define CMD_IP_1 207 93#define CMD_IP_1 207
157 enc_len [len] = n; 168 enc_len [len] = n;
158 dec_len [n] = len; 169 dec_len [n] = len;
159 } 170 }
160} 171}
161 172
173unsigned int
162unsigned int basecoder::encode_len (unsigned int len) 174basecoder::encode_len (unsigned int len)
163{ 175{
164 return enc_len [len]; 176 return enc_len [len];
165} 177}
166 178
179unsigned int
167unsigned int basecoder::decode_len (unsigned int len) 180basecoder::decode_len (unsigned int len)
168{ 181{
169 while (len && !dec_len [len]) 182 while (len && !dec_len [len])
170 --len; 183 --len;
171 184
172 return dec_len [len]; 185 return dec_len [len];
173} 186}
174 187
188unsigned int
175unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) 189basecoder::encode (char *dst, u8 *src, unsigned int len)
176{ 190{
177 if (!len || len > MAX_DEC_LEN) 191 if (!len || len > MAX_DEC_LEN)
178 return 0; 192 return 0;
179 193
180 int elen = encode_len (len); 194 int elen = encode_len (len);
199 *dst++ = cmap.encode [dst_ [i]]; 213 *dst++ = cmap.encode [dst_ [i]];
200 214
201 return elen; 215 return elen;
202} 216}
203 217
218unsigned int
204unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) 219basecoder::decode (u8 *dst, char *src, unsigned int len)
205{ 220{
206 if (!len || len > MAX_ENC_LEN) 221 if (!len || len > MAX_ENC_LEN)
207 return 0; 222 return 0;
208 223
209 u8 src_ [MAX_ENC_LEN]; 224 u8 src_ [MAX_ENC_LEN];
268 283
269///////////////////////////////////////////////////////////////////////////// 284/////////////////////////////////////////////////////////////////////////////
270 285
271#define HDRSIZE 6 286#define HDRSIZE 6
272 287
288inline void
273inline void encode_header (char *data, int clientid, int seqno, int retry = 0) 289encode_header (char *data, int clientid, int seqno, int retry = 0)
274{ 290{
275 seqno &= SEQNO_MASK; 291 seqno &= SEQNO_MASK;
276 292
277 u8 hdr[3] = { 293 u8 hdr[3] = {
278 clientid, 294 clientid,
283 assert (clientid < 256); 299 assert (clientid < 256);
284 300
285 cdc26.encode (data, hdr, 3); 301 cdc26.encode (data, hdr, 3);
286} 302}
287 303
304inline void
288inline void decode_header (char *data, int &clientid, int &seqno) 305decode_header (char *data, int &clientid, int &seqno)
289{ 306{
290 u8 hdr[3]; 307 u8 hdr[3];
291 308
292 cdc26.decode (hdr, data, HDRSIZE); 309 cdc26.decode (hdr, data, HDRSIZE);
293 310
326byte_stream::~byte_stream () 343byte_stream::~byte_stream ()
327{ 344{
328 delete data; 345 delete data;
329} 346}
330 347
348void
331void byte_stream::remove (int count) 349byte_stream::remove (int count)
332{ 350{
333 if (count > fill) 351 if (count > fill)
334 assert (count <= fill); 352 assert (count <= fill);
335 353
336 memmove (data, data + count, fill -= count); 354 memmove (data, data + count, fill -= count);
337} 355}
338 356
357bool
339bool byte_stream::put (u8 *data, unsigned int datalen) 358byte_stream::put (u8 *data, unsigned int datalen)
340{ 359{
341 if (maxsize - fill < datalen) 360 if (maxsize - fill < datalen)
342 return false; 361 return false;
343 362
344 memcpy (this->data + fill, data, datalen); fill += datalen; 363 memcpy (this->data + fill, data, datalen); fill += datalen;
345 364
346 return true; 365 return true;
347} 366}
348 367
368bool
349bool byte_stream::put (vpn_packet *pkt) 369byte_stream::put (vpn_packet *pkt)
350{ 370{
351 if (maxsize - fill < pkt->len + 2) 371 if (maxsize - fill < pkt->len + 2)
352 return false; 372 return false;
353 373
354 data [fill++] = pkt->len >> 8; 374 data [fill++] = pkt->len >> 8;
436 bool valid (); 456 bool valid ();
437}; 457};
438 458
439int dns_cfg::next_uid; 459int dns_cfg::next_uid;
440 460
461void
441void dns_cfg::reset (int clientid) 462dns_cfg::reset (int clientid)
442{ 463{
443 id1 = 'G'; 464 id1 = 'G';
444 id2 = 'V'; 465 id2 = 'V';
445 id3 = 'P'; 466 id3 = 'P';
446 id4 = 'E'; 467 id4 = 'E';
460 481
461 r3 = r4 = 0; 482 r3 = r4 = 0;
462 r4 = r5 = r6 = r7 = 0; 483 r4 = r5 = r6 = r7 = 0;
463} 484}
464 485
486bool
465bool dns_cfg::valid () 487dns_cfg::valid ()
466{ 488{
467 // although the protocol itself allows for some configurability, 489 // although the protocol itself allows for some configurability,
468 // only the following encoding/decoding settings are implemented. 490 // only the following encoding/decoding settings are implemented.
469 return id1 == 'G' 491 return id1 == 'G'
470 && id2 == 'V' 492 && id2 == 'V'
485 u8 data [MAXSIZE - 6 * 2]; 507 u8 data [MAXSIZE - 6 * 2];
486 508
487 int decode_label (char *data, int size, int &offs); 509 int decode_label (char *data, int size, int &offs);
488}; 510};
489 511
512int
490int dns_packet::decode_label (char *data, int size, int &offs) 513dns_packet::decode_label (char *data, int size, int &offs)
491{ 514{
492 char *orig = data; 515 char *orig = data;
493 516
494 memset (data, 0, size); 517 memset (data, 0, size);
495 518
521 return data - orig; 544 return data - orig;
522} 545}
523 546
524///////////////////////////////////////////////////////////////////////////// 547/////////////////////////////////////////////////////////////////////////////
525 548
549static
550u16 next_id ()
551{
526static u16 dns_id = 0; // TODO: should be per-vpn 552 static u16 dns_id = 0; // TODO: should be per-vpn
527 553
528static u16 next_id ()
529{
530 if (!dns_id) 554 if (!dns_id)
531 dns_id = time (0); 555 dns_id = time (0);
532 556
533 // the simplest lsfr with periodicity 65535 i could find 557 // the simplest lsfr with periodicity 65535 i could find
534 dns_id = (dns_id << 1) 558 dns_id = (dns_id << 1)
602dns_snd::~dns_snd () 626dns_snd::~dns_snd ()
603{ 627{
604 delete pkt; 628 delete pkt;
605} 629}
606 630
631static void
607static void append_domain (dns_packet &pkt, int &offs, const char *domain) 632append_domain (dns_packet &pkt, int &offs, const char *domain)
608{ 633{
609 // add tunnel domain 634 // add tunnel domain
610 for (;;) 635 for (;;)
611 { 636 {
612 const char *end = strchr (domain, '.'); 637 const char *end = strchr (domain, '.');
625 650
626 domain = end + 1; 651 domain = end + 1;
627 } 652 }
628} 653}
629 654
655void
630void dns_snd::gen_stream_req (int seqno, byte_stream &stream) 656dns_snd::gen_stream_req (int seqno, byte_stream &stream)
631{ 657{
632 stdhdr = true; 658 stdhdr = true;
633 this->seqno = seqno; 659 this->seqno = seqno;
634 660
635 timeout = ev_now () + INITIAL_TIMEOUT; 661 timeout = ev_now () + INITIAL_TIMEOUT;
673 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 699 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
674 700
675 pkt->len = offs; 701 pkt->len = offs;
676} 702}
677 703
704void
678void dns_snd::gen_syn_req () 705dns_snd::gen_syn_req ()
679{ 706{
680 timeout = ev_now () + INITIAL_SYN_TIMEOUT; 707 timeout = ev_now () + INITIAL_SYN_TIMEOUT;
681 708
682 pkt->flags = htons (DEFAULT_CLIENT_FLAGS); 709 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
683 pkt->qdcount = htons (1); 710 pkt->qdcount = htons (1);
748 i != rcvpq.end (); 775 i != rcvpq.end ();
749 ++i) 776 ++i)
750 delete *i; 777 delete *i;
751} 778}
752 779
780void
753void dns_connection::receive_rep (dns_rcv *r) 781dns_connection::receive_rep (dns_rcv *r)
754{ 782{
755 if (r->datalen) 783 if (r->datalen)
756 { 784 {
757 last_received = ev_now (); 785 last_received = ev_now ();
758 tw (); 786 tw ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines