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