@ -11,7 +11,7 @@
# include <iphlpapi.h>
# include <ws2tcpip.h>
// 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 ;