RoutingTable.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #ifndef ZT_ROUTINGTABLE_HPP
  19. #define ZT_ROUTINGTABLE_HPP
  20. #include "../node/Constants.hpp"
  21. #ifdef __WINDOWS__
  22. #include <WinSock2.h>
  23. #include <Windows.h>
  24. #include <netioapi.h>
  25. #include <IPHlpApi.h>
  26. #endif
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #ifdef __UNIX_LIKE__
  32. #include <unistd.h>
  33. #include <sys/param.h>
  34. #include <sys/sysctl.h>
  35. #include <sys/socket.h>
  36. #include <sys/types.h>
  37. #include <sys/wait.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include <net/route.h>
  41. #include <net/if.h>
  42. #include <net/if_dl.h>
  43. #include <ifaddrs.h>
  44. #endif
  45. #include <vector>
  46. #include <algorithm>
  47. #include <utility>
  48. #include "RoutingTable.hpp"
  49. #define ZT_BSD_ROUTE_CMD "/sbin/route"
  50. #define ZT_LINUX_IP_COMMAND "/sbin/ip"
  51. namespace ZeroTier {
  52. // ---------------------------------------------------------------------------
  53. #ifdef __LINUX__
  54. std::vector<RoutingTable::Entry> RoutingTable::get(bool includeLinkLocal,bool includeLoopback)
  55. {
  56. char buf[131072];
  57. char *stmp,*stmp2;
  58. std::vector<RoutingTable::Entry> entries;
  59. {
  60. int fd = ::open("/proc/net/route",O_RDONLY);
  61. if (fd <= 0)
  62. buf[0] = (char)0;
  63. else {
  64. int n = (int)::read(fd,buf,sizeof(buf) - 1);
  65. ::close(fd);
  66. if (n < 0) n = 0;
  67. buf[n] = (char)0;
  68. }
  69. }
  70. int lineno = 0;
  71. for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) {
  72. if (lineno == 0) {
  73. ++lineno;
  74. continue; // skip header
  75. }
  76. char *iface = (char *)0;
  77. uint32_t destination = 0;
  78. uint32_t gateway = 0;
  79. int metric = 0;
  80. uint32_t mask = 0;
  81. int fno = 0;
  82. for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) {
  83. switch(fno) {
  84. case 0: iface = f; break;
  85. case 1: destination = (uint32_t)Utils::hexStrToULong(f); break;
  86. case 2: gateway = (uint32_t)Utils::hexStrToULong(f); break;
  87. case 6: metric = (int)Utils::strToInt(f); break;
  88. case 7: mask = (uint32_t)Utils::hexStrToULong(f); break;
  89. }
  90. ++fno;
  91. }
  92. if ((iface)&&(destination)) {
  93. RoutingTable::Entry e;
  94. if (destination)
  95. e.destination.set(&destination,4,Utils::countBits(mask));
  96. e.gateway.set(&gateway,4,0);
  97. e.deviceIndex = 0; // not used on Linux
  98. e.metric = metric;
  99. Utils::scopy(e.device,sizeof(e.device),iface);
  100. if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(iface,"lo")))))
  101. entries.push_back(e);
  102. }
  103. ++lineno;
  104. }
  105. {
  106. int fd = ::open("/proc/net/ipv6_route",O_RDONLY);
  107. if (fd <= 0)
  108. buf[0] = (char)0;
  109. else {
  110. int n = (int)::read(fd,buf,sizeof(buf) - 1);
  111. ::close(fd);
  112. if (n < 0) n = 0;
  113. buf[n] = (char)0;
  114. }
  115. }
  116. for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) {
  117. char *destination = (char *)0;
  118. unsigned int destPrefixLen = 0;
  119. char *gateway = (char *)0; // next hop in ipv6 terminology
  120. int metric = 0;
  121. char *device = (char *)0;
  122. int fno = 0;
  123. for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) {
  124. switch(fno) {
  125. case 0: destination = f; break;
  126. case 1: destPrefixLen = (unsigned int)Utils::hexStrToULong(f); break;
  127. case 4: gateway = f; break;
  128. case 5: metric = (int)Utils::hexStrToLong(f); break;
  129. case 9: device = f; break;
  130. }
  131. ++fno;
  132. }
  133. if ((device)&&(destination)) {
  134. unsigned char tmp[16];
  135. RoutingTable::Entry e;
  136. Utils::unhex(destination,tmp,16);
  137. if ((!Utils::isZero(tmp,16))&&(tmp[0] != 0xff))
  138. e.destination.set(tmp,16,destPrefixLen);
  139. Utils::unhex(gateway,tmp,16);
  140. e.gateway.set(tmp,16,0);
  141. e.deviceIndex = 0; // not used on Linux
  142. e.metric = metric;
  143. Utils::scopy(e.device,sizeof(e.device),device);
  144. if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(device,"lo")))))
  145. entries.push_back(e);
  146. }
  147. }
  148. std::sort(entries.begin(),entries.end());
  149. return entries;
  150. }
  151. RoutingTable::Entry RoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric,bool ifscope)
  152. {
  153. char metstr[128];
  154. if ((!gateway)&&((!device)||(!device[0])))
  155. return RoutingTable::Entry();
  156. Utils::snprintf(metstr,sizeof(metstr),"%d",metric);
  157. if (metric < 0) {
  158. long pid = (long)vfork();
  159. if (pid == 0) {
  160. if (gateway) {
  161. if ((device)&&(device[0])) {
  162. ::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);
  163. } else {
  164. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"via",gateway.toIpString().c_str(),(const char *)0);
  165. }
  166. } else {
  167. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"dev",device,(const char *)0);
  168. }
  169. ::_exit(-1);
  170. } else if (pid > 0) {
  171. int exitcode = -1;
  172. ::waitpid(pid,&exitcode,0);
  173. }
  174. } else {
  175. long pid = (long)vfork();
  176. if (pid == 0) {
  177. if (gateway) {
  178. if ((device)&&(device[0])) {
  179. ::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);
  180. } else {
  181. ::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);
  182. }
  183. } else {
  184. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"dev",device,(const char *)0);
  185. }
  186. ::_exit(-1);
  187. } else if (pid > 0) {
  188. int exitcode = -1;
  189. ::waitpid(pid,&exitcode,0);
  190. }
  191. }
  192. std::vector<RoutingTable::Entry> rtab(get(true,true));
  193. std::vector<RoutingTable::Entry>::iterator bestEntry(rtab.end());
  194. for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
  195. if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
  196. if ((device)&&(device[0])) {
  197. if (!strcmp(device,e->device)) {
  198. if (metric == e->metric)
  199. bestEntry = e;
  200. }
  201. }
  202. if (bestEntry == rtab.end())
  203. bestEntry = e;
  204. }
  205. }
  206. if (bestEntry != rtab.end())
  207. return *bestEntry;
  208. return RoutingTable::Entry();
  209. }
  210. #endif // __LINUX__
  211. // ---------------------------------------------------------------------------
  212. #ifdef __BSD__
  213. std::vector<RoutingTable::Entry> RoutingTable::get(bool includeLinkLocal,bool includeLoopback)
  214. {
  215. std::vector<RoutingTable::Entry> entries;
  216. int mib[6];
  217. size_t needed;
  218. mib[0] = CTL_NET;
  219. mib[1] = PF_ROUTE;
  220. mib[2] = 0;
  221. mib[3] = 0;
  222. mib[4] = NET_RT_DUMP;
  223. mib[5] = 0;
  224. if (!sysctl(mib,6,NULL,&needed,NULL,0)) {
  225. if (needed <= 0)
  226. return entries;
  227. char *buf = (char *)::malloc(needed);
  228. if (buf) {
  229. if (!sysctl(mib,6,buf,&needed,NULL,0)) {
  230. struct rt_msghdr *rtm;
  231. for(char *next=buf,*end=buf+needed;next<end;) {
  232. rtm = (struct rt_msghdr *)next;
  233. char *saptr = (char *)(rtm + 1);
  234. char *saend = next + rtm->rtm_msglen;
  235. 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)) {
  236. RoutingTable::Entry e;
  237. e.deviceIndex = -9999; // unset
  238. int which = 0;
  239. while (saptr < saend) {
  240. struct sockaddr *sa = (struct sockaddr *)saptr;
  241. unsigned int salen = sa->sa_len;
  242. if (!salen)
  243. break;
  244. // Skip missing fields in rtm_addrs bit field
  245. while ((rtm->rtm_addrs & 1) == 0) {
  246. rtm->rtm_addrs >>= 1;
  247. ++which;
  248. if (which > 6)
  249. break;
  250. }
  251. if (which > 6)
  252. break;
  253. rtm->rtm_addrs >>= 1;
  254. switch(which++) {
  255. case 0:
  256. //printf("RTA_DST\n");
  257. if (sa->sa_family == AF_INET6) {
  258. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  259. // Nobody expects the Spanish inquisition!
  260. if ((sin6->sin6_addr.s6_addr[0] == 0xfe)&&((sin6->sin6_addr.s6_addr[1] & 0xc0) == 0x80)) {
  261. // Our chief weapon is... in-band signaling!
  262. // Seriously who in the living fuck thought this was a good idea and
  263. // then had the sadistic idea to not document it anywhere? Of course it's
  264. // not like there is any documentation on BSD sysctls anyway.
  265. unsigned int interfaceIndex = ((((unsigned int)sin6->sin6_addr.s6_addr[2]) << 8) & 0xff) | (((unsigned int)sin6->sin6_addr.s6_addr[3]) & 0xff);
  266. sin6->sin6_addr.s6_addr[2] = 0;
  267. sin6->sin6_addr.s6_addr[3] = 0;
  268. if (!sin6->sin6_scope_id)
  269. sin6->sin6_scope_id = interfaceIndex;
  270. }
  271. }
  272. e.destination.set(sa);
  273. break;
  274. case 1:
  275. //printf("RTA_GATEWAY\n");
  276. switch(sa->sa_family) {
  277. case AF_LINK:
  278. e.deviceIndex = (int)((const struct sockaddr_dl *)sa)->sdl_index;
  279. break;
  280. case AF_INET:
  281. case AF_INET6:
  282. e.gateway.set(sa);
  283. break;
  284. }
  285. break;
  286. case 2: {
  287. if (e.destination.isV6()) {
  288. salen = sizeof(struct sockaddr_in6); // Confess!
  289. unsigned int bits = 0;
  290. for(int i=0;i<16;++i) {
  291. unsigned char c = (unsigned char)((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[i];
  292. if (c == 0xff)
  293. bits += 8;
  294. else break;
  295. /* must they be multiples of 8? Most of the BSD source I can find says yes..?
  296. else {
  297. while ((c & 0x80) == 0x80) {
  298. ++bits;
  299. c <<= 1;
  300. }
  301. break;
  302. }
  303. */
  304. }
  305. e.destination.setPort(bits);
  306. } else {
  307. salen = sizeof(struct sockaddr_in); // Confess!
  308. e.destination.setPort((unsigned int)Utils::countBits((uint32_t)((const struct sockaddr_in *)sa)->sin_addr.s_addr));
  309. }
  310. //printf("RTA_NETMASK\n");
  311. } break;
  312. /*
  313. case 3:
  314. //printf("RTA_GENMASK\n");
  315. break;
  316. case 4:
  317. //printf("RTA_IFP\n");
  318. break;
  319. case 5:
  320. //printf("RTA_IFA\n");
  321. break;
  322. case 6:
  323. //printf("RTA_AUTHOR\n");
  324. break;
  325. */
  326. }
  327. saptr += salen;
  328. }
  329. e.metric = (int)rtm->rtm_rmx.rmx_hopcount;
  330. if (e.metric < 0)
  331. e.metric = 0;
  332. if (((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback()))))
  333. entries.push_back(e);
  334. }
  335. next = saend;
  336. }
  337. }
  338. ::free(buf);
  339. }
  340. }
  341. for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e1(entries.begin());e1!=entries.end();++e1) {
  342. if ((!e1->device[0])&&(e1->deviceIndex >= 0))
  343. if_indextoname(e1->deviceIndex,e1->device);
  344. }
  345. for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e1(entries.begin());e1!=entries.end();++e1) {
  346. if ((!e1->device[0])&&(e1->gateway)) {
  347. int bestMetric = 9999999;
  348. for(std::vector<ZeroTier::RoutingTable::Entry>::iterator e2(entries.begin());e2!=entries.end();++e2) {
  349. if ((e1->gateway.within(e2->destination))&&(e2->metric <= bestMetric)) {
  350. bestMetric = e2->metric;
  351. Utils::scopy(e1->device,sizeof(e1->device),e2->device);
  352. }
  353. }
  354. }
  355. }
  356. std::sort(entries.begin(),entries.end());
  357. return entries;
  358. }
  359. RoutingTable::Entry RoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric,bool ifscope)
  360. {
  361. if ((!gateway)&&((!device)||(!device[0])))
  362. return RoutingTable::Entry();
  363. std::vector<RoutingTable::Entry> rtab(get(true,true));
  364. for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
  365. if (e->destination == destination) {
  366. if (((!device)||(!device[0]))||(!strcmp(device,e->device))) {
  367. long p = (long)fork();
  368. if (p > 0) {
  369. int exitcode = -1;
  370. ::waitpid(p,&exitcode,0);
  371. } else if (p == 0) {
  372. ::close(STDOUT_FILENO);
  373. ::close(STDERR_FILENO);
  374. ::execl(ZT_BSD_ROUTE_CMD,ZT_BSD_ROUTE_CMD,"delete",(destination.isV6() ? "-inet6" : "-inet"),destination.toString().c_str(),(const char *)0);
  375. ::_exit(-1);
  376. }
  377. }
  378. }
  379. }
  380. if (metric < 0)
  381. return RoutingTable::Entry();
  382. {
  383. char hcstr[64];
  384. Utils::snprintf(hcstr,sizeof(hcstr),"%d",metric);
  385. long p = (long)fork();
  386. if (p > 0) {
  387. int exitcode = -1;
  388. ::waitpid(p,&exitcode,0);
  389. } else if (p == 0) {
  390. ::close(STDOUT_FILENO);
  391. ::close(STDERR_FILENO);
  392. if (gateway) {
  393. ::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);
  394. } else if ((device)&&(device[0])) {
  395. ::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);
  396. }
  397. ::_exit(-1);
  398. }
  399. }
  400. rtab = get(true,true);
  401. std::vector<RoutingTable::Entry>::iterator bestEntry(rtab.end());
  402. for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
  403. if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
  404. if ((device)&&(device[0])) {
  405. if (!strcmp(device,e->device)) {
  406. if (metric == e->metric)
  407. bestEntry = e;
  408. }
  409. }
  410. if (bestEntry == rtab.end())
  411. bestEntry = e;
  412. }
  413. }
  414. if (bestEntry != rtab.end())
  415. return *bestEntry;
  416. return RoutingTable::Entry();
  417. }
  418. #endif // __BSD__
  419. // ---------------------------------------------------------------------------
  420. #ifdef __WINDOWS__
  421. static void _copyInetAddressToSockaddrInet(const InetAddress &a,SOCKADDR_INET &sinet)
  422. {
  423. memset(&sinet,0,sizeof(sinet));
  424. if (a.isV4()) {
  425. sinet.Ipv4.sin_addr.S_un.S_addr = *((const uint32_t *)a.rawIpData());
  426. sinet.Ipv4.sin_family = AF_INET;
  427. sinet.Ipv4.sin_port = htons(a.port());
  428. } else if (a.isV6()) {
  429. memcpy(sinet.Ipv6.sin6_addr.u.Byte,a.rawIpData(),16);
  430. sinet.Ipv6.sin6_family = AF_INET6;
  431. sinet.Ipv6.sin6_port = htons(a.port());
  432. }
  433. }
  434. std::vector<RoutingTable::Entry> RoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
  435. {
  436. std::vector<RoutingTable::Entry> entries;
  437. PMIB_IPFORWARD_TABLE2 rtbl = NULL;
  438. if (GetIpForwardTable2(AF_UNSPEC,&rtbl) != NO_ERROR)
  439. return entries;
  440. if (!rtbl)
  441. return entries;
  442. for(ULONG r=0;r<rtbl->NumEntries;++r) {
  443. RoutingTable::Entry e;
  444. switch(rtbl->Table[r].DestinationPrefix.Prefix.si_family) {
  445. case AF_INET:
  446. e.destination.set(&(rtbl->Table[r].DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr),4,rtbl->Table[r].DestinationPrefix.PrefixLength);
  447. break;
  448. case AF_INET6:
  449. e.destination.set(rtbl->Table[r].DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte,16,rtbl->Table[r].DestinationPrefix.PrefixLength);
  450. break;
  451. }
  452. switch(rtbl->Table[r].NextHop.si_family) {
  453. case AF_INET:
  454. e.gateway.set(&(rtbl->Table[r].NextHop.Ipv4.sin_addr.S_un.S_addr),4,0);
  455. break;
  456. case AF_INET6:
  457. e.gateway.set(rtbl->Table[r].NextHop.Ipv6.sin6_addr.u.Byte,16,0);
  458. break;
  459. }
  460. e.deviceIndex = (int)rtbl->Table[r].InterfaceIndex;
  461. e.metric = (int)rtbl->Table[r].Metric;
  462. ConvertInterfaceLuidToNameA(&(rtbl->Table[r].InterfaceLuid),e.device,sizeof(e.device));
  463. if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback()))))
  464. entries.push_back(e);
  465. }
  466. FreeMibTable(rtbl);
  467. std::sort(entries.begin(),entries.end());
  468. return entries;
  469. }
  470. RoutingTable::Entry RoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric,bool ifscope)
  471. {
  472. NET_LUID luid;
  473. luid.Value = 0;
  474. if (ConvertInterfaceNameToLuidA(device,&luid) != NO_ERROR)
  475. return RoutingTable::Entry();
  476. bool needCreate = true;
  477. PMIB_IPFORWARD_TABLE2 rtbl = NULL;
  478. if (GetIpForwardTable2(AF_UNSPEC,&rtbl) != NO_ERROR)
  479. return RoutingTable::Entry();
  480. if (!rtbl)
  481. return RoutingTable::Entry();
  482. for(ULONG r=0;r<rtbl->NumEntries;++r) {
  483. if (rtbl->Table[r].InterfaceLuid.Value == luid.Value) {
  484. InetAddress rdest;
  485. switch(rtbl->Table[r].DestinationPrefix.Prefix.si_family) {
  486. case AF_INET:
  487. rdest.set(&(rtbl->Table[r].DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr),4,rtbl->Table[r].DestinationPrefix.PrefixLength);
  488. break;
  489. case AF_INET6:
  490. rdest.set(rtbl->Table[r].DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte,16,rtbl->Table[r].DestinationPrefix.PrefixLength);
  491. break;
  492. }
  493. if (rdest == destination) {
  494. if (metric >= 0) {
  495. _copyInetAddressToSockaddrInet(gateway,rtbl->Table[r].NextHop);
  496. rtbl->Table[r].Metric = metric;
  497. SetIpForwardEntry2(&(rtbl->Table[r]));
  498. needCreate = false;
  499. } else {
  500. DeleteIpForwardEntry2(&(rtbl->Table[r]));
  501. FreeMibTable(rtbl);
  502. return RoutingTable::Entry();
  503. }
  504. }
  505. }
  506. }
  507. FreeMibTable(rtbl);
  508. if ((metric >= 0)&&(needCreate)) {
  509. MIB_IPFORWARD_ROW2 nr;
  510. InitializeIpForwardEntry(&nr);
  511. nr.InterfaceLuid.Value = luid.Value;
  512. _copyInetAddressToSockaddrInet(destination,nr.DestinationPrefix.Prefix);
  513. nr.DestinationPrefix.PrefixLength = destination.netmaskBits();
  514. _copyInetAddressToSockaddrInet(gateway,nr.NextHop);
  515. nr.Metric = metric;
  516. nr.Protocol = MIB_IPPROTO_NETMGMT;
  517. DWORD result = CreateIpForwardEntry2(&nr);
  518. if (result != NO_ERROR)
  519. return RoutingTable::Entry();
  520. }
  521. std::vector<RoutingTable::Entry> rtab(get(true,true));
  522. std::vector<RoutingTable::Entry>::iterator bestEntry(rtab.end());
  523. for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
  524. if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
  525. if ((device)&&(device[0])) {
  526. if (!strcmp(device,e->device)) {
  527. if (metric == e->metric)
  528. bestEntry = e;
  529. }
  530. }
  531. if (bestEntry == rtab.end())
  532. bestEntry = e;
  533. }
  534. }
  535. if (bestEntry != rtab.end())
  536. return *bestEntry;
  537. return RoutingTable::Entry();
  538. }
  539. #endif // __WINDOWS__
  540. // ---------------------------------------------------------------------------
  541. } // namespace ZeroTier
  542. #endif