diff --git a/src/etcp_connections.c b/src/etcp_connections.c index d1f8af4..4190009 100644 --- a/src/etcp_connections.c +++ b/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) +#ifndef _WIN32 if (netif_index > 0) { char ifname[IF_NAMESIZE]; if (if_indextoname(netif_index, ifname)) { socket_bind_to_device(e_sock->fd, ifname); } } +#endif // Store the local address and bind socket if provided if (ip) { diff --git a/src/tun_windows.c b/src/tun_windows.c index 4887bd6..bbf3d91 100644 --- a/src/tun_windows.c +++ b/src/tun_windows.c @@ -11,7 +11,7 @@ #include #include -// Wintun function pointers +// Wintun function pointers (using function pointer syntax) static HMODULE wintun_dll = NULL; static WINTUN_CREATE_ADAPTER_FUNC WintunCreateAdapter = NULL; static WINTUN_CLOSE_ADAPTER_FUNC WintunCloseAdapter = NULL; @@ -69,28 +69,28 @@ static int wintun_load_dll(void) { } // Resolve function pointers - #define WINTUN_GET_FUNC(name) \ - name = (WINTUN_##name##_FUNC)GetProcAddress(wintun_dll, "Wintun" #name); \ - if (!name) { \ - DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to resolve Wintun" #name); \ - 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 + WintunCreateAdapter = (WINTUN_CREATE_ADAPTER_FUNC)GetProcAddress(wintun_dll, "WintunCreateAdapter"); + WintunCloseAdapter = (WINTUN_CLOSE_ADAPTER_FUNC)GetProcAddress(wintun_dll, "WintunCloseAdapter"); + WintunOpenAdapter = (WINTUN_OPEN_ADAPTER_FUNC)GetProcAddress(wintun_dll, "WintunOpenAdapter"); + WintunGetAdapterLUID = (WINTUN_GET_ADAPTER_LUID_FUNC)GetProcAddress(wintun_dll, "WintunGetAdapterLUID"); + WintunStartSession = (WINTUN_START_SESSION_FUNC)GetProcAddress(wintun_dll, "WintunStartSession"); + WintunEndSession = (WINTUN_END_SESSION_FUNC)GetProcAddress(wintun_dll, "WintunEndSession"); + 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; + } DEBUG_INFO(DEBUG_CATEGORY_TUN, "Wintun.dll loaded successfully"); 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; } - // Set MTU first using MIB_IPINTERFACE_ROW + // Set MTU first using MIB_IPINTERFACE_ROW (minimal required fields) MIB_IPINTERFACE_ROW row; memset(&row, 0, sizeof(row)); row.Family = AF_INET; row.InterfaceLuid = *luid; - row.SitePrefixLength = 0; 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); 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.Address = addr; addr_row.OnLinkPrefixLength = 32; // /32 for point-to-point - addr_row.DadState = IpDadStatePreferred; addr_row.ValidLifetime = 0xffffffff; addr_row.PreferredLifetime = 0xffffffff;