mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-20 13:24:09 -07:00
getting there...
This commit is contained in:
parent
5e35346f17
commit
2eef9d22e6
11 changed files with 278 additions and 45 deletions
|
@ -128,4 +128,15 @@ bool EthernetTap::addIps(std::vector<InetAddress> ips)
|
|||
return true;
|
||||
}
|
||||
|
||||
std::string EthernetTap::routingDeviceName() const
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
char tapdev[64];
|
||||
OSUtils::ztsnprintf(tapdev,sizeof(tapdev),"%.16llx",(unsigned long long)(((const WindowsEthernetTap *)(this))->luid().Value));
|
||||
return std::string(tapdev);
|
||||
#else
|
||||
return this->deviceName();
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
|
@ -18,10 +18,13 @@
|
|||
#include "../node/MAC.hpp"
|
||||
#include "../node/InetAddress.hpp"
|
||||
#include "../node/MulticastGroup.hpp"
|
||||
#include "ManagedRoute.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
|
@ -50,9 +53,37 @@ public:
|
|||
virtual std::vector<InetAddress> ips() const = 0;
|
||||
virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) = 0;
|
||||
virtual std::string deviceName() const = 0;
|
||||
virtual std::string routingDeviceName() const;
|
||||
virtual void setFriendlyName(const char *friendlyName) = 0;
|
||||
virtual void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed) = 0;
|
||||
virtual void setMtu(unsigned int mtu) = 0;
|
||||
|
||||
ZT_ALWAYS_INLINE int addRoute(const InetAddress &target,const InetAddress &via,const unsigned int metric)
|
||||
{
|
||||
const std::string dn(this->routingDeviceName());
|
||||
const char *const dnp = (dn.length() > 0) ? dn.c_str() : (const char *)0;
|
||||
std::lock_guard<std::mutex> l(_managedRoutes_l);
|
||||
_managedRoutes[std::pair<InetAddress,unsigned int>(target,metric)] = std::shared_ptr<ManagedRoute>(new ManagedRoute(target,via,dnp));
|
||||
}
|
||||
|
||||
ZT_ALWAYS_INLINE int removeRoute(const InetAddress &target,const InetAddress &via,const unsigned int metric)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_managedRoutes_l);
|
||||
_managedRoutes.erase(std::pair<InetAddress,unsigned int>(target,metric));
|
||||
}
|
||||
|
||||
ZT_ALWAYS_INLINE int syncRoutes()
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_managedRoutes_l);
|
||||
for(auto r=_managedRoutes.begin();r!=_managedRoutes.end();++r) {
|
||||
r->second->sync();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map< std::pair<InetAddress,unsigned int>,std::shared_ptr<ManagedRoute> > _managedRoutes;
|
||||
std::mutex _managedRoutes_l;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
|
@ -36,28 +36,19 @@ class ManagedRoute
|
|||
friend class SharedPtr<ManagedRoute>;
|
||||
|
||||
public:
|
||||
ManagedRoute(const InetAddress &target,const InetAddress &via,const InetAddress &src,const char *device)
|
||||
ZT_ALWAYS_INLINE ManagedRoute(const InetAddress &target,const InetAddress &via,const char *device)
|
||||
{
|
||||
_target = target;
|
||||
_via = via;
|
||||
_src = src;
|
||||
if (via.ss_family == AF_INET)
|
||||
_via.setPort(32);
|
||||
else if (via.ss_family == AF_INET6)
|
||||
_via.setPort(128);
|
||||
if (src.ss_family == AF_INET) {
|
||||
_src.setPort(32);
|
||||
} else if (src.ss_family == AF_INET6) {
|
||||
_src.setPort(128);
|
||||
}
|
||||
Utils::scopy(_device,sizeof(_device),device);
|
||||
_systemDevice[0] = (char)0;
|
||||
}
|
||||
|
||||
~ManagedRoute()
|
||||
{
|
||||
this->remove();
|
||||
}
|
||||
ZT_ALWAYS_INLINE ~ManagedRoute() { this->remove(); }
|
||||
|
||||
/**
|
||||
* Set or update currently set route
|
||||
|
@ -78,18 +69,16 @@ public:
|
|||
*/
|
||||
void remove();
|
||||
|
||||
inline const InetAddress &target() const { return _target; }
|
||||
inline const InetAddress &via() const { return _via; }
|
||||
inline const InetAddress &src() const { return _src; }
|
||||
inline const char *device() const { return _device; }
|
||||
ZT_ALWAYS_INLINE const InetAddress &target() const { return _target; }
|
||||
ZT_ALWAYS_INLINE const InetAddress &via() const { return _via; }
|
||||
ZT_ALWAYS_INLINE const char *device() const { return _device; }
|
||||
|
||||
private:
|
||||
ManagedRoute(const ManagedRoute &) {}
|
||||
inline ManagedRoute &operator=(const ManagedRoute &) { return *this; }
|
||||
ZT_ALWAYS_INLINE ManagedRoute(const ManagedRoute &) {}
|
||||
ZT_ALWAYS_INLINE ManagedRoute &operator=(const ManagedRoute &) { return *this; }
|
||||
|
||||
InetAddress _target;
|
||||
InetAddress _via;
|
||||
InetAddress _src;
|
||||
InetAddress _systemVia; // for route overrides
|
||||
std::map<InetAddress,bool> _applied; // routes currently applied
|
||||
char _device[128];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue