/* * tun.cpp * * Created on: Oct 26, 2017 * Author: root */ #include "common.h" #include "log.h" int get_tun_fd(char * dev_name) { int tun_fd=open("/dev/net/tun",O_RDWR); if(tun_fd <0) { mylog(log_fatal,"open /dev/net/tun failed"); myexit(-1); } struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TUN|IFF_NO_PI; strncpy(ifr.ifr_name, dev_name, IFNAMSIZ); if(ioctl(tun_fd, TUNSETIFF, (void *)&ifr) != 0) { mylog(log_fatal,"open /dev/net/tun failed"); myexit(-1); } return tun_fd; }