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 name[255];
char rdata[4*1024]; char rdata[4*1024];
HEADER *header; HEADER *header;
socklen_t addrlen;
char packet[64*1024]; char packet[64*1024];
struct sockaddr_in from;
r = recv(fd, packet, sizeof(packet), 0); addrlen = sizeof(struct sockaddr);
r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen);
//printf("Read %d bytes DNS reply\n", r);
if(r == -1) { if(r == -1) {
perror("recv"); perror("recvfrom");
} else { } else {
header = (HEADER*)packet; header = (HEADER*)packet;
@ -571,8 +572,6 @@ dnsd_read(int fd, char *buf, int buflen)
addrlen = sizeof(struct sockaddr); addrlen = sizeof(struct sockaddr);
r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen); 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) { if(r == -1) {
perror("recvfrom"); perror("recvfrom");
} else { } else {

View File

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

View File

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

20
tun.c
View File

@ -120,9 +120,21 @@ close_tun(int tun_fd)
} }
int 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"); warn("write_tun");
return 1; return 1;
} }
@ -131,8 +143,8 @@ write_tun(int tun_fd, struct tun_frame *frame, int len)
} }
int 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_ #ifndef _TUN_H_
#define _TUN_H_ #define _TUN_H_
struct tun_frame
{
short flags;
short proto;
char data[];
};
int open_tun(); int open_tun();
void close_tun(int); void close_tun(int);
int write_tun(int, struct tun_frame *, int); int write_tun(int, char *, int);
int read_tun(int, struct tun_frame *, int); int read_tun(int, char *, int);
#endif /* _TUN_H_ */ #endif /* _TUN_H_ */