Bring up tun interface upon creation

This commit is contained in:
Théophile Bastian 2020-06-04 13:31:44 +02:00
parent ba2293e039
commit 0332766f0d

View file

@ -44,6 +44,25 @@ TunDevice::TunDevice(const std::string& dev)
_dev_name = ifr.ifr_name;
_fd = fd;
// Bring interface up
kdebugf("Bringing interface up...\n");
int sockfd = socket(AF_INET, SOCK_DGRAM, 0); // Any socket will do
if(ioctl(sockfd, SIOCGIFFLAGS, (void*) &ifr) < 0) {
close(fd);
close(sockfd);
throw TunDevice::InitializationError(
"Could not get tunnel interface flags", errno, true);
}
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
if(ioctl(sockfd, SIOCSIFFLAGS, (void*) &ifr) < 0) {
close(fd);
close(sockfd);
throw TunDevice::InitializationError(
"Could not bring tunnel interface up", errno, true);
}
close(sockfd);
// The device is now fully set up
_poll_fd.fd = _fd;
_poll_fd.events = POLLIN;