Browse code

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.
Acked-by: Selva Nair <selva.nair@gmail.com>
Message-Id: <20171012080720.7764-1-simon@rozman.si>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg15614.html

Signed-off-by: Gert Doering <gert@greenie.muc.de>

Simon Rozman authored on 2017/10/12 17:07:20
Showing 1 changed files
... ...
@@ -550,23 +550,6 @@ InterfaceLuid(const char *iface_name, PNET_LUID luid)
550 550
     LPWSTR wide_name;
551 551
     int n;
552 552
 
553
-    typedef NETIO_STATUS WINAPI (*ConvertInterfaceAliasToLuidFn) (LPCWSTR, PNET_LUID);
554
-    static ConvertInterfaceAliasToLuidFn ConvertInterfaceAliasToLuid = NULL;
555
-    if (!ConvertInterfaceAliasToLuid)
556
-    {
557
-        HMODULE iphlpapi = GetModuleHandle(TEXT("iphlpapi.dll"));
558
-        if (iphlpapi == NULL)
559
-        {
560
-            return GetLastError();
561
-        }
562
-
563
-        ConvertInterfaceAliasToLuid = (ConvertInterfaceAliasToLuidFn) GetProcAddress(iphlpapi, "ConvertInterfaceAliasToLuid");
564
-        if (!ConvertInterfaceAliasToLuid)
565
-        {
566
-            return GetLastError();
567
-        }
568
-    }
569
-
570 553
     n = MultiByteToWideChar(CP_UTF8, 0, iface_name, -1, NULL, 0);
571 554
     wide_name = malloc(n * sizeof(WCHAR));
572 555
     MultiByteToWideChar(CP_UTF8, 0, iface_name, -1, wide_name, n);
... ...
@@ -585,24 +568,6 @@ CmpAddress(LPVOID item, LPVOID address)
585 585
 static DWORD
586 586
 DeleteAddress(PMIB_UNICASTIPADDRESS_ROW addr_row)
587 587
 {
588
-    typedef NETIOAPI_API (*DeleteUnicastIpAddressEntryFn) (const PMIB_UNICASTIPADDRESS_ROW);
589
-    static DeleteUnicastIpAddressEntryFn DeleteUnicastIpAddressEntry = NULL;
590
-
591
-    if (!DeleteUnicastIpAddressEntry)
592
-    {
593
-        HMODULE iphlpapi = GetModuleHandle(TEXT("iphlpapi.dll"));
594
-        if (iphlpapi == NULL)
595
-        {
596
-            return GetLastError();
597
-        }
598
-
599
-        DeleteUnicastIpAddressEntry = (DeleteUnicastIpAddressEntryFn) GetProcAddress(iphlpapi, "DeleteUnicastIpAddressEntry");
600
-        if (!DeleteUnicastIpAddressEntry)
601
-        {
602
-            return GetLastError();
603
-        }
604
-    }
605
-
606 588
     return DeleteUnicastIpAddressEntry(addr_row);
607 589
 }
608 590
 
... ...
@@ -613,32 +578,6 @@ HandleAddressMessage(address_message_t *msg, undo_lists_t *lists)
613 613
     PMIB_UNICASTIPADDRESS_ROW addr_row;
614 614
     BOOL add = msg->header.type == msg_add_address;
615 615
 
616
-    typedef NETIOAPI_API (*CreateUnicastIpAddressEntryFn) (const PMIB_UNICASTIPADDRESS_ROW);
617
-    typedef NETIOAPI_API (*InitializeUnicastIpAddressEntryFn) (PMIB_UNICASTIPADDRESS_ROW);
618
-    static CreateUnicastIpAddressEntryFn CreateUnicastIpAddressEntry = NULL;
619
-    static InitializeUnicastIpAddressEntryFn InitializeUnicastIpAddressEntry = NULL;
620
-
621
-    if (!CreateUnicastIpAddressEntry || !InitializeUnicastIpAddressEntry)
622
-    {
623
-        HMODULE iphlpapi = GetModuleHandle(TEXT("iphlpapi.dll"));
624
-        if (iphlpapi == NULL)
625
-        {
626
-            return GetLastError();
627
-        }
628
-
629
-        CreateUnicastIpAddressEntry = (CreateUnicastIpAddressEntryFn) GetProcAddress(iphlpapi, "CreateUnicastIpAddressEntry");
630
-        if (!CreateUnicastIpAddressEntry)
631
-        {
632
-            return GetLastError();
633
-        }
634
-
635
-        InitializeUnicastIpAddressEntry = (InitializeUnicastIpAddressEntryFn) GetProcAddress(iphlpapi, "InitializeUnicastIpAddressEntry");
636
-        if (!InitializeUnicastIpAddressEntry)
637
-        {
638
-            return GetLastError();
639
-        }
640
-    }
641
-
642 616
     addr_row = malloc(sizeof(*addr_row));
