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