--- gvpe/src/vpn_dns.C 2005/03/04 10:15:45 1.15 +++ gvpe/src/vpn_dns.C 2009/07/28 00:42:14 1.48 @@ -1,24 +1,38 @@ /* vpn_dns.C -- handle the dns tunnel part of the protocol. - Copyright (C) 2003-2005 Marc Lehmann + Copyright (C) 2003-2008 Marc Lehmann This file is part of GVPE. - GVPE is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with gvpe; if not, write to the Free Software - Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + GVPE is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, see . + + Additional permission under GNU GPL version 3 section 7 + + If you modify this Program, or any covered work, by linking or + combining it with the OpenSSL project's OpenSSL library (or a modified + version of that library), containing parts covered by the terms of the + OpenSSL or SSLeay licenses, the licensors of this Program grant you + additional permission to convey the resulting work. Corresponding + Source for a non-source form of such a combination shall include the + source code for the parts of OpenSSL used as well as that of the + covered work. */ +// TODO: EDNS0 option to increase dns mtu? +// TODO: re-write dns packet parsing/creation using a safe mem-buffer +// to ensure no buffer overflows or similar problems. + #include "config.h" #if ENABLE_DNS @@ -27,6 +41,7 @@ // it's a hack, use only in emergency situations please. #include +#include #include #include @@ -39,31 +54,29 @@ #include +#include /* bug in libgmp: gmp.h relies on cstdio being included */ #include #include "netcompat.h" #include "vpn.h" -#define MIN_POLL_INTERVAL .02 // how often to poll minimally when the server has data -#define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data +#define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data #define ACTIVITY_INTERVAL 5. -#define INITIAL_TIMEOUT 1. // retry timeouts +#define INITIAL_TIMEOUT 0.1 // retry timeouts #define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn -#define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests -#define MAX_SEND_INTERVAL 0.5 // optimistic? +#define MAX_SEND_INTERVAL 2. // optimistic? -#define MAX_OUTSTANDING 40 // max. outstanding requests -#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING -#define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE +#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog +#define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE -#define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well +#define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well // 240 leaves about 4 bytes of server reply data -// every two request bytes less give room for one reply byte +// every request byte less give room for two reply bytes -#define SEQNO_MASK 0xffff +#define SEQNO_MASK 0x3fff #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) #define MAX_LBL_SIZE 63 @@ -72,6 +85,7 @@ #define RR_TYPE_A 1 #define RR_TYPE_NULL 10 #define RR_TYPE_TXT 16 +#define RR_TYPE_AAAA 28 #define RR_TYPE_ANY 255 #define RR_CLASS_IN 1 @@ -266,10 +280,16 @@ ///////////////////////////////////////////////////////////////////////////// #define HDRSIZE 6 - -inline void encode_header (char *data, int clientid, int seqno) + +inline void encode_header (char *data, int clientid, int seqno, int retry = 0) { - u8 hdr[3] = { clientid, seqno >> 8, seqno }; + seqno &= SEQNO_MASK; + + u8 hdr[3] = { + clientid, + (seqno >> 8) | (retry << 6), + seqno, + }; assert (clientid < 256); @@ -283,7 +303,7 @@ cdc26.decode (hdr, data, HDRSIZE); clientid = hdr[0]; - seqno = (hdr[1] << 8) | hdr[2]; + seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK; } ///////////////////////////////////////////////////////////////////////////// @@ -322,7 +342,7 @@ void byte_stream::remove (int count) { if (count > fill) - abort (); + assert (count <= fill); memmove (data, data + count, fill -= count); } @@ -352,11 +372,20 @@ vpn_packet *byte_stream::get () { - unsigned int len = (data [0] << 8) | data [1]; + unsigned int len; + + for (;;) + { + len = (data [0] << 8) | data [1]; - if (len > MAXSIZE && fill >= 2) - abort (); // TODO handle this gracefully, connection reset + if (len <= MAXSIZE || fill < 2) + break; + // TODO: handle this better than skipping, e.g. by reset + slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte...")); + remove (1); + } + if (fill < len + 2) return 0; @@ -395,17 +424,24 @@ static int next_uid; u8 id1, id2, id3, id4; + u8 version; - u8 rrtype; u8 flags; + u8 rrtype; u8 def_ttl; - u8 rcv_cdc; - u8 snd_cdc; - u16 max_size; + u16 client; u16 uid; // to make request unique - u8 reserved[8]; + u16 max_size; + u8 seq_cdc; + u8 req_cdc; + + u8 rep_cdc; + u8 delay; // time in 0.01s units that the server may delay replying packets + u8 r3, r4; + + u8 r5, r6, r7, r8; void reset (int clientid); bool valid (); @@ -425,26 +461,30 @@ rrtype = RR_TYPE_TXT; flags = 0; def_ttl = 0; - rcv_cdc = 0; - snd_cdc = 62; - max_size = ntohs (MAX_PKT_SIZE); - client = ntohs (clientid); + seq_cdc = 26; + req_cdc = 62; + rep_cdc = 0; + max_size = htons (MAX_PKT_SIZE); + client = htons (clientid); uid = next_uid++; + delay = 0; - memset (reserved, 0, 8); + r3 = r4 = 0; + r4 = r5 = r6 = r7 = 0; } bool dns_cfg::valid () { + // although the protocol itself allows for some configurability, + // only the following encoding/decoding settings are implemented. return id1 == 'G' && id2 == 'V' && id3 == 'P' && id4 == 'E' - && version == 1 - && flags == 0 - && rcv_cdc == 0 - && snd_cdc == 62 - && max_size == ntohs (MAX_PKT_SIZE); + && seq_cdc == 26 + && req_cdc == 62 + && rep_cdc == 0 + && version == 1; } struct dns_packet : net_packet @@ -453,7 +493,7 @@ u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 u16 qdcount, ancount, nscount, arcount; - u8 data[MAXSIZE - 6 * 2]; + u8 data [MAXSIZE - 6 * 2]; int decode_label (char *data, int size, int &offs); }; @@ -494,6 +534,52 @@ ///////////////////////////////////////////////////////////////////////////// +static u16 dns_id = 0; // TODO: should be per-vpn + +static u16 next_id () +{ + if (!dns_id) + dns_id = time (0); + + // the simplest lsfr with periodicity 65535 i could find + dns_id = (dns_id << 1) + | (((dns_id >> 1) + ^ (dns_id >> 2) + ^ (dns_id >> 4) + ^ (dns_id >> 15)) & 1); + + return dns_id; +} + +struct dns_rcv; +struct dns_snd; + +struct dns_connection +{ + connection *c; + struct vpn *vpn; + + dns_cfg cfg; + + bool established; + + tstamp last_received; + tstamp last_sent; + double min_latency; + double poll_interval, send_interval; + + vector rcvpq; + + byte_stream rcvdq; int rcvseq; int repseq; + byte_stream snddq; int sndseq; + + inline void time_cb (ev::timer &w, int revents); ev::timer tw; + void receive_rep (dns_rcv *r); + + dns_connection (connection *c); + ~dns_connection (); +}; + struct dns_snd { dns_packet *pkt; @@ -501,35 +587,23 @@ int retry; struct dns_connection *dns; int seqno; + bool stdhdr; void gen_stream_req (int seqno, byte_stream &stream); - void gen_syn_req (const dns_cfg &cfg); + void gen_syn_req (); dns_snd (dns_connection *dns); ~dns_snd (); }; -static u16 dns_id = 12098; // TODO: should be per-vpn - -static u16 next_id () -{ - // the simplest lsfr with periodicity 65535 i could find - dns_id = (dns_id << 1) - | (((dns_id >> 1) - ^ (dns_id >> 2) - ^ (dns_id >> 4) - ^ (dns_id >> 15)) & 1); - - return dns_id; -} - dns_snd::dns_snd (dns_connection *dns) : dns (dns) { timeout = 0; retry = 0; seqno = 0; - sent = NOW; + sent = ev_now (); + stdhdr = false; pkt = new dns_packet; @@ -566,15 +640,16 @@ void dns_snd::gen_stream_req (int seqno, byte_stream &stream) { + stdhdr = true; this->seqno = seqno; - timeout = NOW + INITIAL_TIMEOUT; + timeout = ev_now () + INITIAL_TIMEOUT; pkt->flags = htons (DEFAULT_CLIENT_FLAGS); pkt->qdcount = htons (1); int offs = 6*2; - int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); + int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2); // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra @@ -602,7 +677,7 @@ enclen -= lbllen; } - append_domain (*pkt, offs, THISNODE->domain); + append_domain (*pkt, offs, dns->c->conf->domain); (*pkt)[offs++] = 0; (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; @@ -611,22 +686,22 @@ pkt->len = offs; } -void dns_snd::gen_syn_req (const dns_cfg &cfg) +void dns_snd::gen_syn_req () { - timeout = NOW + INITIAL_SYN_TIMEOUT; + timeout = ev_now () + INITIAL_SYN_TIMEOUT; pkt->flags = htons (DEFAULT_CLIENT_FLAGS); pkt->qdcount = htons (1); - int offs = 6*2; + int offs = 6 * 2; - int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg)); + int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg)); assert (elen <= MAX_LBL_SIZE); (*pkt)[offs] = elen; offs += elen + 1; - append_domain (*pkt, offs, THISNODE->domain); + append_domain (*pkt, offs, dns->c->conf->domain); (*pkt)[offs++] = 0; (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; @@ -659,46 +734,23 @@ ///////////////////////////////////////////////////////////////////////////// -struct dns_connection -{ - connection *c; - struct vpn *vpn; - - dns_cfg cfg; - - bool established; - - tstamp last_received; - tstamp last_sent; - double poll_interval, send_interval; - - vector rcvpq; - - byte_stream rcvdq; int rcvseq; - byte_stream snddq; int sndseq; - - void time_cb (time_watcher &w); time_watcher tw; - void receive_rep (dns_rcv *r); - - dns_connection (connection *c); - ~dns_connection (); -}; - dns_connection::dns_connection (connection *c) : c (c) , rcvdq (MAX_BACKLOG * 2) -, snddq (MAX_BACKLOG * 2) -, tw (this, &dns_connection::time_cb) +, snddq (MAX_BACKLOG) { + tw.set (this); + vpn = c->vpn; established = false; - rcvseq = sndseq = 0; + rcvseq = repseq = sndseq = 0; last_sent = last_received = 0; - poll_interval = MIN_POLL_INTERVAL; - send_interval = 0.2; // starting rate + poll_interval = 0.5; // starting here + send_interval = 0.5; // starting rate + min_latency = INITIAL_TIMEOUT; } dns_connection::~dns_connection () @@ -713,14 +765,15 @@ { if (r->datalen) { - last_received = NOW; - tw.trigger (); + last_received = ev_now (); + tw (); poll_interval = send_interval; } else { - poll_interval *= 1.1; + poll_interval *= 1.5; + if (poll_interval > MAX_POLL_INTERVAL) poll_interval = MAX_POLL_INTERVAL; } @@ -733,6 +786,7 @@ for (vector::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) if (SEQNO_EQ (rcvseq, (*i)->seqno)) { + //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D // enter the packet into our input stream r = *i; @@ -740,6 +794,7 @@ for (vector::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) { + //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D delete *j; rcvpq.erase (j); break; @@ -748,12 +803,15 @@ rcvseq = (rcvseq + 1) & SEQNO_MASK; if (!rcvdq.put (r->data, r->datalen)) - abort (); // MUST never overflow, can be caused by data corruption, TODO + { + slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)"); + abort (); // MUST never overflow, can be caused by data corruption, TODO + } while (vpn_packet *pkt = rcvdq.get ()) { sockinfo si; - si.host = 0; si.port = 0; si.prot = PROT_DNSv4; + si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; vpn->recv_vpn_packet (pkt, si); @@ -777,7 +835,7 @@ if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) && pkt.qdcount == htons (1)) { - char qname[MAXSIZE]; + char qname [MAXSIZE]; int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; @@ -788,13 +846,13 @@ pkt.nscount = 0; // should be self, as other nameservers reply like this pkt.arcount = 0; // a record for self, as other nameservers reply like this - pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); + pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL); int dlen = strlen (THISNODE->domain); if (qclass == RR_CLASS_IN && qlen > dlen + 1 - && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) + && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) { // now generate reply pkt.ancount = htons (1); // one answer RR @@ -826,10 +884,14 @@ // already seen that request: simply reply with the cached reply dns_rcv *r = *i; - slog (L_DEBUG, "DUPLICATE %d\n", htons (r->pkt->id)); + slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno); + + // refresh header & id, as the retry count could have changed + memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE); + r->pkt->id = pkt.id; memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); - pkt.id = r->pkt->id; + goto duplicate_request; } @@ -838,61 +900,73 @@ dns->receive_rep (rcv); } - pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section + { + pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section - int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; - pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type - pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class - pkt [offs++] = 0; pkt [offs++] = 0; - pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL - - int rdlen_offs = offs += 2; - - int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; - // bind doesn't compress well, so reduce further by one label length - dlen -= qlen; + int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; + pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type + pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class + pkt [offs++] = 0; pkt [offs++] = 0; + pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL - if (dns) - { - while (dlen > 1 && !dns->snddq.empty ()) - { - int txtlen = dlen <= 255 ? dlen - 1 : 255; - - if (txtlen > dns->snddq.size ()) - txtlen = dns->snddq.size (); - - pkt[offs++] = txtlen; - memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); - offs += txtlen; - dns->snddq.remove (txtlen); - - dlen -= txtlen + 1; - } - - // avoid empty TXT rdata - if (offs == rdlen_offs) - pkt[offs++] = 0; + int rdlen_offs = offs += 2; - slog (L_NOISE, "snddq %d", dns->snddq.size ()); - } - else - { - // send RST - pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; - pkt [offs++] = CMD_IP_RST; - } + if (dns) + { + int dlen = ntohs (dns->cfg.max_size) - offs; - int rdlen = offs - rdlen_offs; + // bind doesn't compress well, so reduce further by one label length + dlen -= qlen; - pkt [rdlen_offs - 2] = rdlen >> 8; - pkt [rdlen_offs - 1] = rdlen; + // only put data into in-order sequence packets, if + // we receive out-of-order packets we generate empty + // replies + //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D + if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW) + { + dns->repseq = seqno; - if (dns) - { - // now update dns_rcv copy - rcv->pkt->len = offs; - memcpy (rcv->pkt->at (0), pkt.at (0), offs); - } + while (dlen > 1 && !dns->snddq.empty ()) + { + int txtlen = dlen <= 255 ? dlen - 1 : 255; + + if (txtlen > dns->snddq.size ()) + txtlen = dns->snddq.size (); + + pkt[offs++] = txtlen; + memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); + offs += txtlen; + dns->snddq.remove (txtlen); + + dlen -= txtlen + 1; + } + } + + // avoid completely empty TXT rdata + if (offs == rdlen_offs) + pkt[offs++] = 0; + + slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); + } + else + { + // send RST + pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; + pkt [offs++] = CMD_IP_RST; + } + + int rdlen = offs - rdlen_offs; + + pkt [rdlen_offs - 2] = rdlen >> 8; + pkt [rdlen_offs - 1] = rdlen; + + if (dns) + { + // now update dns_rcv copy + rcv->pkt->len = offs; + memcpy (rcv->pkt->at (0), pkt.at (0), offs); + } + } duplicate_request: ; } @@ -914,7 +988,7 @@ pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL pkt [offs++] = 0; pkt [offs++] = 4; // rdlength - slog (L_INFO, _("DNS tunnel: client %d tries to connect"), client); + slog (L_INFO, _("DNS: client %d connects"), client); pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; pkt [offs++] = CMD_IP_REJ; @@ -958,27 +1032,31 @@ connection *c = dns->c; int seqno = (*i)->seqno; u8 data[MAXSIZE], *datap = data; + //printf ("rcv pkt %x\n", seqno);//D if ((*i)->retry) { - dns->send_interval *= 1.001; + dns->send_interval *= 1.01; if (dns->send_interval > MAX_SEND_INTERVAL) dns->send_interval = MAX_SEND_INTERVAL; } else { -#if 1 - dns->send_interval *= 0.9999; +#if 0 + dns->send_interval *= 0.999; #endif - if (dns->send_interval < MIN_SEND_INTERVAL) - dns->send_interval = MIN_SEND_INTERVAL; - // the latency surely puts an upper bound on // the minimum send interval - double latency = NOW - (*i)->sent; + double latency = ev_now () - (*i)->sent; - if (dns->send_interval > latency) - dns->send_interval = latency; + if (latency < dns->min_latency) + dns->min_latency = latency; + + if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) + dns->send_interval = dns->min_latency * conf.dns_overlap_factor; + + if (dns->send_interval < conf.dns_send_interval) + dns->send_interval = conf.dns_send_interval; } delete *i; @@ -1038,11 +1116,11 @@ && ip [1] == CMD_IP_2 && ip [2] == CMD_IP_3) { - slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]); + slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]); if (ip [3] == CMD_IP_RST) { - slog (L_DEBUG, _("got tunnel RST request")); + slog (L_DEBUG, _("DNS: got tunnel RST request")); delete dns; c->dns = 0; @@ -1050,19 +1128,19 @@ } else if (ip [3] == CMD_IP_SYN) { - slog (L_DEBUG, _("got tunnel SYN reply, server likes us.")); + slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); dns->established = true; } else if (ip [3] == CMD_IP_REJ) { - slog (L_DEBUG, _("got tunnel REJ reply, server does not like us, aborting.")); + slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting.")); abort (); } else - slog (L_INFO, _("got unknown meta command %02x"), ip [3]); + slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); } else - slog (L_INFO, _("got spurious a record %d.%d.%d.%d"), + slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), ip [0], ip [1], ip [2], ip [3]); return; @@ -1073,12 +1151,12 @@ if (client != THISNODE->id) { - slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); + slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring")); datap = 0; } else if (rseqno != seqno) { - slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?")); + slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?")); datap = 0; } } @@ -1093,9 +1171,9 @@ } void -vpn::dnsv4_ev (io_watcher &w, short revents) +vpn::dnsv4_ev (ev::io &w, int revents) { - if (revents & EVENT_READ) + if (revents & EV_READ) { dns_packet *pkt = new dns_packet; struct sockaddr_in sa; @@ -1105,13 +1183,13 @@ if (pkt->len > 0) { - if (THISNODE->dns_port) + if (ntohs (pkt->flags) & FLAG_RESPONSE) + dnsv4_client (*pkt); + else { dnsv4_server (*pkt); sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); } - else - dnsv4_client (*pkt); delete pkt; } @@ -1119,16 +1197,21 @@ } bool -connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) +vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) { - if (!dns) - dns = new dns_connection (this); - - if (!dns->snddq.put (pkt)) - return false; + int client = ntohl (si.host); - dns->tw.trigger (); + assert (0 < client && client <= conns.size ()); + connection *c = conns [client - 1]; + + if (!c->dns) + c->dns = new dns_connection (c); + + if (c->dns->snddq.put (pkt)) + c->dns->tw (); + + // always return true even if the buffer overflows return true; } @@ -1141,14 +1224,14 @@ #define NEXT(w) do { if (next > (w)) next = w; } while (0) void -dns_connection::time_cb (time_watcher &w) +dns_connection::time_cb (ev::timer &w, int revents) { // servers have to be polled if (THISNODE->dns_port) return; // check for timeouts and (re)transmit - tstamp next = NOW + poll_interval; + tstamp next = ev::now () + poll_interval; dns_snd *send = 0; for (vector::iterator i = vpn->dns_sndpq.begin (); @@ -1157,68 +1240,86 @@ { dns_snd *r = *i; - if (r->timeout <= NOW) + if (r->timeout <= ev_now ()) { if (!send) { send = r; r->retry++; - r->timeout = NOW + r->retry; + r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); + //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D + + // the following code changes the query section a bit, forcing + // the forwarder to generate a new request + if (r->stdhdr) + encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); } } else NEXT (r->timeout); } - if (last_sent + send_interval <= NOW) + if (!send) { - if (!send) - { - // generate a new packet, if wise + // generate a new packet, if wise - if (!established) + if (!established) + { + if (vpn->dns_sndpq.empty ()) { - if (vpn->dns_sndpq.empty ()) - { - send = new dns_snd (this); + send = new dns_snd (this); - cfg.reset (THISNODE->id); - send->gen_syn_req (cfg); - } + cfg.reset (THISNODE->id); + send->gen_syn_req (); } - else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING) + } + else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding + && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) + { + if (last_sent + send_interval <= ev_now ()) { + //printf ("sending data request etc.\n"); //D + if (!snddq.empty () || last_received + 1. > ev_now ()) + { + poll_interval = send_interval; + NEXT (ev_now () + send_interval); + } + send = new dns_snd (this); send->gen_stream_req (sndseq, snddq); + send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; + //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D sndseq = (sndseq + 1) & SEQNO_MASK; } - - if (send) - vpn->dns_sndpq.push_back (send); + else + NEXT (last_sent + send_interval); } if (send) - { - last_sent = NOW; - sendto (vpn->dnsv4_fd, - send->pkt->at (0), send->pkt->len, 0, - vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); - } + vpn->dns_sndpq.push_back (send); } - else - NEXT (last_sent + send_interval); - slog (L_NOISE, "pi %f si %f N %f (%d:%d)", - poll_interval, send_interval, next - NOW, - vpn->dns_sndpq.size (), snddq.size ()); + if (send) + { + last_sent = ev_now (); + sendto (vpn->dnsv4_fd, + send->pkt->at (0), send->pkt->len, 0, + vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); + } - // TODO: no idea when this happens, but when next < NOW, we have a problem - if (next < NOW + 0.0001) - next = NOW + 0.1; + slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", + poll_interval, send_interval, next - ev_now (), + vpn->dns_sndpq.size (), snddq.size (), + rcvpq.size ()); + + // TODO: no idea when this happens, but when next < ev_now (), we have a problem + // doesn't seem to happen anymore + if (next < ev_now () + 0.001) + next = ev_now () + 0.1; - w.start (next); + w.start (next - ev_now ()); } #endif