--- gvpe/src/vpn_dns.C 2005/03/08 17:25:27 1.26 +++ gvpe/src/vpn_dns.C 2005/03/15 11:43:38 1.31 @@ -46,26 +46,27 @@ #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 TIMEOUT_FACTOR 2. + #define INITIAL_TIMEOUT 0.1 // retry timeouts #define INITIAL_SYN_TIMEOUT 10. // 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 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 20 // max. outstanding requests +#define MAX_OUTSTANDING 100 // max. outstanding requests #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog -#define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE +#define MAX_BACKLOG (32*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE -#define MAX_DOMAIN_SIZE 200 // 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 -#define SEQNO_MASK 0x3fff +#define SEQNO_MASK 0x0fff #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) #define MAX_LBL_SIZE 63 @@ -426,7 +427,8 @@ u8 req_cdc; u8 rep_cdc; - u8 r2, r3, r4; + u8 delay; // time in 0.01s units that the server may delay replying packets + u8 r3, r4; u8 r5, r6, r7, r8; @@ -451,11 +453,12 @@ seq_cdc = 26; req_cdc = 62; rep_cdc = 0; - max_size = ntohs (MAX_PKT_SIZE); - client = ntohs (clientid); + max_size = htons (MAX_PKT_SIZE); + client = htons (clientid); uid = next_uid++; + delay = 0; - r2 = r3 = r4 = 0; + r3 = r4 = 0; r4 = r5 = r6 = r7 = 0; } @@ -468,8 +471,7 @@ && seq_cdc == 26 && req_cdc == 62 && rep_cdc == 0 - && version == 1 - && max_size == ntohs (MAX_PKT_SIZE); + && version == 1; } struct dns_packet : net_packet @@ -555,7 +557,7 @@ vector rcvpq; - byte_stream rcvdq; int rcvseq; + byte_stream rcvdq; int rcvseq; int repseq; byte_stream snddq; int sndseq; void time_cb (time_watcher &w); time_watcher tw; @@ -722,17 +724,17 @@ dns_connection::dns_connection (connection *c) : c (c) , rcvdq (MAX_BACKLOG * 2) -, snddq (MAX_BACKLOG * 2) +, snddq (MAX_BACKLOG) , tw (this, &dns_connection::time_cb) { vpn = c->vpn; established = false; - rcvseq = sndseq = 0; + rcvseq = repseq = sndseq = 0; last_sent = last_received = 0; - poll_interval = MIN_POLL_INTERVAL; + poll_interval = 0.5; // starting here send_interval = 0.5; // starting rate min_latency = INITIAL_TIMEOUT; } @@ -857,7 +859,6 @@ connection *c = conns [client - 1]; dns_connection *dns = c->dns; dns_rcv *rcv; - bool in_seq; if (dns) { @@ -878,8 +879,6 @@ goto duplicate_request; } - in_seq = dns->rcvseq == seqno; - // new packet, queue rcv = new dns_rcv (seqno, data, datalen); dns->receive_rep (rcv); @@ -896,31 +895,38 @@ 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; - if (dns) { + int dlen = ntohs (dns->cfg.max_size) - offs; + + // bind doesn't compress well, so reduce further by one label length + dlen -= qlen; + // only put data into in-order sequence packets, if // we receive out-of-order packets we generate empty // replies - while (dlen > 1 && !dns->snddq.empty () && in_seq) + //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) { - int txtlen = dlen <= 255 ? dlen - 1 : 255; - - if (txtlen > dns->snddq.size ()) - txtlen = dns->snddq.size (); + dns->repseq = seqno; - pkt[offs++] = txtlen; - memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); - offs += txtlen; - dns->snddq.remove (txtlen); + 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; + dlen -= txtlen + 1; + } } - // avoid empty TXT rdata + // avoid completely empty TXT rdata if (offs == rdlen_offs) pkt[offs++] = 0; @@ -1022,9 +1028,6 @@ #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; @@ -1034,6 +1037,9 @@ if (dns->send_interval > dns->min_latency * LATENCY_FACTOR) dns->send_interval = dns->min_latency * LATENCY_FACTOR; + + if (dns->send_interval < MIN_SEND_INTERVAL) + dns->send_interval = MIN_SEND_INTERVAL; } delete *i; @@ -1185,11 +1191,10 @@ if (!c->dns) c->dns = new dns_connection (c); - if (!c->dns->snddq.put (pkt)) - return false; - - c->dns->tw.trigger (); + if (c->dns->snddq.put (pkt)) + c->dns->tw.trigger (); + // always return true even if the buffer overflows return true; } @@ -1225,13 +1230,13 @@ send = r; r->retry++; - r->timeout = NOW + (r->retry * min_latency * 8.); + r->timeout = NOW + (r->retry * min_latency * TIMEOUT_FACTOR); // 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\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); + //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); } } @@ -1240,24 +1245,24 @@ 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.reset (THISNODE->id); + send->gen_syn_req (); } - else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING - && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) + } + else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING + && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) + { + if (last_sent + send_interval <= NOW) { //printf ("sending data request etc.\n"); //D if (!snddq.empty () || last_received + 1. > NOW) @@ -1268,32 +1273,34 @@ send = new dns_snd (this); send->gen_stream_req (sndseq, snddq); - send->timeout = NOW + min_latency * 8.; + send->timeout = NOW + min_latency * TIMEOUT_FACTOR; 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); + } + + if (send) + { + last_sent = NOW; + if (rand () & 15 != 0)//D + sendto (vpn->dnsv4_fd, + send->pkt->at (0), send->pkt->len, 0, + vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); } - else - NEXT (last_sent + send_interval); - slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)", + slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", poll_interval, send_interval, next - NOW, - vpn->dns_sndpq.size (), snddq.size ()); + 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.0001) + if (next < NOW + 0.001) next = NOW + 0.1; w.start (next);