From 0332766f0d67bdc11c47dbb2afb69fad988d1212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Thu, 4 Jun 2020 13:31:44 +0200 Subject: [PATCH] Bring up tun interface upon creation --- TunDevice.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/TunDevice.cpp b/TunDevice.cpp index 0787f15..a7b3fe2 100644 --- a/TunDevice.cpp +++ b/TunDevice.cpp @@ -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;