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