summaryrefslogtreecommitdiff
path: root/osdep
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2016-06-15 15:46:57 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2016-06-15 15:46:57 -0700
commit3c655a4b8479a8c4a332cc4f36315c7841da34e7 (patch)
tree736d6ead0823576a1aab48743487d710ff0e9fda /osdep
parentb90e66f7c7546aaf9c0c8a6bf14cc834f82fa680 (diff)
downloadinfinitytier-3c655a4b8479a8c4a332cc4f36315c7841da34e7.tar.gz
infinitytier-3c655a4b8479a8c4a332cc4f36315c7841da34e7.zip
Default route ready to test on Mac.
Diffstat (limited to 'osdep')
-rw-r--r--osdep/ManagedRoute.cpp167
-rw-r--r--osdep/ManagedRoute.hpp71
2 files changed, 121 insertions, 117 deletions
diff --git a/osdep/ManagedRoute.cpp b/osdep/ManagedRoute.cpp
index c31f6dd0..0afac440 100644
--- a/osdep/ManagedRoute.cpp
+++ b/osdep/ManagedRoute.cpp
@@ -263,11 +263,15 @@ static void _routeCmd(const char *op,const InetAddress &target,const InetAddress
#endif // __WINDOWS__ --------------------------------------------------------
+#ifndef ZT_ROUTING_SUPPORT_FOUND
+#error ManagedRoute.cpp has no support for managing routes on this platform! You'll need to check and see if one of the existing ones will work and make sure proper defines are set, or write one. Please do a Github pull request if you do this for a new OS!
+#endif
+
} // anonymous namespace
bool ManagedRoute::sync()
{
- if (this->target.isDefaultRoute()) {
+ if (_target.isDefaultRoute()) {
/* In ZeroTier we use a forked-route trick to override the default
* with a more specific one while leaving the original system route
* intact. We also create a shadow more specific route to the
@@ -276,52 +280,68 @@ bool ManagedRoute::sync()
* done *slightly* differently on different platforms. */
InetAddress leftt,rightt;
- _forkTarget(this->target,leftt,rightt);
+ _forkTarget(_target,leftt,rightt);
#ifdef __BSD__ // ------------------------------------------------------------
- InetAddress systemVia;
- char systemDevice[128];
+ // Get system default route information
+ InetAddress newSystemVia;
+ char newSystemDevice[128];
+ newSystemDevice[0] = (char)0;
int systemMetric = 9999999;
- systemDevice[0] = (char)0;
-
- std::vector<_RTE> rtes(_getRTEs(this->target,false));
+ std::vector<_RTE> rtes(_getRTEs(_target,false));
for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
if (r->via) {
- if ((!systemVia)||(r->metric < systemMetric)) {
- systemVia = r->via;
- Utils::scopy(systemDevice,sizeof(systemDevice),r->device);
+ if ((!newSystemVia)||(r->metric < systemMetric)) {
+ newSystemVia = r->via;
+ Utils::scopy(_systemDevice,sizeof(_systemDevice),r->device);
systemMetric = r->metric;
}
}
}
-
- if (!systemDevice[0]) {
- rtes = _getRTEs(systemVia,true);
+ if (!newSystemDevice[0]) {
+ rtes = _getRTEs(newSystemVia,true);
for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
- if (r->device[0])
- Utils::scopy(systemDevice,sizeof(systemDevice),r->device);
+ if (r->device[0]) {
+ Utils::scopy(newSystemDevice,sizeof(newSystemDevice),r->device);
+ break;
+ }
}
}
-
- if ((!systemVia)||(!systemDevice[0]))
+ if ((!newSystemVia)||(!newSystemDevice[0]))
return false;
- _routeCmd("add",leftt,systemVia,systemDevice,(const char *)0);
- _routeCmd("change",leftt,systemVia,systemDevice,(const char *)0);
- _routeCmd("add",rightt,systemVia,systemDevice,(const char *)0);
- _routeCmd("change",rightt,systemVia,systemDevice,(const char *)0);
-
- if (this->via) {
- _routeCmd("add",leftt,this->via,(const char *)0,(const char *)0);
- _routeCmd("change",leftt,this->via,(const char *)0,(const char *)0);
- _routeCmd("add",rightt,this->via,(const char *)0,(const char *)0);
- _routeCmd("change",rightt,this->via,(const char *)0,(const char *)0);
- } else if ((this->device)&&(this->device[0])) {
- _routeCmd("add",leftt,this->via,(const char *)0,this->device);
- _routeCmd("change",leftt,this->via,(const char *)0,this->device);
- _routeCmd("add",rightt,this->via,(const char *)0,this->device);
- _routeCmd("change",rightt,this->via,(const char *)0,this->device);
+ // If system default route has changed or hasn't been shadowed yet, update shadow
+ if ((_systemVia != newSystemVia)||(!strcmp(_systemDevice,newSystemDevice))) {
+ if ((_systemVia)&&(_systemDevice[0])) {
+ _routeCmd("delete",leftt,_systemVia,_systemDevice,(const char *)0);
+ _routeCmd("delete",rightt,_systemVia,_systemDevice,(const char *)0);
+ }
+
+ _systemVia = newSystemVia;
+ Utils::scopy(_systemDevice,sizeof(_systemDevice),newSystemDevice);
+
+ _routeCmd("add",leftt,_systemVia,_systemDevice,(const char *)0);
+ _routeCmd("change",leftt,_systemVia,_systemDevice,(const char *)0);
+ _routeCmd("add",rightt,_systemVia,_systemDevice,(const char *)0);
+ _routeCmd("change",rightt,_systemVia,_systemDevice,(const char *)0);
+ }
+
+ // Apply overriding routes
+ if (!_applied) {
+ if (_via) {
+ _routeCmd("add",leftt,_via,(const char *)0,(const char *)0);
+ _routeCmd("change",leftt,_via,(const char *)0,(const char *)0);
+ _routeCmd("add",rightt,_via,(const char *)0,(const char *)0);
+ _routeCmd("change",rightt,_via,(const char *)0,(const char *)0);
+ } else if (_device[0]) {
+ _routeCmd("add",leftt,_via,(const char *)0,_device);
+ _routeCmd("change",leftt,_via,(const char *)0,_device);
+ _routeCmd("add",rightt,_via,(const char *)0,_device);
+ _routeCmd("change",rightt,_via,(const char *)0,_device);
+ }
+
+ _applied = true;
}
#endif // __BSD__ ------------------------------------------------------------
@@ -357,59 +377,32 @@ bool ManagedRoute::sync()
void ManagedRoute::remove()
{
- if (!this->applied)
- return;
-
- if (this->target.isDefaultRoute()) {
- /* In ZeroTier we use a forked-route trick to override the default
- * with a more specific one while leaving the original system route
- * intact. We also create a shadow more specific route to the
- * original gateway that is device-bound so that ZeroTier's device
- * bound ports go via the physical Internet link. This has to be
- * done *slightly* differently on different platforms. */
-
- InetAddress leftt,rightt;
- _forkTarget(this->target,leftt,rightt);
+ if (_applied) {
+ if (_target.isDefaultRoute()) {
+ /* In ZeroTier we use a forked-route trick to override the default
+ * with a more specific one while leaving the original system route
+ * intact. We also create a shadow more specific route to the
+ * original gateway that is device-bound so that ZeroTier's device
+ * bound ports go via the physical Internet link. This has to be
+ * done *slightly* differently on different platforms. */
+
+ InetAddress leftt,rightt;
+ _forkTarget(_target,leftt,rightt);
#ifdef __BSD__ // ------------------------------------------------------------
- InetAddress systemVia;
- char systemDevice[128];
- int systemMetric = 9999999;
- systemDevice[0] = (char)0;
-
- std::vector<_RTE> rtes(_getRTEs(this->target,false));
- for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
- if (r->via) {
- if ((!systemVia)||(r->metric < systemMetric)) {
- systemVia = r->via;
- Utils::scopy(systemDevice,sizeof(systemDevice),r->device);
- systemMetric = r->metric;
- }
+ if ((_systemVia)&&(_systemDevice[0])) {
+ _routeCmd("delete",leftt,_systemVia,_systemDevice,(const char *)0);
+ _routeCmd("delete",rightt,_systemVia,_systemDevice,(const char *)0);
}
- }
- if (!systemDevice[0]) {
- rtes = _getRTEs(systemVia,true);
- for(std::vector<_RTE>::iterator r(rtes.begin());r!=rtes.end();++r) {
- if (r->device[0])
- Utils::scopy(systemDevice,sizeof(systemDevice),r->device);
+ if (_via) {
+ _routeCmd("delete",leftt,_via,(const char *)0,(const char *)0);
+ _routeCmd("delete",rightt,_via,(const char *)0,(const char *)0);
+ } else if (_device[0]) {
+ _routeCmd("delete",leftt,_via,(const char *)0,_device);
+ _routeCmd("delete",rightt,_via,(const char *)0,_device);
}
- }
-
- if ((!systemVia)||(!systemDevice[0]))
- return false;
-
- _routeCmd("delete",leftt,systemVia,systemDevice,(const char *)0);
- _routeCmd("delete",rightt,systemVia,systemDevice,(const char *)0);
-
- if (this->via) {
- _routeCmd("delete",leftt,this->via,(const char *)0,(const char *)0);
- _routeCmd("delete",rightt,this->via,(const char *)0,(const char *)0);
- } else if ((this->device)&&(this->device[0])) {
- _routeCmd("delete",leftt,this->via,(const char *)0,this->device);
- _routeCmd("delete",rightt,this->via,(const char *)0,this->device);
- }
#endif // __BSD__ ------------------------------------------------------------
@@ -421,9 +414,9 @@ void ManagedRoute::remove()
#endif // __WINDOWS__ --------------------------------------------------------
- } else {
+ } else {
- // TODO
+ // TODO
#ifdef __BSD__ // ------------------------------------------------------------
@@ -437,15 +430,19 @@ void ManagedRoute::remove()
#endif // __WINDOWS__ --------------------------------------------------------
+ }
}
+
+ _target.zero();
+ _via.zero();
+ _systemVia.zero();
+ _device[0] = (char)0;
+ _systemDevice[0] = (char)0;
+ _applied = false;
}
} // namespace ZeroTier
-#ifndef ZT_ROUTING_SUPPORT_FOUND
-#error ManagedRoute.cpp has no support for managing routes on this platform! You'll need to check and see if one of the existing ones will work and make sure proper defines are set, or write one. Please do a Github pull request if you do this for a new OS!
-#endif
-
/*
int main(int argc,char **argv)
{
diff --git a/osdep/ManagedRoute.hpp b/osdep/ManagedRoute.hpp
index 081d516d..86468a45 100644
--- a/osdep/ManagedRoute.hpp
+++ b/osdep/ManagedRoute.hpp
@@ -18,12 +18,11 @@ namespace ZeroTier {
class ManagedRoute
{
public:
- ManagedRoute() :
- target(),
- via(),
- applied(false)
+ ManagedRoute()
{
- device[0] = (char)0;
+ _device[0] = (char)0;
+ _systemDevice[0] = (char)0;
+ _applied = false;
}
~ManagedRoute()
@@ -31,7 +30,24 @@ public:
this->remove();
}
+ ManagedRoute(const ManagedRoute &r)
+ {
+ *this = r;
+ }
+
+ inline ManagedRoute &operator=(const ManagedRoute &r)
+ {
+ if ((!_applied)&&(!r._applied)) {
+ memcpy(this,&r,sizeof(ManagedRoute)); // InetAddress is memcpy'able
+ } else {
+ throw std::runtime_error("Applied ManagedRoute is non-copyable!");
+ }
+ return *this;
+ }
+
/**
+ * Initialize object and set route
+ *
* @param target Route target (e.g. 0.0.0.0/0 for default)
* @param via Route next L3 hop or NULL InetAddress if local
* @param device Device name/ID if 'via' is null and route is local, otherwise ignored
@@ -39,13 +55,12 @@ public:
*/
inline bool set(const InetAddress &target,const InetAddress &via,const char *device)
{
- if ((!via)&&((!device)||(!device[0])))
+ if ((!_via)&&(!_device[0]))
return false;
this->remove();
- this->target = target;
- this->via = via;
- this->applied = true;
- Utils::scopy(this->device,sizeof(this->device),device);
+ _target = target;
+ _via = via;
+ Utils::scopy(_device,sizeof(_device),device);
return this->sync();
}
@@ -60,34 +75,26 @@ public:
bool sync();
/**
- * Remove and clear this ManagedRoute (also done automatically on destruct)
+ * Remove and clear this ManagedRoute
*
- * This does nothing if this ManagedRoute is not set or has already been removed.
+ * This does nothing if this ManagedRoute is not set or has already been
+ * removed. If this is not explicitly called it is called automatically on
+ * destruct.
*/
void remove();
-private:
- /*
- static inline bool _viaCompare(const InetAddress &v1,const InetAddress &v2)
- {
- if (v1) {
- if (v2)
- return v1.ipsEqual(v2);
- else return false;
- } else if (v2)
- return false;
- else return true;
- }
- */
+ inline const InetAddress &target() const { return _target; }
+ inline const InetAddress &via() const { return _via; }
+ inline const char *device() const { return _device; }
- // non-copyable
- ManagedRoute(const ManagedRoute &mr) {}
- inline ManagedRoute &operator=(const ManagedRoute &mr) { return *this; }
+private:
- InetAddress target;
- InetAddress via;
- bool applied;
- char device[128];
+ InetAddress _target;
+ InetAddress _via;
+ InetAddress _systemVia; // for route overrides
+ char _device[128];
+ char _systemDevice[128]; // for route overrides
+ bool _applied;
};
} // namespace ZeroTier