From c816cb71d331df93278f5e96f0b8402e2a410992 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Mon, 5 Jun 2006 23:34:23 +0000 Subject: [PATCH] tun hanterar frames --- dnstun.c | 27 ++++++++++++++++++++------- dnstund.c | 14 +++++++++----- tun.c | 8 ++++---- tun.h | 11 +++++++++-- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/dnstun.c b/dnstun.c index ff75dcb..762161d 100644 --- a/dnstun.c +++ b/dnstun.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include "tun.h" @@ -28,6 +30,8 @@ #define MAX(a,b) ((a)>(b)?(a):(b)) +#define FRAMESIZE (64*1024) + int running = 1; static void @@ -41,9 +45,11 @@ tunnel(int tun_fd, int dns_fd) int i; int read; fd_set fds; - char buf[4096]; struct timeval tv; - + struct tun_frame *frame; + + frame = malloc(FRAMESIZE); + while (running) { tv.tv_sec = 1; tv.tv_usec = 0; @@ -64,25 +70,32 @@ tunnel(int tun_fd, int dns_fd) } if(i == 0) { - dns_ping(dns_fd); + //dns_ping(dns_fd); } else { if(FD_ISSET(tun_fd, &fds)) { - read = read_tun(tun_fd, buf, sizeof(buf)); + read = read_tun(tun_fd, frame, FRAMESIZE); if (read > 0) { + int fd; + + fd = open("moo", O_WRONLY | O_CREAT, S_IRGRP); + write(fd, frame->data, read - 4); + close(fd); printf("Got data on tun! %d bytes\n", read); - dns_handle_tun(dns_fd, buf, read); + dns_handle_tun(dns_fd, frame->data, read - 4); } } if(FD_ISSET(dns_fd, &fds)) { - read = dns_read(dns_fd, buf, sizeof(buf)); + read = dns_read(dns_fd, frame->data, FRAMESIZE-4); if (read > 0) { printf("Got data on dns! %d bytes\n", read); - write_tun(tun_fd, buf, read); + write_tun(tun_fd, frame, read + 4); } } } } + free(frame); + return 0; } diff --git a/dnstund.c b/dnstund.c index f1c8ebf..55cdf06 100644 --- a/dnstund.c +++ b/dnstund.c @@ -45,8 +45,10 @@ tunnel(int tun_fd, int dns_fd) int i; int read; fd_set fds; - char buf[64*1024]; struct timeval tv; + struct tun_frame *frame; + + frame = malloc(64*1024); while (running) { tv.tv_sec = 1; @@ -68,18 +70,20 @@ tunnel(int tun_fd, int dns_fd) if(i != 0) { if(FD_ISSET(tun_fd, &fds)) { - read = read_tun(tun_fd, buf, sizeof(buf)); + read = read_tun(tun_fd, frame, 64*1024); if(read > 0) - dnsd_queuepacket(buf, read); + dnsd_queuepacket(frame->data, read - 4); } if(FD_ISSET(dns_fd, &fds)) { - read = dnsd_read(dns_fd, buf, sizeof(buf)); + read = dnsd_read(dns_fd, frame->data, 64*1024-4); if(read > 0) - write_tun(tun_fd, buf, read); + write_tun(tun_fd, frame, read + 4); } } } + free(frame); + return 0; } diff --git a/tun.c b/tun.c index a1fe18a..9bb4783 100644 --- a/tun.c +++ b/tun.c @@ -118,9 +118,9 @@ close_tun(int tun_fd) } int -write_tun(int tun_fd, uint8_t *buf, int len) +write_tun(int tun_fd, struct tun_frame *frame, int len) { - if (write(tun_fd, buf, len) != len) { + if (write(tun_fd, frame, len) != len) { warn("write_tun"); return 1; } @@ -129,8 +129,8 @@ write_tun(int tun_fd, uint8_t *buf, int len) } int -read_tun(int tun_fd, uint8_t *buf, int len) +read_tun(int tun_fd, struct tun_frame *frame, int len) { - return read(tun_fd, buf, len); + return read(tun_fd, frame, len); } diff --git a/tun.h b/tun.h index 4a0b31f..669a442 100644 --- a/tun.h +++ b/tun.h @@ -19,9 +19,16 @@ #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, uint8_t *, int); -int read_tun(int, uint8_t *, int); +int write_tun(int, struct tun_frame *, int); +int read_tun(int, struct tun_frame *, int); #endif /* _TUN_H_ */