From 5c61baa2be5a3538464ad11de84cab1d04e48587 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Mon, 5 Jun 2006 14:43:04 +0000 Subject: [PATCH] tunnel and stuff --- dns.c | 6 ++++++ dns.h | 2 ++ dnstun.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++-------- tun.c | 31 ++++++++++++++-------------- tun.h | 9 +++----- 5 files changed, 80 insertions(+), 30 deletions(-) diff --git a/dns.c b/dns.c index 069f2e3..a156b52 100644 --- a/dns.c +++ b/dns.c @@ -86,6 +86,12 @@ dns_set_peer(const char *host) peer.sin_addr = *((struct in_addr *) h->h_addr); } +void +dns_ping() +{ + +} + void dns_query(int fd, char *host, int type) { diff --git a/dns.h b/dns.h index 21f9a0b..74ff79f 100644 --- a/dns.h +++ b/dns.h @@ -25,6 +25,8 @@ int open_dns(); void close_dns(int); void dns_set_peer(const char *); +void dns_ping(); void dns_query(int, char *, int); + #endif /* _DNS_H_ */ diff --git a/dnstun.c b/dnstun.c index c8e0b02..bf761f0 100644 --- a/dnstun.c +++ b/dnstun.c @@ -16,22 +16,66 @@ #include #include +#include +#include #include "tun.h" #include "dns.h" -int -main() +#define MAX(a,b) ((a)>(b)?(a):(b)) + +static int +tunnel(int tun_fd, int dns_fd) { - int dnssock; + int i; + fd_set fds; + struct timeval tv; + + for (;;) { + tv.tv_sec = 1; + tv.tv_usec = 0; - open_tun(); - dnssock = open_dns(); - dns_set_peer("192.168.11.101"); - dns_query(dnssock, "kryo.se", 1); + FD_ZERO(&fds); + FD_SET(tun_fd, &fds); + FD_SET(dns_fd, &fds); - close_dns(dnssock); - close_tun(); + i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv); + + if(i < 0) { + warn("select"); + return 1; + } + + if(i == 0) { + dns_ping(); + } else { + if(FD_ISSET(tun_fd, &fds)) { + + } + if(FD_ISSET(dns_fd, &fds)) { + + } + } + } + + return 0; +} + +int +main() +{ + int tun_fd; + int dns_fd; + + tun_fd = open_tun(); + dns_fd = open_dns(); + dns_set_peer("192.168.11.101"); + dns_query(dns_fd, "kryo.se", 1); + + tunnel(tun_fd, dns_fd); + + close_dns(dns_fd); + close_tun(tun_fd); return 0; } diff --git a/tun.c b/tun.c index 289bf40..adb4898 100644 --- a/tun.c +++ b/tun.c @@ -28,7 +28,6 @@ #define TUN_MAX_TRY 50 -int tun_fd = -1; char *tun_device = NULL; #ifdef LINUX @@ -40,15 +39,16 @@ char *tun_device = NULL; int open_tun() { - struct ifreq ifreq; int i; + int tun_fd; + struct ifreq ifreq; if (tun_device == NULL) tun_device = "/dev/net/tun"; if ((tun_fd = open(tun_device, O_RDWR)) < 0) { warn("open_tun: %s: %s", tun_device, strerror(errno)); - return 1; + return 0; } bzero(&ifreq, sizeof(ifreq)); @@ -60,18 +60,18 @@ open_tun() if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) { printf("Opened %s\n", ifreq.ifr_name); - return 0; + return tun_fd; } if (errno != EBUSY) { warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno)); - return 1; + return 0; } } warn("open_tun: Couldn't set interface name.\n"); - return 1; + return 0; } #else /* BSD */ @@ -79,21 +79,22 @@ open_tun() int open_tun() { + int i; + int tun_fd; + char tun_name[50]; + if (tun_device != NULL) { if ((tun_fd = open(tun_device, O_RDWR)) < 0) { warn("open_tun: %s: %s", tun_device, strerror(errno)); - return 1; + return 0; } } else { - char tun_name[50]; - int i; - for (i = 0; i < TUN_MAX_TRY; i++) { snprintf(tun_name, sizeof(tun_name), "/dev/tun%d", i); if ((tun_fd = open(tun_name, O_RDWR)) >= 0) { printf("Opened %s\n", tun_name); - return 0; + return tun_fd; } if (errno == ENOENT) @@ -101,7 +102,7 @@ open_tun() } warn("open_tun: Failed to open tunneling device."); - return 1; + return 0; } return 0; @@ -110,14 +111,14 @@ open_tun() #endif /* LINUX */ void -close_tun() +close_tun(int tun_fd) { if (tun_fd >= 0) close(tun_fd); } int -write_tun(uint8_t *buf, int len) +write_tun(int tun_fd, uint8_t *buf, int len) { if (write(tun_fd, buf, len) != len) { warn("write_tun: %s", strerror(errno)); @@ -128,7 +129,7 @@ write_tun(uint8_t *buf, int len) } int -read_tun(uint8_t *buf, int len) +read_tun(int tun_fd, uint8_t *buf, int len) { return read(tun_fd, buf, len); } diff --git a/tun.h b/tun.h index 3cd3420..4a0b31f 100644 --- a/tun.h +++ b/tun.h @@ -19,12 +19,9 @@ #ifndef _TUN_H_ #define _TUN_H_ -extern char *tun_device; -extern int tun_fd; - int open_tun(); -void close_tun(); -int write_tun(uint8_t *buf, int len); -int read_tun(uint8_t *buf, int len); +void close_tun(int); +int write_tun(int, uint8_t *, int); +int read_tun(int, uint8_t *, int); #endif /* _TUN_H_ */