Browse Source

Fix Windows build: tun_windows.c function pointers and MIB_IPINTERFACE_ROW

- Fix function pointer declarations in tun_windows.c (removed broken macro)
- Use explicit GetProcAddress calls for each Wintun function
- Remove invalid fields from MIB_IPINTERFACE_ROW (PromiscuousMode, DadState, etc.)
- Add #ifndef _WIN32 around IF_NAMESIZE/if_indextoname in etcp_connections.c
- All 22 tests pass on Linux
nodeinfo-routing-update
Evgeny 2 months ago
parent
commit
2a546c5fbc
  1. 2
      src/etcp_connections.c
  2. 55
      src/tun_windows.c

2
src/etcp_connections.c

@ -312,12 +312,14 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka
} }
// Bind to interface if specified (Linux only) // Bind to interface if specified (Linux only)
#ifndef _WIN32
if (netif_index > 0) { if (netif_index > 0) {
char ifname[IF_NAMESIZE]; char ifname[IF_NAMESIZE];
if (if_indextoname(netif_index, ifname)) { if (if_indextoname(netif_index, ifname)) {
socket_bind_to_device(e_sock->fd, ifname); socket_bind_to_device(e_sock->fd, ifname);
} }
} }
#endif
// Store the local address and bind socket if provided // Store the local address and bind socket if provided
if (ip) { if (ip) {

55
src/tun_windows.c

@ -11,7 +11,7 @@
#include <iphlpapi.h> #include <iphlpapi.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
// Wintun function pointers // Wintun function pointers (using function pointer syntax)
static HMODULE wintun_dll = NULL; static HMODULE wintun_dll = NULL;
static WINTUN_CREATE_ADAPTER_FUNC WintunCreateAdapter = NULL; static WINTUN_CREATE_ADAPTER_FUNC WintunCreateAdapter = NULL;
static WINTUN_CLOSE_ADAPTER_FUNC WintunCloseAdapter = NULL; static WINTUN_CLOSE_ADAPTER_FUNC WintunCloseAdapter = NULL;
@ -69,29 +69,29 @@ static int wintun_load_dll(void) {
} }
// Resolve function pointers // Resolve function pointers
#define WINTUN_GET_FUNC(name) \ WintunCreateAdapter = (WINTUN_CREATE_ADAPTER_FUNC)GetProcAddress(wintun_dll, "WintunCreateAdapter");
name = (WINTUN_##name##_FUNC)GetProcAddress(wintun_dll, "Wintun" #name); \ WintunCloseAdapter = (WINTUN_CLOSE_ADAPTER_FUNC)GetProcAddress(wintun_dll, "WintunCloseAdapter");
if (!name) { \ WintunOpenAdapter = (WINTUN_OPEN_ADAPTER_FUNC)GetProcAddress(wintun_dll, "WintunOpenAdapter");
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to resolve Wintun" #name); \ WintunGetAdapterLUID = (WINTUN_GET_ADAPTER_LUID_FUNC)GetProcAddress(wintun_dll, "WintunGetAdapterLUID");
FreeLibrary(wintun_dll); \ WintunStartSession = (WINTUN_START_SESSION_FUNC)GetProcAddress(wintun_dll, "WintunStartSession");
wintun_dll = NULL; \ WintunEndSession = (WINTUN_END_SESSION_FUNC)GetProcAddress(wintun_dll, "WintunEndSession");
return -1; \ WintunGetReadWaitEvent = (WINTUN_GET_READ_WAIT_EVENT_FUNC)GetProcAddress(wintun_dll, "WintunGetReadWaitEvent");
WintunReceivePacket = (WINTUN_RECEIVE_PACKET_FUNC)GetProcAddress(wintun_dll, "WintunReceivePacket");
WintunReleaseReceivePacket = (WINTUN_RELEASE_RECEIVE_PACKET_FUNC)GetProcAddress(wintun_dll, "WintunReleaseReceivePacket");
WintunAllocateSendPacket = (WINTUN_ALLOCATE_SEND_PACKET_FUNC)GetProcAddress(wintun_dll, "WintunAllocateSendPacket");
WintunSendPacket = (WINTUN_SEND_PACKET_FUNC)GetProcAddress(wintun_dll, "WintunSendPacket");
// Check that all functions were resolved
if (!WintunCreateAdapter || !WintunCloseAdapter || !WintunOpenAdapter ||
!WintunGetAdapterLUID || !WintunStartSession || !WintunEndSession ||
!WintunGetReadWaitEvent || !WintunReceivePacket || !WintunReleaseReceivePacket ||
!WintunAllocateSendPacket || !WintunSendPacket) {
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to resolve some Wintun functions");
FreeLibrary(wintun_dll);
wintun_dll = NULL;
return -1;
} }
WINTUN_GET_FUNC(CreateAdapter);
WINTUN_GET_FUNC(CloseAdapter);
WINTUN_GET_FUNC(OpenAdapter);
WINTUN_GET_FUNC(GetAdapterLUID);
WINTUN_GET_FUNC(StartSession);
WINTUN_GET_FUNC(EndSession);
WINTUN_GET_FUNC(GetReadWaitEvent);
WINTUN_GET_FUNC(ReceivePacket);
WINTUN_GET_FUNC(ReleaseReceivePacket);
WINTUN_GET_FUNC(AllocateSendPacket);
WINTUN_GET_FUNC(SendPacket);
#undef WINTUN_GET_FUNC
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Wintun.dll loaded successfully"); DEBUG_INFO(DEBUG_CATEGORY_TUN, "Wintun.dll loaded successfully");
return 0; return 0;
} }
@ -142,20 +142,12 @@ static int wintun_set_ip_and_mtu(const NET_LUID* luid, const char* ip_str, int m
return -1; return -1;
} }
// Set MTU first using MIB_IPINTERFACE_ROW // Set MTU first using MIB_IPINTERFACE_ROW (minimal required fields)
MIB_IPINTERFACE_ROW row; MIB_IPINTERFACE_ROW row;
memset(&row, 0, sizeof(row)); memset(&row, 0, sizeof(row));
row.Family = AF_INET; row.Family = AF_INET;
row.InterfaceLuid = *luid; row.InterfaceLuid = *luid;
row.SitePrefixLength = 0;
row.NlMtu = mtu; row.NlMtu = mtu;
row.UseAutomaticMetric = FALSE;
row.Metric = 0;
row.PromiscuousMode = FALSE;
row.DadState = IpDadStatePreferred;
row.ConnectionType = NET_IF_CONNECTION_WAKED_UP;
row.InterfaceIdentifierPadding = 0;
row.SkipAsSource = FALSE;
DWORD ret = SetIpInterfaceEntry(&row); DWORD ret = SetIpInterfaceEntry(&row);
if (ret != NO_ERROR && ret != ERROR_OBJECT_ALREADY_EXISTS) { if (ret != NO_ERROR && ret != ERROR_OBJECT_ALREADY_EXISTS) {
@ -168,7 +160,6 @@ static int wintun_set_ip_and_mtu(const NET_LUID* luid, const char* ip_str, int m
addr_row.InterfaceLuid = *luid; addr_row.InterfaceLuid = *luid;
addr_row.Address = addr; addr_row.Address = addr;
addr_row.OnLinkPrefixLength = 32; // /32 for point-to-point addr_row.OnLinkPrefixLength = 32; // /32 for point-to-point
addr_row.DadState = IpDadStatePreferred;
addr_row.ValidLifetime = 0xffffffff; addr_row.ValidLifetime = 0xffffffff;
addr_row.PreferredLifetime = 0xffffffff; addr_row.PreferredLifetime = 0xffffffff;

Loading…
Cancel
Save