From 60158aa5dd38c979ed823fc477ab84e8d9e7a43d Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 30 Mar 2015 17:48:48 -0700 Subject: Turns out that node/ likely has no business with or need for the system IP routing table. So shelve that code for now. --- attic/rtbl/BSDRoutingTable.cpp | 331 +++++++++++++++++++++++++++++++++++++ attic/rtbl/BSDRoutingTable.hpp | 51 ++++++ attic/rtbl/LinuxRoutingTable.cpp | 235 ++++++++++++++++++++++++++ attic/rtbl/LinuxRoutingTable.hpp | 49 ++++++ attic/rtbl/RoutingTable.cpp | 77 +++++++++ attic/rtbl/RoutingTable.hpp | 122 ++++++++++++++ attic/rtbl/TestRoutingTable.cpp | 50 ++++++ attic/rtbl/TestRoutingTable.hpp | 50 ++++++ attic/rtbl/WindowsRoutingTable.cpp | 178 ++++++++++++++++++++ attic/rtbl/WindowsRoutingTable.hpp | 49 ++++++ include/ZeroTierOne.h | 2 +- main.cpp | 14 +- make-freebsd.mk | 4 +- make-linux.mk | 4 +- make-mac.mk | 4 +- node/Node.cpp | 3 - node/RoutingTable.cpp | 77 --------- node/RoutingTable.hpp | 122 -------------- node/RuntimeEnvironment.hpp | 3 - objects.mk | 1 - osnet/BSDRoutingTable.cpp | 331 ------------------------------------- osnet/BSDRoutingTable.hpp | 51 ------ osnet/LinuxRoutingTable.cpp | 235 -------------------------- osnet/LinuxRoutingTable.hpp | 49 ------ osnet/WindowsRoutingTable.cpp | 178 -------------------- osnet/WindowsRoutingTable.hpp | 49 ------ testnet.cpp | 5 +- testnet/TestRoutingTable.cpp | 50 ------ testnet/TestRoutingTable.hpp | 50 ------ 29 files changed, 1201 insertions(+), 1223 deletions(-) create mode 100644 attic/rtbl/BSDRoutingTable.cpp create mode 100644 attic/rtbl/BSDRoutingTable.hpp create mode 100644 attic/rtbl/LinuxRoutingTable.cpp create mode 100644 attic/rtbl/LinuxRoutingTable.hpp create mode 100644 attic/rtbl/RoutingTable.cpp create mode 100644 attic/rtbl/RoutingTable.hpp create mode 100644 attic/rtbl/TestRoutingTable.cpp create mode 100644 attic/rtbl/TestRoutingTable.hpp create mode 100644 attic/rtbl/WindowsRoutingTable.cpp create mode 100644 attic/rtbl/WindowsRoutingTable.hpp delete mode 100644 node/RoutingTable.cpp delete mode 100644 node/RoutingTable.hpp delete mode 100644 osnet/BSDRoutingTable.cpp delete mode 100644 osnet/BSDRoutingTable.hpp delete mode 100644 osnet/LinuxRoutingTable.cpp delete mode 100644 osnet/LinuxRoutingTable.hpp delete mode 100644 osnet/WindowsRoutingTable.cpp delete mode 100644 osnet/WindowsRoutingTable.hpp delete mode 100644 testnet/TestRoutingTable.cpp delete mode 100644 testnet/TestRoutingTable.hpp diff --git a/attic/rtbl/BSDRoutingTable.cpp b/attic/rtbl/BSDRoutingTable.cpp new file mode 100644 index 00000000..6d44c3ec --- /dev/null +++ b/attic/rtbl/BSDRoutingTable.cpp @@ -0,0 +1,331 @@ +/* + * 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 . + * + * -- + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "../node/Constants.hpp" +#include "BSDRoutingTable.hpp" + +// 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() +{ +} + +BSDRoutingTable::~BSDRoutingTable() +{ +} + +std::vector BSDRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const +{ + std::vector entries; + int mib[6]; + size_t needed; + + mib[0] = CTL_NET; + mib[1] = PF_ROUTE; + mib[2] = 0; + mib[3] = 0; + mib[4] = NET_RT_DUMP; + mib[5] = 0; + if (!sysctl(mib,6,NULL,&needed,NULL,0)) { + if (needed <= 0) + return entries; + + char *buf = (char *)::malloc(needed); + if (buf) { + if (!sysctl(mib,6,buf,&needed,NULL,0)) { + struct rt_msghdr *rtm; + for(char *next=buf,*end=buf+needed;nextrtm_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)) { + RoutingTable::Entry e; + e.deviceIndex = -9999; // unset + + int which = 0; + while (saptr < saend) { + struct sockaddr *sa = (struct sockaddr *)saptr; + unsigned int salen = sa->sa_len; + if (!salen) + break; + + // Skip missing fields in rtm_addrs bit field + while ((rtm->rtm_addrs & 1) == 0) { + rtm->rtm_addrs >>= 1; + ++which; + if (which > 6) + break; + } + if (which > 6) + break; + + rtm->rtm_addrs >>= 1; + switch(which++) { + case 0: + //printf("RTA_DST\n"); + if (sa->sa_family == AF_INET6) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; + // Nobody expects the Spanish inquisition! + if ((sin6->sin6_addr.s6_addr[0] == 0xfe)&&((sin6->sin6_addr.s6_addr[1] & 0xc0) == 0x80)) { + // Our chief weapon is... in-band signaling! + // Seriously who in the living fuck thought this was a good idea and + // then had the sadistic idea to not document it anywhere? Of course it's + // not like there is any documentation on BSD sysctls anyway. + unsigned int interfaceIndex = ((((unsigned int)sin6->sin6_addr.s6_addr[2]) << 8) & 0xff) | (((unsigned int)sin6->sin6_addr.s6_addr[3]) & 0xff); + sin6->sin6_addr.s6_addr[2] = 0; + sin6->sin6_addr.s6_addr[3] = 0; + if (!sin6->sin6_scope_id) + sin6->sin6_scope_id = interfaceIndex; + } + } + e.destination.set(sa); + break; + case 1: + //printf("RTA_GATEWAY\n"); + switch(sa->sa_family) { + case AF_LINK: + e.deviceIndex = (int)((const struct sockaddr_dl *)sa)->sdl_index; + break; + case AF_INET: + case AF_INET6: + e.gateway.set(sa); + break; + } + break; + case 2: { + if (e.destination.isV6()) { + salen = sizeof(struct sockaddr_in6); // Confess! + unsigned int bits = 0; + for(int i=0;i<16;++i) { + unsigned char c = (unsigned char)((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[i]; + if (c == 0xff) + bits += 8; + else break; + /* must they be multiples of 8? Most of the BSD source I can find says yes..? + else { + while ((c & 0x80) == 0x80) { + ++bits; + c <<= 1; + } + break; + } + */ + } + e.destination.setPort(bits); + } else { + salen = sizeof(struct sockaddr_in); // Confess! + e.destination.setPort((unsigned int)Utils::countBits((uint32_t)((const struct sockaddr_in *)sa)->sin_addr.s_addr)); + } + //printf("RTA_NETMASK\n"); + } break; + /* + case 3: + //printf("RTA_GENMASK\n"); + break; + case 4: + //printf("RTA_IFP\n"); + break; + case 5: + //printf("RTA_IFA\n"); + break; + case 6: + //printf("RTA_AUTHOR\n"); + break; + */ + } + + saptr += salen; + } + + 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); + } + + next = saend; + } + } + + ::free(buf); + } + } + + for(std::vector::iterator e1(entries.begin());e1!=entries.end();++e1) { + if ((!e1->device[0])&&(e1->deviceIndex >= 0)) + if_indextoname(e1->deviceIndex,e1->device); + } + for(std::vector::iterator e1(entries.begin());e1!=entries.end();++e1) { + if ((!e1->device[0])&&(e1->gateway)) { + int bestMetric = 9999999; + for(std::vector::iterator e2(entries.begin());e2!=entries.end();++e2) { + if ((e1->gateway.within(e2->destination))&&(e2->metric <= bestMetric)) { + bestMetric = e2->metric; + Utils::scopy(e1->device,sizeof(e1->device),e2->device); + } + } + } + } + + std::sort(entries.begin(),entries.end()); + + return entries; +} + +RoutingTable::Entry BSDRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) +{ + 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) +{ + 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/attic/rtbl/BSDRoutingTable.hpp b/attic/rtbl/BSDRoutingTable.hpp new file mode 100644 index 00000000..97969666 --- /dev/null +++ b/attic/rtbl/BSDRoutingTable.hpp @@ -0,0 +1,51 @@ +/* + * 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 . + * + * -- + * + * 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_BSDROUTINGTABLE_HPP +#define ZT_BSDROUTINGTABLE_HPP + +#include "../node/RoutingTable.hpp" + +namespace ZeroTier { + +/** + * Routing table interface for BSD with sysctl() and BSD /sbin/route + * + * Has currently only been tested on OSX/Darwin. + */ +class BSDRoutingTable : public RoutingTable +{ +public: + BSDRoutingTable(); + virtual ~BSDRoutingTable(); + virtual std::vector 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 diff --git a/attic/rtbl/LinuxRoutingTable.cpp b/attic/rtbl/LinuxRoutingTable.cpp new file mode 100644 index 00000000..581054e2 --- /dev/null +++ b/attic/rtbl/LinuxRoutingTable.cpp @@ -0,0 +1,235 @@ +/* + * 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 . + * + * -- + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "../node/Constants.hpp" +#include "../node/Utils.hpp" +#include "LinuxRoutingTable.hpp" + +#define ZT_LINUX_IP_COMMAND "/sbin/ip" + +namespace ZeroTier { + +LinuxRoutingTable::LinuxRoutingTable() +{ +} + +LinuxRoutingTable::~LinuxRoutingTable() +{ +} + +std::vector LinuxRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const +{ + char buf[131072]; + char *stmp,*stmp2; + std::vector entries; + + { + int fd = ::open("/proc/net/route",O_RDONLY); + if (fd <= 0) + buf[0] = (char)0; + else { + int n = (int)::read(fd,buf,sizeof(buf) - 1); + ::close(fd); + if (n < 0) n = 0; + buf[n] = (char)0; + } + } + + int lineno = 0; + for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) { + if (lineno == 0) { + ++lineno; + continue; // skip header + } + + char *iface = (char *)0; + uint32_t destination = 0; + uint32_t gateway = 0; + int metric = 0; + uint32_t mask = 0; + + int fno = 0; + for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) { + switch(fno) { + case 0: iface = f; break; + case 1: destination = (uint32_t)Utils::hexStrToULong(f); break; + case 2: gateway = (uint32_t)Utils::hexStrToULong(f); break; + case 6: metric = (int)Utils::strToInt(f); break; + case 7: mask = (uint32_t)Utils::hexStrToULong(f); break; + } + ++fno; + } + + if ((iface)&&(destination)) { + RoutingTable::Entry e; + if (destination) + e.destination.set(&destination,4,Utils::countBits(mask)); + e.gateway.set(&gateway,4,0); + e.deviceIndex = 0; // not used on Linux + e.metric = metric; + Utils::scopy(e.device,sizeof(e.device),iface); + if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(iface,"lo"))))) + entries.push_back(e); + } + + ++lineno; + } + + { + int fd = ::open("/proc/net/ipv6_route",O_RDONLY); + if (fd <= 0) + buf[0] = (char)0; + else { + int n = (int)::read(fd,buf,sizeof(buf) - 1); + ::close(fd); + if (n < 0) n = 0; + buf[n] = (char)0; + } + } + + for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) { + char *destination = (char *)0; + unsigned int destPrefixLen = 0; + char *gateway = (char *)0; // next hop in ipv6 terminology + int metric = 0; + char *device = (char *)0; + + int fno = 0; + for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) { + switch(fno) { + case 0: destination = f; break; + case 1: destPrefixLen = (unsigned int)Utils::hexStrToULong(f); break; + case 4: gateway = f; break; + case 5: metric = (int)Utils::hexStrToLong(f); break; + case 9: device = f; break; + } + ++fno; + } + + if ((device)&&(destination)) { + unsigned char tmp[16]; + RoutingTable::Entry e; + Utils::unhex(destination,tmp,16); + if ((!Utils::isZero(tmp,16))&&(tmp[0] != 0xff)) + e.destination.set(tmp,16,destPrefixLen); + Utils::unhex(gateway,tmp,16); + e.gateway.set(tmp,16,0); + e.deviceIndex = 0; // not used on Linux + e.metric = metric; + Utils::scopy(e.device,sizeof(e.device),device); + if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(device,"lo"))))) + entries.push_back(e); + } + } + + std::sort(entries.begin(),entries.end()); + return entries; +} + +RoutingTable::Entry LinuxRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) +{ + char metstr[128]; + + if ((!gateway)&&((!device)||(!device[0]))) + return RoutingTable::Entry(); + + Utils::snprintf(metstr,sizeof(metstr),"%d",metric); + + if (metric < 0) { + long pid = (long)vfork(); + if (pid == 0) { + if (gateway) { + if ((device)&&(device[0])) { + ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"via",gateway.toIpString().c_str(),"dev",device,(const char *)0); + } else { + ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"via",gateway.toIpString().c_str(),(const char *)0); + } + } else { + ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"dev",device,(const char *)0); + } + ::_exit(-1); + } else if (pid > 0) { + int exitcode = -1; + ::waitpid(pid,&exitcode,0); + } + } else { + long pid = (long)vfork(); + if (pid == 0) { + if (gateway) { + if ((device)&&(device[0])) { + ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"via",gateway.toIpString().c_str(),"dev",device,(const char *)0); + } else { + ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"via",gateway.toIpString().c_str(),(const char *)0); + } + } else { + ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"dev",device,(const char *)0); + } + ::_exit(-1); + } else if (pid > 0) { + int exitcode = -1; + ::waitpid(pid,&exitcode,0); + } + } + + std::vector 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 diff --git a/attic/rtbl/LinuxRoutingTable.hpp b/attic/rtbl/LinuxRoutingTable.hpp new file mode 100644 index 00000000..808ec7ea --- /dev/null +++ b/attic/rtbl/LinuxRoutingTable.hpp @@ -0,0 +1,49 @@ +/* + * 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 . + * + * -- + * + * 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 +#define ZT_LINUXROUTINGTABLE_HPP + +#include "../node/RoutingTable.hpp" + +namespace ZeroTier { + +/** + * Routing table interface via /proc/net/route, /proc/net/ipv6_route, and /sbin/route command + */ +class LinuxRoutingTable : public RoutingTable +{ +public: + LinuxRoutingTable(); + virtual ~LinuxRoutingTable(); + virtual std::vector 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 diff --git a/attic/rtbl/RoutingTable.cpp b/attic/rtbl/RoutingTable.cpp new file mode 100644 index 00000000..bae4bea9 --- /dev/null +++ b/attic/rtbl/RoutingTable.cpp @@ -0,0 +1,77 @@ +/* + * 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 . + * + * -- + * + * 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 +#include +#include +#include + +#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() : ""),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 diff --git a/attic/rtbl/RoutingTable.hpp b/attic/rtbl/RoutingTable.hpp new file mode 100644 index 00000000..e1c98984 --- /dev/null +++ b/attic/rtbl/RoutingTable.hpp @@ -0,0 +1,122 @@ +/* + * 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 . + * + * -- + * + * 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 +#include + +#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 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 diff --git a/attic/rtbl/TestRoutingTable.cpp b/attic/rtbl/TestRoutingTable.cpp new file mode 100644 index 00000000..fd61b314 --- /dev/null +++ b/attic/rtbl/TestRoutingTable.cpp @@ -0,0 +1,50 @@ +/* + * 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 . + * + * -- + * + * 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 TestRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const +{ + return std::vector(); +} + +RoutingTable::Entry TestRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) +{ + return RoutingTable::Entry(); +} + +} // namespace ZeroTier diff --git a/attic/rtbl/TestRoutingTable.hpp b/attic/rtbl/TestRoutingTable.hpp new file mode 100644 index 00000000..69bd3d9f --- /dev/null +++ b/attic/rtbl/TestRoutingTable.hpp @@ -0,0 +1,50 @@ +/* + * 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 . + * + * -- + * + * 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 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 diff --git a/attic/rtbl/WindowsRoutingTable.cpp b/attic/rtbl/WindowsRoutingTable.cpp new file mode 100644 index 00000000..00674620 --- /dev/null +++ b/attic/rtbl/WindowsRoutingTable.cpp @@ -0,0 +1,178 @@ +/* + * 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 . + * + * -- + * + * 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 +#include +#include +#include +#include +#include +#include + +#include + +#include "../node/Constants.hpp" +#include "WindowsRoutingTable.hpp" + +namespace ZeroTier { + +static void _copyInetAddressToSockaddrInet(const InetAddress &a,SOCKADDR_INET &sinet) +{ + memset(&sinet,0,sizeof(sinet)); + if (a.isV4()) { + sinet.Ipv4.sin_addr.S_un.S_addr = *((const uint32_t *)a.rawIpData()); + sinet.Ipv4.sin_family = AF_INET; + sinet.Ipv4.sin_port = htons(a.port()); + } else if (a.isV6()) { + memcpy(sinet.Ipv6.sin6_addr.u.Byte,a.rawIpData(),16); + sinet.Ipv6.sin6_family = AF_INET6; + sinet.Ipv6.sin6_port = htons(a.port()); + } +} + +WindowsRoutingTable::WindowsRoutingTable() +{ +} + +WindowsRoutingTable::~WindowsRoutingTable() +{ +} + +std::vector WindowsRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const +{ + std::vector entries; + PMIB_IPFORWARD_TABLE2 rtbl = NULL; + + if (GetIpForwardTable2(AF_UNSPEC,&rtbl) != NO_ERROR) + return entries; + if (!rtbl) + return entries; + + for(ULONG r=0;rNumEntries;++r) { + RoutingTable::Entry e; + switch(rtbl->Table[r].DestinationPrefix.Prefix.si_family) { + case AF_INET: + e.destination.set(&(rtbl->Table[r].DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr),4,rtbl->Table[r].DestinationPrefix.PrefixLength); + break; + case AF_INET6: + e.destination.set(rtbl->Table[r].DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte,16,rtbl->Table[r].DestinationPrefix.PrefixLength); + break; + } + switch(rtbl->Table[r].NextHop.si_family) { + case AF_INET: + e.gateway.set(&(rtbl->Table[r].NextHop.Ipv4.sin_addr.S_un.S_addr),4,0); + break; + case AF_INET6: + e.gateway.set(rtbl->Table[r].NextHop.Ipv6.sin6_addr.u.Byte,16,0); + break; + } + e.deviceIndex = (int)rtbl->Table[r].InterfaceIndex; + e.metric = (int)rtbl->Table[r].Metric; + ConvertInterfaceLuidToNameA(&(rtbl->Table[r].InterfaceLuid),e.device,sizeof(e.device)); + if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())))) + entries.push_back(e); + } + + FreeMibTable(rtbl); + std::sort(entries.begin(),entries.end()); + return entries; +} + +RoutingTable::Entry WindowsRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) +{ + NET_LUID luid; + luid.Value = 0; + if (ConvertInterfaceNameToLuidA(device,&luid) != NO_ERROR) + return RoutingTable::Entry(); + + bool needCreate = true; + PMIB_IPFORWARD_TABLE2 rtbl = NULL; + if (GetIpForwardTable2(AF_UNSPEC,&rtbl) != NO_ERROR) + return RoutingTable::Entry(); + if (!rtbl) + return RoutingTable::Entry(); + for(ULONG r=0;rNumEntries;++r) { + if (rtbl->Table[r].InterfaceLuid.Value == luid.Value) { + InetAddress rdest; + switch(rtbl->Table[r].DestinationPrefix.Prefix.si_family) { + case AF_INET: + rdest.set(&(rtbl->Table[r].DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr),4,rtbl->Table[r].DestinationPrefix.PrefixLength); + break; + case AF_INET6: + rdest.set(rtbl->Table[r].DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte,16,rtbl->Table[r].DestinationPrefix.PrefixLength); + break; + } + if (rdest == destination) { + if (metric >= 0) { + _copyInetAddressToSockaddrInet(gateway,rtbl->Table[r].NextHop); + rtbl->Table[r].Metric = metric; + SetIpForwardEntry2(&(rtbl->Table[r])); + needCreate = false; + } else { + DeleteIpForwardEntry2(&(rtbl->Table[r])); + FreeMibTable(rtbl); + return RoutingTable::Entry(); + } + } + } + } + FreeMibTable(rtbl); + + if ((metric >= 0)&&(needCreate)) { + MIB_IPFORWARD_ROW2 nr; + InitializeIpForwardEntry(&nr); + nr.InterfaceLuid.Value = luid.Value; + _copyInetAddressToSockaddrInet(destination,nr.DestinationPrefix.Prefix); + nr.DestinationPrefix.PrefixLength = destination.netmaskBits(); + _copyInetAddressToSockaddrInet(gateway,nr.NextHop); + nr.Metric = metric; + nr.Protocol = MIB_IPPROTO_NETMGMT; + DWORD result = CreateIpForwardEntry2(&nr); + if (result != NO_ERROR) + return RoutingTable::Entry(); + } + + std::vector 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 diff --git a/attic/rtbl/WindowsRoutingTable.hpp b/attic/rtbl/WindowsRoutingTable.hpp new file mode 100644 index 00000000..491c3424 --- /dev/null +++ b/attic/rtbl/WindowsRoutingTable.hpp @@ -0,0 +1,49 @@ +/* + * 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 . + * + * -- + * + * 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 +#define ZT_WINDOWSROUTINGTABLE_HPP + +#include "../node/RoutingTable.hpp" + +namespace ZeroTier { + +/** + * Interface to Microsoft Windows (Vista or newer) routing table + */ +class WindowsRoutingTable : public RoutingTable +{ +public: + WindowsRoutingTable(); + virtual ~WindowsRoutingTable(); + virtual std::vector 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 diff --git a/include/ZeroTierOne.h b/include/ZeroTierOne.h index 93d5aa8b..0808a750 100644 --- a/include/ZeroTierOne.h +++ b/include/ZeroTierOne.h @@ -55,7 +55,7 @@ struct ZT1_Node_Status char address[16]; /** - * ZeroTier address in least significant 40 bits of 64-bit integer + * ZeroTier address (in least significant 40 bits of 64-bit integer) */ uint64_t rawAddress; diff --git a/main.cpp b/main.cpp index 7467ba41..c78ffa89 100644 --- a/main.cpp +++ b/main.cpp @@ -70,7 +70,6 @@ #include "node/Thread.hpp" #include "node/CertificateOfMembership.hpp" #include "node/EthernetTapFactory.hpp" -#include "node/RoutingTable.hpp" #include "node/SocketManager.hpp" #include "control/NodeControlClient.hpp" @@ -84,31 +83,23 @@ #ifdef __WINDOWS__ #include "osnet/WindowsEthernetTapFactory.hpp" -#include "osnet/WindowsRoutingTable.hpp" #define ZTCreatePlatformEthernetTapFactory (new WindowsEthernetTapFactory(homeDir)) -#define ZTCreatePlatformRoutingTable (new WindowsRoutingTable()) #endif // __WINDOWS__ #ifdef __LINUX__ #include "osnet/LinuxEthernetTapFactory.hpp" -#include "osnet/LinuxRoutingTable.hpp" #define ZTCreatePlatformEthernetTapFactory (new LinuxEthernetTapFactory()) -#define ZTCreatePlatformRoutingTable (new LinuxRoutingTable()) #endif // __LINUX__ #ifdef __APPLE__ #include "osnet/OSXEthernetTapFactory.hpp" -#include "osnet/BSDRoutingTable.hpp" #define ZTCreatePlatformEthernetTapFactory (new OSXEthernetTapFactory(homeDir,"tap.kext")) -#define ZTCreatePlatformRoutingTable (new BSDRoutingTable()) #endif // __APPLE__ #ifndef ZTCreatePlatformEthernetTapFactory #ifdef __BSD__ #include "osnet/BSDEthernetTapFactory.hpp" -#include "osnet/BSDRoutingTable.hpp" #define ZTCreatePlatformEthernetTapFactory (new BSDEthernetTapFactory()) -#define ZTCreatePlatformRoutingTable (new BSDRoutingTable()) #else #error Sorry, this platform has no osnet/ implementation yet. Fork me on GitHub and add one? #endif // __BSD__ @@ -811,7 +802,6 @@ int main(int argc,char **argv) int exitCode = 0; bool needsReset = false; EthernetTapFactory *tapFactory = (EthernetTapFactory *)0; - RoutingTable *routingTable = (RoutingTable *)0; SocketManager *socketManager = (SocketManager *)0; NodeControlService *controlService = (NodeControlService *)0; NetworkConfigMaster *netconfMaster = (NetworkConfigMaster *)0; @@ -823,7 +813,6 @@ int main(int argc,char **argv) std::string authToken(NodeControlClient::getAuthToken((std::string(homeDir) + ZT_PATH_SEPARATOR_S + "authtoken.secret").c_str(),true)); tapFactory = ZTCreatePlatformEthernetTapFactory; - routingTable = ZTCreatePlatformRoutingTable; try { socketManager = new NativeSocketManager(udpPort,tcpPort); @@ -832,7 +821,7 @@ int main(int argc,char **argv) throw; } - node = new Node(homeDir,tapFactory,routingTable,socketManager,netconfMaster,needsReset,(overrideRootTopology.length() > 0) ? overrideRootTopology.c_str() : (const char *)0); + node = new Node(homeDir,tapFactory,socketManager,netconfMaster,needsReset,(overrideRootTopology.length() > 0) ? overrideRootTopology.c_str() : (const char *)0); controlService = new NodeControlService(node,authToken.c_str()); switch(node->run()) { @@ -888,7 +877,6 @@ int main(int argc,char **argv) delete controlService; delete node; node = (Node *)0; delete socketManager; - delete routingTable; delete tapFactory; #ifdef __UNIX_LIKE__ diff --git a/make-freebsd.mk b/make-freebsd.mk index 6a034dc7..0b65ca7a 100644 --- a/make-freebsd.mk +++ b/make-freebsd.mk @@ -6,8 +6,8 @@ DEFS= LIBS= include objects.mk -OBJS+=osnet/BSDEthernetTapFactory.o osnet/BSDEthernetTap.o osnet/BSDRoutingTable.o -TESTNET_OBJS=testnet/SimNet.o testnet/SimNetSocketManager.o testnet/TestEthernetTap.o testnet/TestEthernetTapFactory.o testnet/TestRoutingTable.o +OBJS+=osnet/BSDEthernetTapFactory.o osnet/BSDEthernetTap.o +TESTNET_OBJS=testnet/SimNet.o testnet/SimNetSocketManager.o testnet/TestEthernetTap.o testnet/TestEthernetTapFactory.o # Enable SSE-optimized Salsa20 on x86 and x86_64 machines MACHINE=$(shell uname -m) diff --git a/make-linux.mk b/make-linux.mk index 67a63a7b..627b7737 100644 --- a/make-linux.mk +++ b/make-linux.mk @@ -7,8 +7,8 @@ DEFS= LIBS= include objects.mk -OBJS+=osnet/LinuxRoutingTable.o osnet/LinuxEthernetTap.o osnet/LinuxEthernetTapFactory.o -TESTNET_OBJS=testnet/SimNet.o testnet/SimNetSocketManager.o testnet/TestEthernetTap.o testnet/TestEthernetTapFactory.o testnet/TestRoutingTable.o +OBJS+=osnet/LinuxEthernetTap.o osnet/LinuxEthernetTapFactory.o +TESTNET_OBJS=testnet/SimNet.o testnet/SimNetSocketManager.o testnet/TestEthernetTap.o testnet/TestEthernetTapFactory.o # Enable SSE-optimized Salsa20 on x86 and x86_64 machines MACHINE=$(shell uname -m) diff --git a/make-mac.mk b/make-mac.mk index 28a7c72d..730bda15 100644 --- a/make-mac.mk +++ b/make-mac.mk @@ -7,8 +7,8 @@ LIBS= ARCH_FLAGS=-arch i386 -arch x86_64 include objects.mk -OBJS+=osnet/BSDRoutingTable.o osnet/OSXEthernetTap.o osnet/OSXEthernetTapFactory.o -TESTNET_OBJS=testnet/SimNet.o testnet/SimNetSocketManager.o testnet/TestEthernetTap.o testnet/TestEthernetTapFactory.o testnet/TestRoutingTable.o +OBJS+=osnet/OSXEthernetTap.o osnet/OSXEthernetTapFactory.o +TESTNET_OBJS=testnet/SimNet.o testnet/SimNetSocketManager.o testnet/TestEthernetTap.o testnet/TestEthernetTapFactory.o # Disable codesign since open source users will not have ZeroTier's certs CODESIGN=echo diff --git a/node/Node.cpp b/node/Node.cpp index d013444a..71c2d3fa 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -74,7 +74,6 @@ #include "SoftwareUpdater.hpp" #include "Buffer.hpp" #include "AntiRecursion.hpp" -#include "RoutingTable.hpp" #include "HttpClient.hpp" #include "NetworkConfigMaster.hpp" @@ -126,7 +125,6 @@ struct _NodeImpl Node::Node( const char *hp, EthernetTapFactory *tf, - RoutingTable *rt, SocketManager *sm, NetworkConfigMaster *nm, bool resetIdentity, @@ -140,7 +138,6 @@ Node::Node( else impl->renv.homePath = ZT_DEFAULTS.defaultHomePath; impl->renv.tapFactory = tf; - impl->renv.routingTable = rt; impl->renv.sm = sm; impl->renv.netconfMaster = nm; diff --git a/node/RoutingTable.cpp b/node/RoutingTable.cpp deleted file mode 100644 index bae4bea9..00000000 --- a/node/RoutingTable.cpp +++ /dev/null @@ -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 . - * - * -- - * - * 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 -#include -#include -#include - -#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() : ""),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 diff --git a/node/RoutingTable.hpp b/node/RoutingTable.hpp deleted file mode 100644 index e1c98984..00000000 --- a/node/RoutingTable.hpp +++ /dev/null @@ -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 . - * - * -- - * - * 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 -#include - -#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 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 diff --git a/node/RuntimeEnvironment.hpp b/node/RuntimeEnvironment.hpp index 57a1c262..425c6d84 100644 --- a/node/RuntimeEnvironment.hpp +++ b/node/RuntimeEnvironment.hpp @@ -46,7 +46,6 @@ class SocketManager; class Multicaster; class AntiRecursion; class EthernetTapFactory; -class RoutingTable; class HttpClient; class NetworkConfigMaster; @@ -73,7 +72,6 @@ public: timeOfLastResynchronize(0), timeOfLastPacketReceived(0), tapFactory((EthernetTapFactory *)0), - routingTable((RoutingTable *)0), sm((SocketManager *)0), netconfMaster((NetworkConfigMaster *)0), log((Logger *)0), @@ -110,7 +108,6 @@ public: // These are passed in from outside and are not created or deleted by the ZeroTier node core EthernetTapFactory *tapFactory; - RoutingTable *routingTable; SocketManager *sm; NetworkConfigMaster *netconfMaster; diff --git a/objects.mk b/objects.mk index 8ed5339d..7fce4abb 100644 --- a/objects.mk +++ b/objects.mk @@ -24,7 +24,6 @@ OBJS=\ node/Packet.o \ node/Peer.o \ node/Poly1305.o \ - node/RoutingTable.o \ node/Salsa20.o \ node/SoftwareUpdater.o \ node/SHA512.o \ diff --git a/osnet/BSDRoutingTable.cpp b/osnet/BSDRoutingTable.cpp deleted file mode 100644 index 6d44c3ec..00000000 --- a/osnet/BSDRoutingTable.cpp +++ /dev/null @@ -1,331 +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 . - * - * -- - * - * 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "../node/Constants.hpp" -#include "BSDRoutingTable.hpp" - -// 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() -{ -} - -BSDRoutingTable::~BSDRoutingTable() -{ -} - -std::vector BSDRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const -{ - std::vector entries; - int mib[6]; - size_t needed; - - mib[0] = CTL_NET; - mib[1] = PF_ROUTE; - mib[2] = 0; - mib[3] = 0; - mib[4] = NET_RT_DUMP; - mib[5] = 0; - if (!sysctl(mib,6,NULL,&needed,NULL,0)) { - if (needed <= 0) - return entries; - - char *buf = (char *)::malloc(needed); - if (buf) { - if (!sysctl(mib,6,buf,&needed,NULL,0)) { - struct rt_msghdr *rtm; - for(char *next=buf,*end=buf+needed;nextrtm_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)) { - RoutingTable::Entry e; - e.deviceIndex = -9999; // unset - - int which = 0; - while (saptr < saend) { - struct sockaddr *sa = (struct sockaddr *)saptr; - unsigned int salen = sa->sa_len; - if (!salen) - break; - - // Skip missing fields in rtm_addrs bit field - while ((rtm->rtm_addrs & 1) == 0) { - rtm->rtm_addrs >>= 1; - ++which; - if (which > 6) - break; - } - if (which > 6) - break; - - rtm->rtm_addrs >>= 1; - switch(which++) { - case 0: - //printf("RTA_DST\n"); - if (sa->sa_family == AF_INET6) { - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; - // Nobody expects the Spanish inquisition! - if ((sin6->sin6_addr.s6_addr[0] == 0xfe)&&((sin6->sin6_addr.s6_addr[1] & 0xc0) == 0x80)) { - // Our chief weapon is... in-band signaling! - // Seriously who in the living fuck thought this was a good idea and - // then had the sadistic idea to not document it anywhere? Of course it's - // not like there is any documentation on BSD sysctls anyway. - unsigned int interfaceIndex = ((((unsigned int)sin6->sin6_addr.s6_addr[2]) << 8) & 0xff) | (((unsigned int)sin6->sin6_addr.s6_addr[3]) & 0xff); - sin6->sin6_addr.s6_addr[2] = 0; - sin6->sin6_addr.s6_addr[3] = 0; - if (!sin6->sin6_scope_id) - sin6->sin6_scope_id = interfaceIndex; - } - } - e.destination.set(sa); - break; - case 1: - //printf("RTA_GATEWAY\n"); - switch(sa->sa_family) { - case AF_LINK: - e.deviceIndex = (int)((const struct sockaddr_dl *)sa)->sdl_index; - break; - case AF_INET: - case AF_INET6: - e.gateway.set(sa); - break; - } - break; - case 2: { - if (e.destination.isV6()) { - salen = sizeof(struct sockaddr_in6); // Confess! - unsigned int bits = 0; - for(int i=0;i<16;++i) { - unsigned char c = (unsigned char)((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[i]; - if (c == 0xff) - bits += 8; - else break; - /* must they be multiples of 8? Most of the BSD source I can find says yes..? - else { - while ((c & 0x80) == 0x80) { - ++bits; - c <<= 1; - } - break; - } - */ - } - e.destination.setPort(bits); - } else { - salen = sizeof(struct sockaddr_in); // Confess! - e.destination.setPort((unsigned int)Utils::countBits((uint32_t)((const struct sockaddr_in *)sa)->sin_addr.s_addr)); - } - //printf("RTA_NETMASK\n"); - } break; - /* - case 3: - //printf("RTA_GENMASK\n"); - break; - case 4: - //printf("RTA_IFP\n"); - break; - case 5: - //printf("RTA_IFA\n"); - break; - case 6: - //printf("RTA_AUTHOR\n"); - break; - */ - } - - saptr += salen; - } - - 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); - } - - next = saend; - } - } - - ::free(buf); - } - } - - for(std::vector::iterator e1(entries.begin());e1!=entries.end();++e1) { - if ((!e1->device[0])&&(e1->deviceIndex >= 0)) - if_indextoname(e1->deviceIndex,e1->device); - } - for(std::vector::iterator e1(entries.begin());e1!=entries.end();++e1) { - if ((!e1->device[0])&&(e1->gateway)) { - int bestMetric = 9999999; - for(std::vector::iterator e2(entries.begin());e2!=entries.end();++e2) { - if ((e1->gateway.within(e2->destination))&&(e2->metric <= bestMetric)) { - bestMetric = e2->metric; - Utils::scopy(e1->device,sizeof(e1->device),e2->device); - } - } - } - } - - std::sort(entries.begin(),entries.end()); - - return entries; -} - -RoutingTable::Entry BSDRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) -{ - 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) -{ - 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/osnet/BSDRoutingTable.hpp b/osnet/BSDRoutingTable.hpp deleted file mode 100644 index 97969666..00000000 --- a/osnet/BSDRoutingTable.hpp +++ /dev/null @@ -1,51 +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 . - * - * -- - * - * 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_BSDROUTINGTABLE_HPP -#define ZT_BSDROUTINGTABLE_HPP - -#include "../node/RoutingTable.hpp" - -namespace ZeroTier { - -/** - * Routing table interface for BSD with sysctl() and BSD /sbin/route - * - * Has currently only been tested on OSX/Darwin. - */ -class BSDRoutingTable : public RoutingTable -{ -public: - BSDRoutingTable(); - virtual ~BSDRoutingTable(); - virtual std::vector 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 diff --git a/osnet/LinuxRoutingTable.cpp b/osnet/LinuxRoutingTable.cpp deleted file mode 100644 index 581054e2..00000000 --- a/osnet/LinuxRoutingTable.cpp +++ /dev/null @@ -1,235 +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 . - * - * -- - * - * 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "../node/Constants.hpp" -#include "../node/Utils.hpp" -#include "LinuxRoutingTable.hpp" - -#define ZT_LINUX_IP_COMMAND "/sbin/ip" - -namespace ZeroTier { - -LinuxRoutingTable::LinuxRoutingTable() -{ -} - -LinuxRoutingTable::~LinuxRoutingTable() -{ -} - -std::vector LinuxRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const -{ - char buf[131072]; - char *stmp,*stmp2; - std::vector entries; - - { - int fd = ::open("/proc/net/route",O_RDONLY); - if (fd <= 0) - buf[0] = (char)0; - else { - int n = (int)::read(fd,buf,sizeof(buf) - 1); - ::close(fd); - if (n < 0) n = 0; - buf[n] = (char)0; - } - } - - int lineno = 0; - for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) { - if (lineno == 0) { - ++lineno; - continue; // skip header - } - - char *iface = (char *)0; - uint32_t destination = 0; - uint32_t gateway = 0; - int metric = 0; - uint32_t mask = 0; - - int fno = 0; - for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) { - switch(fno) { - case 0: iface = f; break; - case 1: destination = (uint32_t)Utils::hexStrToULong(f); break; - case 2: gateway = (uint32_t)Utils::hexStrToULong(f); break; - case 6: metric = (int)Utils::strToInt(f); break; - case 7: mask = (uint32_t)Utils::hexStrToULong(f); break; - } - ++fno; - } - - if ((iface)&&(destination)) { - RoutingTable::Entry e; - if (destination) - e.destination.set(&destination,4,Utils::countBits(mask)); - e.gateway.set(&gateway,4,0); - e.deviceIndex = 0; // not used on Linux - e.metric = metric; - Utils::scopy(e.device,sizeof(e.device),iface); - if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(iface,"lo"))))) - entries.push_back(e); - } - - ++lineno; - } - - { - int fd = ::open("/proc/net/ipv6_route",O_RDONLY); - if (fd <= 0) - buf[0] = (char)0; - else { - int n = (int)::read(fd,buf,sizeof(buf) - 1); - ::close(fd); - if (n < 0) n = 0; - buf[n] = (char)0; - } - } - - for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) { - char *destination = (char *)0; - unsigned int destPrefixLen = 0; - char *gateway = (char *)0; // next hop in ipv6 terminology - int metric = 0; - char *device = (char *)0; - - int fno = 0; - for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) { - switch(fno) { - case 0: destination = f; break; - case 1: destPrefixLen = (unsigned int)Utils::hexStrToULong(f); break; - case 4: gateway = f; break; - case 5: metric = (int)Utils::hexStrToLong(f); break; - case 9: device = f; break; - } - ++fno; - } - - if ((device)&&(destination)) { - unsigned char tmp[16]; - RoutingTable::Entry e; - Utils::unhex(destination,tmp,16); - if ((!Utils::isZero(tmp,16))&&(tmp[0] != 0xff)) - e.destination.set(tmp,16,destPrefixLen); - Utils::unhex(gateway,tmp,16); - e.gateway.set(tmp,16,0); - e.deviceIndex = 0; // not used on Linux - e.metric = metric; - Utils::scopy(e.device,sizeof(e.device),device); - if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(device,"lo"))))) - entries.push_back(e); - } - } - - std::sort(entries.begin(),entries.end()); - return entries; -} - -RoutingTable::Entry LinuxRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) -{ - char metstr[128]; - - if ((!gateway)&&((!device)||(!device[0]))) - return RoutingTable::Entry(); - - Utils::snprintf(metstr,sizeof(metstr),"%d",metric); - - if (metric < 0) { - long pid = (long)vfork(); - if (pid == 0) { - if (gateway) { - if ((device)&&(device[0])) { - ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"via",gateway.toIpString().c_str(),"dev",device,(const char *)0); - } else { - ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"via",gateway.toIpString().c_str(),(const char *)0); - } - } else { - ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"dev",device,(const char *)0); - } - ::_exit(-1); - } else if (pid > 0) { - int exitcode = -1; - ::waitpid(pid,&exitcode,0); - } - } else { - long pid = (long)vfork(); - if (pid == 0) { - if (gateway) { - if ((device)&&(device[0])) { - ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"via",gateway.toIpString().c_str(),"dev",device,(const char *)0); - } else { - ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"via",gateway.toIpString().c_str(),(const char *)0); - } - } else { - ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"dev",device,(const char *)0); - } - ::_exit(-1); - } else if (pid > 0) { - int exitcode = -1; - ::waitpid(pid,&exitcode,0); - } - } - - std::vector 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 diff --git a/osnet/LinuxRoutingTable.hpp b/osnet/LinuxRoutingTable.hpp deleted file mode 100644 index 808ec7ea..00000000 --- a/osnet/LinuxRoutingTable.hpp +++ /dev/null @@ -1,49 +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 . - * - * -- - * - * 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 -#define ZT_LINUXROUTINGTABLE_HPP - -#include "../node/RoutingTable.hpp" - -namespace ZeroTier { - -/** - * Routing table interface via /proc/net/route, /proc/net/ipv6_route, and /sbin/route command - */ -class LinuxRoutingTable : public RoutingTable -{ -public: - LinuxRoutingTable(); - virtual ~LinuxRoutingTable(); - virtual std::vector 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 diff --git a/osnet/WindowsRoutingTable.cpp b/osnet/WindowsRoutingTable.cpp deleted file mode 100644 index 00674620..00000000 --- a/osnet/WindowsRoutingTable.cpp +++ /dev/null @@ -1,178 +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 . - * - * -- - * - * 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 -#include -#include -#include -#include -#include -#include - -#include - -#include "../node/Constants.hpp" -#include "WindowsRoutingTable.hpp" - -namespace ZeroTier { - -static void _copyInetAddressToSockaddrInet(const InetAddress &a,SOCKADDR_INET &sinet) -{ - memset(&sinet,0,sizeof(sinet)); - if (a.isV4()) { - sinet.Ipv4.sin_addr.S_un.S_addr = *((const uint32_t *)a.rawIpData()); - sinet.Ipv4.sin_family = AF_INET; - sinet.Ipv4.sin_port = htons(a.port()); - } else if (a.isV6()) { - memcpy(sinet.Ipv6.sin6_addr.u.Byte,a.rawIpData(),16); - sinet.Ipv6.sin6_family = AF_INET6; - sinet.Ipv6.sin6_port = htons(a.port()); - } -} - -WindowsRoutingTable::WindowsRoutingTable() -{ -} - -WindowsRoutingTable::~WindowsRoutingTable() -{ -} - -std::vector WindowsRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const -{ - std::vector entries; - PMIB_IPFORWARD_TABLE2 rtbl = NULL; - - if (GetIpForwardTable2(AF_UNSPEC,&rtbl) != NO_ERROR) - return entries; - if (!rtbl) - return entries; - - for(ULONG r=0;rNumEntries;++r) { - RoutingTable::Entry e; - switch(rtbl->Table[r].DestinationPrefix.Prefix.si_family) { - case AF_INET: - e.destination.set(&(rtbl->Table[r].DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr),4,rtbl->Table[r].DestinationPrefix.PrefixLength); - break; - case AF_INET6: - e.destination.set(rtbl->Table[r].DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte,16,rtbl->Table[r].DestinationPrefix.PrefixLength); - break; - } - switch(rtbl->Table[r].NextHop.si_family) { - case AF_INET: - e.gateway.set(&(rtbl->Table[r].NextHop.Ipv4.sin_addr.S_un.S_addr),4,0); - break; - case AF_INET6: - e.gateway.set(rtbl->Table[r].NextHop.Ipv6.sin6_addr.u.Byte,16,0); - break; - } - e.deviceIndex = (int)rtbl->Table[r].InterfaceIndex; - e.metric = (int)rtbl->Table[r].Metric; - ConvertInterfaceLuidToNameA(&(rtbl->Table[r].InterfaceLuid),e.device,sizeof(e.device)); - if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())))) - entries.push_back(e); - } - - FreeMibTable(rtbl); - std::sort(entries.begin(),entries.end()); - return entries; -} - -RoutingTable::Entry WindowsRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) -{ - NET_LUID luid; - luid.Value = 0; - if (ConvertInterfaceNameToLuidA(device,&luid) != NO_ERROR) - return RoutingTable::Entry(); - - bool needCreate = true; - PMIB_IPFORWARD_TABLE2 rtbl = NULL; - if (GetIpForwardTable2(AF_UNSPEC,&rtbl) != NO_ERROR) - return RoutingTable::Entry(); - if (!rtbl) - return RoutingTable::Entry(); - for(ULONG r=0;rNumEntries;++r) { - if (rtbl->Table[r].InterfaceLuid.Value == luid.Value) { - InetAddress rdest; - switch(rtbl->Table[r].DestinationPrefix.Prefix.si_family) { - case AF_INET: - rdest.set(&(rtbl->Table[r].DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr),4,rtbl->Table[r].DestinationPrefix.PrefixLength); - break; - case AF_INET6: - rdest.set(rtbl->Table[r].DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte,16,rtbl->Table[r].DestinationPrefix.PrefixLength); - break; - } - if (rdest == destination) { - if (metric >= 0) { - _copyInetAddressToSockaddrInet(gateway,rtbl->Table[r].NextHop); - rtbl->Table[r].Metric = metric; - SetIpForwardEntry2(&(rtbl->Table[r])); - needCreate = false; - } else { - DeleteIpForwardEntry2(&(rtbl->Table[r])); - FreeMibTable(rtbl); - return RoutingTable::Entry(); - } - } - } - } - FreeMibTable(rtbl); - - if ((metric >= 0)&&(needCreate)) { - MIB_IPFORWARD_ROW2 nr; - InitializeIpForwardEntry(&nr); - nr.InterfaceLuid.Value = luid.Value; - _copyInetAddressToSockaddrInet(destination,nr.DestinationPrefix.Prefix); - nr.DestinationPrefix.PrefixLength = destination.netmaskBits(); - _copyInetAddressToSockaddrInet(gateway,nr.NextHop); - nr.Metric = metric; - nr.Protocol = MIB_IPPROTO_NETMGMT; - DWORD result = CreateIpForwardEntry2(&nr); - if (result != NO_ERROR) - return RoutingTable::Entry(); - } - - std::vector 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 diff --git a/osnet/WindowsRoutingTable.hpp b/osnet/WindowsRoutingTable.hpp deleted file mode 100644 index 491c3424..00000000 --- a/osnet/WindowsRoutingTable.hpp +++ /dev/null @@ -1,49 +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 . - * - * -- - * - * 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 -#define ZT_WINDOWSROUTINGTABLE_HPP - -#include "../node/RoutingTable.hpp" - -namespace ZeroTier { - -/** - * Interface to Microsoft Windows (Vista or newer) routing table - */ -class WindowsRoutingTable : public RoutingTable -{ -public: - WindowsRoutingTable(); - virtual ~WindowsRoutingTable(); - virtual std::vector 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 diff --git a/testnet.cpp b/testnet.cpp index eeb59b90..5b175728 100644 --- a/testnet.cpp +++ b/testnet.cpp @@ -50,7 +50,6 @@ #include "testnet/SimNetSocketManager.hpp" #include "testnet/TestEthernetTap.hpp" #include "testnet/TestEthernetTapFactory.hpp" -#include "testnet/TestRoutingTable.hpp" #ifdef __WINDOWS__ #include @@ -67,9 +66,8 @@ public: SimNode(SimNet &net,const std::string &hp,const char *rootTopology,bool issn,const InetAddress &addr) : home(hp), tapFactory(), - routingTable(), socketManager(net.newEndpoint(addr)), - node(home.c_str(),&tapFactory,&routingTable,socketManager,false,rootTopology), + node(home.c_str(),&tapFactory,socketManager,false,rootTopology), reasonForTermination(Node::NODE_RUNNING), supernode(issn) { @@ -90,7 +88,6 @@ public: std::string home; TestEthernetTapFactory tapFactory; - TestRoutingTable routingTable; SimNetSocketManager *socketManager; Node node; Node::ReasonForTermination reasonForTermination; diff --git a/testnet/TestRoutingTable.cpp b/testnet/TestRoutingTable.cpp deleted file mode 100644 index fd61b314..00000000 --- a/testnet/TestRoutingTable.cpp +++ /dev/null @@ -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 . - * - * -- - * - * 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 TestRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const -{ - return std::vector(); -} - -RoutingTable::Entry TestRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) -{ - return RoutingTable::Entry(); -} - -} // namespace ZeroTier diff --git a/testnet/TestRoutingTable.hpp b/testnet/TestRoutingTable.hpp deleted file mode 100644 index 69bd3d9f..00000000 --- a/testnet/TestRoutingTable.hpp +++ /dev/null @@ -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 . - * - * -- - * - * 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 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 -- cgit v1.2.3