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