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