iodine/tun.c

137 lines
2.7 KiB
C
Raw Normal View History

2006-06-05 12:41:08 +00:00
/*
2006-06-05 14:15:53 +00:00
* Copyright (c) 2006 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
2006-06-05 12:41:08 +00:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
2006-06-05 12:41:08 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <err.h>
#include "tun.h"
#define TUN_MAX_TRY 50
char *tun_device = NULL;
#ifdef LINUX
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_tun.h>
int
open_tun()
{
int i;
2006-06-05 14:43:04 +00:00
int tun_fd;
struct ifreq ifreq;
2006-06-05 12:41:08 +00:00
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));
2006-06-05 14:43:04 +00:00
return 0;
}
2006-06-05 12:41:08 +00:00
bzero(&ifreq, sizeof(ifreq));
ifreq.ifr_flags = IFF_TUN;
for (i = 0; i < TUN_MAX_TRY; i++) {
2006-06-05 13:00:34 +00:00
snprintf(ifreq.ifr_name, IFNAMSIZ, "dns%d", i);
2006-06-05 12:41:08 +00:00
if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) {
2006-06-05 13:40:07 +00:00
printf("Opened %s\n", ifreq.ifr_name);
2006-06-05 14:43:04 +00:00
return tun_fd;
2006-06-05 12:41:08 +00:00
}
if (errno != EBUSY) {
warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno));
2006-06-05 14:43:04 +00:00
return 0;
}
2006-06-05 12:41:08 +00:00
}
warn("open_tun: Couldn't set interface name.\n");
2006-06-05 12:41:08 +00:00
2006-06-05 14:43:04 +00:00
return 0;
2006-06-05 12:41:08 +00:00
}
#else /* BSD */
int
open_tun()
{
2006-06-05 14:43:04 +00:00
int i;
int tun_fd;
char tun_name[50];
2006-06-05 12:41:08 +00:00
if (tun_device != NULL) {
if ((tun_fd = open(tun_device, O_RDWR)) < 0) {
warn("open_tun: %s: %s", tun_device, strerror(errno));
2006-06-05 14:43:04 +00:00
return 0;
}
2006-06-05 12:41:08 +00:00
} else {
for (i = 0; i < TUN_MAX_TRY; i++) {
snprintf(tun_name, sizeof(tun_name), "/dev/tun%d", i);
2006-06-05 13:40:07 +00:00
if ((tun_fd = open(tun_name, O_RDWR)) >= 0) {
printf("Opened %s\n", tun_name);
2006-06-05 14:43:04 +00:00
return tun_fd;
2006-06-05 13:40:07 +00:00
}
2006-06-05 12:41:08 +00:00
if (errno == ENOENT)
break;
}
warn("open_tun: Failed to open tunneling device.");
2006-06-05 14:43:04 +00:00
return 0;
2006-06-05 12:41:08 +00:00
}
return 0;
}
#endif /* LINUX */
void
2006-06-05 14:43:04 +00:00
close_tun(int tun_fd)
2006-06-05 12:41:08 +00:00
{
if (tun_fd >= 0)
close(tun_fd);
}
int
2006-06-05 14:43:04 +00:00
write_tun(int tun_fd, uint8_t *buf, int len)
2006-06-05 12:41:08 +00:00
{
if (write(tun_fd, buf, len) != len) {
2006-06-05 22:36:05 +00:00
warn("write_tun");
return 1;
}
2006-06-05 12:41:08 +00:00
return 0;
}
int
2006-06-05 14:43:04 +00:00
read_tun(int tun_fd, uint8_t *buf, int len)
2006-06-05 12:41:08 +00:00
{
return read(tun_fd, buf, len);
}