mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-02-07 03:40:09 +00:00
Put routing table code back in osdep/
This commit is contained in:
parent
24469a7f43
commit
e9b2613d5f
@ -25,6 +25,10 @@
|
|||||||
* LLC. Start here: http://www.zerotier.com/
|
* LLC. Start here: http://www.zerotier.com/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "../node/Constants.hpp"
|
||||||
|
|
||||||
|
#ifdef __BSD__
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -45,7 +49,6 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "../node/Constants.hpp"
|
|
||||||
#include "BSDRoutingTable.hpp"
|
#include "BSDRoutingTable.hpp"
|
||||||
|
|
||||||
// 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.
|
||||||
@ -62,9 +65,9 @@ BSDRoutingTable::~BSDRoutingTable()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<RoutingTable::Entry> BSDRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
|
std::vector<RoutingTableEntry> BSDRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
|
||||||
{
|
{
|
||||||
std::vector<RoutingTable::Entry> entries;
|
std::vector<RoutingTableEntry> entries;
|
||||||
int mib[6];
|
int mib[6];
|
||||||
size_t needed;
|
size_t needed;
|
||||||
|
|
||||||
@ -88,7 +91,7 @@ std::vector<RoutingTable::Entry> BSDRoutingTable::get(bool includeLinkLocal,bool
|
|||||||
char *saend = next + rtm->rtm_msglen;
|
char *saend = next + rtm->rtm_msglen;
|
||||||
|
|
||||||
if (((rtm->rtm_flags & RTF_LLINFO) == 0)&&((rtm->rtm_flags & RTF_HOST) == 0)&&((rtm->rtm_flags & RTF_UP) != 0)&&((rtm->rtm_flags & RTF_MULTICAST) == 0)) {
|
if (((rtm->rtm_flags & RTF_LLINFO) == 0)&&((rtm->rtm_flags & RTF_HOST) == 0)&&((rtm->rtm_flags & RTF_UP) != 0)&&((rtm->rtm_flags & RTF_MULTICAST) == 0)) {
|
||||||
RoutingTable::Entry e;
|
RoutingTableEntry e;
|
||||||
e.deviceIndex = -9999; // unset
|
e.deviceIndex = -9999; // unset
|
||||||
|
|
||||||
int which = 0;
|
int which = 0;
|
||||||
@ -202,14 +205,14 @@ std::vector<RoutingTable::Entry> BSDRoutingTable::get(bool includeLinkLocal,bool
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e1(entries.begin());e1!=entries.end();++e1) {
|
for(std::vector<ZeroTier::RoutingTableEntry>::iterator e1(entries.begin());e1!=entries.end();++e1) {
|
||||||
if ((!e1->device[0])&&(e1->deviceIndex >= 0))
|
if ((!e1->device[0])&&(e1->deviceIndex >= 0))
|
||||||
if_indextoname(e1->deviceIndex,e1->device);
|
if_indextoname(e1->deviceIndex,e1->device);
|
||||||
}
|
}
|
||||||
for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e1(entries.begin());e1!=entries.end();++e1) {
|
for(std::vector<ZeroTier::RoutingTableEntry>::iterator e1(entries.begin());e1!=entries.end();++e1) {
|
||||||
if ((!e1->device[0])&&(e1->gateway)) {
|
if ((!e1->device[0])&&(e1->gateway)) {
|
||||||
int bestMetric = 9999999;
|
int bestMetric = 9999999;
|
||||||
for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e2(entries.begin());e2!=entries.end();++e2) {
|
for(std::vector<ZeroTier::RoutingTableEntry>::iterator e2(entries.begin());e2!=entries.end();++e2) {
|
||||||
if ((e1->gateway.within(e2->destination))&&(e2->metric <= bestMetric)) {
|
if ((e1->gateway.within(e2->destination))&&(e2->metric <= bestMetric)) {
|
||||||
bestMetric = e2->metric;
|
bestMetric = e2->metric;
|
||||||
Utils::scopy(e1->device,sizeof(e1->device),e2->device);
|
Utils::scopy(e1->device,sizeof(e1->device),e2->device);
|
||||||
@ -223,14 +226,14 @@ std::vector<RoutingTable::Entry> BSDRoutingTable::get(bool includeLinkLocal,bool
|
|||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
RoutingTable::Entry BSDRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric)
|
RoutingTableEntry BSDRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric)
|
||||||
{
|
{
|
||||||
if ((!gateway)&&((!device)||(!device[0])))
|
if ((!gateway)&&((!device)||(!device[0])))
|
||||||
return RoutingTable::Entry();
|
return RoutingTableEntry();
|
||||||
|
|
||||||
std::vector<RoutingTable::Entry> rtab(get(true,true));
|
std::vector<RoutingTableEntry> rtab(get(true,true));
|
||||||
|
|
||||||
for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
|
for(std::vector<RoutingTableEntry>::iterator e(rtab.begin());e!=rtab.end();++e) {
|
||||||
if (e->destination == destination) {
|
if (e->destination == destination) {
|
||||||
if (((!device)||(!device[0]))||(!strcmp(device,e->device))) {
|
if (((!device)||(!device[0]))||(!strcmp(device,e->device))) {
|
||||||
long p = (long)fork();
|
long p = (long)fork();
|
||||||
@ -248,7 +251,7 @@ RoutingTable::Entry BSDRoutingTable::set(const InetAddress &destination,const In
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (metric < 0)
|
if (metric < 0)
|
||||||
return RoutingTable::Entry();
|
return RoutingTableEntry();
|
||||||
|
|
||||||
{
|
{
|
||||||
char hcstr[64];
|
char hcstr[64];
|
||||||
@ -270,8 +273,8 @@ RoutingTable::Entry BSDRoutingTable::set(const InetAddress &destination,const In
|
|||||||
}
|
}
|
||||||
|
|
||||||
rtab = get(true,true);
|
rtab = get(true,true);
|
||||||
std::vector<RoutingTable::Entry>::iterator bestEntry(rtab.end());
|
std::vector<RoutingTableEntry>::iterator bestEntry(rtab.end());
|
||||||
for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
|
for(std::vector<RoutingTableEntry>::iterator e(rtab.begin());e!=rtab.end();++e) {
|
||||||
if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
|
if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
|
||||||
if ((device)&&(device[0])) {
|
if ((device)&&(device[0])) {
|
||||||
if (!strcmp(device,e->device)) {
|
if (!strcmp(device,e->device)) {
|
||||||
@ -286,7 +289,7 @@ RoutingTable::Entry BSDRoutingTable::set(const InetAddress &destination,const In
|
|||||||
if (bestEntry != rtab.end())
|
if (bestEntry != rtab.end())
|
||||||
return *bestEntry;
|
return *bestEntry;
|
||||||
|
|
||||||
return RoutingTable::Entry();
|
return RoutingTableEntry();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ZeroTier
|
} // namespace ZeroTier
|
||||||
@ -299,8 +302,8 @@ int main(int argc,char **argv)
|
|||||||
BSDRoutingTable rt;
|
BSDRoutingTable rt;
|
||||||
|
|
||||||
printf("<destination> <gateway> <interface> <metric>\n");
|
printf("<destination> <gateway> <interface> <metric>\n");
|
||||||
std::vector<RoutingTable::Entry> ents(rt.get());
|
std::vector<RoutingTableEntry> ents(rt.get());
|
||||||
for(std::vector<RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
|
for(std::vector<RoutingTableEntry>::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("\n");
|
||||||
|
|
||||||
@ -311,7 +314,7 @@ int main(int argc,char **argv)
|
|||||||
|
|
||||||
printf("<destination> <gateway> <interface> <metric>\n");
|
printf("<destination> <gateway> <interface> <metric>\n");
|
||||||
ents = rt.get();
|
ents = rt.get();
|
||||||
for(std::vector<RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
|
for(std::vector<RoutingTableEntry>::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("\n");
|
||||||
|
|
||||||
@ -322,10 +325,12 @@ int main(int argc,char **argv)
|
|||||||
|
|
||||||
printf("<destination> <gateway> <interface> <metric>\n");
|
printf("<destination> <gateway> <interface> <metric>\n");
|
||||||
ents = rt.get();
|
ents = rt.get();
|
||||||
for(std::vector<RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
|
for(std::vector<RoutingTableEntry>::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("\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif // __BSD__
|
||||||
|
@ -28,7 +28,13 @@
|
|||||||
#ifndef ZT_BSDROUTINGTABLE_HPP
|
#ifndef ZT_BSDROUTINGTABLE_HPP
|
||||||
#define ZT_BSDROUTINGTABLE_HPP
|
#define ZT_BSDROUTINGTABLE_HPP
|
||||||
|
|
||||||
#include "../node/RoutingTable.hpp"
|
#include "../node/Constants.hpp"
|
||||||
|
|
||||||
|
#ifdef __BSD__
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "RoutingTableEntry.hpp"
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
|
||||||
@ -37,15 +43,18 @@ namespace ZeroTier {
|
|||||||
*
|
*
|
||||||
* Has currently only been tested on OSX/Darwin.
|
* Has currently only been tested on OSX/Darwin.
|
||||||
*/
|
*/
|
||||||
class BSDRoutingTable : public RoutingTable
|
class BSDRoutingTable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BSDRoutingTable();
|
BSDRoutingTable();
|
||||||
virtual ~BSDRoutingTable();
|
~BSDRoutingTable();
|
||||||
virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const;
|
|
||||||
virtual RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric);
|
std::vector<RoutingTableEntry> get(bool includeLinkLocal,bool includeLoopback) const;
|
||||||
|
RoutingTableEntry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ZeroTier
|
} // namespace ZeroTier
|
||||||
|
|
||||||
|
#endif // __BSD__
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -25,6 +25,10 @@
|
|||||||
* LLC. Start here: http://www.zerotier.com/
|
* LLC. Start here: http://www.zerotier.com/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "../node/Constants.hpp"
|
||||||
|
|
||||||
|
#ifdef __LINUX__
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -42,7 +46,6 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "../node/Constants.hpp"
|
|
||||||
#include "../node/Utils.hpp"
|
#include "../node/Utils.hpp"
|
||||||
#include "LinuxRoutingTable.hpp"
|
#include "LinuxRoutingTable.hpp"
|
||||||
|
|
||||||
@ -58,11 +61,11 @@ LinuxRoutingTable::~LinuxRoutingTable()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<RoutingTable::Entry> LinuxRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
|
std::vector<RoutingTableEntry> LinuxRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
|
||||||
{
|
{
|
||||||
char buf[131072];
|
char buf[131072];
|
||||||
char *stmp,*stmp2;
|
char *stmp,*stmp2;
|
||||||
std::vector<RoutingTable::Entry> entries;
|
std::vector<RoutingTableEntry> entries;
|
||||||
|
|
||||||
{
|
{
|
||||||
int fd = ::open("/proc/net/route",O_RDONLY);
|
int fd = ::open("/proc/net/route",O_RDONLY);
|
||||||
@ -102,7 +105,7 @@ std::vector<RoutingTable::Entry> LinuxRoutingTable::get(bool includeLinkLocal,bo
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((iface)&&(destination)) {
|
if ((iface)&&(destination)) {
|
||||||
RoutingTable::Entry e;
|
RoutingTableEntry e;
|
||||||
if (destination)
|
if (destination)
|
||||||
e.destination.set(&destination,4,Utils::countBits(mask));
|
e.destination.set(&destination,4,Utils::countBits(mask));
|
||||||
e.gateway.set(&gateway,4,0);
|
e.gateway.set(&gateway,4,0);
|
||||||
@ -149,7 +152,7 @@ std::vector<RoutingTable::Entry> LinuxRoutingTable::get(bool includeLinkLocal,bo
|
|||||||
|
|
||||||
if ((device)&&(destination)) {
|
if ((device)&&(destination)) {
|
||||||
unsigned char tmp[16];
|
unsigned char tmp[16];
|
||||||
RoutingTable::Entry e;
|
RoutingTableEntry e;
|
||||||
Utils::unhex(destination,tmp,16);
|
Utils::unhex(destination,tmp,16);
|
||||||
if ((!Utils::isZero(tmp,16))&&(tmp[0] != 0xff))
|
if ((!Utils::isZero(tmp,16))&&(tmp[0] != 0xff))
|
||||||
e.destination.set(tmp,16,destPrefixLen);
|
e.destination.set(tmp,16,destPrefixLen);
|
||||||
@ -167,12 +170,12 @@ std::vector<RoutingTable::Entry> LinuxRoutingTable::get(bool includeLinkLocal,bo
|
|||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
RoutingTable::Entry LinuxRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric)
|
RoutingTableEntry LinuxRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric)
|
||||||
{
|
{
|
||||||
char metstr[128];
|
char metstr[128];
|
||||||
|
|
||||||
if ((!gateway)&&((!device)||(!device[0])))
|
if ((!gateway)&&((!device)||(!device[0])))
|
||||||
return RoutingTable::Entry();
|
return RoutingTableEntry();
|
||||||
|
|
||||||
Utils::snprintf(metstr,sizeof(metstr),"%d",metric);
|
Utils::snprintf(metstr,sizeof(metstr),"%d",metric);
|
||||||
|
|
||||||
@ -212,9 +215,9 @@ RoutingTable::Entry LinuxRoutingTable::set(const InetAddress &destination,const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<RoutingTable::Entry> rtab(get(true,true));
|
std::vector<RoutingTableEntry> rtab(get(true,true));
|
||||||
std::vector<RoutingTable::Entry>::iterator bestEntry(rtab.end());
|
std::vector<RoutingTableEntry>::iterator bestEntry(rtab.end());
|
||||||
for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
|
for(std::vector<RoutingTableEntry>::iterator e(rtab.begin());e!=rtab.end();++e) {
|
||||||
if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
|
if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
|
||||||
if ((device)&&(device[0])) {
|
if ((device)&&(device[0])) {
|
||||||
if (!strcmp(device,e->device)) {
|
if (!strcmp(device,e->device)) {
|
||||||
@ -229,7 +232,9 @@ RoutingTable::Entry LinuxRoutingTable::set(const InetAddress &destination,const
|
|||||||
if (bestEntry != rtab.end())
|
if (bestEntry != rtab.end())
|
||||||
return *bestEntry;
|
return *bestEntry;
|
||||||
|
|
||||||
return RoutingTable::Entry();
|
return RoutingTableEntry();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ZeroTier
|
} // namespace ZeroTier
|
||||||
|
|
||||||
|
#endif // __LINUX__
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* ZeroTier One - Network Virtualization Everywhere
|
* ZeroTier One - Network Virtualization Everywhere
|
||||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -14,36 +14,36 @@
|
|||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
|
||||||
* --
|
|
||||||
*
|
|
||||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
||||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
||||||
*
|
|
||||||
* If you would like to embed ZeroTier into a commercial application or
|
|
||||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
||||||
* LLC. Start here: http://www.zerotier.com/
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef ZT_LINUXROUTINGTABLE_HPP
|
#ifndef ZT_LINUXROUTINGTABLE_HPP
|
||||||
#define ZT_LINUXROUTINGTABLE_HPP
|
#define ZT_LINUXROUTINGTABLE_HPP
|
||||||
|
|
||||||
#include "../node/RoutingTable.hpp"
|
#include "../node/Constants.hpp"
|
||||||
|
|
||||||
|
#ifdef __LINUX__
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "RoutingTableEntry.hpp"
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Routing table interface via /proc/net/route, /proc/net/ipv6_route, and /sbin/route command
|
* Routing table interface via /proc/net/route, /proc/net/ipv6_route, and /sbin/route command
|
||||||
*/
|
*/
|
||||||
class LinuxRoutingTable : public RoutingTable
|
class LinuxRoutingTable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LinuxRoutingTable();
|
LinuxRoutingTable();
|
||||||
virtual ~LinuxRoutingTable();
|
~LinuxRoutingTable();
|
||||||
virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const;
|
|
||||||
virtual RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric);
|
std::vector<RoutingTableEntry> get(bool includeLinkLocal = false,bool includeLoopback = false) const;
|
||||||
|
RoutingTableEntry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ZeroTier
|
} // namespace ZeroTier
|
||||||
|
|
||||||
|
#endif // __LINUX__
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,77 +0,0 @@
|
|||||||
/*
|
|
||||||
* ZeroTier One - Network Virtualization Everywhere
|
|
||||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* --
|
|
||||||
*
|
|
||||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
||||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
||||||
*
|
|
||||||
* If you would like to embed ZeroTier into a commercial application or
|
|
||||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
||||||
* LLC. Start here: http://www.zerotier.com/
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "Constants.hpp"
|
|
||||||
#include "RoutingTable.hpp"
|
|
||||||
#include "Utils.hpp"
|
|
||||||
|
|
||||||
namespace ZeroTier {
|
|
||||||
|
|
||||||
std::string RoutingTable::Entry::toString() const
|
|
||||||
{
|
|
||||||
char tmp[1024];
|
|
||||||
Utils::snprintf(tmp,sizeof(tmp),"%s %s %s %d",destination.toString().c_str(),((gateway) ? gateway.toIpString().c_str() : "<link>"),device,metric);
|
|
||||||
return std::string(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool RoutingTable::Entry::operator==(const Entry &re) const
|
|
||||||
{
|
|
||||||
return ((destination == re.destination)&&(gateway == re.gateway)&&(strcmp(device,re.device) == 0)&&(metric == re.metric));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool RoutingTable::Entry::operator<(const Entry &re) const
|
|
||||||
{
|
|
||||||
if (destination < re.destination)
|
|
||||||
return true;
|
|
||||||
else if (destination == re.destination) {
|
|
||||||
if (gateway < re.gateway)
|
|
||||||
return true;
|
|
||||||
else if (gateway == re.gateway) {
|
|
||||||
int tmp = (int)::strcmp(device,re.device);
|
|
||||||
if (tmp < 0)
|
|
||||||
return true;
|
|
||||||
else if (tmp == 0)
|
|
||||||
return (metric < re.metric);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
RoutingTable::RoutingTable()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
RoutingTable::~RoutingTable()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ZeroTier
|
|
@ -1,122 +0,0 @@
|
|||||||
/*
|
|
||||||
* ZeroTier One - Network Virtualization Everywhere
|
|
||||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* --
|
|
||||||
*
|
|
||||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
||||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
||||||
*
|
|
||||||
* If you would like to embed ZeroTier into a commercial application or
|
|
||||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
||||||
* LLC. Start here: http://www.zerotier.com/
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef ZT_ROUTINGTABLE_HPP
|
|
||||||
#define ZT_ROUTINGTABLE_HPP
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "InetAddress.hpp"
|
|
||||||
#include "NonCopyable.hpp"
|
|
||||||
|
|
||||||
namespace ZeroTier {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Base class for OS routing table interfaces
|
|
||||||
*/
|
|
||||||
class RoutingTable : NonCopyable
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
class Entry
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Entry() throw() { device[0] = (char)0; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Destination IP and netmask bits (CIDR format)
|
|
||||||
*/
|
|
||||||
InetAddress destination;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
inline bool operator>(const Entry &re) const { return (re < *this); }
|
|
||||||
inline bool operator<=(const Entry &re) const { return (!(re < *this)); }
|
|
||||||
inline bool operator>=(const Entry &re) const { return (!(*this < re)); }
|
|
||||||
};
|
|
||||||
|
|
||||||
RoutingTable();
|
|
||||||
virtual ~RoutingTable();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get routing table
|
|
||||||
*
|
|
||||||
* @param includeLinkLocal If true, include link-local address routes (default: false)
|
|
||||||
* @param includeLoopback Include loopback (default: false)
|
|
||||||
* @return Sorted routing table entries
|
|
||||||
*/
|
|
||||||
virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add or update a routing table entry
|
|
||||||
*
|
|
||||||
* 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 RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace ZeroTier
|
|
||||||
|
|
||||||
#endif
|
|
62
osdep/RoutingTableEntry.hpp
Normal file
62
osdep/RoutingTableEntry.hpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* ZeroTier One - Network Virtualization Everywhere
|
||||||
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ZT_ROUTINGTABLEENTRY_HPP
|
||||||
|
#define ZT_ROUTINGTABLEENTRY_HPP
|
||||||
|
|
||||||
|
#include "../node/InetAddress.hpp"
|
||||||
|
|
||||||
|
namespace ZeroTier {
|
||||||
|
|
||||||
|
class RoutingTableEntry
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Destination IP and netmask bits (CIDR format)
|
||||||
|
*/
|
||||||
|
InetAddress destination;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 True if at least one required field is present (object is not null)
|
||||||
|
*/
|
||||||
|
inline operator bool() const { return ((destination)||(gateway)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace ZeroTier
|
||||||
|
|
||||||
|
#endif
|
@ -1,50 +0,0 @@
|
|||||||
/*
|
|
||||||
* ZeroTier One - Network Virtualization Everywhere
|
|
||||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* --
|
|
||||||
*
|
|
||||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
||||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
||||||
*
|
|
||||||
* If you would like to embed ZeroTier into a commercial application or
|
|
||||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
||||||
* LLC. Start here: http://www.zerotier.com/
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "TestRoutingTable.hpp"
|
|
||||||
|
|
||||||
namespace ZeroTier {
|
|
||||||
|
|
||||||
TestRoutingTable::TestRoutingTable()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
TestRoutingTable::~TestRoutingTable()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<RoutingTable::Entry> TestRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
|
|
||||||
{
|
|
||||||
return std::vector<RoutingTable::Entry>();
|
|
||||||
}
|
|
||||||
|
|
||||||
RoutingTable::Entry TestRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric)
|
|
||||||
{
|
|
||||||
return RoutingTable::Entry();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ZeroTier
|
|
@ -1,50 +0,0 @@
|
|||||||
/*
|
|
||||||
* ZeroTier One - Network Virtualization Everywhere
|
|
||||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* --
|
|
||||||
*
|
|
||||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
||||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
||||||
*
|
|
||||||
* If you would like to embed ZeroTier into a commercial application or
|
|
||||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
||||||
* LLC. Start here: http://www.zerotier.com/
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef ZT_TESTROUTINGTABLE_HPP
|
|
||||||
#define ZT_TESTROUTINGTABLE_HPP
|
|
||||||
|
|
||||||
#include "../node/RoutingTable.hpp"
|
|
||||||
|
|
||||||
namespace ZeroTier {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dummy routing table -- right now this just does nothing
|
|
||||||
*/
|
|
||||||
class TestRoutingTable : public RoutingTable
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
TestRoutingTable();
|
|
||||||
virtual ~TestRoutingTable();
|
|
||||||
|
|
||||||
virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const;
|
|
||||||
virtual RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace ZeroTier
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* ZeroTier One - Network Virtualization Everywhere
|
* ZeroTier One - Network Virtualization Everywhere
|
||||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -14,17 +14,12 @@
|
|||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
|
||||||
* --
|
|
||||||
*
|
|
||||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
||||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
||||||
*
|
|
||||||
* If you would like to embed ZeroTier into a commercial application or
|
|
||||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
||||||
* LLC. Start here: http://www.zerotier.com/
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "../node/Constants.hpp"
|
||||||
|
|
||||||
|
#ifdef __WINDOWS__
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -35,7 +30,6 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "../node/Constants.hpp"
|
|
||||||
#include "WindowsRoutingTable.hpp"
|
#include "WindowsRoutingTable.hpp"
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
@ -176,3 +170,5 @@ RoutingTable::Entry WindowsRoutingTable::set(const InetAddress &destination,cons
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ZeroTier
|
} // namespace ZeroTier
|
||||||
|
|
||||||
|
#endif // __WINDOWS__
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* ZeroTier One - Network Virtualization Everywhere
|
* ZeroTier One - Network Virtualization Everywhere
|
||||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -14,36 +14,36 @@
|
|||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
|
||||||
* --
|
|
||||||
*
|
|
||||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
||||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
||||||
*
|
|
||||||
* If you would like to embed ZeroTier into a commercial application or
|
|
||||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
||||||
* LLC. Start here: http://www.zerotier.com/
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef ZT_WINDOWSROUTINGTABLE_HPP
|
#ifndef ZT_WINDOWSROUTINGTABLE_HPP
|
||||||
#define ZT_WINDOWSROUTINGTABLE_HPP
|
#define ZT_WINDOWSROUTINGTABLE_HPP
|
||||||
|
|
||||||
#include "../node/RoutingTable.hpp"
|
#include "../node/Constants.hpp"
|
||||||
|
|
||||||
|
#ifdef __WINDOWS__
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "RoutingTableEntry.hpp"
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface to Microsoft Windows (Vista or newer) routing table
|
* Interface to Microsoft Windows (Vista or newer) routing table
|
||||||
*/
|
*/
|
||||||
class WindowsRoutingTable : public RoutingTable
|
class WindowsRoutingTable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WindowsRoutingTable();
|
WindowsRoutingTable();
|
||||||
virtual ~WindowsRoutingTable();
|
~WindowsRoutingTable();
|
||||||
virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const;
|
|
||||||
virtual RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric);
|
std::vector<RoutingTableEntry> get(bool includeLinkLocal = false,bool includeLoopback = false) const;
|
||||||
|
RoutingTableEntry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ZeroTier
|
} // namespace ZeroTier
|
||||||
|
|
||||||
|
#endif // __WINDOWS__
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user