From 4096417c9047dd012675367cb3a04edec1376fab Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 15 Jan 2012 01:56:24 -0500 Subject: [PATCH] Added Windows implementation of SDLNet_GetLocalAddresses() --- SDLnet.c | 42 +- SDLnetsys.h | 1 + VisualC/SDL_net_VS2008.sln | 13 + VisualC/SDL_net_VS2008.vcproj | 8 +- VisualC/showinterfaces/showinterfaces.vcproj | 408 +++++++++++++++++++ configure | 4 +- configure.in | 4 +- 7 files changed, 468 insertions(+), 12 deletions(-) create mode 100755 VisualC/showinterfaces/showinterfaces.vcproj diff --git a/SDLnet.c b/SDLnet.c index 982043f..261d21e 100644 --- a/SDLnet.c +++ b/SDLnet.c @@ -188,16 +188,50 @@ int SDLNet_GetLocalAddresses(IPaddress *addresses, int maxcount) } ifr = (struct ifreq*)data; - while ((char*)ifr < data+conf.ifc_len && count < maxcount) { + while ((char*)ifr < data+conf.ifc_len) { if (ifr->ifr_addr.sa_family == AF_INET) { - sock_addr = (struct sockaddr_in*)&ifr->ifr_addr; - addresses[count].host = sock_addr->sin_addr.s_addr; - addresses[count].port = sock_addr->sin_port; + if (count < maxcount) { + sock_addr = (struct sockaddr_in*)&ifr->ifr_addr; + addresses[count].host = sock_addr->sin_addr.s_addr; + addresses[count].port = sock_addr->sin_port; + } ++count; } ifr = (struct ifreq*)((char*)ifr + _SIZEOF_ADDR_IFREQ(*ifr)); } closesocket(sock); +#elif defined(__WIN32__) + PIP_ADAPTER_INFO pAdapterInfo; + PIP_ADAPTER_INFO pAdapter; + PIP_ADDR_STRING pAddress; + DWORD dwRetVal = 0; + ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); + + pAdapterInfo = (IP_ADAPTER_INFO *) SDL_malloc(sizeof (IP_ADAPTER_INFO)); + if (pAdapterInfo == NULL) { + return 0; + } + + if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == ERROR_BUFFER_OVERFLOW) { + pAdapterInfo = (IP_ADAPTER_INFO *) SDL_realloc(pAdapterInfo, ulOutBufLen); + if (pAdapterInfo == NULL) { + return 0; + } + dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); + } + + if (dwRetVal == NO_ERROR) { + for (pAdapter = pAdapterInfo; pAdapter; pAdapter = pAdapter->Next) { + for (pAddress = &pAdapterInfo->IpAddressList; pAddress; pAddress = pAddress->Next) { + if (count < maxcount) { + addresses[count].host = inet_addr(pAddress->IpAddress.String); + addresses[count].port = 0; + } + ++count; + } + } + } + SDL_free(pAdapterInfo); #endif return count; } diff --git a/SDLnetsys.h b/SDLnetsys.h index eda4144..9fd11ed 100644 --- a/SDLnetsys.h +++ b/SDLnetsys.h @@ -42,6 +42,7 @@ * and is defined only for winsock2. */ typedef int socklen_t; #endif /* W64 */ +#include #else /* UNIX */ #include #ifdef __FreeBSD__ diff --git a/VisualC/SDL_net_VS2008.sln b/VisualC/SDL_net_VS2008.sln index c8264d0..77bd799 100755 --- a/VisualC/SDL_net_VS2008.sln +++ b/VisualC/SDL_net_VS2008.sln @@ -3,6 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 10.00 # Visual Studio 2008 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SDL_net", "SDL_net_VS2008.vcproj", "{8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "showinterfaces", "showinterfaces\showinterfaces.vcproj", "{7B1F60CD-2A09-4514-937C-D9DD044428FB}" + ProjectSection(ProjectDependencies) = postProject + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4} = {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -19,6 +24,14 @@ Global {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|Win32.Build.0 = Release|Win32 {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|x64.ActiveCfg = Release|x64 {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|x64.Build.0 = Release|x64 + {7B1F60CD-2A09-4514-937C-D9DD044428FB}.Debug|Win32.ActiveCfg = Debug|Win32 + {7B1F60CD-2A09-4514-937C-D9DD044428FB}.Debug|Win32.Build.0 = Debug|Win32 + {7B1F60CD-2A09-4514-937C-D9DD044428FB}.Debug|x64.ActiveCfg = Debug|x64 + {7B1F60CD-2A09-4514-937C-D9DD044428FB}.Debug|x64.Build.0 = Debug|x64 + {7B1F60CD-2A09-4514-937C-D9DD044428FB}.Release|Win32.ActiveCfg = Release|Win32 + {7B1F60CD-2A09-4514-937C-D9DD044428FB}.Release|Win32.Build.0 = Release|Win32 + {7B1F60CD-2A09-4514-937C-D9DD044428FB}.Release|x64.ActiveCfg = Release|x64 + {7B1F60CD-2A09-4514-937C-D9DD044428FB}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/VisualC/SDL_net_VS2008.vcproj b/VisualC/SDL_net_VS2008.vcproj index b53bc58..1c4f173 100755 --- a/VisualC/SDL_net_VS2008.vcproj +++ b/VisualC/SDL_net_VS2008.vcproj @@ -72,7 +72,7 @@ /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/configure b/configure index cad3754..377621a 100755 --- a/configure +++ b/configure @@ -16292,10 +16292,10 @@ case "$host" in INETLIB="-lwinsock -lws2" ;; x86_64-*-mingw32*) - INETLIB="-lws2_32" + INETLIB="-lws2_32 -liphlpapi" ;; *-*-cygwin* | *-*-mingw32*) - INETLIB="-lwsock32" + INETLIB="-lwsock32 -liphlpapi" ;; sparc*-*-solaris*) INETLIB="-lsocket -lnsl" diff --git a/configure.in b/configure.in index 03b8c02..c42bd0c 100644 --- a/configure.in +++ b/configure.in @@ -84,10 +84,10 @@ case "$host" in INETLIB="-lwinsock -lws2" ;; x86_64-*-mingw32*) - INETLIB="-lws2_32" + INETLIB="-lws2_32 -liphlpapi" ;; *-*-cygwin* | *-*-mingw32*) - INETLIB="-lwsock32" + INETLIB="-lwsock32 -liphlpapi" ;; sparc*-*-solaris*) INETLIB="-lsocket -lnsl"