wangyu-UDPspeeder/tun_dev.cpp

35 lines
536 B
C++
Raw Normal View History

2017-10-26 08:38:52 -05:00
/*
* tun.cpp
*
* Created on: Oct 26, 2017
* Author: root
*/
2017-10-26 09:49:46 -05:00
#include "common.h"
#include "log.h"
2017-10-26 08:38:52 -05:00
2017-10-26 09:49:46 -05:00
int get_tun_fd(char * dev_name)
{
int tun_fd=open("/dev/net/tun",O_RDWR);
2017-10-26 08:38:52 -05:00
2017-10-26 09:49:46 -05:00
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;
2017-10-26 08:38:52 -05:00
2017-10-26 09:49:46 -05:00
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;
}
2017-10-26 08:38:52 -05:00