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-2008 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 it |
6 | it under the terms of the GNU General Public License as published by |
8 | under the terms of the GNU General Public License as published by the |
7 | the Free Software Foundation; either version 2 of the License, or |
9 | Free Software Foundation; either version 3 of the License, or (at your |
8 | (at your option) any later version. |
10 | 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, but |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | 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 GNU General |
13 | GNU General Public License for more details. |
15 | 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 along |
16 | along with this program; if not, write to the Free Software |
18 | with this program; if not, see <http://www.gnu.org/licenses/>. |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | |
|
|
20 | Additional permission under GNU GPL version 3 section 7 |
|
|
21 | |
|
|
22 | If you modify this Program, or any covered work, by linking or |
|
|
23 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
24 | version of that library), containing parts covered by the terms of the |
|
|
25 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
26 | additional permission to convey the resulting work. Corresponding |
|
|
27 | Source for a non-source form of such a combination shall include the |
|
|
28 | source code for the parts of OpenSSL used as well as that of the |
|
|
29 | covered work. |
18 | */ |
30 | */ |
|
|
31 | |
|
|
32 | // TODO: EDNS0 option to increase dns mtu? |
|
|
33 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
|
|
34 | // to ensure no buffer overflows or similar problems. |
19 | |
35 | |
20 | #include "config.h" |
36 | #include "config.h" |
21 | |
37 | |
22 | #if ENABLE_DNS |
38 | #if ENABLE_DNS |
23 | |
39 | |
24 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
40 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
25 | // it's a hack, use only in emergency situations please. |
41 | // it's a hack, use only in emergency situations please. |
26 | |
42 | |
27 | #include <cstring> |
43 | #include <cstring> |
|
|
44 | #include <cassert> |
28 | |
45 | |
29 | #include <sys/types.h> |
46 | #include <sys/types.h> |
30 | #include <sys/socket.h> |
47 | #include <sys/socket.h> |
31 | #include <sys/wait.h> |
48 | #include <sys/wait.h> |
32 | #include <sys/uio.h> |
49 | #include <sys/uio.h> |
… | |
… | |
35 | #include <unistd.h> |
52 | #include <unistd.h> |
36 | #include <fcntl.h> |
53 | #include <fcntl.h> |
37 | |
54 | |
38 | #include <map> |
55 | #include <map> |
39 | |
56 | |
|
|
57 | #include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */ |
|
|
58 | #include <gmp.h> |
|
|
59 | |
40 | #include "netcompat.h" |
60 | #include "netcompat.h" |
41 | |
61 | |
42 | #include "vpn.h" |
62 | #include "vpn.h" |
43 | |
63 | |
44 | #define MIN_RETRY 1. |
64 | #define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data |
45 | #define MAX_RETRY 60. |
65 | #define ACTIVITY_INTERVAL 5. |
46 | |
66 | |
47 | #define MAX_OUTSTANDING 10 // max. outstanding requests |
67 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
|
|
68 | #define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn |
|
|
69 | |
|
|
70 | #define MAX_SEND_INTERVAL 2. // optimistic? |
|
|
71 | |
48 | #define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
72 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
49 | #define MAX_RATE 1 // requests/s |
|
|
50 | #define MAX_BACKLOG (10*1024) // size of protocol backlog |
73 | #define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
51 | |
74 | |
52 | /* |
75 | #define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well |
|
|
76 | // 240 leaves about 4 bytes of server reply data |
|
|
77 | // every request byte less give room for two reply bytes |
53 | |
78 | |
54 | protocol, in shorthand :) |
79 | #define SEQNO_MASK 0x3fff |
55 | |
80 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
56 | client -> server <req> ANY? |
|
|
57 | server -> client <req> TXT <rep> |
|
|
58 | |
|
|
59 | <req> is dns64-encoded <client-id:12><recv-seqno:10>[<send-seqno:10><data>] |
|
|
60 | <rep> is dns64-encoded <0:12><recv-seqno:10>[<send-seqno:10><data>] |
|
|
61 | |
|
|
62 | if <client-id> is zero, the connection will be configured: |
|
|
63 | |
|
|
64 | <0:12><0:4>client-id:12><default-ttl:8><max-size:16><flags:16> |
|
|
65 | |
|
|
66 | */ |
|
|
67 | |
81 | |
68 | #define MAX_LBL_SIZE 63 |
82 | #define MAX_LBL_SIZE 63 |
69 | #define MAX_PKT_SIZE 512 |
83 | #define MAX_PKT_SIZE 512 |
70 | |
84 | |
|
|
85 | #define RR_TYPE_A 1 |
|
|
86 | #define RR_TYPE_NULL 10 |
71 | #define RR_TYPE_TXT 16 |
87 | #define RR_TYPE_TXT 16 |
|
|
88 | #define RR_TYPE_AAAA 28 |
72 | #define RR_TYPE_ANY 255 |
89 | #define RR_TYPE_ANY 255 |
|
|
90 | |
73 | #define RR_CLASS_IN 1 |
91 | #define RR_CLASS_IN 1 |
74 | |
92 | |
75 | // the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) |
93 | #define CMD_IP_1 207 |
76 | struct dns64 |
94 | #define CMD_IP_2 46 |
77 | { |
95 | #define CMD_IP_3 236 |
78 | static const char encode_chars[64 + 1]; |
96 | #define CMD_IP_RST 29 |
79 | static s8 decode_chars[256]; |
97 | #define CMD_IP_SYN 113 |
|
|
98 | #define CMD_IP_REJ 32 |
80 | |
99 | |
81 | static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } |
100 | // works for cmaps up to 255 (not 256!) |
82 | static int decode_len (int bytes) { return (bytes * 6) / 8; } |
101 | struct charmap |
|
|
102 | { |
|
|
103 | enum { INVALID = (u8)255 }; |
|
|
104 | |
|
|
105 | char encode [256]; // index => char |
|
|
106 | u8 decode [256]; // char => index |
|
|
107 | unsigned int size; |
|
|
108 | |
|
|
109 | charmap (const char *cmap); |
|
|
110 | }; |
|
|
111 | |
|
|
112 | charmap::charmap (const char *cmap) |
|
|
113 | { |
|
|
114 | char *enc = encode; |
|
|
115 | u8 *dec = decode; |
|
|
116 | |
|
|
117 | memset (enc, (char) 0, 256); |
|
|
118 | memset (dec, (char)INVALID, 256); |
|
|
119 | |
|
|
120 | for (size = 0; cmap [size]; size++) |
|
|
121 | { |
|
|
122 | enc [size] = cmap [size]; |
|
|
123 | dec [(u8)enc [size]] = size; |
|
|
124 | } |
|
|
125 | |
|
|
126 | assert (size < 256); |
|
|
127 | } |
|
|
128 | |
|
|
129 | #define MAX_DEC_LEN 500 |
|
|
130 | #define MAX_ENC_LEN (MAX_DEC_LEN * 2) |
|
|
131 | #define MAX_LIMBS ((MAX_DEC_LEN * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS) |
|
|
132 | |
|
|
133 | // ugly. minimum base is 16(!) |
|
|
134 | struct basecoder |
|
|
135 | { |
|
|
136 | charmap cmap; |
|
|
137 | unsigned int enc_len [MAX_DEC_LEN]; |
|
|
138 | unsigned int dec_len [MAX_ENC_LEN]; |
|
|
139 | |
|
|
140 | unsigned int encode_len (unsigned int len); |
|
|
141 | unsigned int decode_len (unsigned int len); |
|
|
142 | |
83 | static int encode (char *dst, u8 *src, int len); |
143 | unsigned int encode (char *dst, u8 *src, unsigned int len); |
84 | static int decode (u8 *dst, char *src, int len); |
144 | unsigned int decode (u8 *dst, char *src, unsigned int len); |
85 | |
145 | |
86 | dns64 (); |
146 | basecoder (const char *cmap); |
87 | } dns64; |
147 | }; |
88 | |
148 | |
89 | const char dns64::encode_chars[64 + 1] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"; |
149 | basecoder::basecoder (const char *cmap) |
90 | s8 dns64::decode_chars[256]; |
150 | : cmap (cmap) |
91 | |
|
|
92 | dns64::dns64 () |
|
|
93 | { |
151 | { |
94 | for (int i = 0; i < 64; i++) |
152 | for (unsigned int len = 0; len < MAX_DEC_LEN; ++len) |
95 | decode_chars [encode_chars [i]] = i + 1; |
153 | { |
96 | } |
154 | u8 src [MAX_DEC_LEN]; |
|
|
155 | u8 dst [MAX_ENC_LEN]; |
97 | |
156 | |
|
|
157 | memset (src, 255, len); |
|
|
158 | |
|
|
159 | mp_limb_t m [MAX_LIMBS]; |
|
|
160 | mp_size_t n; |
|
|
161 | |
|
|
162 | n = mpn_set_str (m, src, len, 256); |
|
|
163 | n = mpn_get_str (dst, this->cmap.size, m, n); |
|
|
164 | |
|
|
165 | for (int i = 0; !dst [i]; ++i) |
|
|
166 | n--; |
|
|
167 | |
|
|
168 | enc_len [len] = n; |
|
|
169 | dec_len [n] = len; |
|
|
170 | } |
|
|
171 | } |
|
|
172 | |
|
|
173 | unsigned int |
|
|
174 | basecoder::encode_len (unsigned int len) |
|
|
175 | { |
|
|
176 | return enc_len [len]; |
|
|
177 | } |
|
|
178 | |
|
|
179 | unsigned int |
|
|
180 | basecoder::decode_len (unsigned int len) |
|
|
181 | { |
|
|
182 | while (len && !dec_len [len]) |
|
|
183 | --len; |
|
|
184 | |
|
|
185 | return dec_len [len]; |
|
|
186 | } |
|
|
187 | |
|
|
188 | unsigned int |
98 | int dns64::encode (char *dst, u8 *src, int len) |
189 | basecoder::encode (char *dst, u8 *src, unsigned int len) |
99 | { |
190 | { |
100 | // slow, but easy to debug |
191 | if (!len || len > MAX_DEC_LEN) |
101 | char *beg = dst; |
192 | return 0; |
102 | unsigned int accum, bits = 0; |
193 | |
|
|
194 | int elen = encode_len (len); |
|
|
195 | |
|
|
196 | mp_limb_t m [MAX_LIMBS]; |
|
|
197 | mp_size_t n; |
|
|
198 | |
|
|
199 | u8 dst_ [MAX_ENC_LEN]; |
|
|
200 | |
|
|
201 | n = mpn_set_str (m, src, len, 256); |
|
|
202 | n = mpn_get_str (dst_, cmap.size, m, n); |
|
|
203 | |
|
|
204 | int plen = elen; // for padding |
|
|
205 | |
|
|
206 | while (n < plen) |
|
|
207 | { |
|
|
208 | *dst++ = cmap.encode [0]; |
|
|
209 | plen--; |
|
|
210 | } |
|
|
211 | |
|
|
212 | for (unsigned int i = n - plen; i < n; ++i) |
|
|
213 | *dst++ = cmap.encode [dst_ [i]]; |
|
|
214 | |
|
|
215 | return elen; |
|
|
216 | } |
|
|
217 | |
|
|
218 | unsigned int |
|
|
219 | basecoder::decode (u8 *dst, char *src, unsigned int len) |
|
|
220 | { |
|
|
221 | if (!len || len > MAX_ENC_LEN) |
|
|
222 | return 0; |
|
|
223 | |
|
|
224 | u8 src_ [MAX_ENC_LEN]; |
|
|
225 | unsigned int elen = 0; |
103 | |
226 | |
104 | while (len--) |
227 | while (len--) |
105 | { |
228 | { |
106 | accum <<= 8; |
229 | u8 val = cmap.decode [(u8)*src++]; |
107 | accum |= *src++; |
|
|
108 | bits += 8; |
|
|
109 | |
230 | |
110 | while (bits >= 6) |
231 | if (val != charmap::INVALID) |
111 | { |
232 | src_ [elen++] = val; |
112 | *dst++ = encode_chars [(accum >> (bits - 6)) & 63]; |
233 | } |
113 | bits -= 6; |
234 | |
114 | } |
235 | int dlen = decode_len (elen); |
|
|
236 | |
|
|
237 | mp_limb_t m [MAX_LIMBS]; |
|
|
238 | mp_size_t n; |
|
|
239 | |
|
|
240 | u8 dst_ [MAX_DEC_LEN]; |
|
|
241 | |
|
|
242 | n = mpn_set_str (m, src_, elen, cmap.size); |
|
|
243 | n = mpn_get_str (dst_, 256, m, n); |
|
|
244 | |
|
|
245 | if (n < dlen) |
115 | } |
246 | { |
|
|
247 | memset (dst, 0, dlen - n); |
|
|
248 | memcpy (dst + dlen - n, dst_, n); |
|
|
249 | } |
|
|
250 | else |
|
|
251 | memcpy (dst, dst_ + n - dlen, dlen); |
116 | |
252 | |
117 | if (bits) |
253 | return dlen; |
118 | *dst++ = encode_chars [(accum << (6 - bits)) & 63]; |
|
|
119 | |
|
|
120 | return dst - beg; |
|
|
121 | } |
254 | } |
122 | |
255 | |
123 | int dns64::decode (u8 *dst, char *src, int len) |
256 | #if 0 |
124 | { |
257 | struct test { test (); } test; |
125 | // slow, but easy to debug |
|
|
126 | u8 *beg = dst; |
|
|
127 | unsigned int accum, bits = 0; |
|
|
128 | |
258 | |
129 | while (len--) |
259 | test::test () |
|
|
260 | { |
|
|
261 | basecoder cdc ("0123456789abcdefghijklmnopqrstuvwxyz"); |
|
|
262 | |
|
|
263 | u8 in[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
|
|
264 | static char enc[200]; |
|
|
265 | static u8 dec[200]; |
|
|
266 | |
|
|
267 | for (int i = 1; i < 20; i++) |
130 | { |
268 | { |
131 | s8 chr = decode_chars [(u8)*src++]; |
269 | int elen = cdc.encode (enc, in, i); |
|
|
270 | int dlen = cdc.decode (dec, enc, elen); |
132 | |
271 | |
133 | if (!chr) |
272 | printf ("%d>%d>%d (%s>%s)\n", i, elen, dlen, enc, dec); |
134 | break; |
|
|
135 | |
|
|
136 | accum <<= 6; |
|
|
137 | accum |= chr - 1; |
|
|
138 | bits += 6; |
|
|
139 | |
|
|
140 | while (bits >= 8) |
|
|
141 | { |
|
|
142 | *dst++ = accum >> (bits - 8); |
|
|
143 | bits -= 8; |
|
|
144 | } |
|
|
145 | } |
273 | } |
|
|
274 | abort (); |
|
|
275 | } |
|
|
276 | #endif |
146 | |
277 | |
147 | return dst - beg; |
278 | //static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-"); |
|
|
279 | //static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
|
|
280 | static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
|
|
281 | //static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet |
|
|
282 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
|
|
283 | |
|
|
284 | ///////////////////////////////////////////////////////////////////////////// |
|
|
285 | |
|
|
286 | #define HDRSIZE 6 |
|
|
287 | |
|
|
288 | inline void |
|
|
289 | encode_header (char *data, int clientid, int seqno, int retry = 0) |
|
|
290 | { |
|
|
291 | seqno &= SEQNO_MASK; |
|
|
292 | |
|
|
293 | u8 hdr[3] = { |
|
|
294 | clientid, |
|
|
295 | (seqno >> 8) | (retry << 6), |
|
|
296 | seqno, |
|
|
297 | }; |
|
|
298 | |
|
|
299 | assert (clientid < 256); |
|
|
300 | |
|
|
301 | cdc26.encode (data, hdr, 3); |
|
|
302 | } |
|
|
303 | |
|
|
304 | inline void |
|
|
305 | decode_header (char *data, int &clientid, int &seqno) |
|
|
306 | { |
|
|
307 | u8 hdr[3]; |
|
|
308 | |
|
|
309 | cdc26.decode (hdr, data, HDRSIZE); |
|
|
310 | |
|
|
311 | clientid = hdr[0]; |
|
|
312 | seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK; |
148 | } |
313 | } |
149 | |
314 | |
150 | ///////////////////////////////////////////////////////////////////////////// |
315 | ///////////////////////////////////////////////////////////////////////////// |
151 | |
316 | |
152 | struct byte_stream |
317 | struct byte_stream |
… | |
… | |
159 | ~byte_stream (); |
324 | ~byte_stream (); |
160 | |
325 | |
161 | bool empty () { return !fill; } |
326 | bool empty () { return !fill; } |
162 | int size () { return fill; } |
327 | int size () { return fill; } |
163 | |
328 | |
|
|
329 | bool put (u8 *data, unsigned int datalen); |
164 | bool put (vpn_packet *pkt); |
330 | bool put (vpn_packet *pkt); |
165 | vpn_packet *get (); |
331 | vpn_packet *get (); |
166 | |
332 | |
167 | u8 *begin () { return data; } |
333 | u8 *begin () { return data; } |
168 | void remove (int count); |
334 | void remove (int count); |
… | |
… | |
177 | byte_stream::~byte_stream () |
343 | byte_stream::~byte_stream () |
178 | { |
344 | { |
179 | delete data; |
345 | delete data; |
180 | } |
346 | } |
181 | |
347 | |
|
|
348 | void |
182 | void byte_stream::remove (int count) |
349 | byte_stream::remove (int count) |
183 | { |
350 | { |
184 | if (count > fill) |
351 | if (count > fill) |
185 | abort (); |
352 | assert (count <= fill); |
186 | |
353 | |
187 | memmove (data, data + count, fill -= count); |
354 | memmove (data, data + count, fill -= count); |
188 | } |
355 | } |
189 | |
356 | |
|
|
357 | bool |
|
|
358 | byte_stream::put (u8 *data, unsigned int datalen) |
|
|
359 | { |
|
|
360 | if (maxsize - fill < datalen) |
|
|
361 | return false; |
|
|
362 | |
|
|
363 | memcpy (this->data + fill, data, datalen); fill += datalen; |
|
|
364 | |
|
|
365 | return true; |
|
|
366 | } |
|
|
367 | |
|
|
368 | bool |
190 | bool byte_stream::put (vpn_packet *pkt) |
369 | byte_stream::put (vpn_packet *pkt) |
191 | { |
370 | { |
192 | if (maxsize - fill < pkt->len + 2) |
371 | if (maxsize - fill < pkt->len + 2) |
193 | return false; |
372 | return false; |
194 | |
373 | |
195 | data [fill++] = pkt->len >> 8; |
374 | data [fill++] = pkt->len >> 8; |
196 | data [fill++] = pkt->len; |
375 | data [fill++] = pkt->len; |
197 | |
376 | |
198 | memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len; |
377 | memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len; |
199 | |
378 | |
200 | return true; |
379 | return true; |
201 | } |
380 | } |
202 | |
381 | |
203 | vpn_packet *byte_stream::get () |
382 | vpn_packet *byte_stream::get () |
204 | { |
383 | { |
|
|
384 | unsigned int len; |
|
|
385 | |
|
|
386 | for (;;) |
|
|
387 | { |
205 | int len = (data [0] << 8) | data [1]; |
388 | len = (data [0] << 8) | data [1]; |
206 | |
389 | |
|
|
390 | if (len <= MAXSIZE || fill < 2) |
|
|
391 | break; |
|
|
392 | |
|
|
393 | // TODO: handle this better than skipping, e.g. by reset |
|
|
394 | slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte...")); |
|
|
395 | remove (1); |
|
|
396 | } |
|
|
397 | |
207 | if (fill < len + 2) |
398 | if (fill < len + 2) |
208 | return 0; |
399 | return 0; |
209 | |
400 | |
210 | vpn_packet *pkt = new vpn_packet; |
401 | vpn_packet *pkt = new vpn_packet; |
|
|
402 | |
|
|
403 | pkt->len = len; |
211 | memcpy (&((*pkt)[0]), data + 2, len); |
404 | memcpy (pkt->at (0), data + 2, len); |
212 | remove (len + 2); |
405 | remove (len + 2); |
213 | |
406 | |
214 | return pkt; |
407 | return pkt; |
215 | } |
408 | } |
216 | |
409 | |
217 | ///////////////////////////////////////////////////////////////////////////// |
410 | ///////////////////////////////////////////////////////////////////////////// |
218 | |
411 | |
219 | #define FLAG_QUERY ( 0 << 15) |
412 | #define FLAG_QUERY ( 0 << 15) |
220 | #define FLAG_RESPONSE ( 1 << 15) |
413 | #define FLAG_RESPONSE ( 1 << 15) |
221 | #define FLAG_OP_MASK (15 << 14) |
414 | #define FLAG_OP_MASK (15 << 11) |
222 | #define FLAG_OP_QUERY ( 0 << 11) |
415 | #define FLAG_OP_QUERY ( 0 << 11) |
223 | #define FLAG_AA ( 1 << 10) |
416 | #define FLAG_AA ( 1 << 10) |
224 | #define FLAG_TC ( 1 << 9) |
417 | #define FLAG_TC ( 1 << 9) |
225 | #define FLAG_RD ( 1 << 8) |
418 | #define FLAG_RD ( 1 << 8) |
226 | #define FLAG_RA ( 1 << 7) |
419 | #define FLAG_RA ( 1 << 7) |
|
|
420 | #define FLAG_AUTH ( 1 << 5) |
227 | #define FLAG_RCODE_MASK (15 << 0) |
421 | #define FLAG_RCODE_MASK (15 << 0) |
228 | #define FLAG_RCODE_OK ( 0 << 0) |
422 | #define FLAG_RCODE_OK ( 0 << 0) |
229 | #define FLAG_RCODE_FORMERR ( 1 << 0) |
423 | #define FLAG_RCODE_FORMERR ( 1 << 0) |
230 | #define FLAG_RCODE_SERVFAIL ( 2 << 0) |
424 | #define FLAG_RCODE_SERVFAIL ( 2 << 0) |
231 | #define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
425 | #define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
232 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
426 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
233 | |
427 | |
234 | #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
428 | #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) |
429 | #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
|
|
430 | |
|
|
431 | struct dns_cfg |
|
|
432 | { |
|
|
433 | static int next_uid; |
|
|
434 | |
|
|
435 | u8 id1, id2, id3, id4; |
|
|
436 | |
|
|
437 | u8 version; |
|
|
438 | u8 flags; |
|
|
439 | u8 rrtype; |
|
|
440 | u8 def_ttl; |
|
|
441 | |
|
|
442 | u16 client; |
|
|
443 | u16 uid; // to make request unique |
|
|
444 | |
|
|
445 | u16 max_size; |
|
|
446 | u8 seq_cdc; |
|
|
447 | u8 req_cdc; |
|
|
448 | |
|
|
449 | u8 rep_cdc; |
|
|
450 | u8 delay; // time in 0.01s units that the server may delay replying packets |
|
|
451 | u8 r3, r4; |
|
|
452 | |
|
|
453 | u8 r5, r6, r7, r8; |
|
|
454 | |
|
|
455 | void reset (int clientid); |
|
|
456 | bool valid (); |
|
|
457 | }; |
|
|
458 | |
|
|
459 | int dns_cfg::next_uid; |
|
|
460 | |
|
|
461 | void |
|
|
462 | dns_cfg::reset (int clientid) |
|
|
463 | { |
|
|
464 | id1 = 'G'; |
|
|
465 | id2 = 'V'; |
|
|
466 | id3 = 'P'; |
|
|
467 | id4 = 'E'; |
|
|
468 | |
|
|
469 | version = 1; |
|
|
470 | |
|
|
471 | rrtype = RR_TYPE_TXT; |
|
|
472 | flags = 0; |
|
|
473 | def_ttl = 0; |
|
|
474 | seq_cdc = 26; |
|
|
475 | req_cdc = 62; |
|
|
476 | rep_cdc = 0; |
|
|
477 | max_size = htons (MAX_PKT_SIZE); |
|
|
478 | client = htons (clientid); |
|
|
479 | uid = next_uid++; |
|
|
480 | delay = 0; |
|
|
481 | |
|
|
482 | r3 = r4 = 0; |
|
|
483 | r4 = r5 = r6 = r7 = 0; |
|
|
484 | } |
|
|
485 | |
|
|
486 | bool |
|
|
487 | dns_cfg::valid () |
|
|
488 | { |
|
|
489 | // although the protocol itself allows for some configurability, |
|
|
490 | // only the following encoding/decoding settings are implemented. |
|
|
491 | return id1 == 'G' |
|
|
492 | && id2 == 'V' |
|
|
493 | && id3 == 'P' |
|
|
494 | && id4 == 'E' |
|
|
495 | && seq_cdc == 26 |
|
|
496 | && req_cdc == 62 |
|
|
497 | && rep_cdc == 0 |
|
|
498 | && version == 1; |
|
|
499 | } |
236 | |
500 | |
237 | struct dns_packet : net_packet |
501 | struct dns_packet : net_packet |
238 | { |
502 | { |
239 | u16 id; |
503 | u16 id; |
240 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
504 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
241 | u16 qdcount, ancount, nscount, arcount; |
505 | u16 qdcount, ancount, nscount, arcount; |
242 | |
506 | |
243 | u8 data[MAXSIZE - 6 * 2]; |
507 | u8 data [MAXSIZE - 6 * 2]; |
244 | |
508 | |
245 | int decode_label (char *data, int size, int &offs); |
509 | int decode_label (char *data, int size, int &offs); |
246 | }; |
510 | }; |
247 | |
511 | |
|
|
512 | int |
248 | int dns_packet::decode_label (char *data, int size, int &offs) |
513 | dns_packet::decode_label (char *data, int size, int &offs) |
249 | { |
514 | { |
250 | char *orig = data; |
515 | char *orig = data; |
251 | |
516 | |
252 | memset (data, 0, size); |
517 | memset (data, 0, size); |
253 | |
518 | |
… | |
… | |
279 | return data - orig; |
544 | return data - orig; |
280 | } |
545 | } |
281 | |
546 | |
282 | ///////////////////////////////////////////////////////////////////////////// |
547 | ///////////////////////////////////////////////////////////////////////////// |
283 | |
548 | |
|
|
549 | static |
|
|
550 | u16 next_id () |
|
|
551 | { |
|
|
552 | static u16 dns_id = 0; // TODO: should be per-vpn |
|
|
553 | |
|
|
554 | if (!dns_id) |
|
|
555 | dns_id = time (0); |
|
|
556 | |
|
|
557 | // the simplest lsfr with periodicity 65535 i could find |
|
|
558 | dns_id = (dns_id << 1) |
|
|
559 | | (((dns_id >> 1) |
|
|
560 | ^ (dns_id >> 2) |
|
|
561 | ^ (dns_id >> 4) |
|
|
562 | ^ (dns_id >> 15)) & 1); |
|
|
563 | |
|
|
564 | return dns_id; |
|
|
565 | } |
|
|
566 | |
284 | struct dns_req |
567 | struct dns_rcv; |
|
|
568 | struct dns_snd; |
|
|
569 | |
|
|
570 | struct dns_connection |
|
|
571 | { |
|
|
572 | connection *c; |
|
|
573 | struct vpn *vpn; |
|
|
574 | |
|
|
575 | dns_cfg cfg; |
|
|
576 | |
|
|
577 | bool established; |
|
|
578 | |
|
|
579 | tstamp last_received; |
|
|
580 | tstamp last_sent; |
|
|
581 | double min_latency; |
|
|
582 | double poll_interval, send_interval; |
|
|
583 | |
|
|
584 | vector<dns_rcv *> rcvpq; |
|
|
585 | |
|
|
586 | byte_stream rcvdq; int rcvseq; int repseq; |
|
|
587 | byte_stream snddq; int sndseq; |
|
|
588 | |
|
|
589 | inline void time_cb (ev::timer &w, int revents); ev::timer tw; |
|
|
590 | void receive_rep (dns_rcv *r); |
|
|
591 | |
|
|
592 | dns_connection (connection *c); |
|
|
593 | ~dns_connection (); |
|
|
594 | }; |
|
|
595 | |
|
|
596 | struct dns_snd |
285 | { |
597 | { |
286 | dns_packet *pkt; |
598 | dns_packet *pkt; |
287 | tstamp next; |
599 | tstamp timeout, sent; |
288 | int retry; |
600 | int retry; |
289 | connection *conn; |
601 | struct dns_connection *dns; |
|
|
602 | int seqno; |
|
|
603 | bool stdhdr; |
290 | |
604 | |
291 | dns_req (connection *c, int seqno, byte_stream *stream); |
605 | void gen_stream_req (int seqno, byte_stream &stream); |
|
|
606 | void gen_syn_req (); |
|
|
607 | |
|
|
608 | dns_snd (dns_connection *dns); |
|
|
609 | ~dns_snd (); |
292 | }; |
610 | }; |
293 | |
611 | |
294 | struct dns_rep |
612 | dns_snd::dns_snd (dns_connection *dns) |
|
|
613 | : dns (dns) |
295 | { |
614 | { |
296 | }; |
615 | timeout = 0; |
297 | |
|
|
298 | static u16 dns_id; // TODO: should be per-vpn |
|
|
299 | |
|
|
300 | dns_req::dns_req (connection *c, int seqno, byte_stream *stream) |
|
|
301 | : conn (c) |
|
|
302 | { |
|
|
303 | next = 0; |
|
|
304 | retry = 0; |
616 | retry = 0; |
|
|
617 | seqno = 0; |
|
|
618 | sent = ev_now (); |
|
|
619 | stdhdr = false; |
305 | |
620 | |
306 | pkt = new dns_packet; |
621 | pkt = new dns_packet; |
307 | |
622 | |
308 | pkt->id = dns_id++; |
623 | pkt->id = next_id (); |
309 | pkt->flags = DEFAULT_CLIENT_FLAGS; |
624 | } |
310 | pkt->qdcount = htons (1); |
|
|
311 | pkt->ancount = 0; |
|
|
312 | pkt->nscount = 0; |
|
|
313 | pkt->arcount = 0; |
|
|
314 | |
625 | |
315 | int offs = 6*2; |
626 | dns_snd::~dns_snd () |
316 | int dlen = 255 - strlen (THISNODE->domain) - 1; // here we always have room for the max. label length |
627 | { |
|
|
628 | delete pkt; |
|
|
629 | } |
317 | |
630 | |
318 | u8 lbl[64]; //D |
631 | static void |
319 | char enc[65]; |
632 | append_domain (dns_packet &pkt, int &offs, const char *domain) |
320 | int lbllen = 3; |
633 | { |
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 |
634 | // add tunnel domain |
356 | for (;;) |
635 | for (;;) |
357 | { |
636 | { |
358 | const char *end = strchr (suffix, '.'); |
637 | const char *end = strchr (domain, '.'); |
359 | |
638 | |
360 | if (!end) |
639 | if (!end) |
361 | end = suffix + strlen (suffix); |
640 | end = domain + strlen (domain); |
362 | |
641 | |
363 | int len = end - suffix; |
642 | int len = end - domain; |
364 | |
643 | |
365 | (*pkt)[offs++] = len; |
644 | pkt [offs++] = len; |
366 | memcpy (&((*pkt)[offs]), suffix, len); |
645 | memcpy (pkt.at (offs), domain, len); |
367 | offs += len; |
646 | offs += len; |
368 | |
647 | |
369 | if (!*end) |
648 | if (!*end) |
370 | break; |
649 | break; |
371 | |
650 | |
372 | suffix = end + 1; |
651 | domain = end + 1; |
|
|
652 | } |
|
|
653 | } |
|
|
654 | |
|
|
655 | void |
|
|
656 | dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
|
|
657 | { |
|
|
658 | stdhdr = true; |
|
|
659 | this->seqno = seqno; |
|
|
660 | |
|
|
661 | timeout = ev_now () + INITIAL_TIMEOUT; |
|
|
662 | |
|
|
663 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
|
|
664 | pkt->qdcount = htons (1); |
|
|
665 | |
|
|
666 | int offs = 6*2; |
|
|
667 | int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2); |
|
|
668 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
|
|
669 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
|
|
670 | |
|
|
671 | char enc[256], *encp = enc; |
|
|
672 | encode_header (enc, THISNODE->id, seqno); |
|
|
673 | |
|
|
674 | int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); |
|
|
675 | |
|
|
676 | if (datalen > stream.size ()) |
|
|
677 | datalen = stream.size (); |
|
|
678 | |
|
|
679 | int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE; |
|
|
680 | stream.remove (datalen); |
|
|
681 | |
|
|
682 | while (enclen) |
373 | } |
683 | { |
|
|
684 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
|
|
685 | |
|
|
686 | (*pkt)[offs++] = lbllen; |
|
|
687 | memcpy (pkt->at (offs), encp, lbllen); |
|
|
688 | |
|
|
689 | offs += lbllen; |
|
|
690 | encp += lbllen; |
|
|
691 | |
|
|
692 | enclen -= lbllen; |
|
|
693 | } |
|
|
694 | |
|
|
695 | append_domain (*pkt, offs, dns->c->conf->domain); |
374 | |
696 | |
375 | (*pkt)[offs++] = 0; |
697 | (*pkt)[offs++] = 0; |
376 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
698 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
377 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
699 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
378 | |
700 | |
379 | pkt->len = offs; |
701 | pkt->len = offs; |
|
|
702 | } |
380 | |
703 | |
381 | c->vpn->dns_sndpq.push_back (this); |
704 | void |
|
|
705 | dns_snd::gen_syn_req () |
|
|
706 | { |
|
|
707 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
|
|
708 | |
|
|
709 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
|
|
710 | pkt->qdcount = htons (1); |
|
|
711 | |
|
|
712 | int offs = 6 * 2; |
|
|
713 | |
|
|
714 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg)); |
|
|
715 | |
|
|
716 | assert (elen <= MAX_LBL_SIZE); |
|
|
717 | |
|
|
718 | (*pkt)[offs] = elen; |
|
|
719 | offs += elen + 1; |
|
|
720 | append_domain (*pkt, offs, dns->c->conf->domain); |
|
|
721 | |
|
|
722 | (*pkt)[offs++] = 0; |
|
|
723 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
|
|
724 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
|
|
725 | |
|
|
726 | pkt->len = offs; |
|
|
727 | } |
|
|
728 | |
|
|
729 | struct dns_rcv |
|
|
730 | { |
|
|
731 | int seqno; |
|
|
732 | dns_packet *pkt; // reply packet |
|
|
733 | u8 data [MAXSIZE]; // actually part of the reply packet... |
|
|
734 | int datalen; |
|
|
735 | |
|
|
736 | dns_rcv (int seqno, u8 *data, int datalen); |
|
|
737 | ~dns_rcv (); |
|
|
738 | }; |
|
|
739 | |
|
|
740 | dns_rcv::dns_rcv (int seqno, u8 *data, int datalen) |
|
|
741 | : seqno (seqno), pkt (new dns_packet), datalen (datalen) |
|
|
742 | { |
|
|
743 | memcpy (this->data, data, datalen); |
|
|
744 | } |
|
|
745 | |
|
|
746 | dns_rcv::~dns_rcv () |
|
|
747 | { |
|
|
748 | delete pkt; |
382 | } |
749 | } |
383 | |
750 | |
384 | ///////////////////////////////////////////////////////////////////////////// |
751 | ///////////////////////////////////////////////////////////////////////////// |
385 | |
752 | |
386 | struct dns_cfg |
753 | dns_connection::dns_connection (connection *c) |
|
|
754 | : c (c) |
|
|
755 | , rcvdq (MAX_BACKLOG * 2) |
|
|
756 | , snddq (MAX_BACKLOG) |
387 | { |
757 | { |
388 | u8 id1, id2, id3; |
758 | tw.set<dns_connection, &dns_connection::time_cb> (this); |
389 | u8 def_ttl; |
|
|
390 | u8 unused1; |
|
|
391 | u16 max_size; |
|
|
392 | u8 flags1, flags2; |
|
|
393 | }; |
|
|
394 | |
759 | |
395 | dns_packet * |
760 | vpn = c->vpn; |
|
|
761 | |
|
|
762 | established = false; |
|
|
763 | |
|
|
764 | rcvseq = repseq = sndseq = 0; |
|
|
765 | |
|
|
766 | last_sent = last_received = 0; |
|
|
767 | poll_interval = 0.5; // starting here |
|
|
768 | send_interval = 0.5; // starting rate |
|
|
769 | min_latency = INITIAL_TIMEOUT; |
|
|
770 | } |
|
|
771 | |
|
|
772 | dns_connection::~dns_connection () |
|
|
773 | { |
|
|
774 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
|
|
775 | i != rcvpq.end (); |
|
|
776 | ++i) |
|
|
777 | delete *i; |
|
|
778 | } |
|
|
779 | |
|
|
780 | void |
|
|
781 | dns_connection::receive_rep (dns_rcv *r) |
|
|
782 | { |
|
|
783 | if (r->datalen) |
|
|
784 | { |
|
|
785 | last_received = ev_now (); |
|
|
786 | tw (); |
|
|
787 | |
|
|
788 | poll_interval = send_interval; |
|
|
789 | } |
|
|
790 | else |
|
|
791 | { |
|
|
792 | poll_interval *= 1.5; |
|
|
793 | |
|
|
794 | if (poll_interval > MAX_POLL_INTERVAL) |
|
|
795 | poll_interval = MAX_POLL_INTERVAL; |
|
|
796 | } |
|
|
797 | |
|
|
798 | rcvpq.push_back (r); |
|
|
799 | |
|
|
800 | redo: |
|
|
801 | |
|
|
802 | // find next packet |
|
|
803 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
|
|
804 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
|
|
805 | { |
|
|
806 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
|
|
807 | // enter the packet into our input stream |
|
|
808 | r = *i; |
|
|
809 | |
|
|
810 | // remove the oldest packet, look forward, as it's oldest first |
|
|
811 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
|
|
812 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
|
|
813 | { |
|
|
814 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
|
|
815 | delete *j; |
|
|
816 | rcvpq.erase (j); |
|
|
817 | break; |
|
|
818 | } |
|
|
819 | |
|
|
820 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
|
|
821 | |
|
|
822 | if (!rcvdq.put (r->data, r->datalen)) |
|
|
823 | { |
|
|
824 | slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)"); |
|
|
825 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
|
|
826 | } |
|
|
827 | |
|
|
828 | while (vpn_packet *pkt = rcvdq.get ()) |
|
|
829 | { |
|
|
830 | sockinfo si; |
|
|
831 | si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; |
|
|
832 | |
|
|
833 | vpn->recv_vpn_packet (pkt, si); |
|
|
834 | |
|
|
835 | delete pkt; |
|
|
836 | } |
|
|
837 | |
|
|
838 | // check for further packets |
|
|
839 | goto redo; |
|
|
840 | } |
|
|
841 | } |
|
|
842 | |
|
|
843 | void |
396 | vpn::dnsv4_server (dns_packet *pkt) |
844 | vpn::dnsv4_server (dns_packet &pkt) |
397 | { |
845 | { |
398 | u16 flags = ntohs (pkt->flags); |
846 | u16 flags = ntohs (pkt.flags); |
399 | |
847 | |
400 | //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
|
|
401 | int offs = 6 * 2; // skip header |
848 | int offs = 6 * 2; // skip header |
402 | |
849 | |
403 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
850 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
404 | |
851 | |
405 | if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
852 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
406 | && pkt->qdcount == htons (1)) |
853 | && pkt.qdcount == htons (1)) |
407 | { |
854 | { |
408 | char qname[MAXSIZE]; |
855 | char qname [MAXSIZE]; |
409 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
856 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
410 | |
857 | |
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++]; |
858 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
415 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
859 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
416 | |
860 | |
417 | pkt->qdcount = htons (1); |
861 | pkt.qdcount = htons (1); |
418 | pkt->ancount = 0; |
862 | pkt.ancount = 0; |
419 | pkt->nscount = 0; // should be self, as other nameservers reply like this |
863 | 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 |
864 | pkt.arcount = 0; // a record for self, as other nameservers reply like this |
421 | |
865 | |
422 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
866 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL); |
423 | |
867 | |
424 | int dlen = strlen (THISNODE->domain); |
868 | int dlen = strlen (THISNODE->domain); |
425 | |
869 | |
426 | if (qclass == RR_CLASS_IN |
870 | if (qclass == RR_CLASS_IN |
427 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
|
|
428 | && qlen > dlen + 1 |
871 | && qlen > dlen + 1 |
429 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
872 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
430 | { |
873 | { |
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 |
874 | // now generate reply |
444 | rep->ancount = htons (1); // one answer RR |
875 | pkt.ancount = htons (1); // one answer RR |
445 | rep->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
876 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
446 | |
877 | |
447 | (*rep) [offs++] = 0xc0; |
878 | if ((qtype == RR_TYPE_ANY |
448 | (*rep) [offs++] = 6 * 2; // same as in query section |
879 | || qtype == RR_TYPE_TXT |
449 | |
880 | || qtype == RR_TYPE_NULL) |
450 | (*rep) [offs++] = RR_TYPE_TXT >> 8; (*rep) [offs++] = RR_TYPE_TXT; |
881 | && qlen > dlen + 1 + HDRSIZE) |
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; |
|
|
467 | |
|
|
468 | while (dlen > 1) |
|
|
469 | { |
882 | { |
470 | int sndlen = --dlen; |
883 | // correct class, domain: parse |
|
|
884 | int client, seqno; |
|
|
885 | decode_header (qname, client, seqno); |
471 | |
886 | |
472 | if (sndlen + txtlen > 255) |
887 | u8 data[MAXSIZE]; |
473 | sndlen = 255 - txtlen; |
888 | int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
474 | |
889 | |
475 | sndlen = 0; |
890 | if (0 < client && client <= conns.size ()) |
|
|
891 | { |
|
|
892 | connection *c = conns [client - 1]; |
|
|
893 | dns_connection *dns = c->dns; |
|
|
894 | dns_rcv *rcv; |
476 | |
895 | |
477 | (*rep)[offs++] = sndlen + txtlen; |
896 | if (dns) |
478 | memcpy (&((*rep)[offs]), txt, txtlen); |
897 | { |
|
|
898 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
|
|
899 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
|
|
900 | { |
|
|
901 | // already seen that request: simply reply with the cached reply |
|
|
902 | dns_rcv *r = *i; |
|
|
903 | |
|
|
904 | slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno); |
|
|
905 | |
|
|
906 | // refresh header & id, as the retry count could have changed |
|
|
907 | memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE); |
|
|
908 | r->pkt->id = pkt.id; |
|
|
909 | |
|
|
910 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
|
|
911 | |
|
|
912 | goto duplicate_request; |
|
|
913 | } |
|
|
914 | |
|
|
915 | // new packet, queue |
|
|
916 | rcv = new dns_rcv (seqno, data, datalen); |
|
|
917 | dns->receive_rep (rcv); |
|
|
918 | } |
|
|
919 | |
|
|
920 | { |
|
|
921 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
|
|
922 | |
|
|
923 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
|
|
924 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
|
|
925 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
|
|
926 | pkt [offs++] = 0; pkt [offs++] = 0; |
|
|
927 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
|
|
928 | |
|
|
929 | int rdlen_offs = offs += 2; |
|
|
930 | |
|
|
931 | if (dns) |
|
|
932 | { |
|
|
933 | int dlen = ntohs (dns->cfg.max_size) - offs; |
|
|
934 | |
|
|
935 | // bind doesn't compress well, so reduce further by one label length |
|
|
936 | dlen -= qlen; |
|
|
937 | |
|
|
938 | // only put data into in-order sequence packets, if |
|
|
939 | // we receive out-of-order packets we generate empty |
|
|
940 | // replies |
|
|
941 | //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D |
|
|
942 | if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW) |
|
|
943 | { |
|
|
944 | dns->repseq = seqno; |
|
|
945 | |
|
|
946 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
947 | { |
|
|
948 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
|
|
949 | |
|
|
950 | if (txtlen > dns->snddq.size ()) |
|
|
951 | txtlen = dns->snddq.size (); |
|
|
952 | |
|
|
953 | pkt[offs++] = txtlen; |
|
|
954 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
479 | offs += txtlen; |
955 | offs += txtlen; |
|
|
956 | dns->snddq.remove (txtlen); |
480 | |
957 | |
481 | //if (sndlen + txtlen > dns_snddq. |
958 | dlen -= txtlen + 1; |
|
|
959 | } |
|
|
960 | } |
|
|
961 | |
|
|
962 | // avoid completely empty TXT rdata |
|
|
963 | if (offs == rdlen_offs) |
|
|
964 | pkt[offs++] = 0; |
|
|
965 | |
|
|
966 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
|
|
967 | } |
|
|
968 | else |
|
|
969 | { |
|
|
970 | // send RST |
|
|
971 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
|
|
972 | pkt [offs++] = CMD_IP_RST; |
|
|
973 | } |
|
|
974 | |
|
|
975 | int rdlen = offs - rdlen_offs; |
|
|
976 | |
|
|
977 | pkt [rdlen_offs - 2] = rdlen >> 8; |
|
|
978 | pkt [rdlen_offs - 1] = rdlen; |
|
|
979 | |
|
|
980 | if (dns) |
|
|
981 | { |
|
|
982 | // now update dns_rcv copy |
|
|
983 | rcv->pkt->len = offs; |
|
|
984 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
|
|
985 | } |
|
|
986 | } |
|
|
987 | |
|
|
988 | duplicate_request: ; |
|
|
989 | } |
|
|
990 | else |
|
|
991 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
482 | } |
992 | } |
|
|
993 | else if (qtype == RR_TYPE_A |
|
|
994 | && qlen > dlen + 1 + cdc26.encode_len (sizeof (dns_cfg))) |
|
|
995 | { |
|
|
996 | dns_cfg cfg; |
|
|
997 | cdc26.decode ((u8 *)&cfg, qname, cdc26.encode_len (sizeof (dns_cfg))); |
|
|
998 | int client = ntohs (cfg.client); |
483 | |
999 | |
484 | int rdlen = offs - rdlen_offs; |
1000 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
485 | |
1001 | |
486 | (*rep) [rdlen_offs - 2] = rdlen >> 8; |
1002 | pkt [offs++] = RR_TYPE_A >> 8; pkt [offs++] = RR_TYPE_A; // type |
487 | (*rep) [rdlen_offs - 1] = rdlen; |
1003 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
488 | #endif |
1004 | pkt [offs++] = 0; pkt [offs++] = 0; |
|
|
1005 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
|
|
1006 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
|
|
1007 | |
|
|
1008 | slog (L_INFO, _("DNS: client %d connects"), client); |
|
|
1009 | |
|
|
1010 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
|
|
1011 | pkt [offs++] = CMD_IP_REJ; |
|
|
1012 | |
|
|
1013 | if (0 < client && client <= conns.size ()) |
|
|
1014 | { |
|
|
1015 | connection *c = conns [client - 1]; |
|
|
1016 | |
|
|
1017 | if (cfg.valid ()) |
|
|
1018 | { |
|
|
1019 | pkt [offs - 1] = CMD_IP_SYN; |
|
|
1020 | |
|
|
1021 | delete c->dns; |
|
|
1022 | c->dns = new dns_connection (c); |
|
|
1023 | c->dns->cfg = cfg; |
|
|
1024 | } |
|
|
1025 | } |
|
|
1026 | } |
489 | } |
1027 | } |
490 | } |
|
|
491 | |
1028 | |
492 | pkt->len = offs; |
1029 | pkt.len = offs; |
493 | return pkt; |
1030 | } |
494 | } |
1031 | } |
495 | |
1032 | |
496 | void |
1033 | void |
497 | vpn::dnsv4_client (dns_packet *pkt) |
1034 | vpn::dnsv4_client (dns_packet &pkt) |
498 | { |
1035 | { |
499 | u16 flags = ntohs (pkt->flags); |
1036 | u16 flags = ntohs (pkt.flags); |
500 | int offs = 6 * 2; // skip header |
1037 | int offs = 6 * 2; // skip header |
501 | |
1038 | |
502 | pkt->qdcount = ntohs (pkt->qdcount); |
1039 | pkt.qdcount = ntohs (pkt.qdcount); |
503 | pkt->ancount = ntohs (pkt->ancount); |
1040 | pkt.ancount = ntohs (pkt.ancount); |
504 | |
1041 | |
|
|
1042 | // go through our request list and find the corresponding request |
505 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
1043 | for (vector<dns_snd *>::iterator i = dns_sndpq.begin (); |
506 | i != dns_sndpq.end (); |
1044 | i != dns_sndpq.end (); |
507 | ++i) |
1045 | ++i) |
508 | if ((*i)->pkt->id == pkt->id) |
1046 | if ((*i)->pkt->id == pkt.id) |
509 | { |
1047 | { |
|
|
1048 | dns_connection *dns = (*i)->dns; |
510 | connection *c = (*i)->conn; |
1049 | connection *c = dns->c; |
511 | printf ("GOT RESPONSE id %x %p\n", pkt->id, c);//D |
1050 | int seqno = (*i)->seqno; |
|
|
1051 | u8 data[MAXSIZE], *datap = data; |
|
|
1052 | //printf ("rcv pkt %x\n", seqno);//D |
|
|
1053 | |
|
|
1054 | if ((*i)->retry) |
|
|
1055 | { |
|
|
1056 | dns->send_interval *= 1.01; |
|
|
1057 | if (dns->send_interval > MAX_SEND_INTERVAL) |
|
|
1058 | dns->send_interval = MAX_SEND_INTERVAL; |
|
|
1059 | } |
|
|
1060 | else |
|
|
1061 | { |
|
|
1062 | #if 0 |
|
|
1063 | dns->send_interval *= 0.999; |
|
|
1064 | #endif |
|
|
1065 | // the latency surely puts an upper bound on |
|
|
1066 | // the minimum send interval |
|
|
1067 | double latency = ev_now () - (*i)->sent; |
|
|
1068 | |
|
|
1069 | if (latency < dns->min_latency) |
|
|
1070 | dns->min_latency = latency; |
|
|
1071 | |
|
|
1072 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
|
|
1073 | dns->send_interval = dns->min_latency * conf.dns_overlap_factor; |
|
|
1074 | |
|
|
1075 | if (dns->send_interval < conf.dns_send_interval) |
|
|
1076 | dns->send_interval = conf.dns_send_interval; |
|
|
1077 | } |
|
|
1078 | |
512 | delete *i; |
1079 | delete *i; |
513 | dns_sndpq.erase (i); |
1080 | dns_sndpq.erase (i); |
514 | |
1081 | |
515 | if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
1082 | if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK)) |
516 | { |
1083 | { |
517 | char qname[MAXSIZE]; |
1084 | char qname[MAXSIZE]; |
518 | |
1085 | |
519 | while (pkt->qdcount-- && offs < MAXSIZE - 4) |
1086 | while (pkt.qdcount-- && offs < MAXSIZE - 4) |
520 | { |
1087 | { |
521 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
1088 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
522 | offs += 4; // skip qtype, qclass |
1089 | offs += 4; // skip qtype, qclass |
523 | } |
1090 | } |
524 | |
1091 | |
525 | while (pkt->ancount-- && offs < MAXSIZE - 10) |
1092 | while (pkt.ancount-- && offs < MAXSIZE - 10 && datap) |
526 | { |
1093 | { |
527 | pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
1094 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
528 | |
1095 | |
529 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
1096 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
530 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
1097 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
531 | u32 ttl = (*pkt) [offs++] << 24; |
1098 | u32 ttl = pkt [offs++] << 24; |
532 | ttl |= (*pkt) [offs++] << 16; |
1099 | ttl |= pkt [offs++] << 16; |
533 | ttl |= (*pkt) [offs++] << 8; |
1100 | ttl |= pkt [offs++] << 8; |
534 | ttl |= (*pkt) [offs++]; |
1101 | ttl |= pkt [offs++]; |
535 | |
|
|
536 | u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
1102 | u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++]; |
537 | |
1103 | |
538 | printf ("REPLY %d:%d TTL %d RD %d\n", qtype, qclass, ttl, rdlen); |
1104 | if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT) |
539 | |
|
|
540 | offs += rdlen; |
|
|
541 | |
|
|
542 | if (MAXSIZE - offs < rdlen) |
|
|
543 | { |
1105 | { |
|
|
1106 | if (rdlen <= MAXSIZE - offs) |
|
|
1107 | { |
544 | // decode bytes, finally |
1108 | // decode bytes, finally |
|
|
1109 | |
|
|
1110 | while (rdlen) |
|
|
1111 | { |
|
|
1112 | int txtlen = pkt [offs++]; |
|
|
1113 | |
|
|
1114 | assert (txtlen + offs < MAXSIZE - 1); |
|
|
1115 | |
|
|
1116 | memcpy (datap, pkt.at (offs), txtlen); |
|
|
1117 | datap += txtlen; offs += txtlen; |
|
|
1118 | |
|
|
1119 | rdlen -= txtlen + 1; |
|
|
1120 | } |
|
|
1121 | } |
|
|
1122 | } |
|
|
1123 | else if (qtype == RR_TYPE_A) |
|
|
1124 | { |
|
|
1125 | u8 ip [4]; |
|
|
1126 | |
|
|
1127 | ip [0] = pkt [offs++]; |
|
|
1128 | ip [1] = pkt [offs++]; |
|
|
1129 | ip [2] = pkt [offs++]; |
|
|
1130 | ip [3] = pkt [offs++]; |
|
|
1131 | |
|
|
1132 | if (ip [0] == CMD_IP_1 |
|
|
1133 | && ip [1] == CMD_IP_2 |
|
|
1134 | && ip [2] == CMD_IP_3) |
|
|
1135 | { |
|
|
1136 | slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]); |
|
|
1137 | |
|
|
1138 | if (ip [3] == CMD_IP_RST) |
|
|
1139 | { |
|
|
1140 | slog (L_DEBUG, _("DNS: got tunnel RST request")); |
|
|
1141 | |
|
|
1142 | delete dns; c->dns = 0; |
|
|
1143 | |
|
|
1144 | return; |
|
|
1145 | } |
|
|
1146 | else if (ip [3] == CMD_IP_SYN) |
|
|
1147 | { |
|
|
1148 | slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); |
|
|
1149 | dns->established = true; |
|
|
1150 | } |
|
|
1151 | else if (ip [3] == CMD_IP_REJ) |
|
|
1152 | { |
|
|
1153 | slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting.")); |
|
|
1154 | abort (); |
|
|
1155 | } |
|
|
1156 | else |
|
|
1157 | slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); |
|
|
1158 | } |
|
|
1159 | else |
|
|
1160 | slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), |
|
|
1161 | ip [0], ip [1], ip [2], ip [3]); |
|
|
1162 | |
|
|
1163 | return; |
|
|
1164 | } |
|
|
1165 | |
|
|
1166 | int client, rseqno; |
|
|
1167 | decode_header (qname, client, rseqno); |
|
|
1168 | |
|
|
1169 | if (client != THISNODE->id) |
|
|
1170 | { |
|
|
1171 | slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring")); |
|
|
1172 | datap = 0; |
|
|
1173 | } |
|
|
1174 | else if (rseqno != seqno) |
|
|
1175 | { |
|
|
1176 | slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?")); |
|
|
1177 | datap = 0; |
545 | } |
1178 | } |
546 | } |
1179 | } |
547 | } |
1180 | } |
548 | |
1181 | |
|
|
1182 | // todo: pkt now used |
|
|
1183 | if (datap) |
|
|
1184 | dns->receive_rep (new dns_rcv (seqno, data, datap - data)); |
|
|
1185 | |
549 | break; |
1186 | break; |
550 | } |
1187 | } |
551 | } |
1188 | } |
552 | |
1189 | |
553 | void |
1190 | void |
554 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1191 | vpn::dnsv4_ev (ev::io &w, int revents) |
555 | { |
1192 | { |
556 | if (revents & EVENT_READ) |
1193 | if (revents & EV_READ) |
557 | { |
1194 | { |
558 | dns_packet *pkt = new dns_packet; |
1195 | dns_packet *pkt = new dns_packet; |
559 | struct sockaddr_in sa; |
1196 | struct sockaddr_in sa; |
560 | socklen_t sa_len = sizeof (sa); |
1197 | socklen_t sa_len = sizeof (sa); |
561 | |
1198 | |
562 | pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1199 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
563 | |
1200 | |
564 | if (pkt->len > 0) |
1201 | if (pkt->len > 0) |
565 | if (THISNODE->dns_port) |
|
|
566 | { |
1202 | { |
567 | pkt = dnsv4_server (pkt); |
1203 | if (ntohs (pkt->flags) & FLAG_RESPONSE) |
568 | sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
|
|
569 | } |
|
|
570 | else |
|
|
571 | dnsv4_client (pkt); |
1204 | dnsv4_client (*pkt); |
|
|
1205 | else |
|
|
1206 | { |
|
|
1207 | dnsv4_server (*pkt); |
|
|
1208 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
|
|
1209 | } |
|
|
1210 | |
|
|
1211 | delete pkt; |
|
|
1212 | } |
572 | } |
1213 | } |
573 | } |
1214 | } |
574 | |
1215 | |
575 | bool |
1216 | bool |
576 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1217 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
577 | { |
1218 | { |
578 | // never initialized |
1219 | int client = ntohl (si.host); |
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 | |
1220 | |
584 | dns_rcvseq = dns_sndseq = 0; |
1221 | assert (0 < client && client <= conns.size ()); |
585 | |
1222 | |
586 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
1223 | connection *c = conns [client - 1]; |
587 | } |
1224 | |
|
|
1225 | if (!c->dns) |
|
|
1226 | c->dns = new dns_connection (c); |
588 | |
1227 | |
589 | if (!dns_snddq->put (pkt)) |
1228 | if (c->dns->snddq.put (pkt)) |
590 | return false; |
1229 | c->dns->tw (); |
591 | |
1230 | |
592 | // start timer if neccessary |
1231 | // always return true even if the buffer overflows |
593 | if (!dnsv4_tw.active) |
|
|
594 | dnsv4_cb (dnsv4_tw); |
|
|
595 | |
|
|
596 | return true; |
1232 | return true; |
597 | } |
1233 | } |
598 | |
1234 | |
599 | void |
1235 | void |
600 | connection::dnsv4_cb (time_watcher &w) |
1236 | connection::dnsv4_reset_connection () |
601 | { |
1237 | { |
|
|
1238 | //delete dns; dns = 0; //TODO |
|
|
1239 | } |
|
|
1240 | |
|
|
1241 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
|
|
1242 | |
|
|
1243 | void |
|
|
1244 | dns_connection::time_cb (ev::timer &w, int revents) |
|
|
1245 | { |
|
|
1246 | // servers have to be polled |
|
|
1247 | if (THISNODE->dns_port) |
|
|
1248 | return; |
|
|
1249 | |
602 | // check for timeouts and (re)transmit |
1250 | // check for timeouts and (re)transmit |
603 | tstamp next = NOW + 60; |
1251 | tstamp next = ev::now () + poll_interval; |
604 | dns_req *send = 0; |
1252 | dns_snd *send = 0; |
605 | |
1253 | |
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 (); |
1254 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
609 | i != vpn->dns_sndpq.end (); |
1255 | i != vpn->dns_sndpq.end (); |
610 | ++i) |
1256 | ++i) |
611 | { |
1257 | { |
612 | dns_req *r = *i; |
1258 | dns_snd *r = *i; |
613 | |
1259 | |
614 | if (r->next <= NOW) |
1260 | if (r->timeout <= ev_now ()) |
615 | { |
1261 | { |
616 | if (!send) |
1262 | if (!send) |
617 | { |
1263 | { |
618 | send = r; |
1264 | send = r; |
619 | |
1265 | |
620 | slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); |
|
|
621 | r->retry++; |
1266 | r->retry++; |
622 | r->next = NOW + r->retry; |
1267 | r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); |
|
|
1268 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D |
|
|
1269 | |
|
|
1270 | // the following code changes the query section a bit, forcing |
|
|
1271 | // the forwarder to generate a new request |
|
|
1272 | if (r->stdhdr) |
|
|
1273 | encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
623 | } |
1274 | } |
624 | } |
1275 | } |
625 | |
1276 | else |
626 | if (r->next < next) |
1277 | NEXT (r->timeout); |
627 | next = r->next; |
|
|
628 | } |
1278 | } |
629 | |
1279 | |
630 | if (!send |
1280 | if (!send) |
631 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
1281 | { |
632 | send = new dns_req (this, dns_sndseq++, dns_snddq); |
1282 | // generate a new packet, if wise |
633 | |
1283 | |
634 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
1284 | if (!established) |
|
|
1285 | { |
|
|
1286 | if (vpn->dns_sndpq.empty ()) |
|
|
1287 | { |
|
|
1288 | send = new dns_snd (this); |
|
|
1289 | |
|
|
1290 | cfg.reset (THISNODE->id); |
|
|
1291 | send->gen_syn_req (); |
|
|
1292 | } |
|
|
1293 | } |
|
|
1294 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
|
|
1295 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
|
|
1296 | { |
|
|
1297 | if (last_sent + send_interval <= ev_now ()) |
|
|
1298 | { |
|
|
1299 | //printf ("sending data request etc.\n"); //D |
|
|
1300 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
|
|
1301 | { |
|
|
1302 | poll_interval = send_interval; |
|
|
1303 | NEXT (ev_now () + send_interval); |
|
|
1304 | } |
|
|
1305 | |
|
|
1306 | send = new dns_snd (this); |
|
|
1307 | send->gen_stream_req (sndseq, snddq); |
|
|
1308 | send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; |
|
|
1309 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D |
|
|
1310 | |
|
|
1311 | sndseq = (sndseq + 1) & SEQNO_MASK; |
|
|
1312 | } |
|
|
1313 | else |
|
|
1314 | NEXT (last_sent + send_interval); |
|
|
1315 | } |
|
|
1316 | |
|
|
1317 | if (send) |
|
|
1318 | vpn->dns_sndpq.push_back (send); |
|
|
1319 | } |
635 | |
1320 | |
636 | if (send) |
1321 | if (send) |
637 | { |
1322 | { |
638 | dns_packet *pkt = send->pkt; |
1323 | last_sent = ev_now (); |
639 | |
1324 | sendto (vpn->dnsv4_fd, |
640 | next = min_next; |
1325 | send->pkt->at (0), send->pkt->len, 0, |
641 | |
1326 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
642 | sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ()); |
|
|
643 | } |
1327 | } |
644 | else if (next < min_next) |
|
|
645 | next = min_next; |
|
|
646 | |
1328 | |
647 | w.start (next); |
1329 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
|
|
1330 | poll_interval, send_interval, next - ev_now (), |
|
|
1331 | vpn->dns_sndpq.size (), snddq.size (), |
|
|
1332 | rcvpq.size ()); |
|
|
1333 | |
|
|
1334 | // TODO: no idea when this happens, but when next < ev_now (), we have a problem |
|
|
1335 | // doesn't seem to happen anymore |
|
|
1336 | if (next < ev_now () + 0.001) |
|
|
1337 | next = ev_now () + 0.1; |
|
|
1338 | |
|
|
1339 | w.start (next - ev_now ()); |
648 | } |
1340 | } |
649 | |
1341 | |
650 | #endif |
1342 | #endif |
651 | |
1343 | |