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 | |
… | |
… | |
35 | #include <unistd.h> |
37 | #include <unistd.h> |
36 | #include <fcntl.h> |
38 | #include <fcntl.h> |
37 | |
39 | |
38 | #include <map> |
40 | #include <map> |
39 | |
41 | |
|
|
42 | #include <gmp.h> |
|
|
43 | |
40 | #include "netcompat.h" |
44 | #include "netcompat.h" |
41 | |
45 | |
42 | #include "vpn.h" |
46 | #include "vpn.h" |
43 | |
47 | |
44 | #define MIN_RETRY 1. |
48 | #define MIN_RETRY 1. |
45 | #define MAX_RETRY 60. |
49 | #define MAX_RETRY 60. |
46 | |
50 | |
47 | #define MAX_OUTSTANDING 40 // max. outstanding requests |
51 | #define MAX_OUTSTANDING 400 // max. outstanding requests |
48 | #define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
52 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING |
49 | #define MAX_RATE 1000 // requests/s |
53 | #define MAX_RATE 10000 // requests/s |
50 | #define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE |
54 | #define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE |
51 | |
55 | |
52 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
56 | #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 |
57 | // 240 leaves about 4 bytes of server reply data |
54 | // every two request byte sless give room for one reply byte |
58 | // every two request byte sless give room for one reply byte |
55 | |
59 | |
|
|
60 | // seqno has 12 bits (3 bytes a 4 bits in the header) |
56 | #define SEQNO_MASK 0xffff |
61 | #define SEQNO_MASK 0x7fff |
57 | |
62 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
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 | |
63 | |
74 | #define MAX_LBL_SIZE 63 |
64 | #define MAX_LBL_SIZE 63 |
75 | #define MAX_PKT_SIZE 512 |
65 | #define MAX_PKT_SIZE 512 |
76 | |
66 | |
77 | #define RR_TYPE_TXT 16 |
67 | #define RR_TYPE_TXT 16 |
78 | #define RR_TYPE_ANY 255 |
68 | #define RR_TYPE_ANY 255 |
79 | #define RR_CLASS_IN 1 |
69 | #define RR_CLASS_IN 1 |
80 | |
70 | |
|
|
71 | // works for cmaps up to 255 (not 256!) |
|
|
72 | struct charmap |
|
|
73 | { |
|
|
74 | enum { INVALID = (u8)255 }; |
|
|
75 | |
|
|
76 | char encode [256]; // index => char |
|
|
77 | u8 decode [256]; // char => index |
|
|
78 | unsigned int size; |
|
|
79 | |
|
|
80 | charmap (const char *cmap); |
|
|
81 | }; |
|
|
82 | |
|
|
83 | charmap::charmap (const char *cmap) |
|
|
84 | { |
|
|
85 | char *enc = encode; |
|
|
86 | u8 *dec = decode; |
|
|
87 | |
|
|
88 | memset (enc, (char) 0, 256); |
|
|
89 | memset (dec, (char)INVALID, 256); |
|
|
90 | |
|
|
91 | for (size = 0; cmap [size]; size++) |
|
|
92 | { |
|
|
93 | enc [size] = cmap [size]; |
|
|
94 | dec [(u8)enc [size]] = size; |
|
|
95 | } |
|
|
96 | |
|
|
97 | assert (size < 256); |
|
|
98 | } |
|
|
99 | |
|
|
100 | #define MAX_DEC_LEN 500 |
|
|
101 | #define MAX_ENC_LEN (MAX_DEC_LEN * 2) |
|
|
102 | #define MAX_LIMBS ((MAX_DEC_LEN * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS) |
|
|
103 | |
|
|
104 | // ugly. minimum base is 16(!) |
|
|
105 | struct basecoder |
|
|
106 | { |
|
|
107 | charmap cmap; |
|
|
108 | unsigned int enc_len [MAX_DEC_LEN]; |
|
|
109 | unsigned int dec_len [MAX_ENC_LEN]; |
|
|
110 | |
|
|
111 | unsigned int encode_len (unsigned int len); |
|
|
112 | unsigned int decode_len (unsigned int len); |
|
|
113 | |
|
|
114 | unsigned int encode (char *dst, u8 *src, unsigned int len); |
|
|
115 | unsigned int decode (u8 *dst, char *src, unsigned int len); |
|
|
116 | |
|
|
117 | basecoder (const char *cmap); |
|
|
118 | }; |
|
|
119 | |
|
|
120 | basecoder::basecoder (const char *cmap) |
|
|
121 | : cmap (cmap) |
|
|
122 | { |
|
|
123 | for (unsigned int len = 0; len < MAX_DEC_LEN; ++len) |
|
|
124 | { |
|
|
125 | u8 src [MAX_DEC_LEN]; |
|
|
126 | u8 dst [MAX_ENC_LEN]; |
|
|
127 | |
|
|
128 | memset (src, 255, len); |
|
|
129 | |
|
|
130 | mp_limb_t m [MAX_LIMBS]; |
|
|
131 | mp_size_t n; |
|
|
132 | |
|
|
133 | n = mpn_set_str (m, src, len, 256); |
|
|
134 | n = mpn_get_str (dst, this->cmap.size, m, n); |
|
|
135 | |
|
|
136 | for (int i = 0; !dst [i]; ++i) |
|
|
137 | n--; |
|
|
138 | |
|
|
139 | enc_len [len] = n; |
|
|
140 | dec_len [n] = len; |
|
|
141 | } |
|
|
142 | } |
|
|
143 | |
|
|
144 | unsigned int basecoder::encode_len (unsigned int len) |
|
|
145 | { |
|
|
146 | return enc_len [len]; |
|
|
147 | } |
|
|
148 | |
|
|
149 | unsigned int basecoder::decode_len (unsigned int len) |
|
|
150 | { |
|
|
151 | while (len && !dec_len [len]) |
|
|
152 | --len; |
|
|
153 | |
|
|
154 | return dec_len [len]; |
|
|
155 | } |
|
|
156 | |
|
|
157 | unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
|
|
158 | { |
|
|
159 | if (!len) |
|
|
160 | return 0; |
|
|
161 | |
|
|
162 | int elen = encode_len (len); |
|
|
163 | |
|
|
164 | mp_limb_t m [MAX_LIMBS]; |
|
|
165 | mp_size_t n; |
|
|
166 | |
|
|
167 | u8 dst_ [MAX_ENC_LEN]; |
|
|
168 | |
|
|
169 | n = mpn_set_str (m, src, len, 256); |
|
|
170 | n = mpn_get_str (dst_, cmap.size, m, n); |
|
|
171 | |
|
|
172 | int plen = elen; // for padding |
|
|
173 | |
|
|
174 | while (n < plen) |
|
|
175 | { |
|
|
176 | *dst++ = cmap.encode [0]; |
|
|
177 | plen--; |
|
|
178 | } |
|
|
179 | |
|
|
180 | for (unsigned int i = n - plen; i < n; ++i) |
|
|
181 | *dst++ = cmap.encode [dst_ [i]]; |
|
|
182 | |
|
|
183 | return elen; |
|
|
184 | } |
|
|
185 | |
|
|
186 | unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
|
|
187 | { |
|
|
188 | if (!len) |
|
|
189 | return 0; |
|
|
190 | |
|
|
191 | u8 src_ [MAX_ENC_LEN]; |
|
|
192 | unsigned int elen = 0; |
|
|
193 | |
|
|
194 | while (len--) |
|
|
195 | { |
|
|
196 | u8 val = cmap.decode [(u8)*src++]; |
|
|
197 | |
|
|
198 | if (val != charmap::INVALID) |
|
|
199 | src_ [elen++] = val; |
|
|
200 | } |
|
|
201 | |
|
|
202 | int dlen = decode_len (elen); |
|
|
203 | |
|
|
204 | mp_limb_t m [MAX_LIMBS]; |
|
|
205 | mp_size_t n; |
|
|
206 | |
|
|
207 | u8 dst_ [MAX_DEC_LEN]; |
|
|
208 | |
|
|
209 | n = mpn_set_str (m, src_, elen, cmap.size); |
|
|
210 | n = mpn_get_str (dst_, 256, m, n); |
|
|
211 | |
|
|
212 | if (n < dlen) |
|
|
213 | { |
|
|
214 | memset (dst, 0, dlen - n); |
|
|
215 | memcpy (dst + dlen - n, dst_, n); |
|
|
216 | } |
|
|
217 | else |
|
|
218 | memcpy (dst, dst_ + n - dlen, dlen); |
|
|
219 | |
|
|
220 | return dlen; |
|
|
221 | } |
|
|
222 | |
|
|
223 | #if 0 |
|
|
224 | struct test { test (); } test; |
|
|
225 | |
|
|
226 | test::test () |
|
|
227 | { |
|
|
228 | basecoder cdc ("0123456789abcdefghijklmnopqrstuvwxyz"); |
|
|
229 | |
|
|
230 | u8 in[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
|
|
231 | static char enc[200]; |
|
|
232 | static u8 dec[200]; |
|
|
233 | |
|
|
234 | for (int i = 1; i < 20; i++) |
|
|
235 | { |
|
|
236 | int elen = cdc.encode (enc, in, i); |
|
|
237 | int dlen = cdc.decode (dec, enc, elen); |
|
|
238 | |
|
|
239 | printf ("%d>%d>%d (%s>%s)\n", i, elen, dlen, enc, dec); |
|
|
240 | } |
|
|
241 | abort (); |
|
|
242 | } |
|
|
243 | #endif |
|
|
244 | |
|
|
245 | // the following sequence has been crafted to |
|
|
246 | // a) look somewhat random |
|
|
247 | // b) the even (and odd) indices never share the same character as upper/lowercase |
81 | // the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) |
248 | // the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) |
82 | struct dns64 |
249 | // the other sequences are obviously derived |
83 | { |
250 | //static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
84 | static const char encode_chars[64 + 1]; |
251 | static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
85 | static s8 decode_chars[256]; |
252 | //static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet |
|
|
253 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
86 | |
254 | |
87 | static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } |
255 | ///////////////////////////////////////////////////////////////////////////// |
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 | |
256 | |
92 | dns64 (); |
257 | #define HDRSIZE 6 |
93 | } dns64; |
|
|
94 | |
258 | |
95 | const char dns64::encode_chars[64 + 1] = "_4B9dLphHzrqQmGjkTbJt5svlZX8xSaReEYfwKgF1DP2W6NyVOU70IouACcMn3i-"; |
259 | inline void encode_header (char *data, int clientid, int seqno) |
96 | s8 dns64::decode_chars[256]; |
|
|
97 | |
|
|
98 | dns64::dns64 () |
|
|
99 | { |
260 | { |
100 | for (int i = 0; i < 64; i++) |
261 | u8 hdr[3] = { clientid, seqno >> 8, seqno }; |
101 | decode_chars [encode_chars [i]] = i + 1; |
|
|
102 | } |
|
|
103 | |
262 | |
104 | int dns64::encode (char *dst, u8 *src, int len) |
263 | assert (clientid < 256); |
105 | { |
|
|
106 | // slow, but easy to debug |
|
|
107 | char *beg = dst; |
|
|
108 | unsigned int accum, bits = 0; |
|
|
109 | |
264 | |
110 | while (len--) |
265 | cdc26.encode (data, hdr, 3); |
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 | } |
266 | } |
128 | |
267 | |
129 | int dns64::decode (u8 *dst, char *src, int len) |
268 | inline void decode_header (char *data, int &clientid, int &seqno) |
130 | { |
269 | { |
131 | // slow, but easy to debug |
270 | u8 hdr[3]; |
132 | u8 *beg = dst; |
|
|
133 | unsigned int accum, bits = 0; |
|
|
134 | |
271 | |
135 | while (len--) |
272 | cdc26.decode (hdr, data, HDRSIZE); |
136 | { |
|
|
137 | s8 chr = decode_chars [(u8)*src++]; |
|
|
138 | |
273 | |
139 | if (!chr) |
274 | printf ("DEC %02x %02x %02x %02x\n", hdr[0], hdr[1], hdr[2], hdr[3]); |
140 | continue; |
|
|
141 | |
275 | |
142 | accum <<= 6; |
276 | clientid = hdr[0]; |
143 | accum |= chr - 1; |
277 | seqno = (hdr[1] << 8) | hdr[2]; |
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 | } |
278 | } |
155 | |
279 | |
156 | ///////////////////////////////////////////////////////////////////////////// |
280 | ///////////////////////////////////////////////////////////////////////////// |
157 | |
281 | |
158 | struct byte_stream |
282 | struct byte_stream |
… | |
… | |
218 | } |
342 | } |
219 | |
343 | |
220 | vpn_packet *byte_stream::get () |
344 | vpn_packet *byte_stream::get () |
221 | { |
345 | { |
222 | int len = (data [0] << 8) | data [1]; |
346 | int len = (data [0] << 8) | data [1]; |
223 | |
|
|
224 | printf ("get len %d, fill %d\n", len, fill);//D |
|
|
225 | |
347 | |
226 | if (len > MAXSIZE && fill >= 2) |
348 | if (len > MAXSIZE && fill >= 2) |
227 | abort (); // TODO handle this gracefully, connection reset |
349 | abort (); // TODO handle this gracefully, connection reset |
228 | |
350 | |
229 | if (fill < len + 2) |
351 | if (fill < len + 2) |
… | |
… | |
349 | |
471 | |
350 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
472 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
351 | pkt->qdcount = htons (1); |
473 | pkt->qdcount = htons (1); |
352 | |
474 | |
353 | int offs = 6*2; |
475 | int offs = 6*2; |
354 | int dlen = MAX_DOMAIN_SIZE - strlen (THISNODE->domain) - 2; |
476 | int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); |
355 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
477 | // 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 |
478 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
357 | |
479 | |
358 | u8 data[256]; //TODO |
480 | char enc[256], *encp = enc; |
|
|
481 | encode_header (enc, THISNODE->id, seqno); |
359 | |
482 | |
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; |
483 | int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); |
365 | |
484 | |
366 | if (datalen > stream->size ()) |
485 | if (datalen > stream->size ()) |
367 | datalen = stream->size (); |
486 | datalen = stream->size (); |
368 | |
487 | |
369 | char enc[256], *encp = enc; |
488 | int enclen = cdc62.encode (enc + HDRSIZE, stream->begin (), datalen) + HDRSIZE; |
|
|
489 | |
|
|
490 | printf ("cdc62.encode %d->%d:%02x %02x %02x %02x\n", datalen, enclen, |
|
|
491 | stream->begin ()[0], |
|
|
492 | stream->begin ()[1], |
|
|
493 | stream->begin ()[2], |
|
|
494 | stream->begin ()[3]); |
370 | |
495 | |
371 | memcpy (data + 3, stream->begin (), datalen); |
|
|
372 | int enclen = dns64::encode (enc, data, datalen + 3); |
|
|
373 | stream->remove (datalen); |
496 | stream->remove (datalen); |
374 | |
497 | |
375 | while (enclen) |
498 | while (enclen) |
376 | { |
499 | { |
377 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
500 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
… | |
… | |
419 | int seqno; |
542 | int seqno; |
420 | dns_packet *pkt; // reply packet |
543 | dns_packet *pkt; // reply packet |
421 | u8 data [MAXSIZE]; // actually part of the reply packet... |
544 | u8 data [MAXSIZE]; // actually part of the reply packet... |
422 | int datalen; |
545 | int datalen; |
423 | |
546 | |
424 | dns_rcv (int seqno, dns_packet *req, u8 *data, int datalen); |
547 | dns_rcv (int seqno, u8 *data, int datalen); |
425 | ~dns_rcv (); |
548 | ~dns_rcv (); |
426 | }; |
549 | }; |
427 | |
550 | |
428 | dns_rcv::dns_rcv (int seqno, dns_packet *req, u8 *data, int datalen) |
551 | dns_rcv::dns_rcv (int seqno, u8 *data, int datalen) |
429 | : seqno (seqno), pkt (new dns_packet), datalen (datalen) |
552 | : seqno (seqno), pkt (new dns_packet), datalen (datalen) |
430 | { |
553 | { |
431 | memcpy (this->data, data, datalen); |
554 | memcpy (this->data, data, datalen); |
432 | pkt->len = req->len; |
|
|
433 | memcpy (pkt->at (0), req->at (0), req->len); |
|
|
434 | } |
555 | } |
435 | |
556 | |
436 | dns_rcv::~dns_rcv () |
557 | dns_rcv::~dns_rcv () |
437 | { |
558 | { |
438 | delete pkt; |
559 | delete pkt; |
… | |
… | |
451 | |
572 | |
452 | void connection::dnsv4_receive_rep (struct dns_rcv *r) |
573 | void connection::dnsv4_receive_rep (struct dns_rcv *r) |
453 | { |
574 | { |
454 | dns_rcvpq.push_back (r); |
575 | dns_rcvpq.push_back (r); |
455 | |
576 | |
|
|
577 | printf ("%d got inketc %d (%02x %02x %02x %02x)\n", THISNODE->id, r->seqno |
|
|
578 | ,r->data[0] |
|
|
579 | ,r->data[1] |
|
|
580 | ,r->data[2] |
|
|
581 | ,r->data[3] |
|
|
582 | ); |
456 | redo: |
583 | redo: |
457 | |
584 | |
|
|
585 | // find next packet |
458 | for (vector<dns_rcv *>::iterator i = dns_rcvpq.begin (); |
586 | for (vector<dns_rcv *>::iterator i = dns_rcvpq.end (); i-- != dns_rcvpq.begin (); ) |
459 | i != dns_rcvpq.end (); |
|
|
460 | ++i) |
|
|
461 | if (dns_rcvseq == (*i)->seqno) |
587 | if (SEQNO_EQ (dns_rcvseq, (*i)->seqno)) |
462 | { |
588 | { |
|
|
589 | // enter the packet into our input stream |
463 | dns_rcv *r = *i; |
590 | r = *i; |
|
|
591 | |
|
|
592 | printf ("%d checking for older packet %d\n", THISNODE->id, dns_rcvseq); |
|
|
593 | // remove the oldest packet, look forward, as it's oldest first |
|
|
594 | for (vector<dns_rcv *>::iterator j = dns_rcvpq.begin (); j != dns_rcvpq.end (); ++j) |
|
|
595 | if (SEQNO_EQ ((*j)->seqno, dns_rcvseq - MAX_WINDOW)) |
|
|
596 | { |
|
|
597 | printf ("%d removing %d\n", THISNODE->id, (*j)->seqno); |
|
|
598 | delete *j; |
|
|
599 | dns_rcvpq.erase (j); |
|
|
600 | break; |
|
|
601 | } |
464 | |
602 | |
465 | dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; |
603 | dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; |
466 | |
604 | |
467 | if (!dns_snddq && !dns_rcvdq) |
605 | if (!dns_snddq && !dns_rcvdq) |
468 | { |
606 | { |
… | |
… | |
480 | sockinfo si; |
618 | sockinfo si; |
481 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
619 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
482 | |
620 | |
483 | vpn->recv_vpn_packet (pkt, si); |
621 | vpn->recv_vpn_packet (pkt, si); |
484 | } |
622 | } |
485 | } |
623 | |
486 | else if ((u32)dns_rcvseq - MAX_WINDOW - (u32)(*i)->seqno < MAX_WINDOW * 2) |
624 | // check for further packets |
487 | { |
|
|
488 | dns_rcvpq.erase (i); |
|
|
489 | goto redo; |
625 | goto redo; |
490 | } |
626 | } |
491 | } |
627 | } |
492 | |
628 | |
493 | dns_packet * |
629 | dns_packet * |
… | |
… | |
518 | |
654 | |
519 | int dlen = strlen (THISNODE->domain); |
655 | int dlen = strlen (THISNODE->domain); |
520 | |
656 | |
521 | if (qclass == RR_CLASS_IN |
657 | if (qclass == RR_CLASS_IN |
522 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
658 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
523 | && qlen > dlen + 1 |
659 | && qlen > dlen + 1 + HDRSIZE |
524 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
660 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
525 | { |
661 | { |
526 | // correct class, domain: parse |
662 | // correct class, domain: parse |
|
|
663 | int client, seqno; |
|
|
664 | decode_header (qname, client, seqno); |
|
|
665 | |
527 | u8 data[MAXSIZE]; |
666 | u8 data[MAXSIZE]; |
528 | int datalen = dns64::decode (data, qname, qlen - dlen - 1); |
667 | int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
529 | |
668 | |
530 | int client = data[0]; |
669 | printf ("cdc62.decode %d(%d): %02x %02x %02x %02x\n", |
531 | int seqno = ((data[1] << 8) | data[2]) & SEQNO_MASK; |
670 | qlen - (dlen + 1 + HDRSIZE), datalen |
|
|
671 | ,data[0] |
|
|
672 | ,data[1] |
|
|
673 | ,data[2] |
|
|
674 | ,data[3]); |
|
|
675 | |
|
|
676 | printf ("SRV got %d <%.*s>\n", seqno, qlen, qname + HDRSIZE);//D |
|
|
677 | printf ("SRV got %d <%.*s>\n", seqno, qlen - (dlen + 1 + HDRSIZE), qname + HDRSIZE);//D |
532 | |
678 | |
533 | if (0 < client && client <= conns.size ()) |
679 | if (0 < client && client <= conns.size ()) |
534 | { |
680 | { |
535 | connection *c = conns [client - 1]; |
681 | connection *c = conns [client - 1]; |
536 | |
682 | |
537 | for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.begin (); |
683 | for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.end (); i-- != c->dns_rcvpq.begin (); ) |
538 | i != c->dns_rcvpq.end (); |
|
|
539 | ++i) |
|
|
540 | if ((*i)->seqno == seqno) |
684 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
541 | { |
685 | { |
542 | // already seen that request, just reply with the original reply |
686 | // already seen that request: simply reply with the cached reply |
543 | dns_rcv *r = *i; |
687 | dns_rcv *r = *i; |
544 | |
688 | |
545 | offs = r->pkt->len; |
689 | printf ("DUPLICATE %d\n", htons (r->pkt->id));//D |
|
|
690 | |
546 | memcpy (pkt->at (0), r->pkt->at (0), offs); |
691 | memcpy (pkt->at (0), r->pkt->at (0), offs = r->pkt->len); |
|
|
692 | pkt->id = r->pkt->id; |
547 | goto duplicate_request; |
693 | goto duplicate_request; |
548 | } |
694 | } |
549 | |
695 | |
550 | // new packet, queue |
696 | // new packet, queue |
551 | c->dnsv4_receive_rep (new dns_rcv (seqno, pkt, data + 3, datalen - 3)); |
697 | dns_rcv *rcv = new dns_rcv (seqno, data, datalen); |
|
|
698 | c->dnsv4_receive_rep (rcv); |
552 | |
699 | |
553 | // now generate reply |
700 | // now generate reply |
554 | pkt->ancount = htons (1); // one answer RR |
701 | pkt->ancount = htons (1); // one answer RR |
555 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
702 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
556 | |
703 | |
… | |
… | |
594 | int rdlen = offs - rdlen_offs; |
741 | int rdlen = offs - rdlen_offs; |
595 | |
742 | |
596 | (*pkt) [rdlen_offs - 2] = rdlen >> 8; |
743 | (*pkt) [rdlen_offs - 2] = rdlen >> 8; |
597 | (*pkt) [rdlen_offs - 1] = rdlen; |
744 | (*pkt) [rdlen_offs - 1] = rdlen; |
598 | |
745 | |
|
|
746 | // now update dns_rcv copy |
|
|
747 | rcv->pkt->len = offs; |
|
|
748 | memcpy (rcv->pkt->at (0), pkt->at (0), offs); |
|
|
749 | |
599 | duplicate_request: ; |
750 | duplicate_request: ; |
600 | } |
751 | } |
601 | else |
752 | else |
602 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
753 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
603 | } |
754 | } |
604 | } |
|
|
605 | else |
|
|
606 | offs = pkt->len; |
|
|
607 | |
755 | |
608 | pkt->len = offs; |
756 | pkt->len = offs; |
|
|
757 | } |
|
|
758 | |
609 | return pkt; |
759 | return pkt; |
610 | } |
760 | } |
611 | |
761 | |
612 | void |
762 | void |
613 | vpn::dnsv4_client (dns_packet *pkt) |
763 | vpn::dnsv4_client (dns_packet *pkt) |
… | |
… | |
641 | offs += 4; // skip qtype, qclass |
791 | offs += 4; // skip qtype, qclass |
642 | } |
792 | } |
643 | |
793 | |
644 | while (pkt->ancount-- && offs < MAXSIZE - 10) |
794 | while (pkt->ancount-- && offs < MAXSIZE - 10) |
645 | { |
795 | { |
|
|
796 | int qlen = //D |
646 | pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
797 | pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
|
|
798 | |
|
|
799 | printf ("got reply to <%.*s>\n", qlen, qname);//D |
647 | |
800 | |
648 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
801 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
649 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
802 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
650 | u32 ttl = (*pkt) [offs++] << 24; |
803 | u32 ttl = (*pkt) [offs++] << 24; |
651 | ttl |= (*pkt) [offs++] << 16; |
804 | ttl |= (*pkt) [offs++] << 16; |
… | |
… | |
673 | } |
826 | } |
674 | |
827 | |
675 | } |
828 | } |
676 | } |
829 | } |
677 | |
830 | |
678 | if (datap != data) |
831 | // todo: pkt now used |
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)); |
832 | c->dnsv4_receive_rep (new dns_rcv (seqno, data, datap - data)); |
687 | |
833 | |
688 | break; |
834 | break; |
689 | } |
835 | } |
690 | |
836 | |
691 | delete pkt; |
837 | delete pkt; |
… | |
… | |
763 | if (!send) |
909 | if (!send) |
764 | { |
910 | { |
765 | send = r; |
911 | send = r; |
766 | |
912 | |
767 | if (r->retry)//D |
913 | if (r->retry)//D |
768 | printf ("req %d, retry %d\n", r->pkt->id, r->retry); |
914 | printf ("req %d:%d, retry %d\n", r->seqno, r->pkt->id, r->retry); |
769 | r->retry++; |
915 | r->retry++; |
770 | r->next = NOW + r->retry; |
916 | r->next = NOW + r->retry; |
771 | } |
917 | } |
772 | } |
918 | } |
773 | |
919 | |