LinuxRoutingTable.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <sys/socket.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include <netinet/in.h>
  37. #include <arpa/inet.h>
  38. #include <ifaddrs.h>
  39. #include <algorithm>
  40. #include <utility>
  41. #include "../Constants.hpp"
  42. #include "../Utils.hpp"
  43. #include "LinuxRoutingTable.hpp"
  44. #define ZT_LINUX_IP_COMMAND "/sbin/ip"
  45. namespace ZeroTier {
  46. LinuxRoutingTable::LinuxRoutingTable()
  47. {
  48. }
  49. LinuxRoutingTable::~LinuxRoutingTable()
  50. {
  51. }
  52. std::vector<RoutingTable::Entry> LinuxRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
  53. {
  54. char buf[131072];
  55. char *stmp,*stmp2;
  56. std::vector<RoutingTable::Entry> entries;
  57. {
  58. int fd = ::open("/proc/net/route",O_RDONLY);
  59. if (fd <= 0)
  60. buf[0] = (char)0;
  61. else {
  62. int n = (int)::read(fd,buf,sizeof(buf) - 1);
  63. ::close(fd);
  64. if (n < 0) n = 0;
  65. buf[n] = (char)0;
  66. }
  67. }
  68. int lineno = 0;
  69. for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) {
  70. if (lineno == 0) {
  71. ++lineno;
  72. continue; // skip header
  73. }
  74. char *iface = (char *)0;
  75. uint32_t destination = 0;
  76. uint32_t gateway = 0;
  77. int metric = 0;
  78. uint32_t mask = 0;
  79. int fno = 0;
  80. for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) {
  81. switch(fno) {
  82. case 0: iface = f; break;
  83. case 1: destination = (uint32_t)Utils::hexStrToULong(f); break;
  84. case 2: gateway = (uint32_t)Utils::hexStrToULong(f); break;
  85. case 6: metric = (int)Utils::strToInt(f); break;
  86. case 7: mask = (uint32_t)Utils::hexStrToULong(f); break;
  87. }
  88. ++fno;
  89. }
  90. if ((iface)&&(destination)) {
  91. RoutingTable::Entry e;
  92. if (destination)
  93. e.destination.set(&destination,4,Utils::countBits(mask));
  94. e.gateway.set(&gateway,4,0);
  95. e.deviceIndex = 0; // not used on Linux
  96. e.metric = metric;
  97. Utils::scopy(e.device,sizeof(e.device),iface);
  98. if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(iface,"lo")))))
  99. entries.push_back(e);
  100. }
  101. ++lineno;
  102. }
  103. {
  104. int fd = ::open("/proc/net/ipv6_route",O_RDONLY);
  105. if (fd <= 0)
  106. buf[0] = (char)0;
  107. else {
  108. int n = (int)::read(fd,buf,sizeof(buf) - 1);
  109. ::close(fd);
  110. if (n < 0) n = 0;
  111. buf[n] = (char)0;
  112. }
  113. }
  114. for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) {
  115. char *destination = (char *)0;
  116. unsigned int destPrefixLen = 0;
  117. char *gateway = (char *)0; // next hop in ipv6 terminology
  118. int metric = 0;
  119. char *device = (char *)0;
  120. int fno = 0;
  121. for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) {
  122. switch(fno) {
  123. case 0: destination = f; break;
  124. case 1: destPrefixLen = (unsigned int)Utils::hexStrToULong(f); break;
  125. case 4: gateway = f; break;
  126. case 5: metric = (int)Utils::hexStrToLong(f); break;
  127. case 9: device = f; break;
  128. }
  129. ++fno;
  130. }
  131. if ((device)&&(destination)) {
  132. unsigned char tmp[16];
  133. RoutingTable::Entry e;
  134. Utils::unhex(destination,tmp,16);
  135. if ((!Utils::isZero(tmp,16))&&(tmp[0] != 0xff))
  136. e.destination.set(tmp,16,destPrefixLen);
  137. Utils::unhex(gateway,tmp,16);
  138. e.gateway.set(tmp,16,0);
  139. e.deviceIndex = 0; // not used on Linux
  140. e.metric = metric;
  141. Utils::scopy(e.device,sizeof(e.device),device);
  142. if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(device,"lo")))))
  143. entries.push_back(e);
  144. }
  145. }
  146. std::sort(entries.begin(),entries.end());
  147. return entries;
  148. }
  149. RoutingTable::Entry LinuxRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric)
  150. {
  151. if ((!gateway)&&((!device)||(!device[0])))
  152. return RoutingTable::Entry();
  153. std::vector<RoutingTable::Entry> rtab(get(true,true));
  154. for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
  155. if (e->destination == destination) {
  156. if (((!device)||(!device[0]))||(!strcmp(device,e->device))) {
  157. }
  158. }
  159. }
  160. if (metric < 0)
  161. return RoutingTable::Entry();
  162. rtab = get(true,true);
  163. std::vector<RoutingTable::Entry>::iterator bestEntry(rtab.end());
  164. for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
  165. if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
  166. if ((device)&&(device[0])) {
  167. if (!strcmp(device,e->device)) {
  168. if (metric == e->metric)
  169. bestEntry = e;
  170. }
  171. }
  172. if (bestEntry == rtab.end())
  173. bestEntry = e;
  174. }
  175. }
  176. if (bestEntry != rtab.end())
  177. return *bestEntry;
  178. return RoutingTable::Entry();
  179. }
  180. } // namespace ZeroTier
  181. // Enable and build to test routing table interface
  182. //#if 0
  183. using namespace ZeroTier;
  184. int main(int argc,char **argv)
  185. {
  186. LinuxRoutingTable rt;
  187. printf("<destination> <gateway> <interface> <metric>\n");
  188. std::vector<RoutingTable::Entry> ents(rt.get());
  189. for(std::vector<RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
  190. printf("%s\n",e->toString().c_str());
  191. printf("\n");
  192. printf("adding 1.1.1.0 and 2.2.2.0...\n");
  193. rt.set(InetAddress("1.1.1.0",24),InetAddress("1.2.3.4",0),(const char *)0,1);
  194. rt.set(InetAddress("2.2.2.0",24),InetAddress(),"en0",1);
  195. printf("\n");
  196. printf("<destination> <gateway> <interface> <metric>\n");
  197. ents = rt.get();
  198. for(std::vector<RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
  199. printf("%s\n",e->toString().c_str());
  200. printf("\n");
  201. printf("deleting 1.1.1.0 and 2.2.2.0...\n");
  202. rt.set(InetAddress("1.1.1.0",24),InetAddress("1.2.3.4",0),(const char *)0,-1);
  203. rt.set(InetAddress("2.2.2.0",24),InetAddress(),"en0",-1);
  204. printf("\n");
  205. printf("<destination> <gateway> <interface> <metric>\n");
  206. ents = rt.get();
  207. for(std::vector<RoutingTable::Entry>::iterator e(ents.begin());e!=ents.end();++e)
  208. printf("%s\n",e->toString().c_str());
  209. printf("\n");
  210. return 0;
  211. }
  212. //#endif