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);
}
void
dns_ping()
{
}
void
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 dns_set_peer(const char *);
void dns_ping();
void dns_query(int, char *, int);
#endif /* _DNS_H_ */

View File

@ -16,22 +16,66 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <err.h>
#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;
}

31
tun.c
View File

@ -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);
}

9
tun.h
View File

@ -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_ */