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