… | |
… | |
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 10 // max. outstanding requests |
|
|
48 | #define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
|
|
49 | #define MAX_RATE 1 // requests/s |
|
|
50 | #define MAX_BACKLOG (10*1024) // size of protocol backlog |
48 | |
51 | |
49 | /* |
52 | /* |
50 | |
53 | |
51 | protocol, in shorthand :) |
54 | protocol, in shorthand :) |
52 | |
55 | |
… | |
… | |
75 | static const char encode_chars[64 + 1]; |
78 | static const char encode_chars[64 + 1]; |
76 | static s8 decode_chars[256]; |
79 | static s8 decode_chars[256]; |
77 | |
80 | |
78 | static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } |
81 | static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } |
79 | static int decode_len (int bytes) { return (bytes * 6) / 8; } |
82 | static int decode_len (int bytes) { return (bytes * 6) / 8; } |
80 | static void encode (char *dst, u8 *src, int len); |
83 | static int encode (char *dst, u8 *src, int len); |
81 | static void decode (u8 *dst, char *src, int len); |
84 | static int decode (u8 *dst, char *src, int len); |
82 | |
85 | |
83 | dns64 (); |
86 | dns64 (); |
84 | } dns64; |
87 | } dns64; |
85 | |
88 | |
86 | const char dns64::encode_chars[64 + 1] = "0123456789-abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
89 | const char dns64::encode_chars[64 + 1] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"; |
87 | s8 dns64::decode_chars[256]; |
90 | s8 dns64::decode_chars[256]; |
88 | |
91 | |
89 | dns64::dns64 () |
92 | dns64::dns64 () |
90 | { |
93 | { |
91 | for (int i = 0; i < 64; i++) |
94 | for (int i = 0; i < 64; i++) |
92 | decode_chars [encode_chars [i]] = i + 1; |
95 | decode_chars [encode_chars [i]] = i + 1; |
93 | } |
96 | } |
94 | |
97 | |
95 | void dns64::encode (char *dst, u8 *src, int len) |
98 | int dns64::encode (char *dst, u8 *src, int len) |
96 | { |
99 | { |
97 | // slow, but easy to debug |
100 | // slow, but easy to debug |
|
|
101 | char *beg = dst; |
98 | unsigned int accum, bits = 0; |
102 | unsigned int accum, bits = 0; |
99 | |
103 | |
100 | while (len--) |
104 | while (len--) |
101 | { |
105 | { |
102 | accum <<= 8; |
106 | accum <<= 8; |
… | |
… | |
110 | } |
114 | } |
111 | } |
115 | } |
112 | |
116 | |
113 | if (bits) |
117 | if (bits) |
114 | *dst++ = encode_chars [(accum << (6 - bits)) & 63]; |
118 | *dst++ = encode_chars [(accum << (6 - bits)) & 63]; |
115 | } |
|
|
116 | |
119 | |
|
|
120 | return dst - beg; |
|
|
121 | } |
|
|
122 | |
117 | void dns64::decode (u8 *dst, char *src, int len) |
123 | int dns64::decode (u8 *dst, char *src, int len) |
118 | { |
124 | { |
119 | // slow, but easy to debug |
125 | // slow, but easy to debug |
|
|
126 | u8 *beg = dst; |
120 | unsigned int accum, bits = 0; |
127 | unsigned int accum, bits = 0; |
121 | |
128 | |
122 | while (len--) |
129 | while (len--) |
123 | { |
130 | { |
124 | s8 chr = decode_chars [(u8)*src++]; |
131 | s8 chr = decode_chars [(u8)*src++]; |
125 | |
|
|
126 | |
132 | |
127 | if (!chr) |
133 | if (!chr) |
128 | break; |
134 | break; |
129 | |
135 | |
130 | accum <<= 6; |
136 | accum <<= 6; |
… | |
… | |
135 | { |
141 | { |
136 | *dst++ = accum >> (bits - 8); |
142 | *dst++ = accum >> (bits - 8); |
137 | bits -= 8; |
143 | bits -= 8; |
138 | } |
144 | } |
139 | } |
145 | } |
|
|
146 | |
|
|
147 | return dst - beg; |
|
|
148 | } |
|
|
149 | |
|
|
150 | ///////////////////////////////////////////////////////////////////////////// |
|
|
151 | |
|
|
152 | struct byte_stream |
|
|
153 | { |
|
|
154 | u8 *data; |
|
|
155 | int maxsize; |
|
|
156 | int fill; |
|
|
157 | |
|
|
158 | byte_stream (int maxsize); |
|
|
159 | ~byte_stream (); |
|
|
160 | |
|
|
161 | bool empty () { return !fill; } |
|
|
162 | int size () { return fill; } |
|
|
163 | |
|
|
164 | bool put (vpn_packet *pkt); |
|
|
165 | vpn_packet *get (); |
|
|
166 | |
|
|
167 | u8 *begin () { return data; } |
|
|
168 | void remove (int count); |
|
|
169 | }; |
|
|
170 | |
|
|
171 | byte_stream::byte_stream (int maxsize) |
|
|
172 | : maxsize (maxsize), fill (0) |
|
|
173 | { |
|
|
174 | data = new u8 [maxsize]; |
|
|
175 | } |
|
|
176 | |
|
|
177 | byte_stream::~byte_stream () |
|
|
178 | { |
|
|
179 | delete data; |
|
|
180 | } |
|
|
181 | |
|
|
182 | void byte_stream::remove (int count) |
|
|
183 | { |
|
|
184 | if (count > fill) |
|
|
185 | abort (); |
|
|
186 | |
|
|
187 | memmove (data, data + count, fill -= count); |
|
|
188 | } |
|
|
189 | |
|
|
190 | bool byte_stream::put (vpn_packet *pkt) |
|
|
191 | { |
|
|
192 | if (maxsize - fill < pkt->len + 2) |
|
|
193 | return false; |
|
|
194 | |
|
|
195 | data [fill++] = pkt->len >> 8; |
|
|
196 | data [fill++] = pkt->len; |
|
|
197 | |
|
|
198 | memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len; |
|
|
199 | |
|
|
200 | return true; |
|
|
201 | } |
|
|
202 | |
|
|
203 | vpn_packet *byte_stream::get () |
|
|
204 | { |
|
|
205 | int len = (data [0] << 8) | data [1]; |
|
|
206 | |
|
|
207 | if (fill < len + 2) |
|
|
208 | return 0; |
|
|
209 | |
|
|
210 | vpn_packet *pkt = new vpn_packet; |
|
|
211 | memcpy (&((*pkt)[0]), data + 2, len); |
|
|
212 | remove (len + 2); |
|
|
213 | |
|
|
214 | return pkt; |
140 | } |
215 | } |
141 | |
216 | |
142 | ///////////////////////////////////////////////////////////////////////////// |
217 | ///////////////////////////////////////////////////////////////////////////// |
143 | |
218 | |
144 | #define FLAG_QUERY ( 0 << 15) |
219 | #define FLAG_QUERY ( 0 << 15) |
… | |
… | |
209 | struct dns_req |
284 | struct dns_req |
210 | { |
285 | { |
211 | dns_packet *pkt; |
286 | dns_packet *pkt; |
212 | tstamp next; |
287 | tstamp next; |
213 | int retry; |
288 | int retry; |
|
|
289 | connection *conn; |
214 | |
290 | |
215 | dns_req (connection *c, vpn_packet *p, int &offs); |
291 | dns_req (connection *c, int seqno, byte_stream *stream); |
216 | }; |
292 | }; |
217 | |
293 | |
218 | struct dns_rep |
294 | struct dns_rep |
219 | { |
295 | { |
220 | }; |
296 | }; |
221 | |
297 | |
222 | static u16 dns_id; // TODO: should be per-vpn |
298 | static u16 dns_id; // TODO: should be per-vpn |
223 | |
299 | |
224 | dns_req::dns_req (connection *c, vpn_packet *p, int &offs) |
300 | dns_req::dns_req (connection *c, int seqno, byte_stream *stream) |
|
|
301 | : conn (c) |
225 | { |
302 | { |
226 | next = 0; |
303 | next = 0; |
227 | retry = 0; |
304 | retry = 0; |
228 | |
305 | |
229 | pkt = new dns_packet; |
306 | pkt = new dns_packet; |
230 | |
307 | |
231 | pkt->id = dns_id++; |
308 | pkt->id = dns_id++; |
232 | pkt->flags = DEFAULT_CLIENT_FLAGS; |
309 | pkt->flags = DEFAULT_CLIENT_FLAGS; |
233 | pkt->qdcount = htons (1 * 0); |
310 | pkt->qdcount = htons (1); |
234 | pkt->ancount = 0; |
311 | pkt->ancount = 0; |
235 | pkt->nscount = 0; |
312 | pkt->nscount = 0; |
236 | pkt->arcount = 0; |
313 | pkt->arcount = 0; |
237 | |
314 | |
238 | offs += 10000; |
315 | int offs = 6*2; |
|
|
316 | int dlen = 255 - strlen (THISNODE->domain) - 1; // here we always have room for the max. label length |
239 | |
317 | |
|
|
318 | u8 lbl[64]; //D |
|
|
319 | char enc[65]; |
|
|
320 | int lbllen = 3; |
|
|
321 | lbl[0] = c->conf->id; //D |
|
|
322 | lbl[1] = seqno >> 8; //D |
|
|
323 | lbl[2] = seqno; //D |
|
|
324 | |
|
|
325 | while (dlen > 0) |
|
|
326 | { |
|
|
327 | int sndlen = dlen; |
|
|
328 | |
|
|
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 ()) |
|
|
333 | sndlen = stream->size (); |
|
|
334 | |
|
|
335 | if (sndlen + lbllen == 0) |
|
|
336 | break; |
|
|
337 | |
|
|
338 | memcpy (lbl + lbllen, stream->begin (), sndlen); |
|
|
339 | stream->remove (sndlen); |
|
|
340 | lbllen += sndlen; |
|
|
341 | |
|
|
342 | dns64::encode (enc, lbl, lbllen); |
|
|
343 | |
|
|
344 | int elen = dns64::encode_len (lbllen); |
|
|
345 | (*pkt)[offs++] = elen; |
|
|
346 | memcpy (&((*pkt)[offs]), enc, elen); |
|
|
347 | offs += elen; |
|
|
348 | dlen -= elen + 1; |
|
|
349 | |
|
|
350 | lbllen = 0; |
|
|
351 | } |
|
|
352 | |
|
|
353 | const char *suffix = THISNODE->domain; |
|
|
354 | |
|
|
355 | // add tunnel domain |
|
|
356 | for (;;) |
|
|
357 | { |
|
|
358 | const char *end = strchr (suffix, '.'); |
|
|
359 | |
|
|
360 | if (!end) |
|
|
361 | end = suffix + strlen (suffix); |
|
|
362 | |
|
|
363 | int len = end - suffix; |
|
|
364 | |
|
|
365 | (*pkt)[offs++] = len; |
|
|
366 | memcpy (&((*pkt)[offs]), suffix, len); |
|
|
367 | offs += len; |
|
|
368 | |
|
|
369 | if (!*end) |
|
|
370 | break; |
|
|
371 | |
|
|
372 | suffix = end + 1; |
|
|
373 | } |
|
|
374 | |
|
|
375 | (*pkt)[offs++] = 0; |
|
|
376 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
|
|
377 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
|
|
378 | |
|
|
379 | pkt->len = offs; |
|
|
380 | |
240 | c->dns_sndq.push_back (this); |
381 | c->vpn->dns_sndpq.push_back (this); |
241 | } |
382 | } |
242 | |
383 | |
243 | ///////////////////////////////////////////////////////////////////////////// |
384 | ///////////////////////////////////////////////////////////////////////////// |
244 | |
385 | |
245 | struct dns_cfg |
386 | struct dns_cfg |
… | |
… | |
249 | u8 unused1; |
390 | u8 unused1; |
250 | u16 max_size; |
391 | u16 max_size; |
251 | u8 flags1, flags2; |
392 | u8 flags1, flags2; |
252 | }; |
393 | }; |
253 | |
394 | |
254 | // reply to client-poll |
395 | dns_packet * |
255 | void dnsv4_poll (dns_packet *pkt, int &offs) |
396 | vpn::dnsv4_server (dns_packet *pkt) |
256 | { |
397 | { |
257 | int dlen = MAX_PKT_SIZE - offs - 2; |
398 | u16 flags = ntohs (pkt->flags); |
258 | |
399 | |
259 | (*pkt) [offs++] = 0; |
400 | //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
260 | (*pkt) [offs++] = 5; |
401 | int offs = 6 * 2; // skip header |
261 | memcpy (&((*pkt)[offs]), "\01H\02\xff\x00", 5); |
|
|
262 | offs += 5; |
|
|
263 | |
402 | |
264 | pkt->ancount = htons (1); |
|
|
265 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
403 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
266 | |
404 | |
|
|
405 | if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
|
|
406 | && pkt->qdcount == htons (1)) |
|
|
407 | { |
|
|
408 | char qname[MAXSIZE]; |
|
|
409 | 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 | |
|
|
414 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
|
|
415 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
|
|
416 | |
|
|
417 | pkt->qdcount = htons (1); |
|
|
418 | pkt->ancount = 0; |
|
|
419 | pkt->nscount = 0; // should be self, as other nameservers reply like this |
|
|
420 | pkt->arcount = 0; // a record for self, as other nameservers reply like this |
|
|
421 | |
|
|
422 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
|
|
423 | |
|
|
424 | int dlen = strlen (THISNODE->domain); |
|
|
425 | |
|
|
426 | if (qclass == RR_CLASS_IN |
|
|
427 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
|
|
428 | && qlen > dlen + 1 |
|
|
429 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
|
|
430 | { |
|
|
431 | // correct class, domain, parse |
|
|
432 | u8 data[MAXSIZE]; |
|
|
433 | int datalen = dns64::decode (data, qname, qlen - dlen - 1); |
|
|
434 | |
|
|
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 | |
267 | printf ("we have room for %d bytes\n", dlen); |
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; |
|
|
467 | |
|
|
468 | while (dlen > 1) |
|
|
469 | { |
|
|
470 | int sndlen = --dlen; |
|
|
471 | |
|
|
472 | if (sndlen + txtlen > 255) |
|
|
473 | sndlen = 255 - txtlen; |
|
|
474 | |
|
|
475 | sndlen = 0; |
|
|
476 | |
|
|
477 | (*rep)[offs++] = sndlen + txtlen; |
|
|
478 | memcpy (&((*rep)[offs]), txt, txtlen); |
|
|
479 | offs += txtlen; |
|
|
480 | |
|
|
481 | //if (sndlen + txtlen > dns_snddq. |
|
|
482 | } |
|
|
483 | |
|
|
484 | int rdlen = offs - rdlen_offs; |
|
|
485 | |
|
|
486 | (*rep) [rdlen_offs - 2] = rdlen >> 8; |
|
|
487 | (*rep) [rdlen_offs - 1] = rdlen; |
|
|
488 | #endif |
|
|
489 | } |
|
|
490 | } |
|
|
491 | |
|
|
492 | pkt->len = offs; |
|
|
493 | return pkt; |
|
|
494 | } |
|
|
495 | |
|
|
496 | void |
|
|
497 | vpn::dnsv4_client (dns_packet *pkt) |
|
|
498 | { |
|
|
499 | u16 flags = ntohs (pkt->flags); |
|
|
500 | int offs = 6 * 2; // skip header |
|
|
501 | |
|
|
502 | pkt->qdcount = ntohs (pkt->qdcount); |
|
|
503 | pkt->ancount = ntohs (pkt->ancount); |
|
|
504 | |
|
|
505 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
|
|
506 | i != dns_sndpq.end (); |
|
|
507 | ++i) |
|
|
508 | if ((*i)->pkt->id == pkt->id) |
|
|
509 | { |
|
|
510 | connection *c = (*i)->conn; |
|
|
511 | printf ("GOT RESPONSE id %x %p\n", pkt->id, c);//D |
|
|
512 | delete *i; |
|
|
513 | dns_sndpq.erase (i); |
|
|
514 | |
|
|
515 | if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
|
|
516 | { |
|
|
517 | char qname[MAXSIZE]; |
|
|
518 | |
|
|
519 | while (pkt->qdcount-- && offs < MAXSIZE - 4) |
|
|
520 | { |
|
|
521 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
|
|
522 | offs += 4; // skip qtype, qclass |
|
|
523 | } |
|
|
524 | |
|
|
525 | while (pkt->ancount-- && offs < MAXSIZE - 10) |
|
|
526 | { |
|
|
527 | pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
|
|
528 | |
|
|
529 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
|
|
530 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
|
|
531 | u32 ttl = (*pkt) [offs++] << 24; |
|
|
532 | ttl |= (*pkt) [offs++] << 16; |
|
|
533 | ttl |= (*pkt) [offs++] << 8; |
|
|
534 | ttl |= (*pkt) [offs++]; |
|
|
535 | |
|
|
536 | u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
|
|
537 | |
|
|
538 | printf ("REPLY %d:%d TTL %d RD %d\n", qtype, qclass, ttl, rdlen); |
|
|
539 | |
|
|
540 | offs += rdlen; |
|
|
541 | |
|
|
542 | if (MAXSIZE - offs < rdlen) |
|
|
543 | { |
|
|
544 | // decode bytes, finally |
|
|
545 | } |
|
|
546 | } |
|
|
547 | } |
|
|
548 | |
|
|
549 | break; |
|
|
550 | } |
268 | } |
551 | } |
269 | |
552 | |
270 | void |
553 | void |
271 | vpn::dnsv4_ev (io_watcher &w, short revents) |
554 | vpn::dnsv4_ev (io_watcher &w, short revents) |
272 | { |
555 | { |
… | |
… | |
274 | { |
557 | { |
275 | dns_packet *pkt = new dns_packet; |
558 | dns_packet *pkt = new dns_packet; |
276 | struct sockaddr_in sa; |
559 | struct sockaddr_in sa; |
277 | socklen_t sa_len = sizeof (sa); |
560 | socklen_t sa_len = sizeof (sa); |
278 | |
561 | |
279 | int len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
562 | pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
280 | pkt->len = len; |
|
|
281 | |
563 | |
282 | u16 flags = ntohs (pkt->flags); |
564 | if (pkt->len > 0) |
283 | |
|
|
284 | if (THISNODE->dns_port) |
565 | if (THISNODE->dns_port) |
285 | { |
|
|
286 | // server |
|
|
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 | { |
566 | { |
294 | char qname[MAXSIZE]; |
567 | pkt = dnsv4_server (pkt); |
295 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE, offs); |
568 | sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
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 | } |
569 | } |
333 | |
|
|
334 | sendto (w.fd, &((*pkt)[0]), offs, 0, (sockaddr *)&sa, sa_len); |
|
|
335 | } |
|
|
336 | else |
570 | else |
337 | { |
571 | dnsv4_client (pkt); |
338 | // client |
|
|
339 | } |
|
|
340 | } |
572 | } |
341 | } |
573 | } |
342 | |
574 | |
343 | bool |
575 | bool |
344 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
576 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
345 | { |
577 | { |
346 | if (dns_sndq.size () >= MAX_OUTSTANDING) |
578 | // never initialized |
|
|
579 | if (!dns_snddq && !dns_rcvdq) |
|
|
580 | { |
|
|
581 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
|
|
582 | dns_snddq = new byte_stream (MAX_BACKLOG); |
|
|
583 | |
|
|
584 | dns_rcvseq = dns_sndseq = 0; |
|
|
585 | |
|
|
586 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
|
|
587 | } |
|
|
588 | |
|
|
589 | if (!dns_snddq->put (pkt)) |
347 | return false; |
590 | return false; |
348 | |
591 | |
349 | // split vpn packet into dns packets, add to sndq |
592 | // start timer if neccessary |
350 | for (int offs = 0; offs < pkt->len; ) |
593 | if (!dnsv4_tw.active) |
351 | new dns_req (this, pkt, offs); |
|
|
352 | |
|
|
353 | // force transmit |
|
|
354 | dnsv4_cb (dnsv4_tw); |
594 | dnsv4_cb (dnsv4_tw); |
355 | |
595 | |
356 | return true; |
596 | return true; |
357 | } |
597 | } |
358 | |
598 | |
359 | void |
599 | void |
360 | connection::dnsv4_cb (time_watcher &w) |
600 | connection::dnsv4_cb (time_watcher &w) |
361 | { |
601 | { |
362 | // check for timeouts and (re)transmit |
602 | // check for timeouts and (re)transmit |
363 | tstamp next = NOW + 60; |
603 | tstamp next = NOW + 60; |
|
|
604 | dns_req *send = 0; |
364 | |
605 | |
365 | slog (L_NOISE, _("dnsv4, %d packets in send queue\n"), dns_sndq.size ()); |
606 | slog (L_NOISE, _("dnsv4, %d packets in send queue\n"), vpn->dns_sndpq.size ()); |
366 | |
607 | |
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 (); |
608 | for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); |
374 | i != dns_sndq.end (); |
609 | i != vpn->dns_sndpq.end (); |
375 | ++i) |
610 | ++i) |
376 | { |
611 | { |
377 | dns_req *r = *i; |
612 | dns_req *r = *i; |
378 | |
613 | |
379 | if (r->next <= NOW) |
614 | if (r->next <= NOW) |
380 | { |
615 | { |
|
|
616 | if (!send) |
|
|
617 | { |
|
|
618 | send = r; |
|
|
619 | |
381 | slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); |
620 | slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); |
382 | r->retry++; |
621 | r->retry++; |
383 | r->next = NOW + r->retry; |
622 | r->next = NOW + r->retry; |
|
|
623 | } |
384 | } |
624 | } |
385 | |
625 | |
386 | if (r->next < next) |
626 | if (r->next < next) |
387 | next = r->next; |
627 | next = r->next; |
388 | } |
628 | } |
389 | |
629 | |
390 | printf ("%f next\n", next); |
630 | if (!send |
|
|
631 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
|
|
632 | send = new dns_req (this, dns_sndseq++, dns_snddq); |
|
|
633 | |
|
|
634 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
|
|
635 | |
|
|
636 | if (send) |
|
|
637 | { |
|
|
638 | dns_packet *pkt = send->pkt; |
|
|
639 | |
|
|
640 | next = min_next; |
|
|
641 | |
|
|
642 | sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ()); |
|
|
643 | } |
|
|
644 | else if (next < min_next) |
|
|
645 | next = min_next; |
|
|
646 | |
391 | w.start (next); |
647 | w.start (next); |
392 | } |
648 | } |
393 | |
649 | |
394 | #endif |
650 | #endif |
395 | |
651 | |