… | |
… | |
42 | #include "vpn.h" |
42 | #include "vpn.h" |
43 | |
43 | |
44 | #define MIN_RETRY 1. |
44 | #define MIN_RETRY 1. |
45 | #define MAX_RETRY 60. |
45 | #define MAX_RETRY 60. |
46 | |
46 | |
47 | #define MAX_OUTSTANDING 10 // max. outstanding requests |
47 | #define MAX_OUTSTANDING 40 // max. outstanding requests |
48 | #define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
48 | #define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
49 | #define MAX_RATE 1 // requests/s |
49 | #define MAX_RATE 1000 // requests/s |
50 | #define MAX_BACKLOG (10*1024) // size of protocol backlog |
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 | // seqno has 12 bits, but the lower bit is always left as zero |
|
|
57 | // as bind caches ttl=0 records and we have to generate |
|
|
58 | // sequence numbers that always differ case-insensitively |
|
|
59 | #define SEQNO_MASK 0x07ff |
51 | |
60 | |
52 | /* |
61 | /* |
53 | |
62 | |
54 | protocol, in shorthand :) |
63 | protocol, in shorthand :) |
55 | |
64 | |
… | |
… | |
84 | static int decode (u8 *dst, char *src, int len); |
93 | static int decode (u8 *dst, char *src, int len); |
85 | |
94 | |
86 | dns64 (); |
95 | dns64 (); |
87 | } dns64; |
96 | } dns64; |
88 | |
97 | |
89 | const char dns64::encode_chars[64 + 1] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"; |
98 | // the following sequence has been crafted to |
|
|
99 | // a) look somewhat random |
|
|
100 | // b) the even (and odd) indices never share the same character as upper/lowercase |
|
|
101 | const char dns64::encode_chars[64 + 1] = "_-dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"; |
90 | s8 dns64::decode_chars[256]; |
102 | s8 dns64::decode_chars[256]; |
91 | |
103 | |
92 | dns64::dns64 () |
104 | dns64::dns64 () |
93 | { |
105 | { |
94 | for (int i = 0; i < 64; i++) |
106 | for (int i = 0; i < 64; i++) |
… | |
… | |
129 | while (len--) |
141 | while (len--) |
130 | { |
142 | { |
131 | s8 chr = decode_chars [(u8)*src++]; |
143 | s8 chr = decode_chars [(u8)*src++]; |
132 | |
144 | |
133 | if (!chr) |
145 | if (!chr) |
134 | break; |
146 | continue; |
135 | |
147 | |
136 | accum <<= 6; |
148 | accum <<= 6; |
137 | accum |= chr - 1; |
149 | accum |= chr - 1; |
138 | bits += 6; |
150 | bits += 6; |
139 | |
151 | |
… | |
… | |
159 | ~byte_stream (); |
171 | ~byte_stream (); |
160 | |
172 | |
161 | bool empty () { return !fill; } |
173 | bool empty () { return !fill; } |
162 | int size () { return fill; } |
174 | int size () { return fill; } |
163 | |
175 | |
|
|
176 | bool put (u8 *data, unsigned int datalen); |
164 | bool put (vpn_packet *pkt); |
177 | bool put (vpn_packet *pkt); |
165 | vpn_packet *get (); |
178 | vpn_packet *get (); |
166 | |
179 | |
167 | u8 *begin () { return data; } |
180 | u8 *begin () { return data; } |
168 | void remove (int count); |
181 | void remove (int count); |
… | |
… | |
185 | abort (); |
198 | abort (); |
186 | |
199 | |
187 | memmove (data, data + count, fill -= count); |
200 | memmove (data, data + count, fill -= count); |
188 | } |
201 | } |
189 | |
202 | |
|
|
203 | bool byte_stream::put (u8 *data, unsigned int datalen) |
|
|
204 | { |
|
|
205 | if (maxsize - fill < datalen) |
|
|
206 | return false; |
|
|
207 | |
|
|
208 | memcpy (this->data + fill, data, datalen); fill += datalen; |
|
|
209 | |
|
|
210 | return true; |
|
|
211 | } |
|
|
212 | |
190 | bool byte_stream::put (vpn_packet *pkt) |
213 | bool byte_stream::put (vpn_packet *pkt) |
191 | { |
214 | { |
192 | if (maxsize - fill < pkt->len + 2) |
215 | if (maxsize - fill < pkt->len + 2) |
193 | return false; |
216 | return false; |
194 | |
217 | |
… | |
… | |
202 | |
225 | |
203 | vpn_packet *byte_stream::get () |
226 | vpn_packet *byte_stream::get () |
204 | { |
227 | { |
205 | int len = (data [0] << 8) | data [1]; |
228 | int len = (data [0] << 8) | data [1]; |
206 | |
229 | |
|
|
230 | if (len > MAXSIZE && fill >= 2) |
|
|
231 | abort (); // TODO handle this gracefully, connection reset |
|
|
232 | |
207 | if (fill < len + 2) |
233 | if (fill < len + 2) |
208 | return 0; |
234 | return 0; |
209 | |
235 | |
210 | vpn_packet *pkt = new vpn_packet; |
236 | vpn_packet *pkt = new vpn_packet; |
|
|
237 | |
|
|
238 | pkt->len = len; |
211 | memcpy (&((*pkt)[0]), data + 2, len); |
239 | memcpy (&((*pkt)[0]), data + 2, len); |
212 | remove (len + 2); |
240 | remove (len + 2); |
213 | |
241 | |
214 | return pkt; |
242 | return pkt; |
215 | } |
243 | } |
… | |
… | |
222 | #define FLAG_OP_QUERY ( 0 << 11) |
250 | #define FLAG_OP_QUERY ( 0 << 11) |
223 | #define FLAG_AA ( 1 << 10) |
251 | #define FLAG_AA ( 1 << 10) |
224 | #define FLAG_TC ( 1 << 9) |
252 | #define FLAG_TC ( 1 << 9) |
225 | #define FLAG_RD ( 1 << 8) |
253 | #define FLAG_RD ( 1 << 8) |
226 | #define FLAG_RA ( 1 << 7) |
254 | #define FLAG_RA ( 1 << 7) |
|
|
255 | #define FLAG_AUTH ( 1 << 5) |
227 | #define FLAG_RCODE_MASK (15 << 0) |
256 | #define FLAG_RCODE_MASK (15 << 0) |
228 | #define FLAG_RCODE_OK ( 0 << 0) |
257 | #define FLAG_RCODE_OK ( 0 << 0) |
229 | #define FLAG_RCODE_FORMERR ( 1 << 0) |
258 | #define FLAG_RCODE_FORMERR ( 1 << 0) |
230 | #define FLAG_RCODE_SERVFAIL ( 2 << 0) |
259 | #define FLAG_RCODE_SERVFAIL ( 2 << 0) |
231 | #define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
260 | #define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
232 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
261 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
233 | |
262 | |
234 | #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
263 | #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) |
264 | #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
236 | |
265 | |
237 | struct dns_packet : net_packet |
266 | struct dns_packet : net_packet |
238 | { |
267 | { |
239 | u16 id; |
268 | u16 id; |
240 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
269 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
… | |
… | |
285 | { |
314 | { |
286 | dns_packet *pkt; |
315 | dns_packet *pkt; |
287 | tstamp next; |
316 | tstamp next; |
288 | int retry; |
317 | int retry; |
289 | connection *conn; |
318 | connection *conn; |
|
|
319 | int seqno; |
290 | |
320 | |
|
|
321 | dns_req (connection *c); |
291 | dns_req (connection *c, int seqno, byte_stream *stream); |
322 | void gen_stream_req (int seqno, byte_stream *stream); |
292 | }; |
323 | }; |
293 | |
324 | |
294 | struct dns_rep |
|
|
295 | { |
|
|
296 | }; |
|
|
297 | |
|
|
298 | static u16 dns_id; // TODO: should be per-vpn |
325 | static u16 dns_id = 12098; // TODO: should be per-vpn |
299 | |
326 | |
300 | dns_req::dns_req (connection *c, int seqno, byte_stream *stream) |
327 | static u16 next_id () |
|
|
328 | { |
|
|
329 | // the simplest lsfr with periodicity 65535 i could find |
|
|
330 | dns_id = (dns_id << 1) |
|
|
331 | | (((dns_id >> 1) |
|
|
332 | ^ (dns_id >> 2) |
|
|
333 | ^ (dns_id >> 4) |
|
|
334 | ^ (dns_id >> 15)) & 1); |
|
|
335 | |
|
|
336 | return dns_id; |
|
|
337 | } |
|
|
338 | |
|
|
339 | dns_req::dns_req (connection *c) |
301 | : conn (c) |
340 | : conn (c) |
302 | { |
341 | { |
303 | next = 0; |
342 | next = 0; |
304 | retry = 0; |
343 | retry = 0; |
305 | |
344 | |
306 | pkt = new dns_packet; |
345 | pkt = new dns_packet; |
307 | |
346 | |
308 | pkt->id = dns_id++; |
347 | pkt->id = next_id (); |
|
|
348 | } |
|
|
349 | |
|
|
350 | void dns_req::gen_stream_req (int seqno, byte_stream *stream) |
|
|
351 | { |
|
|
352 | this->seqno = seqno; |
|
|
353 | |
309 | pkt->flags = DEFAULT_CLIENT_FLAGS; |
354 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
310 | pkt->qdcount = htons (1); |
355 | pkt->qdcount = htons (1); |
311 | pkt->ancount = 0; |
|
|
312 | pkt->nscount = 0; |
|
|
313 | pkt->arcount = 0; |
|
|
314 | |
356 | |
315 | int offs = 6*2; |
357 | int offs = 6*2; |
316 | int dlen = 255 - strlen (THISNODE->domain) - 1; // here we always have room for the max. label length |
358 | int dlen = MAX_DOMAIN_SIZE - strlen (THISNODE->domain) - 2; |
|
|
359 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
|
|
360 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
317 | |
361 | |
318 | u8 lbl[64]; //D |
362 | u8 data[256]; //TODO |
319 | char enc[65]; |
363 | |
320 | int lbllen = 3; |
364 | data[0] = THISNODE->id; //TODO |
321 | lbl[0] = c->conf->id; //D |
|
|
322 | lbl[1] = seqno >> 8; //D |
365 | data[1] = seqno >> 7; //TODO |
323 | lbl[2] = seqno; //D |
366 | data[2] = seqno << 1; //TODO |
324 | |
367 | |
325 | while (dlen > 0) |
368 | int datalen = dns64::decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE) - 3; |
326 | { |
|
|
327 | int sndlen = dlen; |
|
|
328 | |
369 | |
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 ()) |
370 | if (datalen > stream->size ()) |
333 | sndlen = stream->size (); |
371 | datalen = stream->size (); |
334 | |
372 | |
335 | if (sndlen + lbllen == 0) |
373 | char enc[256], *encp = enc; |
336 | break; |
374 | |
337 | |
|
|
338 | memcpy (lbl + lbllen, stream->begin (), sndlen); |
375 | memcpy (data + 3, stream->begin (), datalen); |
|
|
376 | int enclen = dns64::encode (enc, data, datalen + 3); |
339 | stream->remove (sndlen); |
377 | stream->remove (datalen); |
340 | lbllen += sndlen; |
|
|
341 | |
378 | |
342 | dns64::encode (enc, lbl, lbllen); |
379 | while (enclen) |
|
|
380 | { |
|
|
381 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
343 | |
382 | |
344 | int elen = dns64::encode_len (lbllen); |
|
|
345 | (*pkt)[offs++] = elen; |
383 | (*pkt)[offs++] = lbllen; |
346 | memcpy (&((*pkt)[offs]), enc, elen); |
384 | memcpy (pkt->at (offs), encp, lbllen); |
|
|
385 | |
347 | offs += elen; |
386 | offs += lbllen; |
348 | dlen -= elen + 1; |
387 | encp += lbllen; |
349 | |
388 | |
350 | lbllen = 0; |
389 | enclen -= lbllen; |
351 | } |
390 | } |
352 | |
391 | |
353 | const char *suffix = THISNODE->domain; |
392 | const char *suffix = THISNODE->domain; |
354 | |
393 | |
355 | // add tunnel domain |
394 | // add tunnel domain |
… | |
… | |
375 | (*pkt)[offs++] = 0; |
414 | (*pkt)[offs++] = 0; |
376 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
415 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
377 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
416 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
378 | |
417 | |
379 | pkt->len = offs; |
418 | pkt->len = offs; |
|
|
419 | } |
380 | |
420 | |
381 | c->vpn->dns_sndpq.push_back (this); |
421 | struct dns_rcv |
|
|
422 | { |
|
|
423 | int seqno; |
|
|
424 | dns_packet *pkt; // reply packet |
|
|
425 | u8 data [MAXSIZE]; // actually part of the reply packet... |
|
|
426 | int datalen; |
|
|
427 | |
|
|
428 | dns_rcv (int seqno, u8 *data, int datalen); |
|
|
429 | ~dns_rcv (); |
|
|
430 | }; |
|
|
431 | |
|
|
432 | dns_rcv::dns_rcv (int seqno, u8 *data, int datalen) |
|
|
433 | : seqno (seqno), pkt (new dns_packet), datalen (datalen) |
|
|
434 | { |
|
|
435 | memcpy (this->data, data, datalen); |
|
|
436 | } |
|
|
437 | |
|
|
438 | dns_rcv::~dns_rcv () |
|
|
439 | { |
|
|
440 | delete pkt; |
382 | } |
441 | } |
383 | |
442 | |
384 | ///////////////////////////////////////////////////////////////////////////// |
443 | ///////////////////////////////////////////////////////////////////////////// |
385 | |
444 | |
386 | struct dns_cfg |
445 | struct dns_cfg |
… | |
… | |
390 | u8 unused1; |
449 | u8 unused1; |
391 | u16 max_size; |
450 | u16 max_size; |
392 | u8 flags1, flags2; |
451 | u8 flags1, flags2; |
393 | }; |
452 | }; |
394 | |
453 | |
|
|
454 | void connection::dnsv4_receive_rep (struct dns_rcv *r) |
|
|
455 | { |
|
|
456 | dns_rcvpq.push_back (r); |
|
|
457 | |
|
|
458 | redo: |
|
|
459 | |
|
|
460 | for (vector<dns_rcv *>::iterator i = dns_rcvpq.begin (); |
|
|
461 | i != dns_rcvpq.end (); |
|
|
462 | ++i) |
|
|
463 | if (dns_rcvseq == (*i)->seqno) |
|
|
464 | { |
|
|
465 | dns_rcv *r = *i; |
|
|
466 | |
|
|
467 | dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; |
|
|
468 | |
|
|
469 | if (!dns_snddq && !dns_rcvdq) |
|
|
470 | { |
|
|
471 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
|
|
472 | dns_snddq = new byte_stream (MAX_BACKLOG); |
|
|
473 | |
|
|
474 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
|
|
475 | } |
|
|
476 | |
|
|
477 | if (!dns_rcvdq->put (r->data, r->datalen)) |
|
|
478 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
|
|
479 | |
|
|
480 | while (vpn_packet *pkt = dns_rcvdq->get ()) |
|
|
481 | { |
|
|
482 | sockinfo si; |
|
|
483 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
|
|
484 | |
|
|
485 | vpn->recv_vpn_packet (pkt, si); |
|
|
486 | } |
|
|
487 | } |
|
|
488 | else if ((u32)(*i)->seqno - (u32)dns_rcvseq + MAX_WINDOW > MAX_WINDOW * 2) |
|
|
489 | { |
|
|
490 | //D |
|
|
491 | //abort(); |
|
|
492 | printf ("%d erasing %d (%d)\n", THISNODE->id, (u32)(*i)->seqno, dns_rcvseq); |
|
|
493 | dns_rcvpq.erase (i); |
|
|
494 | goto redo; |
|
|
495 | } |
|
|
496 | } |
|
|
497 | |
395 | dns_packet * |
498 | dns_packet * |
396 | vpn::dnsv4_server (dns_packet *pkt) |
499 | vpn::dnsv4_server (dns_packet *pkt) |
397 | { |
500 | { |
398 | u16 flags = ntohs (pkt->flags); |
501 | u16 flags = ntohs (pkt->flags); |
399 | |
502 | |
… | |
… | |
405 | if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
508 | if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
406 | && pkt->qdcount == htons (1)) |
509 | && pkt->qdcount == htons (1)) |
407 | { |
510 | { |
408 | char qname[MAXSIZE]; |
511 | char qname[MAXSIZE]; |
409 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
512 | 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 | |
513 | |
414 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
514 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
415 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
515 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
416 | |
516 | |
417 | pkt->qdcount = htons (1); |
517 | pkt->qdcount = htons (1); |
… | |
… | |
426 | if (qclass == RR_CLASS_IN |
526 | if (qclass == RR_CLASS_IN |
427 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
527 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
428 | && qlen > dlen + 1 |
528 | && qlen > dlen + 1 |
429 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
529 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
430 | { |
530 | { |
431 | // correct class, domain, parse |
531 | // correct class, domain: parse |
432 | u8 data[MAXSIZE]; |
532 | u8 data[MAXSIZE]; |
433 | int datalen = dns64::decode (data, qname, qlen - dlen - 1); |
533 | int datalen = dns64::decode (data, qname, qlen - dlen - 1); |
434 | |
534 | |
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; |
535 | int client = data[0]; |
|
|
536 | int seqno = ((data[1] << 7) | (data[2] >> 1)) & SEQNO_MASK; |
467 | |
537 | |
468 | while (dlen > 1) |
538 | if (0 < client && client <= conns.size ()) |
469 | { |
539 | { |
470 | int sndlen = --dlen; |
540 | connection *c = conns [client - 1]; |
471 | |
541 | |
472 | if (sndlen + txtlen > 255) |
542 | redo: |
473 | sndlen = 255 - txtlen; |
|
|
474 | |
543 | |
|
|
544 | for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.begin (); |
|
|
545 | i != c->dns_rcvpq.end (); |
|
|
546 | ++i) |
|
|
547 | if ((*i)->seqno == seqno) |
|
|
548 | { |
|
|
549 | // already seen that request: simply reply with the cached reply |
|
|
550 | dns_rcv *r = *i; |
|
|
551 | |
|
|
552 | printf ("DUPLICATE %d\n", htons (r->pkt->id));//D |
|
|
553 | |
|
|
554 | offs = r->pkt->len; |
|
|
555 | memcpy (pkt->at (0), r->pkt->at (0), offs); |
|
|
556 | goto duplicate_request; |
|
|
557 | } |
|
|
558 | |
|
|
559 | // new packet, queue |
|
|
560 | dns_rcv *rcv = new dns_rcv (seqno, data + 3, datalen - 3); |
|
|
561 | c->dnsv4_receive_rep (rcv); |
|
|
562 | |
|
|
563 | // now generate reply |
|
|
564 | pkt->ancount = htons (1); // one answer RR |
|
|
565 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
|
|
566 | |
|
|
567 | (*pkt) [offs++] = 0xc0; |
|
|
568 | (*pkt) [offs++] = 6 * 2; // same as in query section |
|
|
569 | |
|
|
570 | (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; |
|
|
571 | (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; |
|
|
572 | |
|
|
573 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; |
|
|
574 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL |
|
|
575 | |
|
|
576 | int dlen = MAX_PKT_SIZE - offs - 2; |
|
|
577 | |
|
|
578 | // bind doesn't compress well, so reduce further by one label length |
475 | sndlen = 0; |
579 | dlen -= qlen; |
476 | |
580 | |
|
|
581 | int rdlen_offs = offs += 2; |
|
|
582 | |
|
|
583 | while (c->dns_snddq |
|
|
584 | && !c->dns_snddq->empty () |
|
|
585 | && dlen > 1) |
|
|
586 | { |
|
|
587 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
|
|
588 | |
|
|
589 | if (txtlen > c->dns_snddq->size ()) |
|
|
590 | txtlen = c->dns_snddq->size (); |
|
|
591 | |
477 | (*rep)[offs++] = sndlen + txtlen; |
592 | (*pkt)[offs++] = txtlen; |
478 | memcpy (&((*rep)[offs]), txt, txtlen); |
593 | memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen); |
479 | offs += txtlen; |
594 | offs += txtlen; |
|
|
595 | c->dns_snddq->remove (txtlen); |
480 | |
596 | |
481 | //if (sndlen + txtlen > dns_snddq. |
597 | dlen -= txtlen + 1; |
|
|
598 | } |
|
|
599 | |
|
|
600 | // avoid empty TXT rdata |
|
|
601 | if (offs == rdlen_offs) |
|
|
602 | (*pkt)[offs++] = 0; |
|
|
603 | |
|
|
604 | int rdlen = offs - rdlen_offs; |
|
|
605 | |
|
|
606 | (*pkt) [rdlen_offs - 2] = rdlen >> 8; |
|
|
607 | (*pkt) [rdlen_offs - 1] = rdlen; |
|
|
608 | |
|
|
609 | // now update dns_rcv copy |
|
|
610 | rcv->pkt->len = offs; |
|
|
611 | memcpy (rcv->pkt->at (0), pkt->at (0), offs); |
|
|
612 | |
|
|
613 | duplicate_request: ; |
482 | } |
614 | } |
483 | |
615 | else |
484 | int rdlen = offs - rdlen_offs; |
616 | 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 | } |
617 | } |
490 | } |
|
|
491 | |
618 | |
492 | pkt->len = offs; |
619 | pkt->len = offs; |
|
|
620 | } |
|
|
621 | |
493 | return pkt; |
622 | return pkt; |
494 | } |
623 | } |
495 | |
624 | |
496 | void |
625 | void |
497 | vpn::dnsv4_client (dns_packet *pkt) |
626 | vpn::dnsv4_client (dns_packet *pkt) |
… | |
… | |
500 | int offs = 6 * 2; // skip header |
629 | int offs = 6 * 2; // skip header |
501 | |
630 | |
502 | pkt->qdcount = ntohs (pkt->qdcount); |
631 | pkt->qdcount = ntohs (pkt->qdcount); |
503 | pkt->ancount = ntohs (pkt->ancount); |
632 | pkt->ancount = ntohs (pkt->ancount); |
504 | |
633 | |
|
|
634 | // go through our request list and find the corresponding request |
505 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
635 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
506 | i != dns_sndpq.end (); |
636 | i != dns_sndpq.end (); |
507 | ++i) |
637 | ++i) |
508 | if ((*i)->pkt->id == pkt->id) |
638 | if ((*i)->pkt->id == pkt->id) |
509 | { |
639 | { |
510 | connection *c = (*i)->conn; |
640 | connection *c = (*i)->conn; |
511 | printf ("GOT RESPONSE id %x %p\n", pkt->id, c);//D |
641 | int seqno = (*i)->seqno; |
|
|
642 | u8 data[MAXSIZE], *datap = data; |
|
|
643 | |
512 | delete *i; |
644 | delete *i; |
513 | dns_sndpq.erase (i); |
645 | dns_sndpq.erase (i); |
514 | |
646 | |
515 | if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
647 | if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
516 | { |
648 | { |
… | |
… | |
533 | ttl |= (*pkt) [offs++] << 8; |
665 | ttl |= (*pkt) [offs++] << 8; |
534 | ttl |= (*pkt) [offs++]; |
666 | ttl |= (*pkt) [offs++]; |
535 | |
667 | |
536 | u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
668 | u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
537 | |
669 | |
538 | printf ("REPLY %d:%d TTL %d RD %d\n", qtype, qclass, ttl, rdlen); |
|
|
539 | |
|
|
540 | offs += rdlen; |
|
|
541 | |
|
|
542 | if (MAXSIZE - offs < rdlen) |
670 | if (rdlen <= MAXSIZE - offs) |
543 | { |
671 | { |
544 | // decode bytes, finally |
672 | // decode bytes, finally |
|
|
673 | |
|
|
674 | while (rdlen) |
|
|
675 | { |
|
|
676 | int txtlen = (*pkt) [offs++]; |
|
|
677 | |
|
|
678 | assert (txtlen + offs < MAXSIZE - 1); |
|
|
679 | |
|
|
680 | memcpy (datap, pkt->at (offs), txtlen); |
|
|
681 | datap += txtlen; offs += txtlen; |
|
|
682 | |
|
|
683 | rdlen -= txtlen + 1; |
|
|
684 | } |
|
|
685 | |
545 | } |
686 | } |
|
|
687 | |
546 | } |
688 | } |
547 | } |
689 | } |
548 | |
690 | |
|
|
691 | // todo: pkt now used |
|
|
692 | c->dnsv4_receive_rep (new dns_rcv (seqno, data, datap - data)); |
|
|
693 | |
549 | break; |
694 | break; |
550 | } |
695 | } |
|
|
696 | |
|
|
697 | delete pkt; |
551 | } |
698 | } |
552 | |
699 | |
553 | void |
700 | void |
554 | vpn::dnsv4_ev (io_watcher &w, short revents) |
701 | vpn::dnsv4_ev (io_watcher &w, short revents) |
555 | { |
702 | { |
… | |
… | |
560 | socklen_t sa_len = sizeof (sa); |
707 | socklen_t sa_len = sizeof (sa); |
561 | |
708 | |
562 | pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
709 | pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
563 | |
710 | |
564 | if (pkt->len > 0) |
711 | if (pkt->len > 0) |
|
|
712 | { |
|
|
713 | if (pkt->flags & htons (FLAG_TC)) |
|
|
714 | { |
|
|
715 | slog (L_WARN, _("DNS request/response truncated, check protocol settings.")); |
|
|
716 | //TODO connection reset |
|
|
717 | } |
|
|
718 | |
565 | if (THISNODE->dns_port) |
719 | if (THISNODE->dns_port) |
566 | { |
720 | { |
567 | pkt = dnsv4_server (pkt); |
721 | pkt = dnsv4_server (pkt); |
568 | sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
722 | sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
569 | } |
723 | } |
570 | else |
724 | else |
571 | dnsv4_client (pkt); |
725 | dnsv4_client (pkt); |
|
|
726 | } |
572 | } |
727 | } |
573 | } |
728 | } |
574 | |
729 | |
575 | bool |
730 | bool |
576 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
731 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
… | |
… | |
579 | if (!dns_snddq && !dns_rcvdq) |
734 | if (!dns_snddq && !dns_rcvdq) |
580 | { |
735 | { |
581 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
736 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
582 | dns_snddq = new byte_stream (MAX_BACKLOG); |
737 | dns_snddq = new byte_stream (MAX_BACKLOG); |
583 | |
738 | |
584 | dns_rcvseq = dns_sndseq = 0; |
739 | //dns_rcvseq = dns_sndseq = 0; |
585 | |
740 | |
586 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
741 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
587 | } |
742 | } |
588 | |
743 | |
589 | if (!dns_snddq->put (pkt)) |
744 | if (!dns_snddq->put (pkt)) |
590 | return false; |
745 | return false; |
591 | |
746 | |
592 | // start timer if neccessary |
747 | // start timer if neccessary |
593 | if (!dnsv4_tw.active) |
748 | if (!THISNODE->dns_port && !dnsv4_tw.active) |
594 | dnsv4_cb (dnsv4_tw); |
749 | dnsv4_cb (dnsv4_tw); |
595 | |
750 | |
596 | return true; |
751 | return true; |
597 | } |
752 | } |
598 | |
753 | |
… | |
… | |
601 | { |
756 | { |
602 | // check for timeouts and (re)transmit |
757 | // check for timeouts and (re)transmit |
603 | tstamp next = NOW + 60; |
758 | tstamp next = NOW + 60; |
604 | dns_req *send = 0; |
759 | dns_req *send = 0; |
605 | |
760 | |
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 (); |
761 | for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); |
609 | i != vpn->dns_sndpq.end (); |
762 | i != vpn->dns_sndpq.end (); |
610 | ++i) |
763 | ++i) |
611 | { |
764 | { |
612 | dns_req *r = *i; |
765 | dns_req *r = *i; |
… | |
… | |
615 | { |
768 | { |
616 | if (!send) |
769 | if (!send) |
617 | { |
770 | { |
618 | send = r; |
771 | send = r; |
619 | |
772 | |
620 | slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); |
773 | if (r->retry)//D |
|
|
774 | printf ("req %d, retry %d\n", r->pkt->id, r->retry); |
621 | r->retry++; |
775 | r->retry++; |
622 | r->next = NOW + r->retry; |
776 | r->next = NOW + r->retry; |
623 | } |
777 | } |
624 | } |
778 | } |
625 | |
779 | |
… | |
… | |
627 | next = r->next; |
781 | next = r->next; |
628 | } |
782 | } |
629 | |
783 | |
630 | if (!send |
784 | if (!send |
631 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
785 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
632 | send = new dns_req (this, dns_sndseq++, dns_snddq); |
786 | { |
|
|
787 | send = new dns_req (this); |
|
|
788 | send->gen_stream_req (dns_sndseq, dns_snddq); |
|
|
789 | vpn->dns_sndpq.push_back (send); |
|
|
790 | |
|
|
791 | dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK; |
|
|
792 | } |
633 | |
793 | |
634 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
794 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
635 | |
795 | |
636 | if (send) |
796 | if (send) |
637 | { |
797 | { |