diff --git a/node/BSDRoutingTable.cpp b/node/BSDRoutingTable.cpp index d6b5c4886..92a87da0d 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 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 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 rtab(get(true,true)); + + for(std::vector::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::iterator bestEntry(rtab.end()); + for(std::vector::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 ents(rt.get()); - for(std::vector::iterator e(ents.begin());e!=ents.end();++e) + BSDRoutingTable rt; + + printf(" \n"); + std::vector ents(rt.get()); + for(std::vector::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(" \n"); + ents = rt.get(); + for(std::vector::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(" \n"); + ents = rt.get(); + for(std::vector::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 7c4c9dcb4..26237b12e 100644 --- a/node/BSDRoutingTable.hpp +++ b/node/BSDRoutingTable.hpp @@ -43,7 +43,7 @@ public: BSDRoutingTable(); virtual ~BSDRoutingTable(); virtual std::vector 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 3a4a3ddee..4049b8804 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 - 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. + /** + * 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]; + + /** + * @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 get(bool includeLinkLocal = false,bool includeLoopback = false) const = 0; + virtual std::vector 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