tunnel and stuff

This commit is contained in:
Bjorn Andersson 2006-06-05 14:43:04 +00:00
parent f21e85b574
commit 5c61baa2be
5 changed files with 80 additions and 30 deletions

6
dns.c
View file

@ -86,6 +86,12 @@ dns_set_peer(const char *host)
peer.sin_addr = *((struct in_addr *) h->h_addr); peer.sin_addr = *((struct in_addr *) h->h_addr);
} }
void
dns_ping()
{
}
void void
dns_query(int fd, char *host, int type) dns_query(int fd, char *host, int type)
{ {

2
dns.h
View file

@ -25,6 +25,8 @@ int open_dns();
void close_dns(int); void close_dns(int);
void dns_set_peer(const char *); void dns_set_peer(const char *);
void dns_ping();
void dns_query(int, char *, int); void dns_query(int, char *, int);
#endif /* _DNS_H_ */ #endif /* _DNS_H_ */

View file

@ -16,22 +16,66 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include <err.h>
#include "tun.h" #include "tun.h"
#include "dns.h" #include "dns.h"
int #define MAX(a,b) ((a)>(b)?(a):(b))
main()
static int
tunnel(int tun_fd, int dns_fd)
{ {
int dnssock; int i;
fd_set fds;
struct timeval tv;
open_tun(); for (;;) {
dnssock = open_dns(); tv.tv_sec = 1;
dns_set_peer("192.168.11.101"); tv.tv_usec = 0;
dns_query(dnssock, "kryo.se", 1);
close_dns(dnssock); FD_ZERO(&fds);
close_tun(); FD_SET(tun_fd, &fds);
FD_SET(dns_fd, &fds);
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; return 0;
} }

31
tun.c
View file

@ -28,7 +28,6 @@
#define TUN_MAX_TRY 50 #define TUN_MAX_TRY 50
int tun_fd = -1;
char *tun_device = NULL; char *tun_device = NULL;
#ifdef LINUX #ifdef LINUX
@ -40,15 +39,16 @@ char *tun_device = NULL;
int int
open_tun() open_tun()
{ {
struct ifreq ifreq;
int i; int i;
int tun_fd;
struct ifreq ifreq;
if (tun_device == NULL) if (tun_device == NULL)
tun_device = "/dev/net/tun"; tun_device = "/dev/net/tun";
if ((tun_fd = open(tun_device, O_RDWR)) < 0) { if ((tun_fd = open(tun_device, O_RDWR)) < 0) {
warn("open_tun: %s: %s", tun_device, strerror(errno)); warn("open_tun: %s: %s", tun_device, strerror(errno));
return 1; return 0;
} }
bzero(&ifreq, sizeof(ifreq)); bzero(&ifreq, sizeof(ifreq));
@ -60,18 +60,18 @@ open_tun()
if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) { if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) {
printf("Opened %s\n", ifreq.ifr_name); printf("Opened %s\n", ifreq.ifr_name);
return 0; return tun_fd;
} }
if (errno != EBUSY) { if (errno != EBUSY) {
warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno)); warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno));
return 1; return 0;
} }
} }
warn("open_tun: Couldn't set interface name.\n"); warn("open_tun: Couldn't set interface name.\n");
return 1; return 0;
} }
#else /* BSD */ #else /* BSD */
@ -79,21 +79,22 @@ open_tun()
int int
open_tun() open_tun()
{ {
int i;
int tun_fd;
char tun_name[50];
if (tun_device != NULL) { if (tun_device != NULL) {
if ((tun_fd = open(tun_device, O_RDWR)) < 0) { if ((tun_fd = open(tun_device, O_RDWR)) < 0) {
warn("open_tun: %s: %s", tun_device, strerror(errno)); warn("open_tun: %s: %s", tun_device, strerror(errno));
return 1; return 0;
} }
} else { } else {
char tun_name[50];
int i;
for (i = 0; i < TUN_MAX_TRY; i++) { for (i = 0; i < TUN_MAX_TRY; i++) {
snprintf(tun_name, sizeof(tun_name), "/dev/tun%d", i); snprintf(tun_name, sizeof(tun_name), "/dev/tun%d", i);
if ((tun_fd = open(tun_name, O_RDWR)) >= 0) { if ((tun_fd = open(tun_name, O_RDWR)) >= 0) {
printf("Opened %s\n", tun_name); printf("Opened %s\n", tun_name);
return 0; return tun_fd;
} }
if (errno == ENOENT) if (errno == ENOENT)
@ -101,7 +102,7 @@ open_tun()
} }
warn("open_tun: Failed to open tunneling device."); warn("open_tun: Failed to open tunneling device.");
return 1; return 0;
} }
return 0; return 0;
@ -110,14 +111,14 @@ open_tun()
#endif /* LINUX */ #endif /* LINUX */
void void
close_tun() close_tun(int tun_fd)
{ {
if (tun_fd >= 0) if (tun_fd >= 0)
close(tun_fd); close(tun_fd);
} }
int 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) { if (write(tun_fd, buf, len) != len) {
warn("write_tun: %s", strerror(errno)); warn("write_tun: %s", strerror(errno));
@ -128,7 +129,7 @@ write_tun(uint8_t *buf, int len)
} }
int int
read_tun(uint8_t *buf, int len) read_tun(int tun_fd, uint8_t *buf, int len)
{ {
return read(tun_fd, buf, len); return read(tun_fd, buf, len);
} }

9
tun.h
View file

@ -19,12 +19,9 @@
#ifndef _TUN_H_ #ifndef _TUN_H_
#define _TUN_H_ #define _TUN_H_
extern char *tun_device;
extern int tun_fd;
int open_tun(); int open_tun();
void close_tun(); void close_tun(int);
int write_tun(uint8_t *buf, int len); int write_tun(int, uint8_t *, int);
int read_tun(uint8_t *buf, int len); int read_tun(int, uint8_t *, int);
#endif /* _TUN_H_ */ #endif /* _TUN_H_ */