--- gvpe/src/vpn_dns.C 2005/03/05 19:13:16 1.18 +++ gvpe/src/vpn_dns.C 2005/03/12 18:10:40 1.27 @@ -27,6 +27,7 @@ // it's a hack, use only in emergency situations please. #include +#include #include #include @@ -53,13 +54,14 @@ #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 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 40 // 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 200 // 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 @@ -266,7 +268,7 @@ ///////////////////////////////////////////////////////////////////////////// #define HDRSIZE 6 - + inline void encode_header (char *data, int clientid, int seqno, int retry = 0) { seqno &= SEQNO_MASK; @@ -410,17 +412,23 @@ 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 r2, r3, r4; + + u8 r5, r6, r7, r8; void reset (int clientid); bool valid (); @@ -439,14 +447,16 @@ 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++; - memset (reserved, 0, 8); + r2 = r3 = r4 = 0; + r4 = r5 = r6 = r7 = 0; } bool dns_cfg::valid () @@ -455,11 +465,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 @@ -468,7 +477,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); }; @@ -540,7 +549,7 @@ tstamp last_received; tstamp last_sent; - double last_latency; + double min_latency; double poll_interval, send_interval; vector rcvpq; @@ -624,7 +633,7 @@ 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 @@ -652,7 +661,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; @@ -665,8 +674,6 @@ { timeout = NOW + INITIAL_SYN_TIMEOUT; - printf ("send syn\n");//D - pkt->flags = htons (DEFAULT_CLIENT_FLAGS); pkt->qdcount = htons (1); @@ -678,7 +685,7 @@ (*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; @@ -726,7 +733,7 @@ last_sent = last_received = 0; poll_interval = MIN_POLL_INTERVAL; send_interval = 0.5; // starting rate - last_latency = INITIAL_TIMEOUT; + min_latency = INITIAL_TIMEOUT; } dns_connection::~dns_connection () @@ -749,6 +756,7 @@ else { poll_interval *= 1.5; + if (poll_interval > MAX_POLL_INTERVAL) poll_interval = MAX_POLL_INTERVAL; } @@ -808,7 +816,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++]; @@ -825,7 +833,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 @@ -876,64 +884,67 @@ 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) - { - // 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) - { - 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, "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; - } + if (dns) + { + int dlen = ntohs (dns->cfg.max_size) - offs; + + // bind doesn't compress well, so reduce further by one label length + dlen -= qlen; - int rdlen = offs - rdlen_offs; + // 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) + { + int txtlen = dlen <= 255 ? dlen - 1 : 255; - pkt [rdlen_offs - 2] = rdlen >> 8; - pkt [rdlen_offs - 1] = rdlen; + if (txtlen > dns->snddq.size ()) + txtlen = dns->snddq.size (); - if (dns) - { - // now update dns_rcv copy - rcv->pkt->len = offs; - memcpy (rcv->pkt->at (0), pkt.at (0), offs); - } + 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; + + 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: ; } @@ -955,7 +966,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; @@ -1008,19 +1019,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; @@ -1147,13 +1160,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; } @@ -1212,13 +1225,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); } } @@ -1227,7 +1240,7 @@ NEXT (r->timeout); } - if (last_sent + send_interval <= NOW) + if (send || (last_sent + send_interval <= NOW)) { if (!send) { @@ -1239,7 +1252,6 @@ { send = new dns_snd (this); - printf ("new conn %p %d\n", this, c->conf->id);//D cfg.reset (THISNODE->id); send->gen_syn_req (); } @@ -1248,7 +1260,7 @@ && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) { //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); @@ -1256,12 +1268,12 @@ 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) + if (send && !send->retry) vpn->dns_sndpq.push_back (send); } @@ -1276,9 +1288,10 @@ 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)