summaryrefslogtreecommitdiff
path: root/attic/rtbl/LinuxRoutingTable.cpp
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2015-03-30 17:48:48 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2015-03-30 17:48:48 -0700
commit60158aa5dd38c979ed823fc477ab84e8d9e7a43d (patch)
tree8cdc7091547c8173fdc270ed3d56853ddc5e6da4 /attic/rtbl/LinuxRoutingTable.cpp
parent3c1a59fa2493c6ab2758934fe1062d82975de158 (diff)
downloadinfinitytier-60158aa5dd38c979ed823fc477ab84e8d9e7a43d.tar.gz
infinitytier-60158aa5dd38c979ed823fc477ab84e8d9e7a43d.zip
Turns out that node/ likely has no business with or need for the system IP routing table. So shelve that code for now.
Diffstat (limited to 'attic/rtbl/LinuxRoutingTable.cpp')
-rw-r--r--attic/rtbl/LinuxRoutingTable.cpp235
1 files changed, 235 insertions, 0 deletions
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 <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 <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <ifaddrs.h>
+
+#include <algorithm>
+#include <utility>
+
+#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<RoutingTable::Entry> LinuxRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
+{
+ char buf[131072];
+ char *stmp,*stmp2;
+ std::vector<RoutingTable::Entry> 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<RoutingTable::Entry> rtab(get(true,true));
+ std::vector<RoutingTable::Entry>::iterator bestEntry(rtab.end());
+ for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
+ if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
+ if ((device)&&(device[0])) {
+ if (!strcmp(device,e->device)) {
+ if (metric == e->metric)
+ bestEntry = e;
+ }
+ }
+ if (bestEntry == rtab.end())
+ bestEntry = e;
+ }
+ }
+ if (bestEntry != rtab.end())
+ return *bestEntry;
+
+ return RoutingTable::Entry();
+}
+
+} // namespace ZeroTier