diff options
author | Grant Limberg <glimberg@gmail.com> | 2015-05-21 19:14:49 -0700 |
---|---|---|
committer | Grant Limberg <glimberg@gmail.com> | 2015-05-21 19:14:49 -0700 |
commit | c430d88bd40d178685ac0a2e648d8c4ea675996c (patch) | |
tree | f69f497428fa34c6389173d39c889563dea9506c /osdep | |
parent | 9a00366b18bc2bdb3ddf4345edcc7a459eb5ed60 (diff) | |
parent | d9006712f6ffc975d97097caf2d2b4264405b32c (diff) | |
download | infinitytier-c430d88bd40d178685ac0a2e648d8c4ea675996c.tar.gz infinitytier-c430d88bd40d178685ac0a2e648d8c4ea675996c.zip |
Merge branch 'adamierymenko-dev' into android-jni
Conflicts:
.gitignore
Diffstat (limited to 'osdep')
-rw-r--r-- | osdep/BSDEthernetTap.cpp | 91 | ||||
-rw-r--r-- | osdep/BSDEthernetTap.hpp | 37 | ||||
-rw-r--r-- | osdep/BackgroundResolver.cpp | 130 | ||||
-rw-r--r-- | osdep/BackgroundResolver.hpp | 127 | ||||
-rw-r--r-- | osdep/Phy.hpp | 1 | ||||
-rw-r--r-- | osdep/WindowsEthernetTap.cpp | 19 |
6 files changed, 358 insertions, 47 deletions
diff --git a/osdep/BSDEthernetTap.cpp b/osdep/BSDEthernetTap.cpp index e9899bc4..2a504a6f 100644 --- a/osdep/BSDEthernetTap.cpp +++ b/osdep/BSDEthernetTap.cpp @@ -59,10 +59,12 @@ #include <map> #include <set> #include <algorithm> +#include <utility> #include "../node/Constants.hpp" #include "../node/Utils.hpp" #include "../node/Mutex.hpp" +#include "OSUtils.hpp" #include "BSDEthernetTap.hpp" #define ZT_BASE32_CHARS "0123456789abcdefghijklmnopqrstuv" @@ -73,17 +75,17 @@ static const ZeroTier::MulticastGroup _blindWildcardMulticastGroup(ZeroTier::MAC namespace ZeroTier { BSDEthernetTap::BSDEthernetTap( + const char *homePath, const MAC &mac, unsigned int mtu, unsigned int metric, uint64_t nwid, - const char *desiredDevice, const char *friendlyName, - void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &), + void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int), void *arg) : - EthernetTap("BSDEthernetTap",mac,mtu,metric), _handler(handler), _arg(arg), + _nwid(nwid), _mtu(mtu), _metric(metric), _fd(0), @@ -117,11 +119,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<base32 of nwid> - std::map<std::string,bool> devFiles(Utils::listDirectory("/dev")); + std::vector<std::string> devFiles(OSUtils::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 (devFiles.count(std::string(tmpdevname)) == 0) { + 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); @@ -207,7 +209,6 @@ BSDEthernetTap::~BSDEthernetTap() void BSDEthernetTap::setEnabled(bool en) { _enabled = en; - // TODO: interface status change } bool BSDEthernetTap::enabled() const @@ -229,17 +230,17 @@ static bool ___removeIp(const std::string &_dev,const InetAddress &ip) return false; // never reached, make compiler shut up about return value } -bool BSDEthernetTap::addIP(const InetAddress &ip) +bool BSDEthernetTap::addIp(const InetAddress &ip) { if (!ip) return false; - std::set<InetAddress> allIps(ips()); - if (allIps.count(ip) > 0) + std::vector<InetAddress> allIps(ips()); + if (std::find(allIps.begin(),allIps.end(),ip) != allIps.end()) return true; // IP/netmask already assigned // Remove and reconfigure if address is the same but netmask is different - for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i) { + for(std::vector<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i) { if ((i->ipsEqual(ip))&&(i->netmaskBits() != ip.netmaskBits())) { if (___removeIp(_dev,*i)) break; @@ -258,22 +259,25 @@ bool BSDEthernetTap::addIP(const InetAddress &ip) return false; } -bool BSDEthernetTap::removeIP(const InetAddress &ip) +bool BSDEthernetTap::removeIp(const InetAddress &ip) { - if (ips().count(ip) > 0) { + if (!ip) + return false; + std::vector<InetAddress> allIps(ips()); + if (std::find(allIps.begin(),allIps.end(),ip) != allIps.end()) { if (___removeIp(_dev,ip)) return true; } return false; } -std::set<InetAddress> BSDEthernetTap::ips() const +std::vector<InetAddress> BSDEthernetTap::ips() const { struct ifaddrs *ifa = (struct ifaddrs *)0; if (getifaddrs(&ifa)) - return std::set<InetAddress>(); + return std::vector<InetAddress>(); - std::set<InetAddress> r; + std::vector<InetAddress> r; struct ifaddrs *p = ifa; while (p) { @@ -282,14 +286,14 @@ std::set<InetAddress> BSDEthernetTap::ips() const case AF_INET: { struct sockaddr_in *sin = (struct sockaddr_in *)p->ifa_addr; struct sockaddr_in *nm = (struct sockaddr_in *)p->ifa_netmask; - r.insert(InetAddress(&(sin->sin_addr.s_addr),4,Utils::countBits((uint32_t)nm->sin_addr.s_addr))); + r.push_back(InetAddress(&(sin->sin_addr.s_addr),4,Utils::countBits((uint32_t)nm->sin_addr.s_addr))); } break; case AF_INET6: { struct sockaddr_in6 *sin = (struct sockaddr_in6 *)p->ifa_addr; struct sockaddr_in6 *nm = (struct sockaddr_in6 *)p->ifa_netmask; uint32_t b[4]; memcpy(b,nm->sin6_addr.s6_addr,sizeof(b)); - r.insert(InetAddress(sin->sin6_addr.s6_addr,16,Utils::countBits(b[0]) + Utils::countBits(b[1]) + Utils::countBits(b[2]) + Utils::countBits(b[3]))); + r.push_back(InetAddress(sin->sin6_addr.s6_addr,16,Utils::countBits(b[0]) + Utils::countBits(b[1]) + Utils::countBits(b[2]) + Utils::countBits(b[3]))); } break; } } @@ -299,6 +303,9 @@ std::set<InetAddress> BSDEthernetTap::ips() const if (ifa) freeifaddrs(ifa); + std::sort(r.begin(),r.end()); + std::unique(r.begin(),r.end()); + return r; } @@ -324,6 +331,45 @@ void BSDEthernetTap::setFriendlyName(const char *friendlyName) { } +void BSDEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed) +{ + std::vector<MulticastGroup> newGroups; + + struct ifmaddrs *ifmap = (struct ifmaddrs *)0; + if (!getifmaddrs(&ifmap)) { + struct ifmaddrs *p = ifmap; + while (p) { + if (p->ifma_addr->sa_family == AF_LINK) { + struct sockaddr_dl *in = (struct sockaddr_dl *)p->ifma_name; + struct sockaddr_dl *la = (struct sockaddr_dl *)p->ifma_addr; + if ((la->sdl_alen == 6)&&(in->sdl_nlen <= _dev.length())&&(!memcmp(_dev.data(),in->sdl_data,in->sdl_nlen))) + newGroups.push_back(MulticastGroup(MAC(la->sdl_data + la->sdl_nlen,6),0)); + } + p = p->ifma_next; + } + freeifmaddrs(ifmap); + } + + std::vector<InetAddress> allIps(ips()); + for(std::vector<InetAddress>::iterator ip(allIps.begin());ip!=allIps.end();++ip) + newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip)); + + std::sort(newGroups.begin(),newGroups.end()); + std::unique(newGroups.begin(),newGroups.end()); + + for(std::vector<MulticastGroup>::iterator m(newGroups.begin());m!=newGroups.end();++m) { + if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m)) + added.push_back(*m); + } + for(std::vector<MulticastGroup>::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) { + if (!std::binary_search(newGroups.begin(),newGroups.end(),*m)) + removed.push_back(*m); + } + + _multicastGroups.swap(newGroups); +} + +/* bool BSDEthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups) { std::set<MulticastGroup> newGroups; @@ -365,11 +411,7 @@ bool BSDEthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups) return changed; } - -bool BSDEthernetTap::injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) -{ - return false; -} +*/ void BSDEthernetTap::threadMain() throw() @@ -378,7 +420,6 @@ void BSDEthernetTap::threadMain() MAC to,from; int n,nfds,r; char getBuf[8194]; - Buffer<4096> data; // Wait for a moment after startup -- wait for Network to finish // constructing itself. @@ -415,8 +456,8 @@ void BSDEthernetTap::threadMain() to.setTo(getBuf,6); from.setTo(getBuf + 6,6); unsigned int etherType = ntohs(((const uint16_t *)getBuf)[6]); - data.copyFrom(getBuf + 14,(unsigned int)r - 14); - _handler(_arg,from,to,etherType,data); + // TODO: VLAN support + _handler(_arg,_nwid,from,to,etherType,0,(const void *)(getBuf + 14),r - 14); } r = 0; diff --git a/osdep/BSDEthernetTap.hpp b/osdep/BSDEthernetTap.hpp index a96c39b9..c98a4689 100644 --- a/osdep/BSDEthernetTap.hpp +++ b/osdep/BSDEthernetTap.hpp @@ -31,47 +31,52 @@ #include <stdio.h> #include <stdlib.h> +#include <string> +#include <vector> #include <stdexcept> -#include "EthernetTap.hpp" +#include "../node/Constants.hpp" +#include "../node/MulticastGroup.hpp" +#include "../node/MAC.hpp" #include "Thread.hpp" namespace ZeroTier { -class BSDEthernetTap : public EthernetTap +class BSDEthernetTap { public: BSDEthernetTap( + const char *homePath, const MAC &mac, unsigned int mtu, unsigned int metric, uint64_t nwid, - const char *desiredDevice, const char *friendlyName, - void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &), + void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int), void *arg); - virtual ~BSDEthernetTap(); + ~BSDEthernetTap(); - virtual void setEnabled(bool en); - virtual bool enabled() const; - virtual bool addIP(const InetAddress &ip); - virtual bool removeIP(const InetAddress &ip); - virtual std::set<InetAddress> ips() const; - virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len); - virtual std::string deviceName() const; - virtual void setFriendlyName(const char *friendlyName); - virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups); - virtual bool injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len); + void setEnabled(bool en); + bool enabled() const; + bool addIp(const InetAddress &ip); + bool removeIp(const InetAddress &ip); + std::vector<InetAddress> ips() const; + void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len); + std::string deviceName() const; + void setFriendlyName(const char *friendlyName); + void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed); void threadMain() throw(); private: - void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &); + void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int); void *_arg; + uint64_t _nwid; Thread _thread; std::string _dev; + std::vector<MulticastGroup> _multicastGroups; unsigned int _mtu; unsigned int _metric; int _fd; diff --git a/osdep/BackgroundResolver.cpp b/osdep/BackgroundResolver.cpp new file mode 100644 index 00000000..e009fe06 --- /dev/null +++ b/osdep/BackgroundResolver.cpp @@ -0,0 +1,130 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +#include "OSUtils.hpp" +#include "Thread.hpp" +#include "BackgroundResolver.hpp" + +namespace ZeroTier { + +/* + * We can't actually abort a job. This is a legacy characteristic of the + * ancient synchronous resolver APIs. So to abort jobs, we just abandon + * them by setting their parent to null. + */ +class BackgroundResolverJob +{ +public: + std::string name; + BackgroundResolver *volatile parent; + Mutex lock; + + void threadMain() + throw() + { + std::vector<InetAddress> ips; + try { + ips = OSUtils::resolve(name.c_str()); + } catch ( ... ) {} + { + Mutex::Lock _l(lock); + BackgroundResolver *p = parent; + if (p) + p->_postResult(ips); + } + delete this; + } +}; + +BackgroundResolver::BackgroundResolver(const char *name) : + _name(name), + _job((BackgroundResolverJob *)0), + _callback(0), + _arg((void *)0), + _ips(), + _lock() +{ +} + +BackgroundResolver::~BackgroundResolver() +{ + abort(); +} + +std::vector<InetAddress> BackgroundResolver::get() const +{ + Mutex::Lock _l(_lock); + return _ips; +} + +void BackgroundResolver::resolveNow(void (*callback)(BackgroundResolver *,void *),void *arg) +{ + Mutex::Lock _l(_lock); + + if (_job) { + Mutex::Lock _l2(_job->lock); + _job->parent = (BackgroundResolver *)0; + _job = (BackgroundResolverJob *)0; + } + + BackgroundResolverJob *j = new BackgroundResolverJob(); + j->name = _name; + j->parent = this; + + _job = j; + _callback = callback; + _arg = arg; + + _jobThread = Thread::start(j); +} + +void BackgroundResolver::abort() +{ + Mutex::Lock _l(_lock); + if (_job) { + Mutex::Lock _l2(_job->lock); + _job->parent = (BackgroundResolver *)0; + _job = (BackgroundResolverJob *)0; + } +} + +void BackgroundResolver::_postResult(const std::vector<InetAddress> &ips) +{ + void (*cb)(BackgroundResolver *,void *); + void *a; + { + Mutex::Lock _l(_lock); + _job = (BackgroundResolverJob *)0; + cb = _callback; + a = _arg; + _ips = ips; + } + if (cb) + cb(this,a); +} + +} // namespace ZeroTier diff --git a/osdep/BackgroundResolver.hpp b/osdep/BackgroundResolver.hpp new file mode 100644 index 00000000..a848b215 --- /dev/null +++ b/osdep/BackgroundResolver.hpp @@ -0,0 +1,127 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +#ifndef ZT_BACKGROUNDRESOLVER_HPP +#define ZT_BACKGROUNDRESOLVER_HPP + +#include <vector> +#include <string> + +#include "../node/Constants.hpp" +#include "../node/Mutex.hpp" +#include "../node/InetAddress.hpp" +#include "../node/NonCopyable.hpp" +#include "Thread.hpp" + +namespace ZeroTier { + +class BackgroundResolverJob; + +/** + * A simple background resolver + */ +class BackgroundResolver : NonCopyable +{ + friend class BackgroundResolverJob; + +public: + /** + * Construct a new resolver + * + * resolveNow() must be called to actually initiate background resolution. + * + * @param name Name to resolve + */ + BackgroundResolver(const char *name); + + ~BackgroundResolver(); + + /** + * @return Most recent resolver results or empty vector if none + */ + std::vector<InetAddress> get() const; + + /** + * Launch a background resolve job now + * + * If a resolve job is currently in progress, it is aborted and another + * job is started. + * + * Note that jobs can't actually be aborted due to the limitations of the + * ancient synchronous OS resolver APIs. As a result, in progress jobs + * that are aborted are simply abandoned. Don't call this too frequently + * or background threads might pile up. + * + * @param callback Callback function to receive notification or NULL if none + * @praam arg Second argument to callback function + */ + void resolveNow(void (*callback)(BackgroundResolver *,void *) = 0,void *arg = 0); + + /** + * Abort (abandon) any current resolve jobs + */ + void abort(); + + /** + * @return True if a background job is in progress + */ + inline bool running() const + { + Mutex::Lock _l(_lock); + return (_job != (BackgroundResolverJob *)0); + } + + /** + * Wait for pending job to complete (if any) + */ + inline void wait() const + { + Thread t; + { + Mutex::Lock _l(_lock); + if (!_job) + return; + t = _jobThread; + } + Thread::join(t); + } + +private: + void _postResult(const std::vector<InetAddress> &ips); + + std::string _name; + BackgroundResolverJob *_job; + Thread _jobThread; + void (*_callback)(BackgroundResolver *,void *); + void *_arg; + std::vector<InetAddress> _ips; + Mutex _lock; +}; + +} // namespace ZeroTier + +#endif diff --git a/osdep/Phy.hpp b/osdep/Phy.hpp index 5cebe169..23fd2ee2 100644 --- a/osdep/Phy.hpp +++ b/osdep/Phy.hpp @@ -33,6 +33,7 @@ #include <string.h> #include <list> +#include <stdexcept> #if defined(_WIN32) || defined(_WIN64) diff --git a/osdep/WindowsEthernetTap.cpp b/osdep/WindowsEthernetTap.cpp index 4a5d32a4..b373d9e2 100644 --- a/osdep/WindowsEthernetTap.cpp +++ b/osdep/WindowsEthernetTap.cpp @@ -578,11 +578,6 @@ void WindowsEthernetTap::threadMain() HANDLE wait4[3]; char *tapReadBuf = (char *)0; - if (!_enableTapDevice()) { - _enabled = false; - return; // only happens if devcon is missing or totally fails - } - /* No idea why I did this. I did it a long time ago and there was only a * a snarky comment. But I'd never do crap like this without a reason, so * I am leaving it alone with a more descriptive snarky comment. */ @@ -594,10 +589,20 @@ void WindowsEthernetTap::threadMain() Utils::snprintf(tapPath,sizeof(tapPath),"\\\\.\\Global\\%s.tap",_netCfgInstanceId.c_str()); int prevTapResetStatus = _systemTapResetStatus; + bool throwOneAway = true; // Restart once on startup, because Windows. + bool powerCycle = true; // If true, "power cycle" the device, because Windows. while (_run) { + if (powerCycle) { + _disableTapDevice(); + Sleep(500); + _enableTapDevice(); + Sleep(500); + } + _tap = CreateFileA(tapPath,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_SYSTEM|FILE_FLAG_OVERLAPPED,NULL); if (_tap == INVALID_HANDLE_VALUE) { fprintf(stderr,"Error opening %s -- retrying.\r\n",tapPath); + powerCycle = true; continue; } @@ -698,7 +703,9 @@ void WindowsEthernetTap::threadMain() ReadFile(_tap,tapReadBuf,sizeof(tapReadBuf),NULL,&tapOvlRead); bool writeInProgress = false; while (_run) { - if (prevTapResetStatus != _systemTapResetStatus) { + if ((prevTapResetStatus != _systemTapResetStatus)||(throwOneAway)) { + powerCycle = throwOneAway; + throwOneAway = false; prevTapResetStatus = _systemTapResetStatus; break; // this will cause us to close and reopen the tap } |