Created
April 28, 2016 05:06
-
-
Save PinkPandaKatie/b98b68def49c50ea15e9af8428eea399 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Based on example code at https://www.kernel.org/doc/Documentation/networking/tuntap.txt | |
* | |
* Simple program to create a tun device, then wait forever. No external dependencies, except libc. | |
* | |
* Compiling: gcc tuntest.c -o tuntest | |
*/ | |
#include <unistd.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <sys/stat.h> | |
#include <sys/ioctl.h> | |
#include <fcntl.h> | |
#include <linux/if.h> | |
#include <linux/if_tun.h> | |
int tun_alloc(char *dev) | |
{ | |
struct ifreq ifr; | |
int fd, err; | |
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) | |
return 0; | |
memset(&ifr, 0, sizeof(ifr)); | |
/* Flags: IFF_TUN - TUN device (no Ethernet headers) | |
* IFF_TAP - TAP device | |
* | |
* IFF_NO_PI - Do not provide packet information | |
*/ | |
ifr.ifr_flags = IFF_TUN; | |
if( *dev ) | |
strncpy(ifr.ifr_name, dev, IFNAMSIZ); | |
if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){ | |
close(fd); | |
return err; | |
} | |
strcpy(dev, ifr.ifr_name); | |
return fd; | |
} | |
int main(int argc, char** argv) { | |
int fd; | |
char* dev = strdup("tuntest"); | |
fd = tun_alloc(dev); | |
while(1) sleep(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment