From f60dfe496325bfe5d58b3db75e86387799b72c25 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 19 Dec 2014 15:18:20 -0800 Subject: FreeBSD works, and some documentation fixes. --- osnet/BSDEthernetTap.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'osnet') diff --git a/osnet/BSDEthernetTap.cpp b/osnet/BSDEthernetTap.cpp index 8b28510d..100fb171 100644 --- a/osnet/BSDEthernetTap.cpp +++ b/osnet/BSDEthernetTap.cpp @@ -117,10 +117,11 @@ BSDEthernetTap::BSDEthernetTap( // On BSD we create taps and they can have high numbers, so use ones starting // at 9993 to not conflict with other stuff. Then we rename it to zt - for(int i=9993;i<500;++i) { + std::map devFiles(Utils::listDirectory("/dev")); + for(int i=9993;i<(9993+128);++i) { Utils::snprintf(tmpdevname,sizeof(tmpdevname),"tap%d",i); Utils::snprintf(devpath,sizeof(devpath),"/dev/%s",tmpdevname); - if (stat(devpath,&stattmp)) { + if (devFiles.count(std::string(tmpdevname)) == 0) { long cpid = (long)vfork(); if (cpid == 0) { ::execl("/sbin/ifconfig","/sbin/ifconfig",tmpdevname,"create",(const char *)0); @@ -146,6 +147,8 @@ BSDEthernetTap::BSDEthernetTap( if (_fd > 0) break; else throw std::runtime_error("unable to open created tap device"); + } else { + throw std::runtime_error("cannot find /dev node for newly created tap device"); } } } @@ -190,6 +193,15 @@ BSDEthernetTap::~BSDEthernetTap() ::close(_fd); ::close(_shutdownSignalPipe[0]); ::close(_shutdownSignalPipe[1]); + + long cpid = (long)vfork(); + if (cpid == 0) { + ::execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"destroy",(const char *)0); + ::_exit(-1); + } else if (cpid > 0) { + int exitcode = -1; + ::waitpid(cpid,&exitcode,0); + } } void BSDEthernetTap::setEnabled(bool en) -- cgit v1.2.3