diff --git a/dnsd.c b/dnsd.c index 2e411c7..d8c2d38 100644 --- a/dnsd.c +++ b/dnsd.c @@ -36,8 +36,11 @@ struct sockaddr_in peer; char topdomain[256]; // Current IP packet -char activepacket[4096]; int packetlen; +char activepacket[4096]; + +int outbuflen; +char outbuf[64*1024]; static int readname(char *packet, char *dst, char *src) @@ -159,6 +162,20 @@ host2dns(const char *host, char *buffer, int size) return p - buffer; } +int +dnsd_haspacket() +{ + return (outbuflen > 0); +} + +void +dnsd_queuepacket(const char *buf, const int buflen) +{ + memcpy(outbuf, buf, buflen); + + outbuflen = buflen; +} + static void dnsd_respond(int fd, short id, struct sockaddr_in from) { diff --git a/dnsd.h b/dnsd.h index 45f1eb1..fce325b 100644 --- a/dnsd.h +++ b/dnsd.h @@ -24,5 +24,8 @@ void close_dnsd(int); int dnsd_read(int, char *, int); +int dnsd_haspacket(); +void dnsd_queuepacket(const char *, const int); + #endif /* _DNSD_H_ */ diff --git a/dnstund.c b/dnstund.c index 0a49d3f..f07097c 100644 --- a/dnstund.c +++ b/dnstund.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include "tun.h" @@ -31,6 +33,7 @@ int running = 1; + static void sigint(int sig) { running = 0; @@ -42,7 +45,7 @@ tunnel(int tun_fd, int dns_fd) int i; int read; fd_set fds; - char buf[1024]; + char buf[64*1024]; struct timeval tv; while (running) { @@ -50,7 +53,8 @@ tunnel(int tun_fd, int dns_fd) tv.tv_usec = 0; FD_ZERO(&fds); - FD_SET(tun_fd, &fds); + if(!dnsd_haspacket()) + FD_SET(tun_fd, &fds); FD_SET(dns_fd, &fds); i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv); @@ -64,10 +68,23 @@ tunnel(int tun_fd, int dns_fd) if(i != 0) { if(FD_ISSET(tun_fd, &fds)) { - + read = read_tun(tun_fd, buf, sizeof(buf)); + if(read > 0) + dnsd_queuepacket(buf, read); } if(FD_ISSET(dns_fd, &fds)) { read = dnsd_read(dns_fd, buf, sizeof(buf)); + int fd; + + if(read > 0) { + fd = open("moo", O_WRONLY | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH); + write(fd, buf, read); + close(fd); + } + /* + if(read > 0) + write_tun(tun_fd, buf, read); + */ } } } diff --git a/tun.c b/tun.c index adb4898..a1fe18a 100644 --- a/tun.c +++ b/tun.c @@ -121,7 +121,7 @@ int write_tun(int tun_fd, uint8_t *buf, int len) { if (write(tun_fd, buf, len) != len) { - warn("write_tun: %s", strerror(errno)); + warn("write_tun"); return 1; }