From 754998f7effbb8d3b6d74e47d3406ebcfd83dcc9 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Sun, 11 Jun 2006 19:17:20 +0000 Subject: [PATCH] Move packet queue out of dns.c --- dns.c | 68 ++++++++++++++++--------------------------------------- dns.h | 1 + iodined.c | 19 ++++++++++++---- 3 files changed, 34 insertions(+), 54 deletions(-) diff --git a/dns.c b/dns.c index 5076657..347fbdc 100644 --- a/dns.c +++ b/dns.c @@ -46,9 +46,6 @@ uint16_t chunkid; uint16_t pingid; -int outid; -int outbuflen; -char outbuf[64*1024]; char delayed_q_name[256]; short delayed_q_type; short delayed_q_id; @@ -425,12 +422,6 @@ host2dns(const char *host, char *buffer, int size) return p - buffer; } -int -dnsd_haspacket() -{ - return (outbuflen > 0); -} - int dnsd_hasack() { @@ -438,16 +429,7 @@ dnsd_hasack() } void -dnsd_queuepacket(const char *buf, const int buflen) -{ - memcpy(outbuf, buf, buflen); - - outbuflen = buflen; - outid++; -} - -static void -dnsd_send(int fd, char *name, short type, short id, struct sockaddr_in from) +dnsd_send(int fd, char *data, int datalen) { int len; char *p; @@ -459,7 +441,7 @@ dnsd_send(int fd, char *name, short type, short id, struct sockaddr_in from) len = 0; header = (HEADER*)buf; - header->id = htons(id); + header->id = htons(delayed_q_id); header->qr = 1; header->opcode = 0; header->aa = 1; @@ -472,35 +454,27 @@ dnsd_send(int fd, char *name, short type, short id, struct sockaddr_in from) p = buf + sizeof(HEADER); - p += host2dns(name, p, strlen(name)); - PUTSHORT(type, p); + p += host2dns(delayed_q_name, p, strlen(delayed_q_name)); + PUTSHORT(delayed_q_type, p); PUTSHORT(C_IN, p); - p += host2dns(name, p, strlen(name)); - PUTSHORT(type, p); + p += host2dns(delayed_q_name, p, strlen(delayed_q_name)); + PUTSHORT(delayed_q_type, p); PUTSHORT(C_IN, p); PUTLONG(0, p); - if(outbuflen > 0) { - PUTSHORT(outbuflen, p); - memcpy(p, outbuf, outbuflen); - p += outbuflen; + delayed_q_id = 0; + + if(datalen > 0) { + PUTSHORT(datalen, p); + memcpy(p, data, datalen); + p += datalen; } else { PUTSHORT(0, p); } len = p - buf; -// printf("Responding with %d\n", len); - sendto(fd, buf, len, 0, (struct sockaddr*)&from, sizeof(from)); - - outbuflen = 0; -} - -void -dnsd_forceack(int fd) -{ - dnsd_send(fd, delayed_q_name, delayed_q_type, delayed_q_id, delayed_q_from); - delayed_q_id = 0; + sendto(fd, buf, len, 0, (struct sockaddr*)&delayed_q_from, delayed_q_fromlen); } static int @@ -579,16 +553,12 @@ dnsd_read(int fd, char *buf, int buflen) READSHORT(type, data); READSHORT(class, data); - if (dnsd_haspacket()) { - dnsd_send(fd, name, type, id, from); - } else { - // Store needed info about delayed response - strncpy(delayed_q_name, name, 256); - delayed_q_type = type; - delayed_q_id = id; - delayed_q_fromlen = addrlen; - memcpy((struct sockaddr*)&delayed_q_from, (struct sockaddr*)&from, addrlen); - } + // Store needed info about delayed response + strncpy(delayed_q_name, name, 256); + delayed_q_type = type; + delayed_q_id = id; + delayed_q_fromlen = addrlen; + memcpy((struct sockaddr*)&delayed_q_from, (struct sockaddr*)&from, addrlen); return decodepacket(name, buf, buflen); } diff --git a/dns.h b/dns.h index 16b58cb..a3db832 100644 --- a/dns.h +++ b/dns.h @@ -35,6 +35,7 @@ int open_dnsd(const char *); void close_dnsd(int); int dnsd_read(int, char *, int); +void dnsd_send(int, char *, int); int dnsd_haspacket(); int dnsd_hasack(); diff --git a/iodined.c b/iodined.c index 63bbf0e..2899056 100644 --- a/iodined.c +++ b/iodined.c @@ -45,6 +45,8 @@ struct packet }; struct packet packetbuf; +struct packet outpacket; +int outid; static void sigint(int sig) { @@ -66,14 +68,14 @@ tunnel(int tun_fd, int dns_fd) while (running) { if (dnsd_hasack()) { tv.tv_sec = 0; - tv.tv_usec = 50000; + tv.tv_usec = 5000; } else { tv.tv_sec = 1; tv.tv_usec = 0; } FD_ZERO(&fds); - if(!dnsd_haspacket()) + if(outpacket.len == 0) FD_SET(tun_fd, &fds); FD_SET(dns_fd, &fds); @@ -87,7 +89,8 @@ tunnel(int tun_fd, int dns_fd) if (i==0) { if (dnsd_hasack()) - dnsd_forceack(dns_fd); + dnsd_send(dns_fd, outpacket.data, outpacket.len); + outpacket.len = 0; } else { if(FD_ISSET(tun_fd, &fds)) { read = read_tun(tun_fd, in, sizeof(in)); @@ -96,7 +99,8 @@ tunnel(int tun_fd, int dns_fd) outlen = sizeof(out); compress2(out, &outlen, in, read, 9); - dnsd_queuepacket(out, outlen); + memcpy(outpacket.data, out, outlen); + outpacket.len = outlen; } if(FD_ISSET(dns_fd, &fds)) { read = dnsd_read(dns_fd, in, sizeof(in)); @@ -105,7 +109,7 @@ tunnel(int tun_fd, int dns_fd) if(in[0] == 'H' || in[0] == 'h') { read = snprintf(out, sizeof(out), "%s-%d", "172.30.5.2", 1023); - dnsd_queuepacket(out, read); + dnsd_send(dns_fd, out, read); } else if((in[0] >= '0' && in[0] <= '9') || (in[0] >= 'a' && in[0] <= 'f') || (in[0] >= 'A' && in[0] <= 'F')) { @@ -128,6 +132,10 @@ tunnel(int tun_fd, int dns_fd) packetbuf.len = packetbuf.offset = 0; } + if (outpacket.len > 0) { + dnsd_send(dns_fd, outpacket.data, outpacket.len); + outpacket.len = 0; + } } } } @@ -185,6 +193,7 @@ main(int argc, char **argv) packetbuf.len = 0; packetbuf.offset = 0; + outpacket.len = 0; while ((choice = getopt(argc, argv, "vfhu:t:m:")) != -1) { switch(choice) {