643 617
     if (addr_row == NULL)
644 618
     {
... ...
@@ -707,24 +646,6 @@ CmpRoute(LPVOID item, LPVOID route)
707 707
 static DWORD
708 708
 DeleteRoute(PMIB_IPFORWARD_ROW2 fwd_row)
709 709
 {
710
-    typedef NETIOAPI_API (*DeleteIpForwardEntry2Fn) (PMIB_IPFORWARD_ROW2);
711
-    static DeleteIpForwardEntry2Fn DeleteIpForwardEntry2 = NULL;
712
-
713
-    if (!DeleteIpForwardEntry2)
714
-    {
715
-        HMODULE iphlpapi = GetModuleHandle(TEXT("iphlpapi.dll"));
716
-        if (iphlpapi == NULL)
717
-        {
718
-            return GetLastError();
719
-        }
720
-
721
-        DeleteIpForwardEntry2 = (DeleteIpForwardEntry2Fn) GetProcAddress(iphlpapi, "DeleteIpForwardEntry2");
722
-        if (!DeleteIpForwardEntry2)
723
-        {
724
-            return GetLastError();
725
-        }
726
-    }
727
-
728 710
     return DeleteIpForwardEntry2(fwd_row);
729 711
 }
730 712
 
... ...
@@ -735,24 +656,6 @@ HandleRouteMessage(route_message_t *msg, undo_lists_t *lists)
735 735
     PMIB_IPFORWARD_ROW2 fwd_row;
736 736
     BOOL add = msg->header.type == msg_add_route;
737 737
 
738
-    typedef NETIOAPI_API (*CreateIpForwardEntry2Fn) (PMIB_IPFORWARD_ROW2);
739
-    static CreateIpForwardEntry2Fn CreateIpForwardEntry2 = NULL;
740
-
741
-    if (!CreateIpForwardEntry2)
742
-    {
743
-        HMODULE iphlpapi = GetModuleHandle(TEXT("iphlpapi.dll"));
744
-        if (iphlpapi == NULL)
745
-        {
746
-            return GetLastError();
747
-        }
748
-
749
-        CreateIpForwardEntry2 = (CreateIpForwardEntry2Fn) GetProcAddress(iphlpapi, "CreateIpForwardEntry2");
750
-        if (!CreateIpForwardEntry2)
751
-        {
752
-            return GetLastError();
753
-        }
754
-    }
755
-
756 738
     fwd_row = malloc(sizeof(*fwd_row));
757 739
     if (fwd_row == NULL)
758 740
     {
... ...
@@ -821,36 +724,12 @@ out:
821 821
 static DWORD
822 822
 HandleFlushNeighborsMessage(flush_neighbors_message_t *msg)
823 823
 {
824
-    typedef NETIOAPI_API (*FlushIpNetTable2Fn) (ADDRESS_FAMILY, NET_IFINDEX);
825
-    static FlushIpNetTable2Fn flush_fn = NULL;
826
-
827 824
     if (msg->family == AF_INET)
828 825
     {
829 826
         return FlushIpNetTable(msg->iface.index);
830 827
     }
831 828
 
832
-    if (!flush_fn)
833
-    {
834
-        HMODULE iphlpapi = GetModuleHandle(TEXT("iphlpapi.dll"));
835
-        if (iphlpapi == NULL)
836
-        {
837
-            return GetLastError();
838
-        }
839
-
840
-        flush_fn = (FlushIpNetTable2Fn) GetProcAddress(iphlpapi, "FlushIpNetTable2");
841
-        if (!flush_fn)
842
-        {
843
-            if (GetLastError() == ERROR_PROC_NOT_FOUND)
844
-            {
845
-                return WSAEPFNOSUPPORT;
846
-            }
847
-            else
848
-            {
849
-                return GetLastError();
850
-            }
851
-        }
852
-    }
853
-    return flush_fn(msg->family, msg->iface.index);
829
+    return FlushIpNetTable2(msg->family, msg->iface.index);
854 830
 }
855 831
 
856 832
 static void