Windows: Use Standard Networking Functions If Possible

Currently, the Windows code use Winsock2-specific APIs to try to emulate
calls such as inet_pton(), inet_ntop() and if_nametoindex(), which may not
do the job all the time.  On Vista and later, Winsock2 does provide a
proper implementation for these functions, so we can use them if they exist
on the system, by querying for them during g_networking_init().  Otherwise,
we continue to use the original code path for these, in the case of XP and
Server 2003.

This enables many of the network-address tests to pass on Windows as a
result, when the native Winsock2 implementations can be used.

https://bugzilla.gnome.org/show_bug.cgi?id=730352
This commit is contained in:
Chun-wei Fan
2015-03-05 16:13:03 +08:00
parent ec1edef3ab
commit 6fe28eef3c
5 changed files with 199 additions and 60 deletions

View File

@@ -22,6 +22,13 @@
#include "gnetworking.h"
#ifdef G_OS_WIN32
/* For Windows XP run-time compatibility */
#include "gwin32networking.h"
GWin32WinsockFuncs ws2funcs = {0};
#endif
/**
* SECTION:gnetworking
* @title: gnetworking.h
@@ -66,9 +73,39 @@ g_networking_init (void)
if (g_once_init_enter (&inited))
{
WSADATA wsadata;
HMODULE ws2dll, iphlpapidll;
if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
g_error ("Windows Sockets could not be initialized");
g_error ("Windows Sockets could not be initialized");
/* We want to use these functions if they are available, but
* still need to make sure the code still runs on Windows XP
*/
ws2dll = LoadLibraryW (L"ws2_32.dll");
iphlpapidll = LoadLibraryW (L"iphlpapi.dll");
if (ws2dll != NULL)
{
ws2funcs.pInetNtop =
(PFN_InetNtop) GetProcAddress (ws2dll, "inet_ntop");
ws2funcs.pInetPton =
(PFN_InetPton) GetProcAddress (ws2dll, "inet_pton");
FreeLibrary (ws2dll);
}
else
{
ws2funcs.pInetNtop = NULL;
ws2funcs.pInetPton = NULL;
}
if (iphlpapidll != NULL)
{
ws2funcs.pIfNameToIndex =
(PFN_IfNameToIndex) GetProcAddress (iphlpapidll, "if_nametoindex");
FreeLibrary (iphlpapidll);
}
else
ws2funcs.pIfNameToIndex = NULL;
g_once_init_leave (&inited, 1);
}