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.4 by pcg, Wed Mar 2 05:49:31 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
48#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING 50#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING
49#define MAX_RATE 1 // requests/s 51#define MAX_RATE 1000 // requests/s
50#define MAX_BACKLOG (10*1024) // size of protocol backlog 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
51 62
52/* 63/*
53 64
54protocol, in shorthand :) 65protocol, in shorthand :)
55 66
84 static int decode (u8 *dst, char *src, int len); 95 static int decode (u8 *dst, char *src, int len);
85 96
86 dns64 (); 97 dns64 ();
87} dns64; 98} dns64;
88 99
89const char dns64::encode_chars[64 + 1] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"; 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";
90s8 dns64::decode_chars[256]; 104s8 dns64::decode_chars[256];
91 105
92dns64::dns64 () 106dns64::dns64 ()
93{ 107{
94 for (int i = 0; i < 64; i++) 108 for (int i = 0; i < 64; i++)
129 while (len--) 143 while (len--)
130 { 144 {
131 s8 chr = decode_chars [(u8)*src++]; 145 s8 chr = decode_chars [(u8)*src++];
132 146
133 if (!chr) 147 if (!chr)
134 break; 148 continue;
135 149
136 accum <<= 6; 150 accum <<= 6;
137 accum |= chr - 1; 151 accum |= chr - 1;
138 bits += 6; 152 bits += 6;
139 153
159 ~byte_stream (); 173 ~byte_stream ();
160 174
161 bool empty () { return !fill; } 175 bool empty () { return !fill; }
162 int size () { return fill; } 176 int size () { return fill; }
163 177
178 bool put (u8 *data, unsigned int datalen);
164 bool put (vpn_packet *pkt); 179 bool put (vpn_packet *pkt);
165 vpn_packet *get (); 180 vpn_packet *get ();
166 181
167 u8 *begin () { return data; } 182 u8 *begin () { return data; }
168 void remove (int count); 183 void remove (int count);
185 abort (); 200 abort ();
186 201
187 memmove (data, data + count, fill -= count); 202 memmove (data, data + count, fill -= count);
188} 203}
189 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
190bool byte_stream::put (vpn_packet *pkt) 215bool byte_stream::put (vpn_packet *pkt)
191{ 216{
192 if (maxsize - fill < pkt->len + 2) 217 if (maxsize - fill < pkt->len + 2)
193 return false; 218 return false;
194 219
202 227
203vpn_packet *byte_stream::get () 228vpn_packet *byte_stream::get ()
204{ 229{
205 int len = (data [0] << 8) | data [1]; 230 int len = (data [0] << 8) | data [1];
206 231
232 if (len > MAXSIZE && fill >= 2)
233 abort (); // TODO handle this gracefully, connection reset
234
207 if (fill < len + 2) 235 if (fill < len + 2)
208 return 0; 236 return 0;
209 237
210 vpn_packet *pkt = new vpn_packet; 238 vpn_packet *pkt = new vpn_packet;
239
240 pkt->len = len;
211 memcpy (&((*pkt)[0]), data + 2, len); 241 memcpy (&((*pkt)[0]), data + 2, len);
212 remove (len + 2); 242 remove (len + 2);
213 243
214 return pkt; 244 return pkt;
215} 245}
222#define FLAG_OP_QUERY ( 0 << 11) 252#define FLAG_OP_QUERY ( 0 << 11)
223#define FLAG_AA ( 1 << 10) 253#define FLAG_AA ( 1 << 10)
224#define FLAG_TC ( 1 << 9) 254#define FLAG_TC ( 1 << 9)
225#define FLAG_RD ( 1 << 8) 255#define FLAG_RD ( 1 << 8)
226#define FLAG_RA ( 1 << 7) 256#define FLAG_RA ( 1 << 7)
257#define FLAG_AUTH ( 1 << 5)
227#define FLAG_RCODE_MASK (15 << 0) 258#define FLAG_RCODE_MASK (15 << 0)
228#define FLAG_RCODE_OK ( 0 << 0) 259#define FLAG_RCODE_OK ( 0 << 0)
229#define FLAG_RCODE_FORMERR ( 1 << 0) 260#define FLAG_RCODE_FORMERR ( 1 << 0)
230#define FLAG_RCODE_SERVFAIL ( 2 << 0) 261#define FLAG_RCODE_SERVFAIL ( 2 << 0)
231#define FLAG_RCODE_NXDOMAIN ( 3 << 0) 262#define FLAG_RCODE_NXDOMAIN ( 3 << 0)
232#define FLAG_RCODE_REFUSED ( 5 << 0) 263#define FLAG_RCODE_REFUSED ( 5 << 0)
233 264
234#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) 265#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD)
235#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)
236 267
237struct dns_packet : net_packet 268struct dns_packet : net_packet
238{ 269{
239 u16 id; 270 u16 id;
240 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
285{ 316{
286 dns_packet *pkt; 317 dns_packet *pkt;
287 tstamp next; 318 tstamp next;
288 int retry; 319 int retry;
289 connection *conn; 320 connection *conn;
321 int seqno;
290 322
323 dns_req (connection *c);
291 dns_req (connection *c, int seqno, byte_stream *stream); 324 void gen_stream_req (int seqno, byte_stream *stream);
292}; 325};
293 326
294struct dns_rep
295{
296};
297
298static u16 dns_id; // TODO: should be per-vpn 327static u16 dns_id = 12098; // TODO: should be per-vpn
299 328
300dns_req::dns_req (connection *c, int seqno, byte_stream *stream) 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)
301: conn (c) 342: conn (c)
302{ 343{
303 next = 0; 344 next = 0;
304 retry = 0; 345 retry = 0;
305 346
306 pkt = new dns_packet; 347 pkt = new dns_packet;
307 348
308 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
309 pkt->flags = DEFAULT_CLIENT_FLAGS; 356 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
310 pkt->qdcount = htons (1); 357 pkt->qdcount = htons (1);
311 pkt->ancount = 0;
312 pkt->nscount = 0;
313 pkt->arcount = 0;
314 358
315 int offs = 6*2; 359 int offs = 6*2;
316 int dlen = 255 - strlen (THISNODE->domain) - 1; // here we always have room for the max. label length 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
317 363
318 u8 lbl[64]; //D 364 u8 data[256]; //TODO
319 char enc[65]; 365
320 int lbllen = 3; 366 data[0] = THISNODE->id; //TODO
321 lbl[0] = c->conf->id; //D
322 lbl[1] = seqno >> 8; //D 367 data[1] = seqno >> 7; //TODO
323 lbl[2] = seqno; //D 368 data[2] = seqno << 1; //TODO
324 369
325 while (dlen > 0) 370 int datalen = dns64::decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE) - 3;
326 {
327 int sndlen = dlen;
328 371
329 if (sndlen + lbllen > dns64::decode_len (MAX_LBL_SIZE))
330 sndlen = dns64::decode_len (MAX_LBL_SIZE) - lbllen;
331
332 if (sndlen > stream->size ()) 372 if (datalen > stream->size ())
333 sndlen = stream->size (); 373 datalen = stream->size ();
334 374
335 if (sndlen + lbllen == 0) 375 char enc[256], *encp = enc;
336 break; 376
337
338 memcpy (lbl + lbllen, stream->begin (), sndlen); 377 memcpy (data + 3, stream->begin (), datalen);
378 int enclen = dns64::encode (enc, data, datalen + 3);
339 stream->remove (sndlen); 379 stream->remove (datalen);
340 lbllen += sndlen;
341 380
342 dns64::encode (enc, lbl, lbllen); 381 while (enclen)
382 {
383 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE;
343 384
344 int elen = dns64::encode_len (lbllen);
345 (*pkt)[offs++] = elen; 385 (*pkt)[offs++] = lbllen;
346 memcpy (&((*pkt)[offs]), enc, elen); 386 memcpy (pkt->at (offs), encp, lbllen);
387
347 offs += elen; 388 offs += lbllen;
348 dlen -= elen + 1; 389 encp += lbllen;
349 390
350 lbllen = 0; 391 enclen -= lbllen;
351 } 392 }
352 393
353 const char *suffix = THISNODE->domain; 394 const char *suffix = THISNODE->domain;
354 395
355 // add tunnel domain 396 // add tunnel domain
375 (*pkt)[offs++] = 0; 416 (*pkt)[offs++] = 0;
376 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; 417 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY;
377 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 418 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
378 419
379 pkt->len = offs; 420 pkt->len = offs;
421}
380 422
381 c->vpn->dns_sndpq.push_back (this); 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;
382} 443}
383 444
384///////////////////////////////////////////////////////////////////////////// 445/////////////////////////////////////////////////////////////////////////////
385 446
386struct dns_cfg 447struct dns_cfg
390 u8 unused1; 451 u8 unused1;
391 u16 max_size; 452 u16 max_size;
392 u8 flags1, flags2; 453 u8 flags1, flags2;
393}; 454};
394 455
456void connection::dnsv4_receive_rep (struct dns_rcv *r)
457{
458 dns_rcvpq.push_back (r);
459
460 redo:
461
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
395dns_packet * 500dns_packet *
396vpn::dnsv4_server (dns_packet *pkt) 501vpn::dnsv4_server (dns_packet *pkt)
397{ 502{
398 u16 flags = ntohs (pkt->flags); 503 u16 flags = ntohs (pkt->flags);
399 504
405 if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) 510 if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC))
406 && pkt->qdcount == htons (1)) 511 && pkt->qdcount == htons (1))
407 { 512 {
408 char qname[MAXSIZE]; 513 char qname[MAXSIZE];
409 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 514 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs);
410
411 printf ("rcvd packet len %d, id%04x flags%04x q%04x a%04x n%04x a%04x <%s>\n", pkt->len,
412 pkt->id, flags, pkt->qdcount, pkt->ancount, pkt->nscount, pkt->arcount, qname);//D
413 515
414 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; 516 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++];
415 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; 517 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++];
416 518
417 pkt->qdcount = htons (1); 519 pkt->qdcount = htons (1);
426 if (qclass == RR_CLASS_IN 528 if (qclass == RR_CLASS_IN
427 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) 529 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT)
428 && qlen > dlen + 1 530 && qlen > dlen + 1
429 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) 531 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen))
430 { 532 {
431 // correct class, domain, parse 533 // correct class, domain: parse
432 u8 data[MAXSIZE]; 534 u8 data[MAXSIZE];
433 int datalen = dns64::decode (data, qname, qlen - dlen - 1); 535 int datalen = dns64::decode (data, qname, qlen - dlen - 1);
434 536
435 for (int i = 0; i < datalen; i++)
436 printf ("%02x ", data[i]);
437 printf ("\n");
438
439
440
441
442#if 0
443 // now generate reply
444 rep->ancount = htons (1); // one answer RR
445 rep->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
446
447 (*rep) [offs++] = 0xc0;
448 (*rep) [offs++] = 6 * 2; // same as in query section
449
450 (*rep) [offs++] = RR_TYPE_TXT >> 8; (*rep) [offs++] = RR_TYPE_TXT;
451 (*rep) [offs++] = RR_CLASS_IN >> 8; (*rep) [offs++] = RR_CLASS_IN;
452
453 (*rep) [offs++] = 0; (*rep) [offs++] = 0;
454 (*rep) [offs++] = 0; (*rep) [offs++] = 0; // TTL
455
456 int dlen = MAX_PKT_SIZE - offs - 2;
457
458 printf ("we have room for %d bytes\n", dlen);
459
460 int rdlen_offs = offs += 2;
461
462 u8 txt[255];
463 txt[0] = 0;
464 txt[1] = 0;
465
466 int txtlen = 2; 537 int client = data[0];
538 int seqno = ((data[1] << 7) | (data[2] >> 1)) & SEQNO_MASK;
467 539
468 while (dlen > 1) 540 if (0 < client && client <= conns.size ())
469 { 541 {
470 int sndlen = --dlen; 542 connection *c = conns [client - 1];
471 543
472 if (sndlen + txtlen > 255) 544 redo:
473 sndlen = 255 - txtlen;
474 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
475 sndlen = 0; 581 dlen -= qlen;
476 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
477 (*rep)[offs++] = sndlen + txtlen; 594 (*pkt)[offs++] = txtlen;
478 memcpy (&((*rep)[offs]), txt, txtlen); 595 memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen);
479 offs += txtlen; 596 offs += txtlen;
597 c->dns_snddq->remove (txtlen);
480 598
481 //if (sndlen + txtlen > dns_snddq. 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: ;
482 } 616 }
483 617 else
484 int rdlen = offs - rdlen_offs; 618 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
485
486 (*rep) [rdlen_offs - 2] = rdlen >> 8;
487 (*rep) [rdlen_offs - 1] = rdlen;
488#endif
489 } 619 }
490 }
491 620
492 pkt->len = offs; 621 pkt->len = offs;
622 }
623
493 return pkt; 624 return pkt;
494} 625}
495 626
496void 627void
497vpn::dnsv4_client (dns_packet *pkt) 628vpn::dnsv4_client (dns_packet *pkt)
500 int offs = 6 * 2; // skip header 631 int offs = 6 * 2; // skip header
501 632
502 pkt->qdcount = ntohs (pkt->qdcount); 633 pkt->qdcount = ntohs (pkt->qdcount);
503 pkt->ancount = ntohs (pkt->ancount); 634 pkt->ancount = ntohs (pkt->ancount);
504 635
636 // go through our request list and find the corresponding request
505 for (vector<dns_req *>::iterator i = dns_sndpq.begin (); 637 for (vector<dns_req *>::iterator i = dns_sndpq.begin ();
506 i != dns_sndpq.end (); 638 i != dns_sndpq.end ();
507 ++i) 639 ++i)
508 if ((*i)->pkt->id == pkt->id) 640 if ((*i)->pkt->id == pkt->id)
509 { 641 {
510 connection *c = (*i)->conn; 642 connection *c = (*i)->conn;
511 printf ("GOT RESPONSE id %x %p\n", pkt->id, c);//D 643 int seqno = (*i)->seqno;
644 u8 data[MAXSIZE], *datap = data;
645
512 delete *i; 646 delete *i;
513 dns_sndpq.erase (i); 647 dns_sndpq.erase (i);
514 648
515 if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) 649 if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC))
516 { 650 {
533 ttl |= (*pkt) [offs++] << 8; 667 ttl |= (*pkt) [offs++] << 8;
534 ttl |= (*pkt) [offs++]; 668 ttl |= (*pkt) [offs++];
535 669
536 u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; 670 u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++];
537 671
538 printf ("REPLY %d:%d TTL %d RD %d\n", qtype, qclass, ttl, rdlen);
539
540 offs += rdlen;
541
542 if (MAXSIZE - offs < rdlen) 672 if (rdlen <= MAXSIZE - offs)
543 { 673 {
544 // decode bytes, finally 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
545 } 688 }
689
546 } 690 }
547 } 691 }
548 692
693 // todo: pkt now used
694 c->dnsv4_receive_rep (new dns_rcv (seqno, data, datap - data));
695
549 break; 696 break;
550 } 697 }
698
699 delete pkt;
551} 700}
552 701
553void 702void
554vpn::dnsv4_ev (io_watcher &w, short revents) 703vpn::dnsv4_ev (io_watcher &w, short revents)
555{ 704{
560 socklen_t sa_len = sizeof (sa); 709 socklen_t sa_len = sizeof (sa);
561 710
562 pkt->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);
563 712
564 if (pkt->len > 0) 713 if (pkt->len > 0)
714 {
715 if (pkt->flags & htons (FLAG_TC))
716 {
717 slog (L_WARN, _("DNS request/response truncated, check protocol settings."));
718 //TODO connection reset
719 }
720
565 if (THISNODE->dns_port) 721 if (THISNODE->dns_port)
566 { 722 {
567 pkt = dnsv4_server (pkt); 723 pkt = dnsv4_server (pkt);
568 sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); 724 sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len);
569 } 725 }
570 else 726 else
571 dnsv4_client (pkt); 727 dnsv4_client (pkt);
728 }
572 } 729 }
573} 730}
574 731
575bool 732bool
576connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 733connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
579 if (!dns_snddq && !dns_rcvdq) 736 if (!dns_snddq && !dns_rcvdq)
580 { 737 {
581 dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); 738 dns_rcvdq = new byte_stream (MAX_BACKLOG * 2);
582 dns_snddq = new byte_stream (MAX_BACKLOG); 739 dns_snddq = new byte_stream (MAX_BACKLOG);
583 740
584 dns_rcvseq = dns_sndseq = 0; 741 //dns_rcvseq = dns_sndseq = 0;
585 742
586 dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); 743 dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4);
587 } 744 }
588 745
589 if (!dns_snddq->put (pkt)) 746 if (!dns_snddq->put (pkt))
590 return false; 747 return false;
591 748
592 // start timer if neccessary 749 // start timer if neccessary
593 if (!dnsv4_tw.active) 750 if (!THISNODE->dns_port && !dnsv4_tw.active)
594 dnsv4_cb (dnsv4_tw); 751 dnsv4_cb (dnsv4_tw);
595 752
596 return true; 753 return true;
597} 754}
598 755
601{ 758{
602 // check for timeouts and (re)transmit 759 // check for timeouts and (re)transmit
603 tstamp next = NOW + 60; 760 tstamp next = NOW + 60;
604 dns_req *send = 0; 761 dns_req *send = 0;
605 762
606 slog (L_NOISE, _("dnsv4, %d packets in send queue\n"), vpn->dns_sndpq.size ());
607
608 for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); 763 for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin ();
609 i != vpn->dns_sndpq.end (); 764 i != vpn->dns_sndpq.end ();
610 ++i) 765 ++i)
611 { 766 {
612 dns_req *r = *i; 767 dns_req *r = *i;
615 { 770 {
616 if (!send) 771 if (!send)
617 { 772 {
618 send = r; 773 send = r;
619 774
620 slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); 775 if (r->retry)//D
776 printf ("req %d, retry %d\n", r->pkt->id, r->retry);
621 r->retry++; 777 r->retry++;
622 r->next = NOW + r->retry; 778 r->next = NOW + r->retry;
623 } 779 }
624 } 780 }
625 781
627 next = r->next; 783 next = r->next;
628 } 784 }
629 785
630 if (!send 786 if (!send
631 && vpn->dns_sndpq.size () < MAX_OUTSTANDING) 787 && vpn->dns_sndpq.size () < MAX_OUTSTANDING)
632 send = new dns_req (this, dns_sndseq++, dns_snddq); 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 }
633 795
634 tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); 796 tstamp min_next = NOW + (1. / (tstamp)MAX_RATE);
635 797
636 if (send) 798 if (send)
637 { 799 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines