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