cleanup in the tunnelcode, now sending the frameheader

This commit is contained in:
Bjorn Andersson 2006-06-11 13:49:36 +00:00
parent 20789ac53d
commit 5696b3c869
5 changed files with 65 additions and 81 deletions

11
dns.c
View file

@ -326,14 +326,15 @@ dns_read(int fd, char *buf, int buflen)
char name[255];
char rdata[4*1024];
HEADER *header;
socklen_t addrlen;
char packet[64*1024];
struct sockaddr_in from;
r = recv(fd, packet, sizeof(packet), 0);
//printf("Read %d bytes DNS reply\n", r);
addrlen = sizeof(struct sockaddr);
r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen);
if(r == -1) {
perror("recv");
perror("recvfrom");
} else {
header = (HEADER*)packet;
@ -571,8 +572,6 @@ dnsd_read(int fd, char *buf, int buflen)
addrlen = sizeof(struct sockaddr);
r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen);
//printf("Read %d bytes DNS query from %s\n", r, inet_ntoa(from.sin_addr));
if(r == -1) {
perror("recvfrom");
} else {

View file

@ -35,8 +35,6 @@
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
#define FRAMESIZE (64*1024)
int running = 1;
static void
@ -51,11 +49,9 @@ tunnel(int tun_fd, int dns_fd)
int read;
fd_set fds;
struct timeval tv;
struct tun_frame *frame;
long buflen;
char buf[64*1024];
frame = malloc(FRAMESIZE);
char in[64*1024];
long outlen;
char out[64*1024];
while (running) {
tv.tv_sec = 1;
@ -79,36 +75,28 @@ tunnel(int tun_fd, int dns_fd)
dns_ping(dns_fd);
} else {
if(FD_ISSET(tun_fd, &fds)) {
read = read_tun(tun_fd, frame, FRAMESIZE);
if (read > 0) {
buflen = sizeof(buf);
compress2(buf, &buflen, frame->data, read - 4, 9);
dns_handle_tun(dns_fd, buf, buflen);
}
read = read_tun(tun_fd, in, sizeof(in));
if(read <= 0)
continue;
outlen = sizeof(out);
compress2(out, &outlen, in, read, 9);
dns_handle_tun(dns_fd, out, outlen);
}
if(FD_ISSET(dns_fd, &fds)) {
read = dns_read(dns_fd, buf, FRAMESIZE-4);
if (read > 0) {
buflen = 64*1024-4;
uncompress(frame->data, &buflen, buf, read);
read = dns_read(dns_fd, in, sizeof(in));
if (read <= 0)
continue;
frame->flags = htons(0x0000);
#ifdef LINUX
frame->proto = htons(0x0800); // Linux wants ETH_P_IP
#else
frame->proto = htons(0x0002); // BSD wants AF_INET as long word
#endif
outlen = sizeof(out);
uncompress(out, &outlen, in, read);
write_tun(tun_fd, frame, buflen + 4);
if (!dns_sending()) {
write_tun(tun_fd, out, outlen);
if (!dns_sending())
dns_ping(dns_fd);
}
}
}
}
}
free(frame);
return 0;
}
@ -151,11 +139,6 @@ main(int argc, char **argv)
username = NULL;
foreground = 0;
if (geteuid() != 0) {
printf("Run as root and you'll be happy.\n");
usage();
}
while ((choice = getopt(argc, argv, "vfhu:")) != -1) {
switch(choice) {
case 'v':
@ -176,6 +159,11 @@ main(int argc, char **argv)
}
}
if (geteuid() != 0) {
printf("Run as root and you'll be happy.\n");
usage();
}
argc -= optind;
argv += optind;

View file

@ -49,11 +49,9 @@ tunnel(int tun_fd, int dns_fd)
int read;
fd_set fds;
struct timeval tv;
struct tun_frame *frame;
long buflen;
char buf[64*1024];
frame = malloc(64*1024);
char in[64*1024];
long outlen;
char out[64*1024];
while (running) {
if (dnsd_hasack()) {
@ -82,32 +80,26 @@ tunnel(int tun_fd, int dns_fd)
dnsd_forceack(dns_fd);
} else {
if(FD_ISSET(tun_fd, &fds)) {
read = read_tun(tun_fd, frame, 64*1024);
if(read > 0) {
buflen = sizeof(buf);
compress2(buf, &buflen, frame->data, read - 4, 9);
dnsd_queuepacket(buf, buflen);
}
read = read_tun(tun_fd, in, sizeof(in));
if (read <= 0)
continue;
outlen = sizeof(out);
compress2(out, &outlen, in, read, 9);
dnsd_queuepacket(out, outlen);
}
if(FD_ISSET(dns_fd, &fds)) {
read = dnsd_read(dns_fd, buf, 64*1024-4);
if(read > 0) {
buflen = 64*1024-4;
uncompress(frame->data, &buflen, buf, read);
read = dnsd_read(dns_fd, in, sizeof(in));
if (read <= 0)
continue;
frame->flags = htons(0x0000);
#ifdef LINUX
frame->proto = htons(0x0800); // Linux wants ETH_P_IP
#else
frame->proto = htons(0x0002); // BSD wants AF_INET as long word
#endif
write_tun(tun_fd, frame, buflen + 4);
}
}
}
}
outlen = sizeof(out);
uncompress(out, &outlen, in, read);
free(frame);
write_tun(tun_fd, out, outlen);
}
}
}
return 0;
}

20
tun.c
View file

@ -120,9 +120,21 @@ close_tun(int tun_fd)
}
int
write_tun(int tun_fd, struct tun_frame *frame, int len)
write_tun(int tun_fd, char *data, int len)
{
if (write(tun_fd, frame, len) != len) {
#ifdef LINUX
data[0] = 0x00;
data[1] = 0x00;
data[2] = 0x08;
data[3] = 0x00;
#else /* LINUX */
data[0] = 0x00;
data[1] = 0x00;
data[2] = 0x00;
data[3] = 0x02;
#endif /* !LINUX */
if (write(tun_fd, data, len) != len) {
warn("write_tun");
return 1;
}
@ -131,8 +143,8 @@ write_tun(int tun_fd, struct tun_frame *frame, int len)
}
int
read_tun(int tun_fd, struct tun_frame *frame, int len)
read_tun(int tun_fd, char *buf, int len)
{
return read(tun_fd, frame, len);
return read(tun_fd, buf, len);
}

11
tun.h
View file

@ -19,16 +19,9 @@
#ifndef _TUN_H_
#define _TUN_H_
struct tun_frame
{
short flags;
short proto;
char data[];
};
int open_tun();
void close_tun(int);
int write_tun(int, struct tun_frame *, int);
int read_tun(int, struct tun_frame *, int);
int write_tun(int, char *, int);
int read_tun(int, char *, int);
#endif /* _TUN_H_ */