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 | { |
222 | int len = (data [0] << 8) | data [1]; |
361 | unsigned int len = (data [0] << 8) | data [1]; |
223 | |
|
|
224 | printf ("get len %d, fill %d\n", len, fill);//D |
|
|
225 | |
362 | |
226 | if (len > MAXSIZE && fill >= 2) |
363 | if (len > MAXSIZE && fill >= 2) |
227 | abort (); // TODO handle this gracefully, connection reset |
364 | assert (len <= MAXSIZE || fill < 2); // TODO handle this gracefully, connection reset |
228 | |
365 | |
229 | if (fill < len + 2) |
366 | if (fill < len + 2) |
230 | return 0; |
367 | return 0; |
231 | |
368 | |
232 | vpn_packet *pkt = new vpn_packet; |
369 | vpn_packet *pkt = new vpn_packet; |
233 | |
370 | |
234 | pkt->len = len; |
371 | pkt->len = len; |
235 | memcpy (&((*pkt)[0]), data + 2, len); |
372 | memcpy (pkt->at (0), data + 2, len); |
236 | remove (len + 2); |
373 | remove (len + 2); |
237 | |
374 | |
238 | return pkt; |
375 | return pkt; |
239 | } |
376 | } |
240 | |
377 | |
241 | ///////////////////////////////////////////////////////////////////////////// |
378 | ///////////////////////////////////////////////////////////////////////////// |
242 | |
379 | |
243 | #define FLAG_QUERY ( 0 << 15) |
380 | #define FLAG_QUERY ( 0 << 15) |
244 | #define FLAG_RESPONSE ( 1 << 15) |
381 | #define FLAG_RESPONSE ( 1 << 15) |
245 | #define FLAG_OP_MASK (15 << 14) |
382 | #define FLAG_OP_MASK (15 << 11) |
246 | #define FLAG_OP_QUERY ( 0 << 11) |
383 | #define FLAG_OP_QUERY ( 0 << 11) |
247 | #define FLAG_AA ( 1 << 10) |
384 | #define FLAG_AA ( 1 << 10) |
248 | #define FLAG_TC ( 1 << 9) |
385 | #define FLAG_TC ( 1 << 9) |
249 | #define FLAG_RD ( 1 << 8) |
386 | #define FLAG_RD ( 1 << 8) |
250 | #define FLAG_RA ( 1 << 7) |
387 | #define FLAG_RA ( 1 << 7) |
… | |
… | |
257 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
394 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
258 | |
395 | |
259 | #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
396 | #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) |
397 | #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
261 | |
398 | |
|
|
399 | struct dns_cfg |
|
|
400 | { |
|
|
401 | static int next_uid; |
|
|
402 | |
|
|
403 | u8 id1, id2, id3, id4; |
|
|
404 | u8 version; |
|
|
405 | u8 rrtype; |
|
|
406 | u8 flags; |
|
|
407 | u8 def_ttl; |
|
|
408 | u8 rcv_cdc; |
|
|
409 | u8 snd_cdc; |
|
|
410 | u16 max_size; |
|
|
411 | u16 client; |
|
|
412 | u16 uid; // to make request unique |
|
|
413 | |
|
|
414 | u8 reserved[8]; |
|
|
415 | |
|
|
416 | void reset (int clientid); |
|
|
417 | bool valid (); |
|
|
418 | }; |
|
|
419 | |
|
|
420 | int dns_cfg::next_uid; |
|
|
421 | |
|
|
422 | void dns_cfg::reset (int clientid) |
|
|
423 | { |
|
|
424 | id1 = 'G'; |
|
|
425 | id2 = 'V'; |
|
|
426 | id3 = 'P'; |
|
|
427 | id4 = 'E'; |
|
|
428 | |
|
|
429 | version = 1; |
|
|
430 | |
|
|
431 | rrtype = RR_TYPE_TXT; |
|
|
432 | flags = 0; |
|
|
433 | def_ttl = 1; |
|
|
434 | rcv_cdc = 0; |
|
|
435 | snd_cdc = 62; |
|
|
436 | max_size = ntohs (MAX_PKT_SIZE); |
|
|
437 | client = ntohs (clientid); |
|
|
438 | uid = next_uid++; |
|
|
439 | |
|
|
440 | memset (reserved, 0, 8); |
|
|
441 | } |
|
|
442 | |
|
|
443 | bool dns_cfg::valid () |
|
|
444 | { |
|
|
445 | return id1 == 'G' |
|
|
446 | && id2 == 'V' |
|
|
447 | && id3 == 'P' |
|
|
448 | && id4 == 'E' |
|
|
449 | && version == 1 |
|
|
450 | && flags == 0 |
|
|
451 | && rcv_cdc == 0 |
|
|
452 | && snd_cdc == 62 |
|
|
453 | && max_size == ntohs (MAX_PKT_SIZE); |
|
|
454 | } |
|
|
455 | |
262 | struct dns_packet : net_packet |
456 | struct dns_packet : net_packet |
263 | { |
457 | { |
264 | u16 id; |
458 | u16 id; |
265 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
459 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
266 | u16 qdcount, ancount, nscount, arcount; |
460 | u16 qdcount, ancount, nscount, arcount; |
… | |
… | |
304 | return data - orig; |
498 | return data - orig; |
305 | } |
499 | } |
306 | |
500 | |
307 | ///////////////////////////////////////////////////////////////////////////// |
501 | ///////////////////////////////////////////////////////////////////////////// |
308 | |
502 | |
309 | struct dns_req |
503 | struct dns_snd |
310 | { |
504 | { |
311 | dns_packet *pkt; |
505 | dns_packet *pkt; |
312 | tstamp next; |
506 | tstamp timeout, sent; |
313 | int retry; |
507 | int retry; |
314 | connection *conn; |
508 | struct dns_connection *dns; |
315 | int seqno; |
509 | int seqno; |
|
|
510 | bool stdhdr; |
316 | |
511 | |
317 | dns_req (connection *c); |
|
|
318 | void gen_stream_req (int seqno, byte_stream *stream); |
512 | void gen_stream_req (int seqno, byte_stream &stream); |
|
|
513 | void gen_syn_req (const dns_cfg &cfg); |
|
|
514 | |
|
|
515 | dns_snd (dns_connection *dns); |
|
|
516 | ~dns_snd (); |
319 | }; |
517 | }; |
320 | |
518 | |
321 | static u16 dns_id = 12098; // TODO: should be per-vpn |
519 | static u16 dns_id = 12098; // TODO: should be per-vpn |
322 | |
520 | |
323 | static u16 next_id () |
521 | static u16 next_id () |
… | |
… | |
330 | ^ (dns_id >> 15)) & 1); |
528 | ^ (dns_id >> 15)) & 1); |
331 | |
529 | |
332 | return dns_id; |
530 | return dns_id; |
333 | } |
531 | } |
334 | |
532 | |
335 | dns_req::dns_req (connection *c) |
533 | dns_snd::dns_snd (dns_connection *dns) |
336 | : conn (c) |
534 | : dns (dns) |
337 | { |
535 | { |
338 | next = 0; |
536 | timeout = 0; |
339 | retry = 0; |
537 | retry = 0; |
|
|
538 | seqno = 0; |
|
|
539 | sent = NOW; |
|
|
540 | stdhdr = false; |
340 | |
541 | |
341 | pkt = new dns_packet; |
542 | pkt = new dns_packet; |
342 | |
543 | |
343 | pkt->id = next_id (); |
544 | pkt->id = next_id (); |
344 | } |
545 | } |
345 | |
546 | |
|
|
547 | dns_snd::~dns_snd () |
|
|
548 | { |
|
|
549 | delete pkt; |
|
|
550 | } |
|
|
551 | |
|
|
552 | static void append_domain (dns_packet &pkt, int &offs, const char *domain) |
|
|
553 | { |
|
|
554 | // add tunnel domain |
|
|
555 | for (;;) |
|
|
556 | { |
|
|
557 | const char *end = strchr (domain, '.'); |
|
|
558 | |
|
|
559 | if (!end) |
|
|
560 | end = domain + strlen (domain); |
|
|
561 | |
|
|
562 | int len = end - domain; |
|
|
563 | |
|
|
564 | pkt [offs++] = len; |
|
|
565 | memcpy (pkt.at (offs), domain, len); |
|
|
566 | offs += len; |
|
|
567 | |
|
|
568 | if (!*end) |
|
|
569 | break; |
|
|
570 | |
|
|
571 | domain = end + 1; |
|
|
572 | } |
|
|
573 | } |
|
|
574 | |
346 | void dns_req::gen_stream_req (int seqno, byte_stream *stream) |
575 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
347 | { |
576 | { |
|
|
577 | stdhdr = true; |
348 | this->seqno = seqno; |
578 | this->seqno = seqno; |
|
|
579 | |
|
|
580 | timeout = NOW + INITIAL_TIMEOUT; |
349 | |
581 | |
350 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
582 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
351 | pkt->qdcount = htons (1); |
583 | pkt->qdcount = htons (1); |
352 | |
584 | |
353 | int offs = 6*2; |
585 | int offs = 6*2; |
354 | int dlen = MAX_DOMAIN_SIZE - strlen (THISNODE->domain) - 2; |
586 | int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); |
355 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
587 | // 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 |
588 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
357 | |
589 | |
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; |
590 | char enc[256], *encp = enc; |
370 | |
591 | encode_header (enc, THISNODE->id, seqno); |
371 | memcpy (data + 3, stream->begin (), datalen); |
592 | |
372 | int enclen = dns64::encode (enc, data, datalen + 3); |
593 | int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); |
|
|
594 | |
|
|
595 | if (datalen > stream.size ()) |
|
|
596 | datalen = stream.size (); |
|
|
597 | |
|
|
598 | int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE; |
373 | stream->remove (datalen); |
599 | stream.remove (datalen); |
374 | |
600 | |
375 | while (enclen) |
601 | while (enclen) |
376 | { |
602 | { |
377 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
603 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
378 | |
604 | |
… | |
… | |
383 | encp += lbllen; |
609 | encp += lbllen; |
384 | |
610 | |
385 | enclen -= lbllen; |
611 | enclen -= lbllen; |
386 | } |
612 | } |
387 | |
613 | |
388 | const char *suffix = THISNODE->domain; |
614 | 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 | |
615 | |
410 | (*pkt)[offs++] = 0; |
616 | (*pkt)[offs++] = 0; |
411 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
617 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
|
|
618 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
|
|
619 | |
|
|
620 | pkt->len = offs; |
|
|
621 | } |
|
|
622 | |
|
|
623 | void dns_snd::gen_syn_req (const dns_cfg &cfg) |
|
|
624 | { |
|
|
625 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
|
|
626 | |
|
|
627 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
|
|
628 | pkt->qdcount = htons (1); |
|
|
629 | |
|
|
630 | int offs = 6*2; |
|
|
631 | |
|
|
632 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg)); |
|
|
633 | |
|
|
634 | assert (elen <= MAX_LBL_SIZE); |
|
|
635 | |
|
|
636 | (*pkt)[offs] = elen; |
|
|
637 | offs += elen + 1; |
|
|
638 | append_domain (*pkt, offs, THISNODE->domain); |
|
|
639 | |
|
|
640 | (*pkt)[offs++] = 0; |
|
|
641 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
412 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
642 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
413 | |
643 | |
414 | pkt->len = offs; |
644 | pkt->len = offs; |
415 | } |
645 | } |
416 | |
646 | |
… | |
… | |
419 | int seqno; |
649 | int seqno; |
420 | dns_packet *pkt; // reply packet |
650 | dns_packet *pkt; // reply packet |
421 | u8 data [MAXSIZE]; // actually part of the reply packet... |
651 | u8 data [MAXSIZE]; // actually part of the reply packet... |
422 | int datalen; |
652 | int datalen; |
423 | |
653 | |
424 | dns_rcv (int seqno, dns_packet *req, u8 *data, int datalen); |
654 | dns_rcv (int seqno, u8 *data, int datalen); |
425 | ~dns_rcv (); |
655 | ~dns_rcv (); |
426 | }; |
656 | }; |
427 | |
657 | |
428 | dns_rcv::dns_rcv (int seqno, dns_packet *req, u8 *data, int datalen) |
658 | dns_rcv::dns_rcv (int seqno, u8 *data, int datalen) |
429 | : seqno (seqno), pkt (new dns_packet), datalen (datalen) |
659 | : seqno (seqno), pkt (new dns_packet), datalen (datalen) |
430 | { |
660 | { |
431 | memcpy (this->data, data, datalen); |
661 | memcpy (this->data, data, datalen); |
432 | pkt->len = req->len; |
|
|
433 | memcpy (pkt->at (0), req->at (0), req->len); |
|
|
434 | } |
662 | } |
435 | |
663 | |
436 | dns_rcv::~dns_rcv () |
664 | dns_rcv::~dns_rcv () |
437 | { |
665 | { |
438 | delete pkt; |
666 | delete pkt; |
439 | } |
667 | } |
440 | |
668 | |
441 | ///////////////////////////////////////////////////////////////////////////// |
669 | ///////////////////////////////////////////////////////////////////////////// |
442 | |
670 | |
443 | struct dns_cfg |
671 | struct dns_connection |
444 | { |
672 | { |
445 | u8 id1, id2, id3; |
673 | connection *c; |
446 | u8 def_ttl; |
674 | struct vpn *vpn; |
447 | u8 unused1; |
675 | |
448 | u16 max_size; |
676 | dns_cfg cfg; |
449 | u8 flags1, flags2; |
677 | |
|
|
678 | bool established; |
|
|
679 | |
|
|
680 | tstamp last_received; |
|
|
681 | tstamp last_sent; |
|
|
682 | double last_latency; |
|
|
683 | double poll_interval, send_interval; |
|
|
684 | |
|
|
685 | vector<dns_rcv *> rcvpq; |
|
|
686 | |
|
|
687 | byte_stream rcvdq; int rcvseq; |
|
|
688 | byte_stream snddq; int sndseq; |
|
|
689 | |
|
|
690 | void time_cb (time_watcher &w); time_watcher tw; |
|
|
691 | void receive_rep (dns_rcv *r); |
|
|
692 | |
|
|
693 | dns_connection (connection *c); |
|
|
694 | ~dns_connection (); |
450 | }; |
695 | }; |
451 | |
696 | |
|
|
697 | dns_connection::dns_connection (connection *c) |
|
|
698 | : c (c) |
|
|
699 | , rcvdq (MAX_BACKLOG * 2) |
|
|
700 | , snddq (MAX_BACKLOG * 2) |
|
|
701 | , tw (this, &dns_connection::time_cb) |
|
|
702 | { |
|
|
703 | vpn = c->vpn; |
|
|
704 | |
|
|
705 | established = false; |
|
|
706 | |
|
|
707 | rcvseq = sndseq = 0; |
|
|
708 | |
|
|
709 | last_sent = last_received = 0; |
|
|
710 | poll_interval = MIN_POLL_INTERVAL; |
|
|
711 | send_interval = 0.5; // starting rate |
|
|
712 | last_latency = INITIAL_TIMEOUT; |
|
|
713 | } |
|
|
714 | |
|
|
715 | dns_connection::~dns_connection () |
|
|
716 | { |
|
|
717 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
|
|
718 | i != rcvpq.end (); |
|
|
719 | ++i) |
|
|
720 | delete *i; |
|
|
721 | } |
|
|
722 | |
452 | void connection::dnsv4_receive_rep (struct dns_rcv *r) |
723 | void dns_connection::receive_rep (dns_rcv *r) |
453 | { |
724 | { |
|
|
725 | if (r->datalen) |
|
|
726 | { |
|
|
727 | last_received = NOW; |
|
|
728 | tw.trigger (); |
|
|
729 | |
|
|
730 | poll_interval = send_interval; |
|
|
731 | } |
|
|
732 | else |
|
|
733 | { |
|
|
734 | poll_interval *= 1.5; |
|
|
735 | if (poll_interval > MAX_POLL_INTERVAL) |
|
|
736 | poll_interval = MAX_POLL_INTERVAL; |
|
|
737 | } |
|
|
738 | |
454 | dns_rcvpq.push_back (r); |
739 | rcvpq.push_back (r); |
455 | |
740 | |
456 | redo: |
741 | redo: |
457 | |
742 | |
|
|
743 | // find next packet |
458 | for (vector<dns_rcv *>::iterator i = dns_rcvpq.begin (); |
744 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
459 | i != dns_rcvpq.end (); |
|
|
460 | ++i) |
|
|
461 | if (dns_rcvseq == (*i)->seqno) |
745 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
462 | { |
746 | { |
|
|
747 | // enter the packet into our input stream |
463 | dns_rcv *r = *i; |
748 | r = *i; |
464 | |
749 | |
|
|
750 | // remove the oldest packet, look forward, as it's oldest first |
|
|
751 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
|
|
752 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
|
|
753 | { |
|
|
754 | delete *j; |
|
|
755 | rcvpq.erase (j); |
|
|
756 | break; |
|
|
757 | } |
|
|
758 | |
465 | dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; |
759 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
466 | |
760 | |
467 | if (!dns_snddq && !dns_rcvdq) |
761 | if (!rcvdq.put (r->data, r->datalen)) |
468 | { |
762 | { |
469 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
763 | slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)"); |
470 | dns_snddq = new byte_stream (MAX_BACKLOG); |
764 | 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 | } |
765 | } |
474 | |
766 | |
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 ()) |
767 | while (vpn_packet *pkt = rcvdq.get ()) |
479 | { |
768 | { |
480 | sockinfo si; |
769 | sockinfo si; |
481 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
770 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
482 | |
771 | |
483 | vpn->recv_vpn_packet (pkt, si); |
772 | vpn->recv_vpn_packet (pkt, si); |
|
|
773 | |
|
|
774 | delete pkt; |
484 | } |
775 | } |
485 | } |
776 | |
486 | else if ((u32)dns_rcvseq - MAX_WINDOW - (u32)(*i)->seqno < MAX_WINDOW * 2) |
777 | // check for further packets |
487 | { |
|
|
488 | dns_rcvpq.erase (i); |
|
|
489 | goto redo; |
778 | goto redo; |
490 | } |
779 | } |
491 | } |
780 | } |
492 | |
781 | |
493 | dns_packet * |
782 | void |
494 | vpn::dnsv4_server (dns_packet *pkt) |
783 | vpn::dnsv4_server (dns_packet &pkt) |
495 | { |
784 | { |
496 | u16 flags = ntohs (pkt->flags); |
785 | u16 flags = ntohs (pkt.flags); |
497 | |
786 | |
498 | //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
|
|
499 | int offs = 6 * 2; // skip header |
787 | int offs = 6 * 2; // skip header |
500 | |
788 | |
501 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
789 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
502 | |
790 | |
503 | if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
791 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
504 | && pkt->qdcount == htons (1)) |
792 | && pkt.qdcount == htons (1)) |
505 | { |
793 | { |
506 | char qname[MAXSIZE]; |
794 | char qname[MAXSIZE]; |
507 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
795 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
508 | |
796 | |
509 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
797 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
510 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
798 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
511 | |
799 | |
512 | pkt->qdcount = htons (1); |
800 | pkt.qdcount = htons (1); |
513 | pkt->ancount = 0; |
801 | pkt.ancount = 0; |
514 | pkt->nscount = 0; // should be self, as other nameservers reply like this |
802 | 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 |
803 | pkt.arcount = 0; // a record for self, as other nameservers reply like this |
516 | |
804 | |
517 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
805 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL); |
518 | |
806 | |
519 | int dlen = strlen (THISNODE->domain); |
807 | int dlen = strlen (THISNODE->domain); |
520 | |
808 | |
521 | if (qclass == RR_CLASS_IN |
809 | if (qclass == RR_CLASS_IN |
522 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
|
|
523 | && qlen > dlen + 1 |
810 | && qlen > dlen + 1 |
524 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
811 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
525 | { |
812 | { |
526 | // correct class, domain: parse |
813 | // now generate reply |
527 | u8 data[MAXSIZE]; |
814 | pkt.ancount = htons (1); // one answer RR |
528 | int datalen = dns64::decode (data, qname, qlen - dlen - 1); |
815 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
529 | |
816 | |
530 | int client = data[0]; |
817 | if ((qtype == RR_TYPE_ANY |
531 | int seqno = ((data[1] << 8) | data[2]) & SEQNO_MASK; |
818 | || qtype == RR_TYPE_TXT |
532 | |
819 | || qtype == RR_TYPE_NULL) |
533 | if (0 < client && client <= conns.size ()) |
820 | && qlen > dlen + 1 + HDRSIZE) |
534 | { |
821 | { |
535 | connection *c = conns [client - 1]; |
822 | // correct class, domain: parse |
|
|
823 | int client, seqno; |
|
|
824 | decode_header (qname, client, seqno); |
536 | |
825 | |
537 | for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.begin (); |
826 | u8 data[MAXSIZE]; |
538 | i != c->dns_rcvpq.end (); |
827 | 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 | |
828 | |
545 | offs = r->pkt->len; |
829 | 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 | { |
830 | { |
|
|
831 | connection *c = conns [client - 1]; |
|
|
832 | dns_connection *dns = c->dns; |
|
|
833 | dns_rcv *rcv; |
|
|
834 | bool in_seq; |
|
|
835 | |
|
|
836 | if (dns) |
|
|
837 | { |
|
|
838 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
|
|
839 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
|
|
840 | { |
|
|
841 | // already seen that request: simply reply with the cached reply |
|
|
842 | dns_rcv *r = *i; |
|
|
843 | |
|
|
844 | slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno); |
|
|
845 | |
|
|
846 | // refresh header & id, as the retry count could have changed |
|
|
847 | memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE); |
|
|
848 | r->pkt->id = pkt.id; |
|
|
849 | |
|
|
850 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
|
|
851 | |
|
|
852 | goto duplicate_request; |
|
|
853 | } |
|
|
854 | |
|
|
855 | in_seq = dns->rcvseq == seqno; |
|
|
856 | |
|
|
857 | // new packet, queue |
|
|
858 | rcv = new dns_rcv (seqno, data, datalen); |
|
|
859 | dns->receive_rep (rcv); |
|
|
860 | } |
|
|
861 | |
|
|
862 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
|
|
863 | |
|
|
864 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
|
|
865 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
|
|
866 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
|
|
867 | pkt [offs++] = 0; pkt [offs++] = 0; |
|
|
868 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
|
|
869 | |
|
|
870 | int rdlen_offs = offs += 2; |
|
|
871 | |
|
|
872 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
|
|
873 | // bind doesn't compress well, so reduce further by one label length |
|
|
874 | dlen -= qlen; |
|
|
875 | |
|
|
876 | if (dns) |
|
|
877 | { |
|
|
878 | // only put data into in-order sequence packets, if |
|
|
879 | // we receive out-of-order packets we generate empty |
|
|
880 | // replies |
|
|
881 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
|
|
882 | { |
577 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
883 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
578 | |
884 | |
579 | if (txtlen > c->dns_snddq->size ()) |
885 | if (txtlen > dns->snddq.size ()) |
580 | txtlen = c->dns_snddq->size (); |
886 | txtlen = dns->snddq.size (); |
581 | |
887 | |
582 | (*pkt)[offs++] = txtlen; |
888 | pkt[offs++] = txtlen; |
583 | memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen); |
889 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
584 | offs += txtlen; |
890 | offs += txtlen; |
585 | c->dns_snddq->remove (txtlen); |
891 | dns->snddq.remove (txtlen); |
586 | |
892 | |
587 | dlen -= txtlen + 1; |
893 | dlen -= txtlen + 1; |
|
|
894 | } |
|
|
895 | |
|
|
896 | // avoid empty TXT rdata |
|
|
897 | if (offs == rdlen_offs) |
|
|
898 | pkt[offs++] = 0; |
|
|
899 | |
|
|
900 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
|
|
901 | } |
|
|
902 | else |
|
|
903 | { |
|
|
904 | // send RST |
|
|
905 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
|
|
906 | pkt [offs++] = CMD_IP_RST; |
|
|
907 | } |
|
|
908 | |
|
|
909 | int rdlen = offs - rdlen_offs; |
|
|
910 | |
|
|
911 | pkt [rdlen_offs - 2] = rdlen >> 8; |
|
|
912 | pkt [rdlen_offs - 1] = rdlen; |
|
|
913 | |
|
|
914 | if (dns) |
|
|
915 | { |
|
|
916 | // now update dns_rcv copy |
|
|
917 | rcv->pkt->len = offs; |
|
|
918 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
|
|
919 | } |
|
|
920 | |
|
|
921 | duplicate_request: ; |
588 | } |
922 | } |
589 | |
923 | else |
590 | // avoid empty TXT rdata |
924 | 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 | } |
925 | } |
601 | else |
926 | else if (qtype == RR_TYPE_A |
602 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
927 | && qlen > dlen + 1 + cdc26.encode_len (sizeof (dns_cfg))) |
|
|
928 | { |
|
|
929 | dns_cfg cfg; |
|
|
930 | cdc26.decode ((u8 *)&cfg, qname, cdc26.encode_len (sizeof (dns_cfg))); |
|
|
931 | int client = ntohs (cfg.client); |
|
|
932 | |
|
|
933 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
|
|
934 | |
|
|
935 | pkt [offs++] = RR_TYPE_A >> 8; pkt [offs++] = RR_TYPE_A; // type |
|
|
936 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
|
|
937 | pkt [offs++] = 0; pkt [offs++] = 0; |
|
|
938 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
|
|
939 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
|
|
940 | |
|
|
941 | slog (L_INFO, _("DNS: client %d tries to connect"), client); |
|
|
942 | |
|
|
943 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
|
|
944 | pkt [offs++] = CMD_IP_REJ; |
|
|
945 | |
|
|
946 | if (0 < client && client <= conns.size ()) |
|
|
947 | { |
|
|
948 | connection *c = conns [client - 1]; |
|
|
949 | |
|
|
950 | if (cfg.valid ()) |
|
|
951 | { |
|
|
952 | pkt [offs - 1] = CMD_IP_SYN; |
|
|
953 | |
|
|
954 | delete c->dns; |
|
|
955 | c->dns = new dns_connection (c); |
|
|
956 | c->dns->cfg = cfg; |
|
|
957 | } |
|
|
958 | } |
|
|
959 | } |
603 | } |
960 | } |
604 | } |
|
|
605 | else |
|
|
606 | offs = pkt->len; |
|
|
607 | |
961 | |
608 | pkt->len = offs; |
962 | pkt.len = offs; |
609 | return pkt; |
963 | } |
610 | } |
964 | } |
611 | |
965 | |
612 | void |
966 | void |
613 | vpn::dnsv4_client (dns_packet *pkt) |
967 | vpn::dnsv4_client (dns_packet &pkt) |
614 | { |
968 | { |
615 | u16 flags = ntohs (pkt->flags); |
969 | u16 flags = ntohs (pkt.flags); |
616 | int offs = 6 * 2; // skip header |
970 | int offs = 6 * 2; // skip header |
617 | |
971 | |
618 | pkt->qdcount = ntohs (pkt->qdcount); |
972 | pkt.qdcount = ntohs (pkt.qdcount); |
619 | pkt->ancount = ntohs (pkt->ancount); |
973 | pkt.ancount = ntohs (pkt.ancount); |
620 | |
974 | |
621 | // go through our request list and find the corresponding request |
975 | // go through our request list and find the corresponding request |
622 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
976 | for (vector<dns_snd *>::iterator i = dns_sndpq.begin (); |
623 | i != dns_sndpq.end (); |
977 | i != dns_sndpq.end (); |
624 | ++i) |
978 | ++i) |
625 | if ((*i)->pkt->id == pkt->id) |
979 | if ((*i)->pkt->id == pkt.id) |
626 | { |
980 | { |
|
|
981 | dns_connection *dns = (*i)->dns; |
627 | connection *c = (*i)->conn; |
982 | connection *c = dns->c; |
628 | int seqno = (*i)->seqno; |
983 | int seqno = (*i)->seqno; |
629 | u8 data[MAXSIZE], *datap = data; |
984 | u8 data[MAXSIZE], *datap = data; |
630 | |
985 | |
|
|
986 | if ((*i)->retry) |
|
|
987 | { |
|
|
988 | dns->send_interval *= 1.01; |
|
|
989 | if (dns->send_interval > MAX_SEND_INTERVAL) |
|
|
990 | dns->send_interval = MAX_SEND_INTERVAL; |
|
|
991 | } |
|
|
992 | else |
|
|
993 | { |
|
|
994 | #if 1 |
|
|
995 | dns->send_interval *= 0.999; |
|
|
996 | #endif |
|
|
997 | if (dns->send_interval < MIN_SEND_INTERVAL) |
|
|
998 | dns->send_interval = MIN_SEND_INTERVAL; |
|
|
999 | |
|
|
1000 | // the latency surely puts an upper bound on |
|
|
1001 | // the minimum send interval |
|
|
1002 | double latency = NOW - (*i)->sent; |
|
|
1003 | dns->last_latency = latency; |
|
|
1004 | |
|
|
1005 | if (dns->send_interval > latency) |
|
|
1006 | dns->send_interval = latency; |
|
|
1007 | } |
|
|
1008 | |
631 | delete *i; |
1009 | delete *i; |
632 | dns_sndpq.erase (i); |
1010 | dns_sndpq.erase (i); |
633 | |
1011 | |
634 | if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
1012 | if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK)) |
635 | { |
1013 | { |
636 | char qname[MAXSIZE]; |
1014 | char qname[MAXSIZE]; |
637 | |
1015 | |
638 | while (pkt->qdcount-- && offs < MAXSIZE - 4) |
1016 | while (pkt.qdcount-- && offs < MAXSIZE - 4) |
639 | { |
1017 | { |
640 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
1018 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
641 | offs += 4; // skip qtype, qclass |
1019 | offs += 4; // skip qtype, qclass |
642 | } |
1020 | } |
643 | |
1021 | |
644 | while (pkt->ancount-- && offs < MAXSIZE - 10) |
1022 | while (pkt.ancount-- && offs < MAXSIZE - 10 && datap) |
645 | { |
1023 | { |
646 | pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
1024 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
647 | |
1025 | |
648 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
1026 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
649 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
1027 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
650 | u32 ttl = (*pkt) [offs++] << 24; |
1028 | u32 ttl = pkt [offs++] << 24; |
651 | ttl |= (*pkt) [offs++] << 16; |
1029 | ttl |= pkt [offs++] << 16; |
652 | ttl |= (*pkt) [offs++] << 8; |
1030 | ttl |= pkt [offs++] << 8; |
653 | ttl |= (*pkt) [offs++]; |
1031 | ttl |= pkt [offs++]; |
654 | |
|
|
655 | u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
1032 | u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++]; |
656 | |
1033 | |
657 | if (rdlen <= MAXSIZE - offs) |
1034 | if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT) |
658 | { |
1035 | { |
659 | // decode bytes, finally |
1036 | if (rdlen <= MAXSIZE - offs) |
660 | |
|
|
661 | while (rdlen) |
|
|
662 | { |
1037 | { |
|
|
1038 | // decode bytes, finally |
|
|
1039 | |
|
|
1040 | while (rdlen) |
|
|
1041 | { |
663 | int txtlen = (*pkt) [offs++]; |
1042 | int txtlen = pkt [offs++]; |
664 | |
1043 | |
665 | assert (txtlen + offs < MAXSIZE - 1); |
1044 | assert (txtlen + offs < MAXSIZE - 1); |
666 | |
1045 | |
667 | memcpy (datap, pkt->at (offs), txtlen); |
1046 | memcpy (datap, pkt.at (offs), txtlen); |
668 | datap += txtlen; offs += txtlen; |
1047 | datap += txtlen; offs += txtlen; |
669 | |
1048 | |
670 | rdlen -= txtlen + 1; |
1049 | rdlen -= txtlen + 1; |
|
|
1050 | } |
671 | } |
1051 | } |
672 | |
|
|
673 | } |
1052 | } |
|
|
1053 | else if (qtype == RR_TYPE_A) |
|
|
1054 | { |
|
|
1055 | u8 ip [4]; |
674 | |
1056 | |
|
|
1057 | ip [0] = pkt [offs++]; |
|
|
1058 | ip [1] = pkt [offs++]; |
|
|
1059 | ip [2] = pkt [offs++]; |
|
|
1060 | ip [3] = pkt [offs++]; |
|
|
1061 | |
|
|
1062 | if (ip [0] == CMD_IP_1 |
|
|
1063 | && ip [1] == CMD_IP_2 |
|
|
1064 | && ip [2] == CMD_IP_3) |
|
|
1065 | { |
|
|
1066 | slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]); |
|
|
1067 | |
|
|
1068 | if (ip [3] == CMD_IP_RST) |
|
|
1069 | { |
|
|
1070 | slog (L_DEBUG, _("DNS: got tunnel RST request")); |
|
|
1071 | |
|
|
1072 | delete dns; c->dns = 0; |
|
|
1073 | |
|
|
1074 | return; |
|
|
1075 | } |
|
|
1076 | else if (ip [3] == CMD_IP_SYN) |
|
|
1077 | { |
|
|
1078 | slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); |
|
|
1079 | dns->established = true; |
|
|
1080 | } |
|
|
1081 | else if (ip [3] == CMD_IP_REJ) |
|
|
1082 | { |
|
|
1083 | slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting.")); |
|
|
1084 | abort (); |
|
|
1085 | } |
|
|
1086 | else |
|
|
1087 | slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); |
|
|
1088 | } |
|
|
1089 | else |
|
|
1090 | slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), |
|
|
1091 | ip [0], ip [1], ip [2], ip [3]); |
|
|
1092 | |
|
|
1093 | return; |
|
|
1094 | } |
|
|
1095 | |
|
|
1096 | int client, rseqno; |
|
|
1097 | decode_header (qname, client, rseqno); |
|
|
1098 | |
|
|
1099 | if (client != THISNODE->id) |
|
|
1100 | { |
|
|
1101 | slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring")); |
|
|
1102 | datap = 0; |
|
|
1103 | } |
|
|
1104 | else if (rseqno != seqno) |
|
|
1105 | { |
|
|
1106 | slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?")); |
|
|
1107 | datap = 0; |
|
|
1108 | } |
675 | } |
1109 | } |
676 | } |
1110 | } |
677 | |
1111 | |
|
|
1112 | // todo: pkt now used |
678 | if (datap != data) |
1113 | 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)); |
1114 | dns->receive_rep (new dns_rcv (seqno, data, datap - data)); |
687 | |
1115 | |
688 | break; |
1116 | break; |
689 | } |
1117 | } |
690 | |
|
|
691 | delete pkt; |
|
|
692 | } |
1118 | } |
693 | |
1119 | |
694 | void |
1120 | void |
695 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1121 | vpn::dnsv4_ev (io_watcher &w, short revents) |
696 | { |
1122 | { |
… | |
… | |
698 | { |
1124 | { |
699 | dns_packet *pkt = new dns_packet; |
1125 | dns_packet *pkt = new dns_packet; |
700 | struct sockaddr_in sa; |
1126 | struct sockaddr_in sa; |
701 | socklen_t sa_len = sizeof (sa); |
1127 | socklen_t sa_len = sizeof (sa); |
702 | |
1128 | |
703 | pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1129 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
704 | |
1130 | |
705 | if (pkt->len > 0) |
1131 | if (pkt->len > 0) |
706 | { |
1132 | { |
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) |
1133 | if (THISNODE->dns_port) |
714 | { |
1134 | { |
715 | pkt = dnsv4_server (pkt); |
1135 | dnsv4_server (*pkt); |
716 | sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1136 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
717 | } |
1137 | } |
718 | else |
1138 | else |
719 | dnsv4_client (pkt); |
1139 | dnsv4_client (*pkt); |
|
|
1140 | |
|
|
1141 | delete pkt; |
720 | } |
1142 | } |
721 | } |
1143 | } |
722 | } |
1144 | } |
723 | |
1145 | |
724 | bool |
1146 | bool |
725 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1147 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
726 | { |
1148 | { |
727 | // never initialized |
1149 | if (!dns) |
728 | if (!dns_snddq && !dns_rcvdq) |
1150 | dns = new dns_connection (this); |
729 | { |
|
|
730 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
|
|
731 | dns_snddq = new byte_stream (MAX_BACKLOG); |
|
|
732 | |
|
|
733 | //dns_rcvseq = dns_sndseq = 0; |
|
|
734 | |
|
|
735 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
|
|
736 | } |
|
|
737 | |
1151 | |
738 | if (!dns_snddq->put (pkt)) |
1152 | if (!dns->snddq.put (pkt)) |
739 | return false; |
1153 | return false; |
740 | |
1154 | |
741 | // start timer if neccessary |
1155 | dns->tw.trigger (); |
742 | if (!THISNODE->dns_port && !dnsv4_tw.active) |
|
|
743 | dnsv4_cb (dnsv4_tw); |
|
|
744 | |
1156 | |
745 | return true; |
1157 | return true; |
746 | } |
1158 | } |
747 | |
1159 | |
748 | void |
1160 | void |
|
|
1161 | connection::dnsv4_reset_connection () |
|
|
1162 | { |
|
|
1163 | //delete dns; dns = 0; //TODO |
|
|
1164 | } |
|
|
1165 | |
|
|
1166 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
|
|
1167 | |
|
|
1168 | void |
749 | connection::dnsv4_cb (time_watcher &w) |
1169 | dns_connection::time_cb (time_watcher &w) |
750 | { |
1170 | { |
|
|
1171 | // servers have to be polled |
|
|
1172 | if (THISNODE->dns_port) |
|
|
1173 | return; |
|
|
1174 | |
751 | // check for timeouts and (re)transmit |
1175 | // check for timeouts and (re)transmit |
752 | tstamp next = NOW + 60; |
1176 | tstamp next = NOW + poll_interval; |
753 | dns_req *send = 0; |
1177 | dns_snd *send = 0; |
754 | |
1178 | |
755 | for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); |
1179 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
756 | i != vpn->dns_sndpq.end (); |
1180 | i != vpn->dns_sndpq.end (); |
757 | ++i) |
1181 | ++i) |
758 | { |
1182 | { |
759 | dns_req *r = *i; |
1183 | dns_snd *r = *i; |
760 | |
1184 | |
761 | if (r->next <= NOW) |
1185 | if (r->timeout <= NOW) |
762 | { |
1186 | { |
763 | if (!send) |
1187 | if (!send) |
764 | { |
1188 | { |
765 | send = r; |
1189 | send = r; |
766 | |
1190 | |
767 | if (r->retry)//D |
|
|
768 | printf ("req %d, retry %d\n", r->pkt->id, r->retry); |
|
|
769 | r->retry++; |
1191 | r->retry++; |
770 | r->next = NOW + r->retry; |
1192 | r->timeout = NOW + (r->retry * last_latency * 8.); |
|
|
1193 | |
|
|
1194 | // the following code changes the query section a bit, forcing |
|
|
1195 | // the forwarder to generate a new request |
|
|
1196 | if (r->stdhdr) |
|
|
1197 | { |
|
|
1198 | //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); |
|
|
1199 | //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
|
|
1200 | } |
771 | } |
1201 | } |
772 | } |
1202 | } |
|
|
1203 | else |
|
|
1204 | NEXT (r->timeout); |
|
|
1205 | } |
773 | |
1206 | |
774 | if (r->next < next) |
1207 | if (last_sent + send_interval <= NOW) |
775 | next = r->next; |
|
|
776 | } |
1208 | { |
777 | |
|
|
778 | if (!send |
1209 | if (!send) |
|
|
1210 | { |
|
|
1211 | // generate a new packet, if wise |
|
|
1212 | |
|
|
1213 | if (!established) |
|
|
1214 | { |
|
|
1215 | if (vpn->dns_sndpq.empty ()) |
|
|
1216 | { |
|
|
1217 | send = new dns_snd (this); |
|
|
1218 | |
|
|
1219 | cfg.reset (THISNODE->id); |
|
|
1220 | send->gen_syn_req (cfg); |
|
|
1221 | } |
|
|
1222 | } |
779 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
1223 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
780 | { |
1224 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
|
|
1225 | { |
|
|
1226 | if (!snddq.empty ()) |
|
|
1227 | { |
|
|
1228 | poll_interval = send_interval; |
|
|
1229 | NEXT (NOW + send_interval); |
|
|
1230 | } |
|
|
1231 | |
781 | send = new dns_req (this); |
1232 | send = new dns_snd (this); |
782 | send->gen_stream_req (dns_sndseq, dns_snddq); |
1233 | send->gen_stream_req (sndseq, snddq); |
|
|
1234 | send->timeout = NOW + last_latency * 8.; |
|
|
1235 | |
|
|
1236 | sndseq = (sndseq + 1) & SEQNO_MASK; |
|
|
1237 | } |
|
|
1238 | |
|
|
1239 | if (send) |
783 | vpn->dns_sndpq.push_back (send); |
1240 | vpn->dns_sndpq.push_back (send); |
|
|
1241 | } |
784 | |
1242 | |
785 | dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK; |
|
|
786 | } |
|
|
787 | |
|
|
788 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
|
|
789 | |
|
|
790 | if (send) |
1243 | if (send) |
|
|
1244 | { |
|
|
1245 | last_sent = NOW; |
|
|
1246 | sendto (vpn->dnsv4_fd, |
|
|
1247 | send->pkt->at (0), send->pkt->len, 0, |
|
|
1248 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
|
|
1249 | } |
791 | { |
1250 | } |
792 | dns_packet *pkt = send->pkt; |
1251 | else |
|
|
1252 | NEXT (last_sent + send_interval); |
793 | |
1253 | |
794 | next = min_next; |
1254 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)", |
|
|
1255 | poll_interval, send_interval, next - NOW, |
|
|
1256 | vpn->dns_sndpq.size (), snddq.size ()); |
795 | |
1257 | |
796 | sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ()); |
1258 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
797 | } |
1259 | if (next < NOW + 0.0001) |
798 | else if (next < min_next) |
1260 | next = NOW + 0.1; |
799 | next = min_next; |
|
|
800 | |
1261 | |
801 | w.start (next); |
1262 | w.start (next); |
802 | } |
1263 | } |
803 | |
1264 | |
804 | #endif |
1265 | #endif |