tun hanterar frames

This commit is contained in:
Bjorn Andersson 2006-06-05 23:34:23 +00:00
parent ce6ff57280
commit c816cb71d3
4 changed files with 42 additions and 18 deletions

View File

@ -21,6 +21,8 @@
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <err.h> #include <err.h>
#include "tun.h" #include "tun.h"
@ -28,6 +30,8 @@
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
#define FRAMESIZE (64*1024)
int running = 1; int running = 1;
static void static void
@ -41,9 +45,11 @@ tunnel(int tun_fd, int dns_fd)
int i; int i;
int read; int read;
fd_set fds; fd_set fds;
char buf[4096];
struct timeval tv; struct timeval tv;
struct tun_frame *frame;
frame = malloc(FRAMESIZE);
while (running) { while (running) {
tv.tv_sec = 1; tv.tv_sec = 1;
tv.tv_usec = 0; tv.tv_usec = 0;
@ -64,25 +70,32 @@ tunnel(int tun_fd, int dns_fd)
} }
if(i == 0) { if(i == 0) {
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, buf, sizeof(buf)); read = read_tun(tun_fd, frame, FRAMESIZE);
if (read > 0) { 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); 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)) { 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) { if (read > 0) {
printf("Got data on dns! %d bytes\n", read); 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; return 0;
} }

View File

@ -45,8 +45,10 @@ tunnel(int tun_fd, int dns_fd)
int i; int i;
int read; int read;
fd_set fds; fd_set fds;
char buf[64*1024];
struct timeval tv; struct timeval tv;
struct tun_frame *frame;
frame = malloc(64*1024);
while (running) { while (running) {
tv.tv_sec = 1; tv.tv_sec = 1;
@ -68,18 +70,20 @@ tunnel(int tun_fd, int dns_fd)
if(i != 0) { if(i != 0) {
if(FD_ISSET(tun_fd, &fds)) { 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) if(read > 0)
dnsd_queuepacket(buf, read); dnsd_queuepacket(frame->data, read - 4);
} }
if(FD_ISSET(dns_fd, &fds)) { 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) if(read > 0)
write_tun(tun_fd, buf, read); write_tun(tun_fd, frame, read + 4);
} }
} }
} }
free(frame);
return 0; return 0;
} }

8
tun.c
View File

@ -118,9 +118,9 @@ close_tun(int tun_fd)
} }
int 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"); warn("write_tun");
return 1; return 1;
} }
@ -129,8 +129,8 @@ write_tun(int tun_fd, uint8_t *buf, int len)
} }
int 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);
} }

11
tun.h
View File

@ -19,9 +19,16 @@
#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, uint8_t *, int); int write_tun(int, struct tun_frame *, int);
int read_tun(int, uint8_t *, int); int read_tun(int, struct tun_frame *, int);
#endif /* _TUN_H_ */ #endif /* _TUN_H_ */