WindowsRoutingTable.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "../node/Constants.hpp"
  19. #ifdef __WINDOWS__
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <WinSock2.h>
  24. #include <Windows.h>
  25. #include <netioapi.h>
  26. #include <IPHlpApi.h>
  27. #include <vector>
  28. #include "WindowsRoutingTable.hpp"
  29. namespace ZeroTier {
  30. static void _copyInetAddressToSockaddrInet(const InetAddress &a,SOCKADDR_INET &sinet)
  31. {
  32. memset(&sinet,0,sizeof(sinet));
  33. if (a.isV4()) {
  34. sinet.Ipv4.sin_addr.S_un.S_addr = *((const uint32_t *)a.rawIpData());
  35. sinet.Ipv4.sin_family = AF_INET;
  36. sinet.Ipv4.sin_port = htons(a.port());
  37. } else if (a.isV6()) {
  38. memcpy(sinet.Ipv6.sin6_addr.u.Byte,a.rawIpData(),16);
  39. sinet.Ipv6.sin6_family = AF_INET6;
  40. sinet.Ipv6.sin6_port = htons(a.port());
  41. }
  42. }
  43. WindowsRoutingTable::WindowsRoutingTable()
  44. {
  45. }
  46. WindowsRoutingTable::~WindowsRoutingTable()
  47. {
  48. }
  49. std::vector<RoutingTable::Entry> WindowsRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
  50. {
  51. std::vector<RoutingTable::Entry> entries;
  52. PMIB_IPFORWARD_TABLE2 rtbl = NULL;
  53. if (GetIpForwardTable2(AF_UNSPEC,&rtbl) != NO_ERROR)
  54. return entries;
  55. if (!rtbl)
  56. return entries;
  57. for(ULONG r=0;r<rtbl->NumEntries;++r) {
  58. RoutingTable::Entry e;
  59. switch(rtbl->Table[r].DestinationPrefix.Prefix.si_family) {
  60. case AF_INET:
  61. e.destination.set(&(rtbl->Table[r].DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr),4,rtbl->Table[r].DestinationPrefix.PrefixLength);
  62. break;
  63. case AF_INET6:
  64. e.destination.set(rtbl->Table[r].DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte,16,rtbl->Table[r].DestinationPrefix.PrefixLength);
  65. break;
  66. }
  67. switch(rtbl->Table[r].NextHop.si_family) {
  68. case AF_INET:
  69. e.gateway.set(&(rtbl->Table[r].NextHop.Ipv4.sin_addr.S_un.S_addr),4,0);
  70. break;
  71. case AF_INET6:
  72. e.gateway.set(rtbl->Table[r].NextHop.Ipv6.sin6_addr.u.Byte,16,0);
  73. break;
  74. }
  75. e.deviceIndex = (int)rtbl->Table[r].InterfaceIndex;
  76. e.metric = (int)rtbl->Table[r].Metric;
  77. ConvertInterfaceLuidToNameA(&(rtbl->Table[r].InterfaceLuid),e.device,sizeof(e.device));
  78. if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback()))))
  79. entries.push_back(e);
  80. }
  81. FreeMibTable(rtbl);
  82. std::sort(entries.begin(),entries.end());
  83. return entries;
  84. }
  85. RoutingTable::Entry WindowsRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric)
  86. {
  87. NET_LUID luid;
  88. luid.Value = 0;
  89. if (ConvertInterfaceNameToLuidA(device,&luid) != NO_ERROR)
  90. return RoutingTable::Entry();
  91. bool needCreate = true;
  92. PMIB_IPFORWARD_TABLE2 rtbl = NULL;
  93. if (GetIpForwardTable2(AF_UNSPEC,&rtbl) != NO_ERROR)
  94. return RoutingTable::Entry();
  95. if (!rtbl)
  96. return RoutingTable::Entry();
  97. for(ULONG r=0;r<rtbl->NumEntries;++r) {
  98. if (rtbl->Table[r].InterfaceLuid.Value == luid.Value) {
  99. InetAddress rdest;
  100. switch(rtbl->Table[r].DestinationPrefix.Prefix.si_family) {
  101. case AF_INET:
  102. rdest.set(&(rtbl->Table[r].DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr),4,rtbl->Table[r].DestinationPrefix.PrefixLength);
  103. break;
  104. case AF_INET6:
  105. rdest.set(rtbl->Table[r].DestinationPrefix.Prefix.Ipv6.sin6_addr.u.Byte,16,rtbl->Table[r].DestinationPrefix.PrefixLength);
  106. break;
  107. }
  108. if (rdest == destination) {
  109. if (metric >= 0) {
  110. _copyInetAddressToSockaddrInet(gateway,rtbl->Table[r].NextHop);
  111. rtbl->Table[r].Metric = metric;
  112. SetIpForwardEntry2(&(rtbl->Table[r]));
  113. needCreate = false;
  114. } else {
  115. DeleteIpForwardEntry2(&(rtbl->Table[r]));
  116. FreeMibTable(rtbl);
  117. return RoutingTable::Entry();
  118. }
  119. }
  120. }
  121. }
  122. FreeMibTable(rtbl);
  123. if ((metric >= 0)&&(needCreate)) {
  124. MIB_IPFORWARD_ROW2 nr;
  125. InitializeIpForwardEntry(&nr);
  126. nr.InterfaceLuid.Value = luid.Value;
  127. _copyInetAddressToSockaddrInet(destination,nr.DestinationPrefix.Prefix);
  128. nr.DestinationPrefix.PrefixLength = destination.netmaskBits();
  129. _copyInetAddressToSockaddrInet(gateway,nr.NextHop);
  130. nr.Metric = metric;
  131. nr.Protocol = MIB_IPPROTO_NETMGMT;
  132. DWORD result = CreateIpForwardEntry2(&nr);
  133. if (result != NO_ERROR)
  134. return RoutingTable::Entry();
  135. }
  136. std::vector<RoutingTable::Entry> rtab(get(true,true));
  137. std::vector<RoutingTable::Entry>::iterator bestEntry(rtab.end());
  138. for(std::vector<RoutingTable::Entry>::iterator e(rtab.begin());e!=rtab.end();++e) {
  139. if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
  140. if ((device)&&(device[0])) {
  141. if (!strcmp(device,e->device)) {
  142. if (metric == e->metric)
  143. bestEntry = e;
  144. }
  145. }
  146. if (bestEntry == rtab.end())
  147. bestEntry = e;
  148. }
  149. }
  150. if (bestEntry != rtab.end())
  151. return *bestEntry;
  152. return RoutingTable::Entry();
  153. }
  154. } // namespace ZeroTier
  155. #endif // __WINDOWS__