Skip to content

Instantly share code, notes, and snippets.

@baris
Created March 11, 2010 15:46
Show Gist options
  • Save baris/329255 to your computer and use it in GitHub Desktop.
Save baris/329255 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <error.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/if_tun.h>
#include <net/if.h>
int main()
{
struct ifreq ifr;
int f, err;
char* dev;
memset(&ifr, 0, sizeof(ifr));
err = 0;
f = open("/dev/net/tun", O_RDWR);
dev= strdup("tap0");
ifr.ifr_flags = IFF_TAP;
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
printf("%s\n", dev);
if( (err = ioctl(f, TUNSETIFF,(void *) &ifr)) < 0 ){
perror("error\n");
close(f);
return err;
}
strcpy(dev, ifr.ifr_name);
printf("%s\n", dev);
free(dev);
return system("ifconfig tap0 up");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment