summaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2014-07-17 17:17:59 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2014-07-17 17:17:59 -0700
commit1189f4a568a5a141ba8f257f6317197d5083019a (patch)
treeb1ef2fdd5872faf902b2e03b1f70f8e862bf64cd /node
parent51766e6549ebf0e8e9e668549f28cc1331d1912b (diff)
downloadinfinitytier-1189f4a568a5a141ba8f257f6317197d5083019a.tar.gz
infinitytier-1189f4a568a5a141ba8f257f6317197d5083019a.zip
Set/delete functionality in BSD routing table.
Diffstat (limited to 'node')
-rw-r--r--node/BSDRoutingTable.cpp106
-rw-r--r--node/BSDRoutingTable.hpp2
-rw-r--r--node/RoutingTable.hpp49
3 files changed, 141 insertions, 16 deletions
diff --git a/node/BSDRoutingTable.cpp b/node/BSDRoutingTable.cpp
index d6b5c488..92a87da0 100644
--- a/node/BSDRoutingTable.cpp
+++ b/node/BSDRoutingTable.cpp
@@ -48,6 +48,8 @@
// All I wanted was the bloody rounting table. I didn't expect the Spanish inquisition.
+#define ZT_BSD_ROUTE_CMD "/sbin/route"
+
namespace ZeroTier {
BSDRoutingTable::BSDRoutingTable()
@@ -180,6 +182,8 @@ std::vector<RoutingTable::Entry> BSDRoutingTable::get(bool includeLinkLocal,bool
}
e.metric = (int)rtm->rtm_rmx.rmx_hopcount;
+ if (e.metric < 0)
+ e.metric = 0;
if (((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback()))))
entries.push_back(e);
@@ -214,21 +218,109 @@ std::vector<RoutingTable::Entry> BSDRoutingTable::get(bool includeLinkLocal,bool
return entries;
}
-bool BSDRoutingTable::set(const RoutingTable::Entry &re)
+RoutingTable::Entry BSDRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric)
{
- return true;
+ if ((!gateway)&&((!device)||(!device[0])))
+ return RoutingTable::Entry();
+
+ std::vector<RoutingTable::Entry> rtab(get(true,true));
+
+ for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
+ if (e->destination == destination) {
+ if (((!device)||(!device[0]))||(!strcmp(device,e->device))) {
+ long p = (long)fork();
+ if (p > 0) {
+ int exitcode = -1;
+ ::waitpid(p,&exitcode,0);
+ } else if (p == 0) {
+ ::close(STDOUT_FILENO);
+ ::close(STDERR_FILENO);
+ ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,"delete",(destination.isV6() ? "-inet6" : "-inet"),destination.toString().c_str(),(const char *)0);
+ ::_exit(-1);
+ }
+ }
+ }
+ }
+
+ if (metric < 0)
+ return RoutingTable::Entry();
+
+ {
+ char hcstr[64];
+ Utils::snprintf(hcstr,sizeof(hcstr),"%d",metric);
+ long p = (long)fork();
+ if (p > 0) {
+ int exitcode = -1;
+ ::waitpid(p,&exitcode,0);
+ } else if (p == 0) {
+ ::close(STDOUT_FILENO);
+ ::close(STDERR_FILENO);
+ if (gateway) {
+ ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,"add",(destination.isV6() ? "-inet6" : "-inet"),destination.toString().c_str(),gateway.toIpString().c_str(),"-hopcount",hcstr,(const char *)0);
+ } else if ((device)&&(device[0])) {
+ ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,"add",(destination.isV6() ? "-inet6" : "-inet"),destination.toString().c_str(),"-interface",device,"-hopcount",hcstr,(const char *)0);
+ }
+ ::_exit(-1);
+ }
+ }
+
+ rtab = get(true,true);
+ std::vector<RoutingTable::Entry>::iterator bestEntry(rtab.end());
+ for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
+ if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
+ if ((device)&&(device[0])) {
+ if (!strcmp(device,e->device)) {
+ if (metric == e->metric)
+ bestEntry = e;
+ }
+ }
+ if (bestEntry == rtab.end())
+ bestEntry = e;
+ }
+ }
+ if (bestEntry != rtab.end())
+ return *bestEntry;
+
+ return RoutingTable::Entry();
}
} // namespace ZeroTier
// Enable and build to test routing table interface
-///*
+#if 0
+using namespace ZeroTier;
int main(int argc,char **argv)
{
- ZeroTier::BSDRoutingTable rt;
- std::vector<ZeroTier::RoutingTable::Entry> ents(rt.get());
- for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
+ BSDRoutingTable rt;
+
+ printf("<destination> <gateway> <interface> <metric>\n");
+ std::vector<RoutingTable::Entry> ents(rt.get());
+ for(std::vector<RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
printf("%s\n",e->toString().c_str());
+ printf("\n");
+
+ printf("adding 1.1.1.0 and 2.2.2.0...\n");
+ rt.set(InetAddress("1.1.1.0",24),InetAddress("1.2.3.4",0),(const char *)0,1);
+ rt.set(InetAddress("2.2.2.0",24),InetAddress(),"en0",1);
+ printf("\n");
+
+ printf("<destination> <gateway> <interface> <metric>\n");
+ ents = rt.get();
+ for(std::vector<RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
+ printf("%s\n",e->toString().c_str());
+ printf("\n");
+
+ printf("deleting 1.1.1.0 and 2.2.2.0...\n");
+ rt.set(InetAddress("1.1.1.0",24),InetAddress("1.2.3.4",0),(const char *)0,-1);
+ rt.set(InetAddress("2.2.2.0",24),InetAddress(),"en0",-1);
+ printf("\n");
+
+ printf("<destination> <gateway> <interface> <metric>\n");
+ ents = rt.get();
+ for(std::vector<RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
+ printf("%s\n",e->toString().c_str());
+ printf("\n");
+
return 0;
}
-//*/
+#endif
diff --git a/node/BSDRoutingTable.hpp b/node/BSDRoutingTable.hpp
index 7c4c9dcb..26237b12 100644
--- a/node/BSDRoutingTable.hpp
+++ b/node/BSDRoutingTable.hpp
@@ -43,7 +43,7 @@ public:
BSDRoutingTable();
virtual ~BSDRoutingTable();
virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const;
- virtual bool set(const RoutingTable::Entry &re);
+ virtual RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric);
};
} // namespace ZeroTier
diff --git a/node/RoutingTable.hpp b/node/RoutingTable.hpp
index 3a4a3dde..4049b880 100644
--- a/node/RoutingTable.hpp
+++ b/node/RoutingTable.hpp
@@ -45,16 +45,43 @@ public:
class Entry
{
public:
- Entry() { device[0] = (char)0; }
+ Entry() throw() { device[0] = (char)0; }
+ /**
+ * Destination IP and netmask bits (CIDR format)
+ */
InetAddress destination;
- InetAddress gateway; // port/netmaskBits field not used, should be 0 -- null if direct-to-device route
+
+ /**
+ * Gateway or null address if direct link-level route, netmask/port part of InetAddress not used
+ */
+ InetAddress gateway;
+
+ /**
+ * System device index or ID (not included in comparison operators, may not be set on all platforms)
+ */
+ int deviceIndex;
+
+ /**
+ * Metric or hop count -- higher = lower routing priority
+ */
+ int metric;
+
+ /**
+ * System device name
+ */
char device[128];
- int deviceIndex; // may not always be set, depending on OS -- for internal use only
- int metric; // higher = lower priority -- on some OSes this is "hop count," etc.
+ /**
+ * @return Human-readable representation of this route
+ */
std::string toString() const;
+ /**
+ * @return True if at least one required field is present (object is not null)
+ */
+ inline operator bool() const { return ((destination)||(gateway)||(device[0])); }
+
bool operator==(const Entry &re) const;
inline bool operator!=(const Entry &re) const { return (!(*this == re)); }
bool operator<(const Entry &re) const;
@@ -73,15 +100,21 @@ public:
* @param includeLoopback Include loopback (default: false)
* @return Sorted routing table entries
*/
- virtual std::vector<Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const = 0;
+ virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const = 0;
/**
* Add or update a routing table entry
*
- * @param re Entry to add/update
- * @return True if change successful (or unchanged)
+ * If there is no change, the existing entry is returned. Use a value of -1
+ * for metric to delete a route.
+ *
+ * @param destination Destination IP/netmask
+ * @param gateway Gateway IP (netmask/port part unused) or NULL/zero for device-level route
+ * @param device Device name (can be null for gateway routes)
+ * @param metric Route metric or hop count (higher = lower priority) or negative to delete
+ * @return Entry or null entry on failure (or delete)
*/
- virtual bool set(const Entry &re) = 0;
+ virtual RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) = 0;
/**
* Compute a 64-bit value that hashes the current state of the network environment