--- gvpe/src/vpn_dns.C 2005/03/15 18:15:39 1.32 +++ gvpe/src/vpn_dns.C 2007/11/10 05:14:22 1.42 @@ -16,9 +16,13 @@ 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 + Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +// 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 @@ -49,22 +53,17 @@ #define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data #define ACTIVITY_INTERVAL 5. -#define TIMEOUT_FACTOR 4. - #define INITIAL_TIMEOUT 0.1 // retry timeouts -#define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn +#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn -#define MIN_SEND_INTERVAL 0.001 // wait at least this time between sending requests #define MAX_SEND_INTERVAL 2. // optimistic? -#define LATENCY_FACTOR 0.5 // RTT * LATENCY_FACTOR == sending rate -#define MAX_OUTSTANDING 100 // max. outstanding requests #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog -#define MAX_BACKLOG (32*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE +#define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE #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 0x3fff #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) @@ -464,6 +463,8 @@ 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' @@ -560,7 +561,7 @@ byte_stream rcvdq; int rcvseq; int repseq; byte_stream snddq; int sndseq; - void time_cb (time_watcher &w); time_watcher tw; + void time_cb (ev::timer &w, int revents); ev::timer tw; void receive_rep (dns_rcv *r); dns_connection (connection *c); @@ -589,7 +590,7 @@ timeout = 0; retry = 0; seqno = 0; - sent = NOW; + sent = ev::ev_now (); stdhdr = false; pkt = new dns_packet; @@ -630,7 +631,7 @@ stdhdr = true; this->seqno = seqno; - timeout = NOW + INITIAL_TIMEOUT; + timeout = ev::ev_now () + INITIAL_TIMEOUT; pkt->flags = htons (DEFAULT_CLIENT_FLAGS); pkt->qdcount = htons (1); @@ -675,7 +676,7 @@ void dns_snd::gen_syn_req () { - timeout = NOW + INITIAL_SYN_TIMEOUT; + timeout = ev::ev_now () + INITIAL_SYN_TIMEOUT; pkt->flags = htons (DEFAULT_CLIENT_FLAGS); pkt->qdcount = htons (1); @@ -751,8 +752,8 @@ { if (r->datalen) { - last_received = NOW; - tw.trigger (); + last_received = ev::ev_now (); + tw (); poll_interval = send_interval; } @@ -772,6 +773,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; @@ -779,6 +781,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; @@ -795,7 +798,7 @@ while (vpn_packet *pkt = rcvdq.get ()) { sockinfo si; - si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4; + si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; vpn->recv_vpn_packet (pkt, si); @@ -1016,6 +1019,7 @@ connection *c = dns->c; int seqno = (*i)->seqno; u8 data[MAXSIZE], *datap = data; + //printf ("rcv pkt %x\n", seqno);//D if ((*i)->retry) { @@ -1030,16 +1034,16 @@ #endif // the latency surely puts an upper bound on // the minimum send interval - double latency = NOW - (*i)->sent; + double latency = ev::ev_now () - (*i)->sent; if (latency < dns->min_latency) dns->min_latency = latency; - if (dns->send_interval > dns->min_latency * LATENCY_FACTOR) - dns->send_interval = dns->min_latency * LATENCY_FACTOR; + 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 < MIN_SEND_INTERVAL) - dns->send_interval = MIN_SEND_INTERVAL; + if (dns->send_interval < conf.dns_send_interval) + dns->send_interval = conf.dns_send_interval; } delete *i; @@ -1154,9 +1158,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; @@ -1182,7 +1186,7 @@ bool vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) { - int client = ntohs (si.port); + int client = ntohl (si.host); assert (0 < client && client <= conns.size ()); @@ -1192,7 +1196,7 @@ c->dns = new dns_connection (c); if (c->dns->snddq.put (pkt)) - c->dns->tw.trigger (); + c->dns->tw (); // always return true even if the buffer overflows return true; @@ -1207,14 +1211,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 (); @@ -1223,22 +1227,20 @@ { dns_snd *r = *i; - if (r->timeout <= NOW) + if (r->timeout <= ev::ev_now ()) { if (!send) { send = r; r->retry++; - r->timeout = NOW + (r->retry * min_latency * TIMEOUT_FACTOR); + r->timeout = ev::ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); + //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev::ev_now ());//D // the following code changes the query section a bit, forcing // the forwarder to generate a new request if (r->stdhdr) - { - //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); - //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); - } + encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); } } else @@ -1259,21 +1261,22 @@ 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 <= NOW) + if (last_sent + send_interval <= ev::ev_now ()) { //printf ("sending data request etc.\n"); //D - if (!snddq.empty () || last_received + 1. > NOW) + if (!snddq.empty () || last_received + 1. > ev::ev_now ()) { poll_interval = send_interval; - NEXT (NOW + send_interval); + NEXT (ev::ev_now () + send_interval); } send = new dns_snd (this); send->gen_stream_req (sndseq, snddq); - send->timeout = NOW + min_latency * TIMEOUT_FACTOR; + send->timeout = ev::ev_now () + min_latency * conf.dns_timeout_factor; + //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev::ev_now (), min_latency, conf.dns_timeout_factor);//D sndseq = (sndseq + 1) & SEQNO_MASK; } @@ -1287,23 +1290,23 @@ if (send) { - last_sent = NOW; - if (rand () & 15 != 0)//D + last_sent = ev::ev_now (); sendto (vpn->dnsv4_fd, send->pkt->at (0), send->pkt->len, 0, vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); } slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", - poll_interval, send_interval, next - NOW, + poll_interval, send_interval, next - ev::ev_now (), vpn->dns_sndpq.size (), snddq.size (), rcvpq.size ()); - // TODO: no idea when this happens, but when next < NOW, we have a problem - if (next < NOW + 0.001) - next = NOW + 0.1; + // TODO: no idea when this happens, but when next < ev::ev_now (), we have a problem + // doesn't seem to happen anymore + if (next < ev::ev_now () + 0.001) + next = ev::ev_now () + 0.1; - w.start (next); + w.start (next - ev::ev_now ()); } #endif