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