From ba07d1f91d49203856a28890521daf1a5cb2cfcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Skowro=C5=84ski?= Date: Tue, 27 Dec 2016 11:57:05 +0100 Subject: [PATCH] cleanup --- README.md | 2 +- osdep/NetBSDEthernetTap.cpp | 43 +++++++------------------------------ osdep/freebsd_getifmaddrs.c | 5 +++++ osdep/freebsd_getifmaddrs.h | 5 +++++ 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index a34a1a53a..f9ee85ee4 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ For Mac, Linux, and BSD, just type "make" (or "gmake" on BSD). You won't need mu * **Mac**: Xcode command line tools. It should build on OSX 10.7 or newer. * **Linux**: gcc/g++ (4.9 or newer recommended) or clang/clang++ (3.4 or newer recommended) Makefile will use clang by default if available. The Linux build will auto-detect the presence of development headers for *json-parser*, *http-parser*, *li8bnatpmp*, and *libminiupnpc* and will link against the system libraries for these if they are present and recent enough. Otherwise the bundled versions in [ext/](ext/) will be used. Type `make install` to install the binaries and other files on the system, though this will not create init.d or systemd links. - * **FreeBSD**: C++ compiler (G++ usually) and GNU make (gmake). + * **FreeBSD/OpenBSD/NetBSD**: C++ compiler (G++ usually) and GNU make (gmake). Each supported platform has its own *make-XXX.mk* file that contains the actual make rules for the platform. The right .mk file is included by the main Makefile based on the GNU make *OSTYPE* variable. Take a look at the .mk file for your platform for other targets, debug build rules, etc. diff --git a/osdep/NetBSDEthernetTap.cpp b/osdep/NetBSDEthernetTap.cpp index 8f715171e..5d1c96257 100644 --- a/osdep/NetBSDEthernetTap.cpp +++ b/osdep/NetBSDEthernetTap.cpp @@ -91,35 +91,17 @@ NetBSDEthernetTap::NetBSDEthernetTap( char devpath[64],ethaddr[64],mtustr[32],metstr[32],tmpdevname[32]; struct stat stattmp; - // On FreeBSD at least we can rename, so use nwid to generate a deterministic unique zt#### name using base32 - // As a result we don't use desiredDevice - _dev = "zt"; - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 60) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 55) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 50) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 45) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 40) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 35) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 30) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 25) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 20) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 15) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 10) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 5) & 0x1f)]); - _dev.push_back(ZT_BASE32_CHARS[(unsigned long)(nwid & 0x1f)]); - Mutex::Lock _gl(globalTapCreateLock); if (mtu > 2800) throw std::runtime_error("max tap MTU is 2800"); - // On NetBSD there are /dev/tap{0..3} pre-created and for a moment I will stick with only them + // we can create /dev/tap* std::vector devFiles(OSUtils::listDirectory("/dev")); -// 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()) { + if (std::find(devFiles.begin(),devFiles.end(),std::string(tmpdevname)) == devFiles.end()) { long cpid = (long)vfork(); if (cpid == 0) { ::execl("/sbin/ifconfig","/sbin/ifconfig",tmpdevname,"create",(const char *)0); @@ -134,7 +116,9 @@ NetBSDEthernetTap::NetBSDEthernetTap( 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 + ::execl("/sbin/mknod","/sbin/mknod",devpath,"c","169",minor.c_str(),(const char *)0); + // http://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/conf/majors + // major 169 => tap ::_exit(-1); } else if (cpid > 0) { int exitcode = -1; @@ -146,25 +130,14 @@ NetBSDEthernetTap::NetBSDEthernetTap( _dev = tmpdevname; _fd = ::open( devpath,O_RDWR); if (!stat(devpath,&stattmp)) { - /*cpid = (long)vfork(); - if (cpid == 0) { - ::execl("/sbin/ifconfig","/sbin/ifconfig",tmpdevname,"name",_dev.c_str(),(const char *)0); - ::_exit(-1); - } else if (cpid > 0) { - int exitcode = -1; - ::waitpid(cpid,&exitcode,0); - if (exitcode) - throw std::runtime_error("ifconfig rename operation failed"); - } else throw std::runtime_error("fork() failed");*/ - - if (_fd > 0) break; - //else throw std::runtime_error("unable to open created tap device "); + 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"); } - //} + } } if (_fd <= 0) diff --git a/osdep/freebsd_getifmaddrs.c b/osdep/freebsd_getifmaddrs.c index 559fdc065..8b1741466 100644 --- a/osdep/freebsd_getifmaddrs.c +++ b/osdep/freebsd_getifmaddrs.c @@ -1,3 +1,8 @@ +/* + this BSD licensed code is from https://github.com/freebsd/freebsd/blob/386ddae58459341ec567604707805814a2128a57/lib/libc/net/getifmaddrs.c + as in older OS X there is no getifmaddrs() and related functions is NetBSD +*/ + #define NET_RT_IFMALIST 4 /* return multicast address list */ #define RTM_NEWMADDR 0xf /* mcast group membership being added to if */ diff --git a/osdep/freebsd_getifmaddrs.h b/osdep/freebsd_getifmaddrs.h index a3eeec713..166b6d230 100644 --- a/osdep/freebsd_getifmaddrs.h +++ b/osdep/freebsd_getifmaddrs.h @@ -1,3 +1,8 @@ +/* + this BSD code is from https://github.com/freebsd/freebsd/blob/386ddae58459341ec567604707805814a2128a57/include/ifaddrs.h + as in older OS X there is no getifmaddrs() and related functions is NetBSD +*/ + /* $FreeBSD$ */ /*