--- gvpe/src/vpn_dns.C 2005/03/05 19:13:16 1.18 +++ gvpe/src/vpn_dns.C 2005/03/07 01:31:26 1.24 @@ -27,6 +27,7 @@ // it's a hack, use only in emergency situations please. #include +#include #include #include @@ -266,7 +267,7 @@ ///////////////////////////////////////////////////////////////////////////// #define HDRSIZE 6 - + inline void encode_header (char *data, int clientid, int seqno, int retry = 0) { seqno &= SEQNO_MASK; @@ -410,17 +411,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 +446,16 @@ rrtype = RR_TYPE_TXT; flags = 0; - def_ttl = 1; - rcv_cdc = 0; - snd_cdc = 62; + def_ttl = 0; + seq_cdc = 26; + req_cdc = 62; + rep_cdc = 0; max_size = ntohs (MAX_PKT_SIZE); client = ntohs (clientid); uid = next_uid++; - memset (reserved, 0, 8); + r2 = r3 = r4 = 0; + r4 = r5 = r6 = r7 = 0; } bool dns_cfg::valid () @@ -455,10 +464,10 @@ && id2 == 'V' && id3 == 'P' && id4 == 'E' + && seq_cdc == 26 + && req_cdc == 62 + && rep_cdc == 0 && version == 1 - && flags == 0 - && rcv_cdc == 0 - && snd_cdc == 62 && max_size == ntohs (MAX_PKT_SIZE); } @@ -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); }; @@ -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; @@ -808,7 +815,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 +832,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 +883,66 @@ 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; - } + 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 rdlen = offs - rdlen_offs; + 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; - 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 +964,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; @@ -1147,13 +1156,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; } @@ -1239,7 +1248,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 (); }