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