Simplify iphlpapi.dll API calls
PATCH 1/1 Simplify iphlpapi.dll API calls
Dynamically locating API function addresses at run-time using GetProcAddress() was a leftover from the early days of the interactive service development. It was required before NTDDI_VERSION was raised from Windows XP to Windows Vista.
After NTDDI_VERSION API level was raised to NTDDI_VISTA, the direct calling of Vista introduced API functions is possible and much simpler.
This patch simplifies the code while in the same time it removes
controversial function type definitions that caused interactive service not to compile on MSVC.
--- src/openvpnserv/interactive.c | 123 +----------------------------------------- 1 file changed, 1 insertion(+), 122 deletions(-)
src/openvpnserv/interactive.c +1 -122
@@ -549,23 +549,6 @@ InterfaceLuid(const char *iface_name, PNET_LUID luid)LPWSTR wide_name;int n; - typedef NETIO_STATUS WINAPI (*ConvertInterfaceAliasToLuidFn) (LPCWSTR, PNET_LUID); -static ConvertInterfaceAliasToLuidFn ConvertInterfaceAliasToLuid =NULL ; -if (!ConvertInterfaceAliasToLuid) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - ConvertInterfaceAliasToLuid = (ConvertInterfaceAliasToLuidFn )GetProcAddress (iphlpapi,"ConvertInterfaceAliasToLuid" ); -if (!ConvertInterfaceAliasToLuid) - { -return GetLastError (); - } - } - n =MultiByteToWideChar (CP_UTF8 ,0 , iface_name,-1 ,NULL ,0 ); wide_name =malloc (n *sizeof (WCHAR ));MultiByteToWideChar (CP_UTF8 ,0 , iface_name,-1 , wide_name, n);
@@ -584,24 +567,6 @@ CmpAddress(LPVOID item, LPVOID address)static DWORD DeleteAddress (PMIB_UNICASTIPADDRESS_ROW addr_row) { -typedef NETIOAPI_API (*DeleteUnicastIpAddressEntryFn ) (const PMIB_UNICASTIPADDRESS_ROW ); -static DeleteUnicastIpAddressEntryFn DeleteUnicastIpAddressEntry =NULL ; - -if (!DeleteUnicastIpAddressEntry) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - DeleteUnicastIpAddressEntry = (DeleteUnicastIpAddressEntryFn )GetProcAddress (iphlpapi,"DeleteUnicastIpAddressEntry" ); -if (!DeleteUnicastIpAddressEntry) - { -return GetLastError (); - } - } -return DeleteUnicastIpAddressEntry (addr_row); }
@@ -612,32 +577,6 @@ HandleAddressMessage(address_message_t *msg, undo_lists_t *lists)PMIB_UNICASTIPADDRESS_ROW addr_row;BOOL add = msg->header.type == msg_add_address; -typedef NETIOAPI_API (*CreateUnicastIpAddressEntryFn ) (const PMIB_UNICASTIPADDRESS_ROW ); -typedef NETIOAPI_API (*InitializeUnicastIpAddressEntryFn ) (PMIB_UNICASTIPADDRESS_ROW ); -static CreateUnicastIpAddressEntryFn CreateUnicastIpAddressEntry =NULL ; -static InitializeUnicastIpAddressEntryFn InitializeUnicastIpAddressEntry =NULL ; - -if (!CreateUnicastIpAddressEntry || !InitializeUnicastIpAddressEntry) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - CreateUnicastIpAddressEntry = (CreateUnicastIpAddressEntryFn )GetProcAddress (iphlpapi,"CreateUnicastIpAddressEntry" ); -if (!CreateUnicastIpAddressEntry) - { -return GetLastError (); - } - - InitializeUnicastIpAddressEntry = (InitializeUnicastIpAddressEntryFn )GetProcAddress (iphlpapi,"InitializeUnicastIpAddressEntry" ); -if (!InitializeUnicastIpAddressEntry) - { -return GetLastError (); - } - } - addr_row =malloc (sizeof (*addr_row));if (addr_row ==NULL ) {
@@ -706,24 +645,6 @@ CmpRoute(LPVOID item, LPVOID route)static DWORD DeleteRoute (PMIB_IPFORWARD_ROW2 fwd_row) { -typedef NETIOAPI_API (*DeleteIpForwardEntry2Fn ) (PMIB_IPFORWARD_ROW2 ); -static DeleteIpForwardEntry2Fn DeleteIpForwardEntry2 =NULL ; - -if (!DeleteIpForwardEntry2) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - DeleteIpForwardEntry2 = (DeleteIpForwardEntry2Fn )GetProcAddress (iphlpapi,"DeleteIpForwardEntry2" ); -if (!DeleteIpForwardEntry2) - { -return GetLastError (); - } - } -return DeleteIpForwardEntry2 (fwd_row); }
@@ -734,24 +655,6 @@ HandleRouteMessage(route_message_t *msg, undo_lists_t *lists)PMIB_IPFORWARD_ROW2 fwd_row;BOOL add = msg->header.type == msg_add_route; -typedef NETIOAPI_API (*CreateIpForwardEntry2Fn ) (PMIB_IPFORWARD_ROW2 ); -static CreateIpForwardEntry2Fn CreateIpForwardEntry2 =NULL ; - -if (!CreateIpForwardEntry2) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - CreateIpForwardEntry2 = (CreateIpForwardEntry2Fn )GetProcAddress (iphlpapi,"CreateIpForwardEntry2" ); -if (!CreateIpForwardEntry2) - { -return GetLastError (); - } - } - fwd_row =malloc (sizeof (*fwd_row));if (fwd_row ==NULL ) {
@@ -820,36 +723,12 @@ out:static DWORD HandleFlushNeighborsMessage (flush_neighbors_message_t *msg) { -typedef NETIOAPI_API (*FlushIpNetTable2Fn ) (ADDRESS_FAMILY ,NET_IFINDEX ); -static FlushIpNetTable2Fn flush_fn =NULL ; -if (msg->family ==AF_INET ) {return FlushIpNetTable (msg->iface.index); } -if (!flush_fn) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - flush_fn = (FlushIpNetTable2Fn )GetProcAddress (iphlpapi,"FlushIpNetTable2" ); -if (!flush_fn) - { -if (GetLastError () ==ERROR_PROC_NOT_FOUND ) - { -return WSAEPFNOSUPPORT ; - } -else - { -return GetLastError (); - } - } - } -return flush_fn (msg->family, msg->iface.index); +return FlushIpNetTable2 (msg->family, msg->iface.index); } static void
Signature · 1 line
2.9.0.windows.1
Hi,
Note to self: this is v2 of [PATCH 05/13]
https://www.mail-archive.com/[email protected]...
.net/msg15589.html
Quoted 45 lines · Simon Rozman
Dynamically locating API function addresses at run-time using
GetProcAddress()was a leftover from the early days of the interactive service development. It was required beforeNTDDI_VERSIONwas raised from Windows XP to Windows Vista.After
NTDDI_VERSIONAPI level was raised toNTDDI_VISTA, the direct calling of Vista introduced API functions is possible and much simpler.This patch simplifies the code while in the same time it removes controversial function type definitions that caused interactive service not to compile on MSVC.
--- src/openvpnserv/interactive.c | 123 +----------------------------- ------------ 1 file changed, 1 insertion(+), 122 deletions(-)src/openvpnserv/interactive.c +0
-17@@ -549,23 +549,6 @@ InterfaceLuid(const char *iface_name, PNET_LUID luid)LPWSTR wide_name;int n; - typedef NETIO_STATUS WINAPI (*ConvertInterfaceAliasToLuidFn) (LPCWSTR ,PNET_LUID ); -static ConvertInterfaceAliasToLuidFn ConvertInterfaceAliasToLuid =NULL ; -if (!ConvertInterfaceAliasToLuid) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - ConvertInterfaceAliasToLuid = (ConvertInterfaceAliasToLuidFn )GetProcAddress (iphlpapi,"ConvertInterfaceAliasToLuid" ); -if (!ConvertInterfaceAliasToLuid) - { -return GetLastError (); - } - } -
Quoted 2 lines
n = MultiByteToWideChar(CP_UTF8, 0, iface_name, -1, NULL, 0);
wide_name = malloc(n * sizeof(WCHAR));MultiByteToWideChar(CP_UTF8, 0, iface_name, -1, wide_name, n);
Quoted 1 line
Not a fault of this patch, but malloc's return value not checked... Let's address that in a separate patch.
Quoted 150 lines
src/openvpnserv/interactive.c +0
-18@@ -584,24 +567,6 @@ CmpAddress(LPVOID item, LPVOID address)static DWORD DeleteAddress (PMIB_UNICASTIPADDRESS_ROW addr_row) { -typedef NETIOAPI_API (*DeleteUnicastIpAddressEntryFn ) (const PMIB_UNICASTIPADDRESS_ROW); -static DeleteUnicastIpAddressEntryFn DeleteUnicastIpAddressEntry =NULL ; - -if (!DeleteUnicastIpAddressEntry) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - DeleteUnicastIpAddressEntry = (DeleteUnicastIpAddressEntryFn )GetProcAddress (iphlpapi,"DeleteUnicastIpAddressEntry" ); -if (!DeleteUnicastIpAddressEntry) - { -return GetLastError (); - } - } -return DeleteUnicastIpAddressEntry(addr_row);}
src/openvpnserv/interactive.c +0
-3@@ -612,32 +577,6 @@ HandleAddressMessage(address_message_t *msg, undo_lists_t *lists) PMIB_UNICASTIPADDRESS_ROW addr_row;BOOL add = msg->header.type == msg_add_address; -typedef NETIOAPI_API (*CreateUnicastIpAddressEntryFn ) (const PMIB_UNICASTIPADDRESS_ROW); -typedef NETIOAPI_API (*InitializeUnicastIpAddressEntryFn ) (PMIB_UNICASTIPADDRESS_ROW ); - static CreateUnicastIpAddressEntryFn CreateUnicastIpAddressEntry =NULL;
- static InitializeUnicastIpAddressEntryFn
InitializeUnicastIpAddressEntry = NULL;src/openvpnserv/interactive.c +0
-10- -if (!CreateUnicastIpAddressEntry || !InitializeUnicastIpAddressEntry) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - CreateUnicastIpAddressEntry = (CreateUnicastIpAddressEntryFn)
GetProcAddress(iphlpapi, "CreateUnicastIpAddressEntry");src/openvpnserv/interactive.c +0
-6-if (!CreateUnicastIpAddressEntry) - { -return GetLastError (); - } - - InitializeUnicastIpAddressEntry = (InitializeUnicastIpAddressEntryFn)
GetProcAddress(iphlpapi, "InitializeUnicastIpAddressEntry");src/openvpnserv/interactive.c +0
-42-if (!InitializeUnicastIpAddressEntry) - { -return GetLastError (); - } - } - addr_row =malloc (sizeof (*addr_row));if (addr_row ==NULL ) {@@ -706,24 +645,6 @@ CmpRoute(LPVOID item, LPVOID route)static DWORD DeleteRoute (PMIB_IPFORWARD_ROW2 fwd_row) { -typedef NETIOAPI_API (*DeleteIpForwardEntry2Fn ) (PMIB_IPFORWARD_ROW2 ); -static DeleteIpForwardEntry2Fn DeleteIpForwardEntry2 =NULL ; - -if (!DeleteIpForwardEntry2) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - DeleteIpForwardEntry2 = (DeleteIpForwardEntry2Fn )GetProcAddress (iphlpapi,"DeleteIpForwardEntry2" ); -if (!DeleteIpForwardEntry2) - { -return GetLastError (); - } - } -return DeleteIpForwardEntry2 (fwd_row); }@@ -734,24 +655,6 @@ HandleRouteMessage(route_message_t *msg, undo_lists_t *lists) PMIB_IPFORWARD_ROW2 fwd_row;BOOL add = msg->header.type == msg_add_route; -typedef NETIOAPI_API (*CreateIpForwardEntry2Fn ) (PMIB_IPFORWARD_ROW2 ); -static CreateIpForwardEntry2Fn CreateIpForwardEntry2 =NULL ; - -if (!CreateIpForwardEntry2) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - CreateIpForwardEntry2 = (CreateIpForwardEntry2Fn )GetProcAddress (iphlpapi,"CreateIpForwardEntry2" ); -if (!CreateIpForwardEntry2) - { -return GetLastError (); - } - } - fwd_row =malloc (sizeof (*fwd_row));if (fwd_row == NULL) {src/openvpnserv/interactive.c +0
-17@@ -820,36 +723,12 @@ out:static DWORD HandleFlushNeighborsMessage (flush_neighbors_message_t *msg) { -typedef NETIOAPI_API (*FlushIpNetTable2Fn ) (ADDRESS_FAMILY , NET_IFINDEX); -static FlushIpNetTable2Fn flush_fn =NULL ; -if (msg->family ==AF_INET ) {return FlushIpNetTable (msg->iface.index); } -if (!flush_fn) - { -HMODULE iphlpapi =GetModuleHandle (TEXT ("iphlpapi.dll" )); -if (iphlpapi ==NULL ) - { -return GetLastError (); - } - - flush_fn = (FlushIpNetTable2Fn )GetProcAddress (iphlpapi, "FlushIpNetTable2"); -if (!flush_fn) - { -if (GetLastError () ==ERROR_PROC_NOT_FOUND ) - { -return WSAEPFNOSUPPORT ;
src/openvpnserv/interactive.c out: · line 734
Quoted 10 lines
src/openvpnserv/interactive.c +1
-8- } -else - { -return GetLastError (); - } - } - } -return flush_fn (msg->family, msg->iface.index); +return FlushIpNetTable2 (msg->family, msg->iface.index); }
Looks good and cross-compiles without issues.
ACK
Selva
I like patches that *remove* lots of lines of code :-)
Your patch has been applied to the master and release/2.4 branch.
commit a5d73667ffebea93960c135322aa3a8d0fd70d7a (master) commit c5f0ccf1376b6c7e1fe58c87a67b009be3ac9361 (release/2.4)
Author: Simon Rozman Date: Thu Oct 12 10:07:20 2017 +0200
Simplify iphlpapi.dll API calls
Acked-by: Selva Nair <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg15614.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering