mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2024-12-27 08:22:31 +00:00
Set/delete functionality in BSD routing table.
This commit is contained in:
parent
51766e6549
commit
1189f4a568
@ -48,6 +48,8 @@
|
|||||||
|
|
||||||
// All I wanted was the bloody rounting table. I didn't expect the Spanish inquisition.
|
// All I wanted was the bloody rounting table. I didn't expect the Spanish inquisition.
|
||||||
|
|
||||||
|
#define ZT_BSD_ROUTE_CMD "/sbin/route"
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
|
||||||
BSDRoutingTable::BSDRoutingTable()
|
BSDRoutingTable::BSDRoutingTable()
|
||||||
@ -180,6 +182,8 @@ std::vector<RoutingTable::Entry> BSDRoutingTable::get(bool includeLinkLocal,bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
e.metric = (int)rtm->rtm_rmx.rmx_hopcount;
|
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()))))
|
if (((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback()))))
|
||||||
entries.push_back(e);
|
entries.push_back(e);
|
||||||
@ -214,21 +218,109 @@ std::vector<RoutingTable::Entry> BSDRoutingTable::get(bool includeLinkLocal,bool
|
|||||||
return entries;
|
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
|
} // namespace ZeroTier
|
||||||
|
|
||||||
// Enable and build to test routing table interface
|
// Enable and build to test routing table interface
|
||||||
///*
|
#if 0
|
||||||
|
using namespace ZeroTier;
|
||||||
int main(int argc,char **argv)
|
int main(int argc,char **argv)
|
||||||
{
|
{
|
||||||
ZeroTier::BSDRoutingTable rt;
|
BSDRoutingTable rt;
|
||||||
std::vector<ZeroTier::RoutingTable::Entry> ents(rt.get());
|
|
||||||
for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
|
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("%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;
|
return 0;
|
||||||
}
|
}
|
||||||
//*/
|
#endif
|
||||||
|
@ -43,7 +43,7 @@ public:
|
|||||||
BSDRoutingTable();
|
BSDRoutingTable();
|
||||||
virtual ~BSDRoutingTable();
|
virtual ~BSDRoutingTable();
|
||||||
virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const;
|
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
|
} // namespace ZeroTier
|
||||||
|
@ -45,16 +45,43 @@ public:
|
|||||||
class Entry
|
class Entry
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Entry() { device[0] = (char)0; }
|
Entry() throw() { device[0] = (char)0; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destination IP and netmask bits (CIDR format)
|
||||||
|
*/
|
||||||
InetAddress destination;
|
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;
|
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;
|
bool operator==(const Entry &re) const;
|
||||||
inline bool operator!=(const Entry &re) const { return (!(*this == re)); }
|
inline bool operator!=(const Entry &re) const { return (!(*this == re)); }
|
||||||
bool operator<(const Entry &re) const;
|
bool operator<(const Entry &re) const;
|
||||||
@ -73,15 +100,21 @@ public:
|
|||||||
* @param includeLoopback Include loopback (default: false)
|
* @param includeLoopback Include loopback (default: false)
|
||||||
* @return Sorted routing table entries
|
* @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
|
* Add or update a routing table entry
|
||||||
*
|
*
|
||||||
* @param re Entry to add/update
|
* If there is no change, the existing entry is returned. Use a value of -1
|
||||||
* @return True if change successful (or unchanged)
|
* 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
|
* Compute a 64-bit value that hashes the current state of the network environment
|
||||||
|
Loading…
Reference in New Issue
Block a user