ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_dns.C
Revision: 1.17
Committed: Sat Mar 5 03:47:05 2005 UTC (19 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.16: +73 -32 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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.1 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20     */
21    
22     #include "config.h"
23    
24     #if ENABLE_DNS
25    
26     // dns processing is EXTREMELY ugly. For obvious(?) reasons.
27     // it's a hack, use only in emergency situations please.
28    
29     #include <cstring>
30    
31     #include <sys/types.h>
32     #include <sys/socket.h>
33     #include <sys/wait.h>
34     #include <sys/uio.h>
35     #include <errno.h>
36     #include <time.h>
37     #include <unistd.h>
38     #include <fcntl.h>
39    
40     #include <map>
41    
42 pcg 1.8 #include <gmp.h>
43    
44 pcg 1.1 #include "netcompat.h"
45    
46     #include "vpn.h"
47    
48 pcg 1.15 #define MIN_POLL_INTERVAL .02 // how often to poll minimally when the server has data
49 pcg 1.10 #define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data
50     #define ACTIVITY_INTERVAL 5.
51    
52 pcg 1.17 #define INITIAL_TIMEOUT 0.1 // retry timeouts
53     #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn
54 pcg 1.10
55 pcg 1.15 #define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests
56 pcg 1.10 #define MAX_SEND_INTERVAL 0.5 // optimistic?
57 pcg 1.1
58 pcg 1.17 #define MAX_OUTSTANDING 10 // max. outstanding requests
59     #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog
60 pcg 1.15 #define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE
61 pcg 1.5
62     #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well
63     // 240 leaves about 4 bytes of server reply data
64 pcg 1.15 // every two request bytes less give room for one reply byte
65 pcg 1.5
66 pcg 1.17 #define SEQNO_MASK 0x3fff
67 pcg 1.8 #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) )
68 pcg 1.1
69 pcg 1.8 #define MAX_LBL_SIZE 63
70     #define MAX_PKT_SIZE 512
71 pcg 1.1
72 pcg 1.10 #define RR_TYPE_A 1
73     #define RR_TYPE_NULL 10
74     #define RR_TYPE_TXT 16
75 pcg 1.8 #define RR_TYPE_ANY 255
76 pcg 1.10
77     #define RR_CLASS_IN 1
78    
79     #define CMD_IP_1 207
80     #define CMD_IP_2 46
81     #define CMD_IP_3 236
82     #define CMD_IP_RST 29
83     #define CMD_IP_SYN 113
84     #define CMD_IP_REJ 32
85 pcg 1.1
86 pcg 1.8 // works for cmaps up to 255 (not 256!)
87     struct charmap
88     {
89     enum { INVALID = (u8)255 };
90 pcg 1.1
91 pcg 1.8 char encode [256]; // index => char
92     u8 decode [256]; // char => index
93     unsigned int size;
94 pcg 1.2
95 pcg 1.8 charmap (const char *cmap);
96     };
97    
98     charmap::charmap (const char *cmap)
99     {
100     char *enc = encode;
101     u8 *dec = decode;
102 pcg 1.2
103 pcg 1.8 memset (enc, (char) 0, 256);
104     memset (dec, (char)INVALID, 256);
105 pcg 1.1
106 pcg 1.8 for (size = 0; cmap [size]; size++)
107     {
108     enc [size] = cmap [size];
109     dec [(u8)enc [size]] = size;
110     }
111 pcg 1.1
112 pcg 1.8 assert (size < 256);
113     }
114 pcg 1.2
115 pcg 1.8 #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 pcg 1.2
119 pcg 1.8 // ugly. minimum base is 16(!)
120     struct basecoder
121 pcg 1.2 {
122 pcg 1.8 charmap cmap;
123     unsigned int enc_len [MAX_DEC_LEN];
124     unsigned int dec_len [MAX_ENC_LEN];
125 pcg 1.2
126 pcg 1.8 unsigned int encode_len (unsigned int len);
127     unsigned int decode_len (unsigned int len);
128    
129     unsigned int encode (char *dst, u8 *src, unsigned int len);
130     unsigned int decode (u8 *dst, char *src, unsigned int len);
131    
132     basecoder (const char *cmap);
133     };
134    
135     basecoder::basecoder (const char *cmap)
136     : cmap (cmap)
137     {
138     for (unsigned int len = 0; len < MAX_DEC_LEN; ++len)
139     {
140     u8 src [MAX_DEC_LEN];
141     u8 dst [MAX_ENC_LEN];
142 pcg 1.2
143 pcg 1.8 memset (src, 255, len);
144 pcg 1.2
145 pcg 1.8 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)
160     {
161     return enc_len [len];
162     }
163 pcg 1.2
164 pcg 1.8 unsigned int basecoder::decode_len (unsigned int len)
165 pcg 1.2 {
166 pcg 1.8 while (len && !dec_len [len])
167     --len;
168    
169     return dec_len [len];
170 pcg 1.2 }
171    
172 pcg 1.8 unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len)
173 pcg 1.2 {
174 pcg 1.10 if (!len || len > MAX_DEC_LEN)
175 pcg 1.8 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 pcg 1.2
189 pcg 1.8 while (n < plen)
190 pcg 1.2 {
191 pcg 1.8 *dst++ = cmap.encode [0];
192     plen--;
193 pcg 1.2 }
194    
195 pcg 1.8 for (unsigned int i = n - plen; i < n; ++i)
196     *dst++ = cmap.encode [dst_ [i]];
197 pcg 1.4
198 pcg 1.8 return elen;
199 pcg 1.2 }
200    
201 pcg 1.8 unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len)
202 pcg 1.2 {
203 pcg 1.10 if (!len || len > MAX_ENC_LEN)
204 pcg 1.8 return 0;
205    
206     u8 src_ [MAX_ENC_LEN];
207     unsigned int elen = 0;
208 pcg 1.2
209     while (len--)
210     {
211 pcg 1.8 u8 val = cmap.decode [(u8)*src++];
212 pcg 1.2
213 pcg 1.8 if (val != charmap::INVALID)
214     src_ [elen++] = val;
215     }
216    
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 pcg 1.2
224 pcg 1.8 n = mpn_set_str (m, src_, elen, cmap.size);
225     n = mpn_get_str (dst_, 256, m, n);
226 pcg 1.2
227 pcg 1.8 if (n < dlen)
228     {
229     memset (dst, 0, dlen - n);
230     memcpy (dst + dlen - n, dst_, n);
231 pcg 1.2 }
232 pcg 1.8 else
233     memcpy (dst, dst_ + n - dlen, dlen);
234 pcg 1.4
235 pcg 1.8 return dlen;
236     }
237    
238     #if 0
239     struct test { test (); } test;
240    
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++)
250     {
251     int elen = cdc.encode (enc, in, i);
252     int dlen = cdc.decode (dec, enc, elen);
253    
254     printf ("%d>%d>%d (%s>%s)\n", i, elen, dlen, enc, dec);
255     }
256     abort ();
257     }
258     #endif
259    
260 pcg 1.10 //static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-");
261 pcg 1.8 //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 pcg 1.17 inline void encode_header (char *data, int clientid, int seqno, int retry = 0)
271 pcg 1.8 {
272 pcg 1.17 seqno &= SEQNO_MASK;
273    
274     u8 hdr[3] = {
275     clientid,
276     (seqno >> 8) | (retry << 6),
277     seqno,
278     };
279 pcg 1.8
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 pcg 1.17 seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK;
293 pcg 1.4 }
294    
295     /////////////////////////////////////////////////////////////////////////////
296    
297     struct byte_stream
298     {
299     u8 *data;
300     int maxsize;
301     int fill;
302    
303     byte_stream (int maxsize);
304     ~byte_stream ();
305    
306     bool empty () { return !fill; }
307     int size () { return fill; }
308    
309 pcg 1.5 bool put (u8 *data, unsigned int datalen);
310 pcg 1.4 bool put (vpn_packet *pkt);
311     vpn_packet *get ();
312    
313     u8 *begin () { return data; }
314     void remove (int count);
315     };
316    
317     byte_stream::byte_stream (int maxsize)
318     : maxsize (maxsize), fill (0)
319     {
320     data = new u8 [maxsize];
321     }
322    
323     byte_stream::~byte_stream ()
324     {
325     delete data;
326     }
327    
328     void byte_stream::remove (int count)
329     {
330     if (count > fill)
331 pcg 1.17 assert (count <= fill);
332 pcg 1.4
333     memmove (data, data + count, fill -= count);
334     }
335    
336 pcg 1.5 bool byte_stream::put (u8 *data, unsigned int datalen)
337     {
338     if (maxsize - fill < datalen)
339     return false;
340    
341     memcpy (this->data + fill, data, datalen); fill += datalen;
342    
343     return true;
344     }
345    
346 pcg 1.4 bool byte_stream::put (vpn_packet *pkt)
347     {
348     if (maxsize - fill < pkt->len + 2)
349     return false;
350    
351     data [fill++] = pkt->len >> 8;
352     data [fill++] = pkt->len;
353    
354 pcg 1.10 memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len;
355 pcg 1.4
356     return true;
357     }
358    
359     vpn_packet *byte_stream::get ()
360     {
361 pcg 1.12 unsigned int len = (data [0] << 8) | data [1];
362 pcg 1.4
363 pcg 1.5 if (len > MAXSIZE && fill >= 2)
364 pcg 1.17 assert (len <= MAXSIZE || fill < 2); // TODO handle this gracefully, connection reset
365 pcg 1.5
366 pcg 1.4 if (fill < len + 2)
367     return 0;
368    
369     vpn_packet *pkt = new vpn_packet;
370 pcg 1.5
371     pkt->len = len;
372 pcg 1.10 memcpy (pkt->at (0), data + 2, len);
373 pcg 1.4 remove (len + 2);
374    
375     return pkt;
376 pcg 1.2 }
377    
378 pcg 1.3 /////////////////////////////////////////////////////////////////////////////
379    
380 pcg 1.2 #define FLAG_QUERY ( 0 << 15)
381     #define FLAG_RESPONSE ( 1 << 15)
382 pcg 1.9 #define FLAG_OP_MASK (15 << 11)
383 pcg 1.2 #define FLAG_OP_QUERY ( 0 << 11)
384     #define FLAG_AA ( 1 << 10)
385     #define FLAG_TC ( 1 << 9)
386     #define FLAG_RD ( 1 << 8)
387     #define FLAG_RA ( 1 << 7)
388 pcg 1.5 #define FLAG_AUTH ( 1 << 5)
389 pcg 1.2 #define FLAG_RCODE_MASK (15 << 0)
390     #define FLAG_RCODE_OK ( 0 << 0)
391     #define FLAG_RCODE_FORMERR ( 1 << 0)
392     #define FLAG_RCODE_SERVFAIL ( 2 << 0)
393     #define FLAG_RCODE_NXDOMAIN ( 3 << 0)
394     #define FLAG_RCODE_REFUSED ( 5 << 0)
395    
396     #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD)
397 pcg 1.5 #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA)
398 pcg 1.2
399 pcg 1.10 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 pcg 1.17 def_ttl = 1;
434 pcg 1.10 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    
456 pcg 1.2 struct dns_packet : net_packet
457     {
458     u16 id;
459     u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4
460     u16 qdcount, ancount, nscount, arcount;
461    
462     u8 data[MAXSIZE - 6 * 2];
463    
464     int decode_label (char *data, int size, int &offs);
465     };
466    
467     int dns_packet::decode_label (char *data, int size, int &offs)
468     {
469     char *orig = data;
470    
471     memset (data, 0, size);
472    
473     while (offs < size - 1)
474     {
475     u8 len = (*this)[offs++];
476    
477     if (!len)
478     break;
479     else if (len < 64)
480     {
481     if (size < len + 1 || offs + len >= MAXSIZE - 1)
482     break;
483    
484     memcpy (data, &((*this)[offs]), len);
485    
486     data += len; size -= len; offs += len;
487     *data++ = '.'; size--;
488     }
489     else
490     {
491     int offs2 = ((len & 63) << 8) + (*this)[offs++];
492    
493     data += decode_label (data, size, offs2);
494     break;
495     }
496     }
497    
498     return data - orig;
499     }
500    
501 pcg 1.3 /////////////////////////////////////////////////////////////////////////////
502    
503 pcg 1.10 struct dns_snd
504 pcg 1.3 {
505     dns_packet *pkt;
506 pcg 1.10 tstamp timeout, sent;
507 pcg 1.3 int retry;
508 pcg 1.9 struct dns_connection *dns;
509 pcg 1.5 int seqno;
510 pcg 1.17 bool stdhdr;
511 pcg 1.3
512 pcg 1.9 void gen_stream_req (int seqno, byte_stream &stream);
513 pcg 1.10 void gen_syn_req (const dns_cfg &cfg);
514 pcg 1.11
515     dns_snd (dns_connection *dns);
516     ~dns_snd ();
517 pcg 1.3 };
518    
519 pcg 1.5 static u16 dns_id = 12098; // TODO: should be per-vpn
520    
521     static u16 next_id ()
522 pcg 1.3 {
523 pcg 1.5 // the simplest lsfr with periodicity 65535 i could find
524     dns_id = (dns_id << 1)
525     | (((dns_id >> 1)
526     ^ (dns_id >> 2)
527     ^ (dns_id >> 4)
528     ^ (dns_id >> 15)) & 1);
529 pcg 1.3
530 pcg 1.5 return dns_id;
531     }
532 pcg 1.3
533 pcg 1.10 dns_snd::dns_snd (dns_connection *dns)
534 pcg 1.9 : dns (dns)
535 pcg 1.3 {
536 pcg 1.10 timeout = 0;
537 pcg 1.3 retry = 0;
538 pcg 1.10 seqno = 0;
539 pcg 1.11 sent = NOW;
540 pcg 1.17 stdhdr = false;
541 pcg 1.3
542     pkt = new dns_packet;
543    
544 pcg 1.5 pkt->id = next_id ();
545     }
546    
547 pcg 1.11 dns_snd::~dns_snd ()
548     {
549     delete pkt;
550     }
551    
552 pcg 1.10 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    
575     void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
576 pcg 1.5 {
577 pcg 1.17 stdhdr = true;
578 pcg 1.5 this->seqno = seqno;
579    
580 pcg 1.10 timeout = NOW + INITIAL_TIMEOUT;
581    
582 pcg 1.5 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
583 pcg 1.4 pkt->qdcount = htons (1);
584 pcg 1.3
585 pcg 1.4 int offs = 6*2;
586 pcg 1.8 int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2);
587 pcg 1.5 // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well,
588     // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra
589    
590 pcg 1.8 char enc[256], *encp = enc;
591     encode_header (enc, THISNODE->id, seqno);
592 pcg 1.4
593 pcg 1.8 int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE);
594 pcg 1.4
595 pcg 1.9 if (datalen > stream.size ())
596     datalen = stream.size ();
597 pcg 1.8
598 pcg 1.9 int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE;
599     stream.remove (datalen);
600 pcg 1.4
601 pcg 1.5 while (enclen)
602     {
603     int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE;
604    
605     (*pkt)[offs++] = lbllen;
606     memcpy (pkt->at (offs), encp, lbllen);
607 pcg 1.4
608 pcg 1.5 offs += lbllen;
609     encp += lbllen;
610 pcg 1.4
611 pcg 1.5 enclen -= lbllen;
612 pcg 1.4 }
613    
614 pcg 1.10 append_domain (*pkt, offs, THISNODE->domain);
615 pcg 1.3
616 pcg 1.10 (*pkt)[offs++] = 0;
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 pcg 1.4
627 pcg 1.10 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
628     pkt->qdcount = htons (1);
629 pcg 1.4
630 pcg 1.10 int offs = 6*2;
631 pcg 1.4
632 pcg 1.10 int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg));
633 pcg 1.4
634 pcg 1.10 assert (elen <= MAX_LBL_SIZE);
635 pcg 1.4
636 pcg 1.10 (*pkt)[offs] = elen;
637     offs += elen + 1;
638     append_domain (*pkt, offs, THISNODE->domain);
639 pcg 1.4
640     (*pkt)[offs++] = 0;
641 pcg 1.10 (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A;
642 pcg 1.4 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
643    
644     pkt->len = offs;
645 pcg 1.5 }
646    
647     struct dns_rcv
648     {
649     int seqno;
650     dns_packet *pkt; // reply packet
651     u8 data [MAXSIZE]; // actually part of the reply packet...
652     int datalen;
653    
654 pcg 1.6 dns_rcv (int seqno, u8 *data, int datalen);
655 pcg 1.5 ~dns_rcv ();
656     };
657    
658 pcg 1.6 dns_rcv::dns_rcv (int seqno, u8 *data, int datalen)
659 pcg 1.5 : seqno (seqno), pkt (new dns_packet), datalen (datalen)
660     {
661     memcpy (this->data, data, datalen);
662     }
663 pcg 1.4
664 pcg 1.5 dns_rcv::~dns_rcv ()
665     {
666     delete pkt;
667 pcg 1.3 }
668    
669     /////////////////////////////////////////////////////////////////////////////
670 pcg 1.9
671     struct dns_connection
672     {
673     connection *c;
674     struct vpn *vpn;
675    
676 pcg 1.10 dns_cfg cfg;
677    
678     bool established;
679    
680     tstamp last_received;
681     tstamp last_sent;
682 pcg 1.17 double last_latency;
683 pcg 1.10 double poll_interval, send_interval;
684    
685 pcg 1.9 vector<dns_rcv *> rcvpq;
686    
687 pcg 1.10 byte_stream rcvdq; int rcvseq;
688     byte_stream snddq; int sndseq;
689 pcg 1.9
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 ();
695     };
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 pcg 1.10 established = false;
706    
707 pcg 1.9 rcvseq = sndseq = 0;
708 pcg 1.10
709     last_sent = last_received = 0;
710     poll_interval = MIN_POLL_INTERVAL;
711 pcg 1.17 send_interval = 0.5; // starting rate
712     last_latency = INITIAL_TIMEOUT;
713 pcg 1.9 }
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 pcg 1.3
723 pcg 1.10 void dns_connection::receive_rep (dns_rcv *r)
724 pcg 1.2 {
725 pcg 1.10 if (r->datalen)
726     {
727     last_received = NOW;
728     tw.trigger ();
729    
730 pcg 1.12 poll_interval = send_interval;
731 pcg 1.10 }
732     else
733     {
734 pcg 1.17 poll_interval *= 1.5;
735 pcg 1.10 if (poll_interval > MAX_POLL_INTERVAL)
736     poll_interval = MAX_POLL_INTERVAL;
737     }
738 pcg 1.2
739 pcg 1.9 rcvpq.push_back (r);
740 pcg 1.5
741     redo:
742    
743 pcg 1.8 // find next packet
744 pcg 1.9 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); )
745     if (SEQNO_EQ (rcvseq, (*i)->seqno))
746 pcg 1.5 {
747 pcg 1.8 // enter the packet into our input stream
748     r = *i;
749    
750     // remove the oldest packet, look forward, as it's oldest first
751 pcg 1.9 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j)
752     if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW))
753 pcg 1.8 {
754     delete *j;
755 pcg 1.9 rcvpq.erase (j);
756 pcg 1.8 break;
757     }
758 pcg 1.5
759 pcg 1.9 rcvseq = (rcvseq + 1) & SEQNO_MASK;
760 pcg 1.5
761 pcg 1.9 if (!rcvdq.put (r->data, r->datalen))
762 pcg 1.17 {
763     slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)");
764     abort (); // MUST never overflow, can be caused by data corruption, TODO
765     }
766 pcg 1.5
767 pcg 1.9 while (vpn_packet *pkt = rcvdq.get ())
768 pcg 1.5 {
769     sockinfo si;
770     si.host = 0; si.port = 0; si.prot = PROT_DNSv4;
771    
772     vpn->recv_vpn_packet (pkt, si);
773 pcg 1.11
774     delete pkt;
775 pcg 1.5 }
776 pcg 1.8
777     // check for further packets
778 pcg 1.5 goto redo;
779     }
780     }
781    
782 pcg 1.10 void
783     vpn::dnsv4_server (dns_packet &pkt)
784 pcg 1.2 {
785 pcg 1.10 u16 flags = ntohs (pkt.flags);
786 pcg 1.2
787 pcg 1.4 int offs = 6 * 2; // skip header
788 pcg 1.2
789 pcg 1.10 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
790 pcg 1.2
791 pcg 1.10 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK))
792     && pkt.qdcount == htons (1))
793 pcg 1.4 {
794     char qname[MAXSIZE];
795 pcg 1.10 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
796 pcg 1.4
797 pcg 1.10 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
798     u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
799 pcg 1.2
800 pcg 1.10 pkt.qdcount = htons (1);
801     pkt.ancount = 0;
802     pkt.nscount = 0; // should be self, as other nameservers reply like this
803     pkt.arcount = 0; // a record for self, as other nameservers reply like this
804 pcg 1.2
805 pcg 1.16 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL);
806 pcg 1.2
807 pcg 1.4 int dlen = strlen (THISNODE->domain);
808 pcg 1.2
809 pcg 1.4 if (qclass == RR_CLASS_IN
810 pcg 1.10 && qlen > dlen + 1
811 pcg 1.4 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen))
812 pcg 1.2 {
813 pcg 1.10 // now generate reply
814     pkt.ancount = htons (1); // one answer RR
815     pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
816    
817     if ((qtype == RR_TYPE_ANY
818     || qtype == RR_TYPE_TXT
819     || qtype == RR_TYPE_NULL)
820     && qlen > dlen + 1 + HDRSIZE)
821 pcg 1.5 {
822 pcg 1.10 // correct class, domain: parse
823     int client, seqno;
824     decode_header (qname, client, seqno);
825 pcg 1.5
826 pcg 1.10 u8 data[MAXSIZE];
827     int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE));
828 pcg 1.9
829 pcg 1.10 if (0 < client && client <= conns.size ())
830     {
831     connection *c = conns [client - 1];
832     dns_connection *dns = c->dns;
833     dns_rcv *rcv;
834 pcg 1.17 bool in_seq;
835 pcg 1.10
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 pcg 1.17 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 pcg 1.10
850     memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len);
851 pcg 1.17
852 pcg 1.10 goto duplicate_request;
853     }
854    
855 pcg 1.17 in_seq = dns->rcvseq == seqno;
856    
857 pcg 1.10 // 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 pcg 1.11 pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type
866     pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
867 pcg 1.10 pkt [offs++] = 0; pkt [offs++] = 0;
868 pcg 1.11 pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL
869 pcg 1.10
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 pcg 1.17 // 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 pcg 1.10 {
883     int txtlen = dlen <= 255 ? dlen - 1 : 255;
884    
885     if (txtlen > dns->snddq.size ())
886     txtlen = dns->snddq.size ();
887    
888     pkt[offs++] = txtlen;
889     memcpy (pkt.at (offs), dns->snddq.begin (), txtlen);
890     offs += txtlen;
891     dns->snddq.remove (txtlen);
892    
893     dlen -= txtlen + 1;
894     }
895    
896     // avoid empty TXT rdata
897     if (offs == rdlen_offs)
898     pkt[offs++] = 0;
899 pcg 1.15
900 pcg 1.17 slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ());
901 pcg 1.10 }
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 pcg 1.9
921 pcg 1.10 duplicate_request: ;
922     }
923     else
924     pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
925     }
926     else if (qtype == RR_TYPE_A
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 pcg 1.4
933 pcg 1.10 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
934 pcg 1.2
935 pcg 1.10 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 pcg 1.2
941 pcg 1.17 slog (L_INFO, _("DNS: client %d tries to connect"), client);
942 pcg 1.2
943 pcg 1.10 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
944     pkt [offs++] = CMD_IP_REJ;
945 pcg 1.2
946 pcg 1.10 if (0 < client && client <= conns.size ())
947 pcg 1.5 {
948 pcg 1.10 connection *c = conns [client - 1];
949 pcg 1.2
950 pcg 1.10 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 pcg 1.5 }
959     }
960 pcg 1.2 }
961 pcg 1.6
962 pcg 1.10 pkt.len = offs;
963 pcg 1.4 }
964     }
965    
966     void
967 pcg 1.10 vpn::dnsv4_client (dns_packet &pkt)
968 pcg 1.4 {
969 pcg 1.10 u16 flags = ntohs (pkt.flags);
970 pcg 1.4 int offs = 6 * 2; // skip header
971    
972 pcg 1.10 pkt.qdcount = ntohs (pkt.qdcount);
973     pkt.ancount = ntohs (pkt.ancount);
974 pcg 1.4
975 pcg 1.5 // go through our request list and find the corresponding request
976 pcg 1.10 for (vector<dns_snd *>::iterator i = dns_sndpq.begin ();
977 pcg 1.4 i != dns_sndpq.end ();
978     ++i)
979 pcg 1.10 if ((*i)->pkt->id == pkt.id)
980 pcg 1.4 {
981 pcg 1.9 dns_connection *dns = (*i)->dns;
982 pcg 1.12 connection *c = dns->c;
983 pcg 1.5 int seqno = (*i)->seqno;
984     u8 data[MAXSIZE], *datap = data;
985    
986 pcg 1.10 if ((*i)->retry)
987     {
988 pcg 1.17 dns->send_interval *= 1.01;
989 pcg 1.12 if (dns->send_interval > MAX_SEND_INTERVAL)
990 pcg 1.10 dns->send_interval = MAX_SEND_INTERVAL;
991     }
992     else
993     {
994 pcg 1.12 #if 1
995 pcg 1.17 dns->send_interval *= 0.999;
996 pcg 1.12 #endif
997 pcg 1.10 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 pcg 1.12 double latency = NOW - (*i)->sent;
1003 pcg 1.17 dns->last_latency = latency;
1004 pcg 1.12
1005     if (dns->send_interval > latency)
1006     dns->send_interval = latency;
1007 pcg 1.10 }
1008    
1009 pcg 1.4 delete *i;
1010     dns_sndpq.erase (i);
1011    
1012 pcg 1.10 if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK))
1013 pcg 1.4 {
1014     char qname[MAXSIZE];
1015    
1016 pcg 1.10 while (pkt.qdcount-- && offs < MAXSIZE - 4)
1017 pcg 1.4 {
1018 pcg 1.10 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
1019 pcg 1.4 offs += 4; // skip qtype, qclass
1020     }
1021    
1022 pcg 1.10 while (pkt.ancount-- && offs < MAXSIZE - 10 && datap)
1023 pcg 1.4 {
1024 pcg 1.10 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
1025    
1026     u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
1027     u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
1028     u32 ttl = pkt [offs++] << 24;
1029     ttl |= pkt [offs++] << 16;
1030     ttl |= pkt [offs++] << 8;
1031     ttl |= pkt [offs++];
1032     u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++];
1033 pcg 1.8
1034 pcg 1.10 if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT)
1035     {
1036     if (rdlen <= MAXSIZE - offs)
1037     {
1038     // decode bytes, finally
1039    
1040     while (rdlen)
1041     {
1042     int txtlen = pkt [offs++];
1043 pcg 1.4
1044 pcg 1.10 assert (txtlen + offs < MAXSIZE - 1);
1045 pcg 1.4
1046 pcg 1.10 memcpy (datap, pkt.at (offs), txtlen);
1047     datap += txtlen; offs += txtlen;
1048    
1049     rdlen -= txtlen + 1;
1050     }
1051     }
1052     }
1053     else if (qtype == RR_TYPE_A)
1054 pcg 1.5 {
1055 pcg 1.10 u8 ip [4];
1056 pcg 1.5
1057 pcg 1.10 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 pcg 1.5 {
1066 pcg 1.17 slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]);
1067 pcg 1.5
1068 pcg 1.10 if (ip [3] == CMD_IP_RST)
1069     {
1070 pcg 1.17 slog (L_DEBUG, _("DNS: got tunnel RST request"));
1071 pcg 1.10
1072 pcg 1.12 delete dns; c->dns = 0;
1073 pcg 1.10
1074     return;
1075     }
1076     else if (ip [3] == CMD_IP_SYN)
1077 pcg 1.12 {
1078 pcg 1.17 slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us."));
1079 pcg 1.12 dns->established = true;
1080     }
1081     else if (ip [3] == CMD_IP_REJ)
1082     {
1083 pcg 1.17 slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting."));
1084 pcg 1.12 abort ();
1085     }
1086 pcg 1.10 else
1087 pcg 1.17 slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]);
1088 pcg 1.10 }
1089     else
1090 pcg 1.17 slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"),
1091 pcg 1.10 ip [0], ip [1], ip [2], ip [3]);
1092 pcg 1.5
1093 pcg 1.10 return;
1094 pcg 1.9 }
1095 pcg 1.4
1096 pcg 1.9 int client, rseqno;
1097     decode_header (qname, client, rseqno);
1098    
1099     if (client != THISNODE->id)
1100     {
1101 pcg 1.17 slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring"));
1102 pcg 1.9 datap = 0;
1103     }
1104     else if (rseqno != seqno)
1105     {
1106 pcg 1.17 slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?"));
1107 pcg 1.9 datap = 0;
1108 pcg 1.4 }
1109     }
1110     }
1111    
1112 pcg 1.6 // todo: pkt now used
1113 pcg 1.9 if (datap)
1114     dns->receive_rep (new dns_rcv (seqno, data, datap - data));
1115 pcg 1.5
1116 pcg 1.4 break;
1117     }
1118     }
1119    
1120     void
1121     vpn::dnsv4_ev (io_watcher &w, short revents)
1122     {
1123     if (revents & EVENT_READ)
1124     {
1125     dns_packet *pkt = new dns_packet;
1126     struct sockaddr_in sa;
1127     socklen_t sa_len = sizeof (sa);
1128    
1129 pcg 1.10 pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
1130 pcg 1.4
1131     if (pkt->len > 0)
1132 pcg 1.5 {
1133     if (THISNODE->dns_port)
1134     {
1135 pcg 1.10 dnsv4_server (*pkt);
1136     sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len);
1137 pcg 1.5 }
1138     else
1139 pcg 1.10 dnsv4_client (*pkt);
1140    
1141     delete pkt;
1142 pcg 1.5 }
1143 pcg 1.1 }
1144     }
1145    
1146     bool
1147 pcg 1.3 connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1148     {
1149 pcg 1.9 if (!dns)
1150     dns = new dns_connection (this);
1151 pcg 1.4
1152 pcg 1.9 if (!dns->snddq.put (pkt))
1153 pcg 1.3 return false;
1154    
1155 pcg 1.10 dns->tw.trigger ();
1156 pcg 1.3
1157     return true;
1158     }
1159    
1160 pcg 1.12 void
1161     connection::dnsv4_reset_connection ()
1162     {
1163     //delete dns; dns = 0; //TODO
1164     }
1165    
1166 pcg 1.10 #define NEXT(w) do { if (next > (w)) next = w; } while (0)
1167    
1168 pcg 1.3 void
1169 pcg 1.9 dns_connection::time_cb (time_watcher &w)
1170 pcg 1.1 {
1171 pcg 1.10 // servers have to be polled
1172     if (THISNODE->dns_port)
1173     return;
1174    
1175 pcg 1.3 // check for timeouts and (re)transmit
1176 pcg 1.10 tstamp next = NOW + poll_interval;
1177     dns_snd *send = 0;
1178 pcg 1.3
1179 pcg 1.10 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
1180 pcg 1.4 i != vpn->dns_sndpq.end ();
1181 pcg 1.3 ++i)
1182     {
1183 pcg 1.10 dns_snd *r = *i;
1184 pcg 1.3
1185 pcg 1.10 if (r->timeout <= NOW)
1186 pcg 1.3 {
1187 pcg 1.4 if (!send)
1188     {
1189     send = r;
1190    
1191     r->retry++;
1192 pcg 1.17 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     }
1201 pcg 1.4 }
1202 pcg 1.3 }
1203 pcg 1.11 else
1204 pcg 1.10 NEXT (r->timeout);
1205 pcg 1.3 }
1206    
1207 pcg 1.10 if (last_sent + send_interval <= NOW)
1208 pcg 1.5 {
1209 pcg 1.10 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     }
1223 pcg 1.17 else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING
1224     && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1)))
1225 pcg 1.10 {
1226 pcg 1.17 if (!snddq.empty ())
1227     {
1228     poll_interval = send_interval;
1229     NEXT (NOW + send_interval);
1230     }
1231    
1232 pcg 1.10 send = new dns_snd (this);
1233     send->gen_stream_req (sndseq, snddq);
1234 pcg 1.17 send->timeout = NOW + last_latency * 8.;
1235 pcg 1.5
1236 pcg 1.10 sndseq = (sndseq + 1) & SEQNO_MASK;
1237     }
1238 pcg 1.4
1239 pcg 1.10 if (send)
1240     vpn->dns_sndpq.push_back (send);
1241     }
1242 pcg 1.4
1243 pcg 1.10 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     }
1250 pcg 1.4 }
1251 pcg 1.10 else
1252     NEXT (last_sent + send_interval);
1253    
1254 pcg 1.17 slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)",
1255 pcg 1.12 poll_interval, send_interval, next - NOW,
1256     vpn->dns_sndpq.size (), snddq.size ());
1257 pcg 1.11
1258     // TODO: no idea when this happens, but when next < NOW, we have a problem
1259     if (next < NOW + 0.0001)
1260     next = NOW + 0.1;
1261 pcg 1.4
1262 pcg 1.3 w.start (next);
1263 pcg 1.1 }
1264    
1265     #endif
1266