--- gvpe/src/vpn_dns.C 2005/03/06 21:33:40 1.21 +++ gvpe/src/vpn_dns.C 2005/03/14 17:40:01 1.30 @@ -46,21 +46,21 @@ #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 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 MAX_OUTSTANDING 10 // max. outstanding requests +#define LATENCY_FACTOR 0.5 // RTT * LATENCY_FACTOR == sending rate +#define MAX_OUTSTANDING 2 // 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_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 @@ -267,7 +267,7 @@ ///////////////////////////////////////////////////////////////////////////// #define HDRSIZE 6 - + inline void encode_header (char *data, int clientid, int seqno, int retry = 0) { seqno &= SEQNO_MASK; @@ -411,17 +411,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 (); @@ -440,14 +447,17 @@ rrtype = RR_TYPE_TXT; flags = 0; - def_ttl = 1; - rcv_cdc = 0; - snd_cdc = 62; - max_size = ntohs (MAX_PKT_SIZE); - client = ntohs (clientid); + def_ttl = 0; + 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 () @@ -456,11 +466,10 @@ && 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 @@ -469,7 +478,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); }; @@ -541,7 +550,7 @@ tstamp last_received; tstamp last_sent; - double last_latency; + double min_latency; double poll_interval, send_interval; vector rcvpq; @@ -666,8 +675,6 @@ { timeout = NOW + INITIAL_SYN_TIMEOUT; - printf ("send syn\n");//D - pkt->flags = htons (DEFAULT_CLIENT_FLAGS); pkt->qdcount = htons (1); @@ -725,9 +732,9 @@ rcvseq = sndseq = 0; last_sent = last_received = 0; - poll_interval = MIN_POLL_INTERVAL; + poll_interval = 0.5; // starting here send_interval = 0.5; // starting rate - last_latency = INITIAL_TIMEOUT; + min_latency = INITIAL_TIMEOUT; } dns_connection::~dns_connection () @@ -750,6 +757,7 @@ else { poll_interval *= 1.5; + if (poll_interval > MAX_POLL_INTERVAL) poll_interval = MAX_POLL_INTERVAL; } @@ -809,7 +817,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++]; @@ -826,7 +834,7 @@ 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 @@ -888,12 +896,13 @@ 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 @@ -958,7 +967,7 @@ pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL pkt [offs++] = 0; pkt [offs++] = 4; // rdlength - slog (L_INFO, _("DNS: 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; @@ -1011,19 +1020,21 @@ } else { -#if 1 +#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; - dns->last_latency = latency; - 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 * 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; @@ -1150,13 +1161,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; } @@ -1215,13 +1226,13 @@ send = r; r->retry++; - r->timeout = NOW + (r->retry * last_latency * 8.); + r->timeout = NOW + (r->retry * min_latency * 8.); // 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); } } @@ -1230,28 +1241,27 @@ 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); - printf ("new conn %p %d\n", this, c->conf->id);//D - 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 ()) + if (!snddq.empty () || last_received + 1. > NOW) { poll_interval = send_interval; NEXT (NOW + send_interval); @@ -1259,32 +1269,33 @@ send = new dns_snd (this); send->gen_stream_req (sndseq, snddq); - send->timeout = NOW + last_latency * 8.; + send->timeout = NOW + min_latency * 8.; 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; + 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);