WindowsRoutingTable.cpp 5.7 KB

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