NetPlatform_win32.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define _WIN32_WINNT 0x0600
  16. #include "exception/WinFail.h"
  17. #include "util/platform/netdev/NetPlatform.h"
  18. #include "util/Bits.h"
  19. #include "util/platform/Sockaddr.h"
  20. #include <winsock2.h>
  21. #include <windows.h>
  22. #define NET_LUID misalligned_NET_LUID
  23. #define PNET_LUID misalligned_PNET_LUID
  24. #define IF_LUID misalligned_IF_LUID
  25. #define PIF_LUID misalligned_PIF_LUID
  26. #include <ifdef.h>
  27. #undef NET_LUID
  28. #undef PNET_LUID
  29. #undef IF_LUID
  30. #undef PIF_LUID
  31. // mingw-w64 incorrectly pragma pack's this to 1 byte.
  32. typedef union NET_LUID {
  33. ULONG64 Value;
  34. __C89_NAMELESS struct { /* bitfield with 64 bit types. */
  35. ULONG64 Reserved :24;
  36. ULONG64 NetLuidIndex :24;
  37. ULONG64 IfType :16;
  38. } Info;
  39. } NET_LUID, *PNET_LUID;
  40. Assert_compileTime(sizeof(NET_LUID) == 8);
  41. typedef NET_LUID IF_LUID, *PIF_LUID;
  42. #include <ws2ipdef.h>
  43. #include <naptypes.h>
  44. #include <ntddndis.h>
  45. #include <ws2def.h>
  46. #include <iprtrmib.h>
  47. #include <ifdef.h>
  48. #include <iphlpapi.h>
  49. static NET_LUID getLuid(const char* name, struct Except* eh)
  50. {
  51. uint16_t ifName[IF_MAX_STRING_SIZE + 1] = {0};
  52. WinFail_check(eh,
  53. (!MultiByteToWideChar(CP_UTF8, 0, name, strlen(name), ifName, IF_MAX_STRING_SIZE + 1))
  54. );
  55. NET_LUID out;
  56. WinFail_check(eh, ConvertInterfaceAliasToLuid(ifName, &out));
  57. return out;
  58. }
  59. static LONG flushAddresses(NET_LUID luid, MIB_UNICASTIPADDRESS_TABLE* table)
  60. {
  61. LONG out = NO_ERROR;
  62. for (int i = 0; i < (int)table->NumEntries; i++) {
  63. if (table->Table[i].InterfaceLuid.Value == luid.Value) {
  64. if ((out = DeleteUnicastIpAddressEntry(&table->Table[i]))) {
  65. return out;
  66. }
  67. }
  68. }
  69. return out;
  70. }
  71. void NetPlatform_flushAddresses(const char* deviceName, struct Except* eh)
  72. {
  73. NET_LUID luid = getLuid(deviceName, eh);
  74. MIB_UNICASTIPADDRESS_TABLE* table;
  75. WinFail_check(eh, GetUnicastIpAddressTable(AF_INET, &table));
  76. LONG ret = flushAddresses(luid, table);
  77. FreeMibTable(table);
  78. if (ret) {
  79. WinFail_fail(eh, "DeleteUnicastIpAddressEntry(&table->Table[i])", ret);
  80. }
  81. WinFail_check(eh, GetUnicastIpAddressTable(AF_INET6, &table));
  82. ret = flushAddresses(luid, table);
  83. FreeMibTable(table);
  84. if (ret) {
  85. WinFail_fail(eh, "DeleteUnicastIpAddressEntry(&table->Table[i])", ret);
  86. }
  87. }
  88. #include "util/Hex.h"
  89. #include <stdio.h>
  90. static void setupRoute(const char* deviceName,
  91. const uint8_t* addrBytes,
  92. int prefixLen,
  93. int addrFam,
  94. struct Except* eh)
  95. {
  96. void WINAPI InitializeIpForwardEntry(PMIB_IPFORWARD_ROW2 Row);
  97. MIB_IPFORWARD_ROW2 row = {
  98. .InterfaceLuid = getLuid(deviceName, eh),
  99. .ValidLifetime = WSA_INFINITE,
  100. .PreferredLifetime = WSA_INFINITE,
  101. .Metric = 0xffffffff,
  102. .Protocol = MIB_IPPROTO_NETMGMT,
  103. .SitePrefixLength = 255,
  104. .DestinationPrefix = {
  105. .PrefixLength = prefixLen,
  106. .Prefix = { .si_family = addrFam }
  107. },
  108. .NextHop = { .si_family = addrFam },
  109. .Loopback = false,
  110. .AutoconfigureAddress = false,
  111. .Immortal = false,
  112. .Age = 0,
  113. .Origin = 0
  114. };
  115. if (addrFam == AF_INET6) {
  116. Bits_memcpyConst(&row.DestinationPrefix.Prefix.Ipv6.sin6_addr, addrBytes, 15);
  117. row.DestinationPrefix.Prefix.Ipv6.sin6_family = AF_INET6;
  118. // set the gateway addr to the client's addr +1
  119. uint64_t addr[2];
  120. Bits_memcpyConst(addr, addrBytes, 16);
  121. addr[1] = Endian_hostToBigEndian64(Endian_bigEndianToHost64(addr[1]) + 1);
  122. if (!addr[1]) {
  123. addr[0] = Endian_hostToBigEndian64(Endian_bigEndianToHost64(addr[0]) + 1);
  124. }
  125. // Bits_memcpyConst(&row.NextHop.Ipv6.sin6_addr, addr, 16);
  126. } else {
  127. Bits_memcpyConst(&row.DestinationPrefix.Prefix.Ipv4.sin_addr, addrBytes, 4);
  128. row.DestinationPrefix.Prefix.Ipv4.sin_family = AF_INET;
  129. uint32_t addr;
  130. Bits_memcpyConst(&addr, addrBytes, 4);
  131. addr = Endian_hostToBigEndian32(Endian_bigEndianToHost32(addr) + 1);
  132. Bits_memcpyConst(&row.NextHop.Ipv4.sin_addr, &addr, 4);
  133. }
  134. //InitializeIpForwardEntry(&row);
  135. uint8_t buff[sizeof(row) * 2 + 1];
  136. Hex_encode(buff, sizeof(buff), (uint8_t*) &row, sizeof(row));
  137. printf("%s %d\n", buff, row.SitePrefixLength);
  138. // Hex_encode(buff, sizeof(buff), (uint8_t*) &row, sizeof(row));
  139. // printf("%s %d<\n", buff, row.SitePrefixLength);
  140. WinFail_check(eh, CreateIpForwardEntry2(&row));
  141. }
  142. void NetPlatform_addAddress(const char* name,
  143. const uint8_t* addrBytes,
  144. int prefixLen,
  145. int addrFam,
  146. struct Log* logger,
  147. struct Except* eh)
  148. {
  149. MIB_UNICASTIPADDRESS_ROW ipRow = {
  150. .PrefixOrigin = IpPrefixOriginUnchanged,
  151. .SuffixOrigin = IpSuffixOriginUnchanged,
  152. .ValidLifetime = 0xFFFFFFFF,
  153. .PreferredLifetime = 0xFFFFFFFF,
  154. .OnLinkPrefixLength = 0xFF
  155. };
  156. ipRow.InterfaceLuid = getLuid(name, eh);
  157. ipRow.Address.si_family = addrFam;
  158. if (addrFam == Sockaddr_AF_INET6) {
  159. Bits_memcpyConst(&ipRow.Address.Ipv6.sin6_addr, addrBytes, 16);
  160. } else if (addrFam == Sockaddr_AF_INET) {
  161. Bits_memcpyConst(&ipRow.Address.Ipv4.sin_addr, addrBytes, 4);
  162. } else {
  163. Assert_always(0);
  164. }
  165. ipRow.OnLinkPrefixLength = prefixLen;
  166. WinFail_check(eh, CreateUnicastIpAddressEntry(&ipRow));
  167. return;
  168. setupRoute(name, addrBytes, prefixLen, addrFam, eh);
  169. }
  170. void NetPlatform_setMTU(const char* interfaceName,
  171. uint32_t mtu,
  172. struct Log* logger,
  173. struct Except* eh)
  174. {
  175. Except_throw(eh, "unimplemented");
  176. }