From 61a3cb27a6d7b0f0f798ae1381b4cceb10e93762 Mon Sep 17 00:00:00 2001 From: josh Date: Mon, 20 Sep 2010 00:38:44 +0000 Subject: [PATCH] added tun git-svn-id: svn://anubis/misc/tun@244 bd8a9e45-a331-0410-811e-c64571078777 --- Makefile | 5 +++++ tun.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 Makefile create mode 100644 tun.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f87195c --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ + +all: tun + +tun: tun.c + gcc -o $@ $^ diff --git a/tun.c b/tun.c new file mode 100644 index 0000000..66b8de1 --- /dev/null +++ b/tun.c @@ -0,0 +1,38 @@ + +#include +#include +#include +#include +#include +#include + +int main() +{ + struct ifreq ifr; + int fd, err; + if ( (fd = open("/dev/net/tun", O_RDWR)) < 0) + { + fprintf(stderr, "Couldn't open /dev/net/tun!\n"); + return -1; + } + + memset(&ifr, 0, sizeof(ifr)); + + ifr.ifr_flags = IFF_TUN; + strncpy(ifr.ifr_name, "tun99", IFNAMSIZ); + + if ( (err = ioctl(fd, TUNSETIFF, &ifr)) < 0) + { + close(fd); + fprintf(stderr, "Couldn't ioctl(fd, TUNSETIFF, &ifr)!\n"); + return -2; + } + printf("Device name: %s\n", ifr.ifr_name); + + printf("Hit enter\n"); + char chr; + scanf("%c", &chr); + + close(fd); + return 0; +}