Move packet queue out of dns.c

This commit is contained in:
Bjorn Andersson 2006-06-11 19:17:20 +00:00
parent 5c01e8d8e3
commit 754998f7ef
3 changed files with 34 additions and 54 deletions

68
dns.c
View File

@ -46,9 +46,6 @@ uint16_t chunkid;
uint16_t pingid; uint16_t pingid;
int outid;
int outbuflen;
char outbuf[64*1024];
char delayed_q_name[256]; char delayed_q_name[256];
short delayed_q_type; short delayed_q_type;
short delayed_q_id; short delayed_q_id;
@ -425,12 +422,6 @@ host2dns(const char *host, char *buffer, int size)
return p - buffer; return p - buffer;
} }
int
dnsd_haspacket()
{
return (outbuflen > 0);
}
int int
dnsd_hasack() dnsd_hasack()
{ {
@ -438,16 +429,7 @@ dnsd_hasack()
} }
void void
dnsd_queuepacket(const char *buf, const int buflen) dnsd_send(int fd, char *data, int datalen)
{
memcpy(outbuf, buf, buflen);
outbuflen = buflen;
outid++;
}
static void
dnsd_send(int fd, char *name, short type, short id, struct sockaddr_in from)
{ {
int len; int len;
char *p; char *p;
@ -459,7 +441,7 @@ dnsd_send(int fd, char *name, short type, short id, struct sockaddr_in from)
len = 0; len = 0;
header = (HEADER*)buf; header = (HEADER*)buf;
header->id = htons(id); header->id = htons(delayed_q_id);
header->qr = 1; header->qr = 1;
header->opcode = 0; header->opcode = 0;
header->aa = 1; 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 = buf + sizeof(HEADER);
p += host2dns(name, p, strlen(name)); p += host2dns(delayed_q_name, p, strlen(delayed_q_name));
PUTSHORT(type, p); PUTSHORT(delayed_q_type, p);
PUTSHORT(C_IN, p); PUTSHORT(C_IN, p);
p += host2dns(name, p, strlen(name)); p += host2dns(delayed_q_name, p, strlen(delayed_q_name));
PUTSHORT(type, p); PUTSHORT(delayed_q_type, p);
PUTSHORT(C_IN, p); PUTSHORT(C_IN, p);
PUTLONG(0, p); PUTLONG(0, p);
if(outbuflen > 0) { delayed_q_id = 0;
PUTSHORT(outbuflen, p);
memcpy(p, outbuf, outbuflen); if(datalen > 0) {
p += outbuflen; PUTSHORT(datalen, p);
memcpy(p, data, datalen);
p += datalen;
} else { } else {
PUTSHORT(0, p); PUTSHORT(0, p);
} }
len = p - buf; len = p - buf;
// printf("Responding with %d\n", len); sendto(fd, buf, len, 0, (struct sockaddr*)&delayed_q_from, delayed_q_fromlen);
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;
} }
static int static int
@ -579,16 +553,12 @@ dnsd_read(int fd, char *buf, int buflen)
READSHORT(type, data); READSHORT(type, data);
READSHORT(class, data); READSHORT(class, data);
if (dnsd_haspacket()) { // Store needed info about delayed response
dnsd_send(fd, name, type, id, from); strncpy(delayed_q_name, name, 256);
} else { delayed_q_type = type;
// Store needed info about delayed response delayed_q_id = id;
strncpy(delayed_q_name, name, 256); delayed_q_fromlen = addrlen;
delayed_q_type = type; memcpy((struct sockaddr*)&delayed_q_from, (struct sockaddr*)&from, addrlen);
delayed_q_id = id;
delayed_q_fromlen = addrlen;
memcpy((struct sockaddr*)&delayed_q_from, (struct sockaddr*)&from, addrlen);
}
return decodepacket(name, buf, buflen); return decodepacket(name, buf, buflen);
} }

1
dns.h
View File

@ -35,6 +35,7 @@ int open_dnsd(const char *);
void close_dnsd(int); void close_dnsd(int);
int dnsd_read(int, char *, int); int dnsd_read(int, char *, int);
void dnsd_send(int, char *, int);
int dnsd_haspacket(); int dnsd_haspacket();
int dnsd_hasack(); int dnsd_hasack();

View File

@ -45,6 +45,8 @@ struct packet
}; };
struct packet packetbuf; struct packet packetbuf;
struct packet outpacket;
int outid;
static void static void
sigint(int sig) { sigint(int sig) {
@ -66,14 +68,14 @@ tunnel(int tun_fd, int dns_fd)
while (running) { while (running) {
if (dnsd_hasack()) { if (dnsd_hasack()) {
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 50000; tv.tv_usec = 5000;
} else { } else {
tv.tv_sec = 1; tv.tv_sec = 1;
tv.tv_usec = 0; tv.tv_usec = 0;
} }
FD_ZERO(&fds); FD_ZERO(&fds);
if(!dnsd_haspacket()) if(outpacket.len == 0)
FD_SET(tun_fd, &fds); FD_SET(tun_fd, &fds);
FD_SET(dns_fd, &fds); FD_SET(dns_fd, &fds);
@ -87,7 +89,8 @@ tunnel(int tun_fd, int dns_fd)
if (i==0) { if (i==0) {
if (dnsd_hasack()) if (dnsd_hasack())
dnsd_forceack(dns_fd); dnsd_send(dns_fd, outpacket.data, outpacket.len);
outpacket.len = 0;
} else { } else {
if(FD_ISSET(tun_fd, &fds)) { if(FD_ISSET(tun_fd, &fds)) {
read = read_tun(tun_fd, in, sizeof(in)); read = read_tun(tun_fd, in, sizeof(in));
@ -96,7 +99,8 @@ tunnel(int tun_fd, int dns_fd)
outlen = sizeof(out); outlen = sizeof(out);
compress2(out, &outlen, in, read, 9); compress2(out, &outlen, in, read, 9);
dnsd_queuepacket(out, outlen); memcpy(outpacket.data, out, outlen);
outpacket.len = outlen;
} }
if(FD_ISSET(dns_fd, &fds)) { if(FD_ISSET(dns_fd, &fds)) {
read = dnsd_read(dns_fd, in, sizeof(in)); 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') { if(in[0] == 'H' || in[0] == 'h') {
read = snprintf(out, sizeof(out), "%s-%d", "172.30.5.2", 1023); 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') } else if((in[0] >= '0' && in[0] <= '9')
|| (in[0] >= 'a' && in[0] <= 'f') || (in[0] >= 'a' && in[0] <= 'f')
|| (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; 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.len = 0;
packetbuf.offset = 0; packetbuf.offset = 0;
outpacket.len = 0;
while ((choice = getopt(argc, argv, "vfhu:t:m:")) != -1) { while ((choice = getopt(argc, argv, "vfhu:t:m:")) != -1) {
switch(choice) { switch(choice) {