Skip to content

Commit

Permalink
windows fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vaguue committed Aug 19, 2024
1 parent 29778d2 commit 87b80f9
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 122 deletions.
7 changes: 6 additions & 1 deletion cxx/transports/socket/Sys.hpp → cxx/Sys.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#pragma once

#ifdef _WIN32
#include <winsock2.h>
#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <iphlpapi.h>
#include <Ws2tcpip.h>
#include <Windows.h>
//#include <winsock2.h>
#define SOCKET_OPT_TYPE char *
#define SOCKET_LEN_TYPE int
#else
Expand Down
6 changes: 3 additions & 3 deletions cxx/arp/win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ std::pair<std::string, arp_table_t> fromSys() {
if (if_indextoname(pipTable->Table[i].InterfaceIndex, ifname) == NULL) {
strcpy(ifname, "unknown");
}
auto& vec = table[ifname];
auto& vec = res[ifname];

ArpRecord rec;

char str[INET6_ADDRSTRLEN];
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;
Expand Down
10 changes: 5 additions & 5 deletions cxx/arp/win32.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <windows.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include <stdio.h>
//#include <windows.h>
//#include <winsock2.h>
//#include <ws2ipdef.h>
//#include <iphlpapi.h>
//#include <stdio.h>
#include <stdlib.h>

#include "Arp.hpp"
2 changes: 2 additions & 0 deletions cxx/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <cxxabi.h>
#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
Expand Down
99 changes: 40 additions & 59 deletions cxx/routing/win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,57 @@

#include "win32.hpp"

namespace OverTheWire::Arp {
namespace OverTheWire::Routing {

std::pair<std::string, arp_table_t> fromSys() {
arp_table_t res{};
std::pair<std::string, routing_table_t> 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};
Expand Down
7 changes: 2 additions & 5 deletions cxx/routing/win32.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#pragma once

#include <windows.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include "Sys.hpp"
#include <stdio.h>
#include <stdlib.h>

#include "Arp.hpp"
#include "Routing.hpp"
1 change: 0 additions & 1 deletion cxx/transports/socket/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion cxx/transports/socket/Enums/Enums.hpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
94 changes: 47 additions & 47 deletions cxx/transports/socket/Enums/Windows.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 87b80f9

Please sign in to comment.