From 9a2310395d8e4b9666530e5b57547482877b2668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Skowro=C5=84ski?= Date: Tue, 27 Dec 2016 11:42:43 +0100 Subject: [PATCH] mimic BSDEthernetTap but with creating/destroying char tap device --- osdep/NetBSDEthernetTap.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/osdep/NetBSDEthernetTap.cpp b/osdep/NetBSDEthernetTap.cpp index 1dbe312f4..8f715171e 100644 --- a/osdep/NetBSDEthernetTap.cpp +++ b/osdep/NetBSDEthernetTap.cpp @@ -115,7 +115,8 @@ NetBSDEthernetTap::NetBSDEthernetTap( // On NetBSD there are /dev/tap{0..3} pre-created and for a moment I will stick with only them std::vector devFiles(OSUtils::listDirectory("/dev")); - for(int i=0;i<4;++i) { +// for(int i=0;i<4;++i) { + 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 (std::find(devFiles.begin(),devFiles.end(),std::string(tmpdevname)) == devFiles.end()) { @@ -128,6 +129,20 @@ NetBSDEthernetTap::NetBSDEthernetTap( ::waitpid(cpid,&exitcode,0); } else throw std::runtime_error("fork() failed"); + cpid = (long)vfork(); + if (cpid == 0) { + string tmp; + sprintf((char*)tmp.c_str(), "%d", i); + string minor = tmp.c_str(); + ::execl("/sbin/mknod","/sbin/mknod",devpath,"c","169",minor.c_str(),(const char *)0); //major 169 => tap + ::_exit(-1); + } else if (cpid > 0) { + int exitcode = -1; + ::waitpid(cpid,&exitcode,0); + } else throw std::runtime_error("fork() failed"); + + cerr<<"created device "< 0) { + int exitcode = -1; + ::waitpid(cpid,&exitcode,0); + } else throw std::runtime_error("fork() failed"); } void NetBSDEthernetTap::setEnabled(bool en)