From 87b80f95f6397a8fb58ca1e20ea835f8e111d4dc Mon Sep 17 00:00:00 2001 From: vaguue Date: Mon, 19 Aug 2024 03:12:58 +0300 Subject: [PATCH] windows fixes --- cxx/{transports/socket => }/Sys.hpp | 7 +- cxx/arp/win32.cpp | 6 +- cxx/arp/win32.hpp | 10 +-- cxx/common.hpp | 2 + cxx/routing/win32.cpp | 99 ++++++++++--------------- cxx/routing/win32.hpp | 7 +- cxx/transports/socket/CMakeLists.txt | 1 - cxx/transports/socket/Enums/Enums.hpp | 1 - cxx/transports/socket/Enums/Windows.cpp | 94 +++++++++++------------ 9 files changed, 105 insertions(+), 122 deletions(-) rename cxx/{transports/socket => }/Sys.hpp (83%) diff --git a/cxx/transports/socket/Sys.hpp b/cxx/Sys.hpp similarity index 83% rename from cxx/transports/socket/Sys.hpp rename to cxx/Sys.hpp index 06024aa..36886db 100644 --- a/cxx/transports/socket/Sys.hpp +++ b/cxx/Sys.hpp @@ -1,8 +1,13 @@ #pragma once #ifdef _WIN32 -#include +#define WIN32_LEAN_AND_MEAN +#include +#include +#include #include +#include +//#include #define SOCKET_OPT_TYPE char * #define SOCKET_LEN_TYPE int #else diff --git a/cxx/arp/win32.cpp b/cxx/arp/win32.cpp index 9834413..76b7045 100644 --- a/cxx/arp/win32.cpp +++ b/cxx/arp/win32.cpp @@ -19,7 +19,7 @@ std::pair fromSys() { if (if_indextoname(pipTable->Table[i].InterfaceIndex, ifname) == NULL) { strcpy(ifname, "unknown"); } - auto& vec = table[ifname]; + auto& vec = res[ifname]; ArpRecord rec; @@ -27,8 +27,8 @@ std::pair fromSys() { uv_inet_ntop( pipTable->Table[i].Address.si_family, pipTable->Table[i].Address.si_family == AF_INET6 ? - pipTable->Table[i].Address.IPv6 : - pipTable->Table[i].Address.IPv4, + (const void*)(&pipTable->Table[i].Address.Ipv6) : + (const void*)(&pipTable->Table[i].Address.Ipv4), str, INET6_ADDRSTRLEN); rec.ipAddr = str; diff --git a/cxx/arp/win32.hpp b/cxx/arp/win32.hpp index 578debb..644a799 100644 --- a/cxx/arp/win32.hpp +++ b/cxx/arp/win32.hpp @@ -1,10 +1,10 @@ #pragma once -#include -#include -#include -#include -#include +//#include +//#include +//#include +//#include +//#include #include #include "Arp.hpp" diff --git a/cxx/common.hpp b/cxx/common.hpp index bc7000a..94e6659 100644 --- a/cxx/common.hpp +++ b/cxx/common.hpp @@ -13,6 +13,8 @@ #include #endif +#include "Sys.hpp" + /* For me, utils or helpers are anti-patterns, * hence I names this file common.hpp. * But actually this file has some commonly used C++ headers diff --git a/cxx/routing/win32.cpp b/cxx/routing/win32.cpp index 9834413..e42b277 100644 --- a/cxx/routing/win32.cpp +++ b/cxx/routing/win32.cpp @@ -2,76 +2,57 @@ #include "win32.hpp" -namespace OverTheWire::Arp { +namespace OverTheWire::Routing { -std::pair fromSys() { - arp_table_t res{}; +std::pair fromSys() { + routing_table_t res{}; - PMIB_IPNET_TABLE2 pipTable = NULL; + DWORD retval; + MIB_IPFORWARD_TABLE2 *routes = NULL; + MIB_IPFORWARD_ROW2 *route; - auto status = GetIpNetTable2(AF_UNSPEC, &pipTable); - if (status != NO_ERROR) { - return {"GetIpNetTable returned error" + std::to_string(status), res}; + retval = GetIpForwardTable2(AF_INET, &routes); + if (retval != ERROR_SUCCESS) { + return {"GetIpForwardTable2 failed", res}; } - for (int i = 0; (unsigned) i < pipTable->NumEntries; ++i) { - char ifname[IF_NAMESIZE]; - if (if_indextoname(pipTable->Table[i].InterfaceIndex, ifname) == NULL) { - strcpy(ifname, "unknown"); - } - auto& vec = table[ifname]; + for (int idx = 0; idx < routes->NumEntries; idx++) { + route = routes->Table + idx; - ArpRecord rec; + std::string iface; + iface.resize(IF_MAX_STRING_SIZE + 1); + + DWORD dwSize = IF_MAX_STRING_SIZE + 1; + DWORD dwRetVal = ConvertInterfaceLuidToNameW(&route->InterfaceLuid, (PWSTR)iface.data(), iface.size()); + + auto& vec = res[iface]; + + RoutingRecord rec; char str[INET6_ADDRSTRLEN]; + auto& ipPrefix = route->DestinationPrefix; + + uv_inet_ntop( + ipPrefix.Prefix.si_family, + ipPrefix.Prefix.si_family == AF_INET6 ? + (const void*)(&ipPrefix.Prefix.Ipv6) : + (const void*)(&ipPrefix.Prefix.Ipv4), + str, INET6_ADDRSTRLEN); + + rec.destination = str; + + auto& nextHop = route->NextHop; + uv_inet_ntop( - pipTable->Table[i].Address.si_family, - pipTable->Table[i].Address.si_family == AF_INET6 ? - pipTable->Table[i].Address.IPv6 : - pipTable->Table[i].Address.IPv4, + nextHop.si_family, + nextHop.si_family == AF_INET6 ? + (const void*)(&nextHop.Ipv6) : + (const void*)(&nextHop.Ipv4), str, INET6_ADDRSTRLEN); - rec.ipAddr = str; - std::stringstream ss; - ss << std::hex; - for (int j = 0; j < pipTable->Table[i].PhysicalAddressLength; j++) { - if (j) { - ss << ":"; - } - ss << (int)pipTable->Table[i].PhysicalAddress[j]; - } - - rec.hwAddr = ss.str(); - rec.hwType = pipTable->Table[i].InterfaceLuid.Info.IfType; - - switch (pipTable->Table[i].State) { - case NlnsUnreachable: - rec.flags.push_back("NlnsUnreachable"); - break; - case NlnsIncomplete: - rec.flags.push_back("NlnsIncomplete"); - break; - case NlnsProbe: - rec.flags.push_back("NlnsProbe"); - break; - case NlnsDelay: - rec.flags.push_back("NlnsDelay"); - break; - case NlnsStale: - rec.flags.push_back("NlnsStale"); - break; - case NlnsReachable: - rec.flags.push_back("NlnsReachable"); - break; - case NlnsPermanent: - rec.flags.push_back("NlnsPermanent"); - break; - default: - rec.flags.push_back("Unknown"); - break; - } - - vec.push_back(std::move(rec)); + rec.gateway = str; + + vec.push_back(rec); } return {"", res}; diff --git a/cxx/routing/win32.hpp b/cxx/routing/win32.hpp index 578debb..42eccfa 100644 --- a/cxx/routing/win32.hpp +++ b/cxx/routing/win32.hpp @@ -1,10 +1,7 @@ #pragma once -#include -#include -#include -#include +#include "Sys.hpp" #include #include -#include "Arp.hpp" +#include "Routing.hpp" diff --git a/cxx/transports/socket/CMakeLists.txt b/cxx/transports/socket/CMakeLists.txt index 72f5e96..4bed7e5 100644 --- a/cxx/transports/socket/CMakeLists.txt +++ b/cxx/transports/socket/CMakeLists.txt @@ -10,7 +10,6 @@ set(SOCKET_HDR "${CMAKE_CURRENT_SOURCE_DIR}/Socket.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/Packets.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/InputPackets.hpp" - "${CMAKE_CURRENT_SOURCE_DIR}/Sys.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/SockAddr.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/SockAddr.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/Enums/Enums.hpp" diff --git a/cxx/transports/socket/Enums/Enums.hpp b/cxx/transports/socket/Enums/Enums.hpp index 20d1245..112cf26 100644 --- a/cxx/transports/socket/Enums/Enums.hpp +++ b/cxx/transports/socket/Enums/Enums.hpp @@ -1,7 +1,6 @@ #pragma once #include "common.hpp" -#include "../Sys.hpp" /* This module is supposed to export some * system constants, just by putting them in the exports. diff --git a/cxx/transports/socket/Enums/Windows.cpp b/cxx/transports/socket/Enums/Windows.cpp index b1ca2f0..fa3e273 100644 --- a/cxx/transports/socket/Enums/Windows.cpp +++ b/cxx/transports/socket/Enums/Windows.cpp @@ -1,50 +1,50 @@ Napi::Object InitSystemLocal(Napi::Env env, Napi::Object exports) { - ENUM_VALUE(AF_UNSPEC); - ENUM_VALUE(AF_INET); - ENUM_VALUE(AF_IPX); - ENUM_VALUE(AF_APPLETALK); - ENUM_VALUE(AF_NETBIOS); - ENUM_VALUE(AF_INET6); - ENUM_VALUE(AF_IRDA); - ENUM_VALUE(AF_BTH); - - ENUM_VALUE(SOCK_STREAM); - ENUM_VALUE(SOCK_DGRAM); - ENUM_VALUE(SOCK_RAW); - ENUM_VALUE(SOCK_RDM); - ENUM_VALUE(SOCK_SEQPACKET); - - ENUM_VALUE(IPPROTO_ICMP); - ENUM_VALUE(IPPROTO_IGMP); - //ENUM_VALUE(BTHPROTO_RFCOMM); - ENUM_VALUE(IPPROTO_TCP); - ENUM_VALUE(IPPROTO_UDP); - ENUM_VALUE(IPPROTO_ICMPV6); - //ENUM_VALUE(IPPROTO_RM); - ENUM_VALUE(IPPROTO_IP); - ENUM_VALUE(IPPROTO_GGP); - ENUM_VALUE(IPPROTO_IPV4); - ENUM_VALUE(IPPROTO_ST); - ENUM_VALUE(IPPROTO_CBT); - ENUM_VALUE(IPPROTO_EGP); - ENUM_VALUE(IPPROTO_IGP); - ENUM_VALUE(IPPROTO_PUP); - ENUM_VALUE(IPPROTO_IDP); - ENUM_VALUE(IPPROTO_RDP); - ENUM_VALUE(IPPROTO_ND); - ENUM_VALUE(IPPROTO_ICLFXBM); - ENUM_VALUE(IPPROTO_PIM); - ENUM_VALUE(IPPROTO_PGM); - ENUM_VALUE(IPPROTO_L2TP); - ENUM_VALUE(IPPROTO_SCTP); - ENUM_VALUE(IPPROTO_RAW); - - ENUM_VALUE(SO_BROADCAST); - ENUM_VALUE(SO_CONDITIONAL_ACCEPT); - ENUM_VALUE(SO_EXCLUSIVEADDRUSE); - ENUM_VALUE(SO_KEEPALIVE); - ENUM_VALUE(SO_RCVBUF); - ENUM_VALUE(SO_REUSEADDR); - +// ENUM_VALUE(AF_UNSPEC); +// ENUM_VALUE(AF_INET); +// ENUM_VALUE(AF_IPX); +// ENUM_VALUE(AF_APPLETALK); +// ENUM_VALUE(AF_NETBIOS); +// ENUM_VALUE(AF_INET6); +// ENUM_VALUE(AF_IRDA); +// ENUM_VALUE(AF_BTH); +// +// ENUM_VALUE(SOCK_STREAM); +// ENUM_VALUE(SOCK_DGRAM); +// ENUM_VALUE(SOCK_RAW); +// ENUM_VALUE(SOCK_RDM); +// ENUM_VALUE(SOCK_SEQPACKET); +// +// ENUM_VALUE(IPPROTO_ICMP); +// ENUM_VALUE(IPPROTO_IGMP); +// //ENUM_VALUE(BTHPROTO_RFCOMM); +// ENUM_VALUE(IPPROTO_TCP); +// ENUM_VALUE(IPPROTO_UDP); +// ENUM_VALUE(IPPROTO_ICMPV6); +// //ENUM_VALUE(IPPROTO_RM); +// ENUM_VALUE(IPPROTO_IP); +// ENUM_VALUE(IPPROTO_GGP); +// ENUM_VALUE(IPPROTO_IPV4); +// ENUM_VALUE(IPPROTO_ST); +// ENUM_VALUE(IPPROTO_CBT); +// ENUM_VALUE(IPPROTO_EGP); +// ENUM_VALUE(IPPROTO_IGP); +// ENUM_VALUE(IPPROTO_PUP); +// ENUM_VALUE(IPPROTO_IDP); +// ENUM_VALUE(IPPROTO_RDP); +// ENUM_VALUE(IPPROTO_ND); +// ENUM_VALUE(IPPROTO_ICLFXBM); +// ENUM_VALUE(IPPROTO_PIM); +// ENUM_VALUE(IPPROTO_PGM); +// ENUM_VALUE(IPPROTO_L2TP); +// ENUM_VALUE(IPPROTO_SCTP); +// ENUM_VALUE(IPPROTO_RAW); +// +// ENUM_VALUE(SO_BROADCAST); +// ENUM_VALUE(SO_CONDITIONAL_ACCEPT); +// ENUM_VALUE(SO_EXCLUSIVEADDRUSE); +// ENUM_VALUE(SO_KEEPALIVE); +// ENUM_VALUE(SO_RCVBUF); +// ENUM_VALUE(SO_REUSEADDR); +// return exports; }