Skip to content
This repository was archived by the owner on Mar 21, 2021. It is now read-only.

Commit 35cbb8f

Browse files
committed
Added Windows implementation of SDLNet_GetLocalAddresses()
1 parent 84a0d48 commit 35cbb8f

File tree

7 files changed

+468
-12
lines changed

7 files changed

+468
-12
lines changed

SDLnet.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,50 @@ int SDLNet_GetLocalAddresses(IPaddress *addresses, int maxcount)
188188
}
189189

190190
ifr = (struct ifreq*)data;
191-
while ((char*)ifr < data+conf.ifc_len && count < maxcount) {
191+
while ((char*)ifr < data+conf.ifc_len) {
192192
if (ifr->ifr_addr.sa_family == AF_INET) {
193-
sock_addr = (struct sockaddr_in*)&ifr->ifr_addr;
194-
addresses[count].host = sock_addr->sin_addr.s_addr;
195-
addresses[count].port = sock_addr->sin_port;
193+
if (count < maxcount) {
194+
sock_addr = (struct sockaddr_in*)&ifr->ifr_addr;
195+
addresses[count].host = sock_addr->sin_addr.s_addr;
196+
addresses[count].port = sock_addr->sin_port;
197+
}
196198
++count;
197199
}
198200
ifr = (struct ifreq*)((char*)ifr + _SIZEOF_ADDR_IFREQ(*ifr));
199201
}
200202
closesocket(sock);
203+
#elif defined(__WIN32__)
204+
PIP_ADAPTER_INFO pAdapterInfo;
205+
PIP_ADAPTER_INFO pAdapter;
206+
PIP_ADDR_STRING pAddress;
207+
DWORD dwRetVal = 0;
208+
ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
209+
210+
pAdapterInfo = (IP_ADAPTER_INFO *) SDL_malloc(sizeof (IP_ADAPTER_INFO));
211+
if (pAdapterInfo == NULL) {
212+
return 0;
213+
}
214+
215+
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == ERROR_BUFFER_OVERFLOW) {
216+
pAdapterInfo = (IP_ADAPTER_INFO *) SDL_realloc(pAdapterInfo, ulOutBufLen);
217+
if (pAdapterInfo == NULL) {
218+
return 0;
219+
}
220+
dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
221+
}
222+
223+
if (dwRetVal == NO_ERROR) {
224+
for (pAdapter = pAdapterInfo; pAdapter; pAdapter = pAdapter->Next) {
225+
for (pAddress = &pAdapterInfo->IpAddressList; pAddress; pAddress = pAddress->Next) {
226+
if (count < maxcount) {
227+
addresses[count].host = inet_addr(pAddress->IpAddress.String);
228+
addresses[count].port = 0;
229+
}
230+
++count;
231+
}
232+
}
233+
}
234+
SDL_free(pAdapterInfo);
201235
#endif
202236
return count;
203237
}

SDLnetsys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* and is defined only for winsock2. */
4343
typedef int socklen_t;
4444
#endif /* W64 */
45+
#include <iphlpapi.h>
4546
#else /* UNIX */
4647
#include <sys/types.h>
4748
#ifdef __FreeBSD__

VisualC/SDL_net_VS2008.sln

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 10.00
33
# Visual Studio 2008
44
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SDL_net", "SDL_net_VS2008.vcproj", "{8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}"
55
EndProject
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "showinterfaces", "showinterfaces\showinterfaces.vcproj", "{7B1F60CD-2A09-4514-937C-D9DD044428FB}"
7+
ProjectSection(ProjectDependencies) = postProject
8+
{8AB3504F-5E58-4910-AFE8-7A1E595AC3F4} = {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}
9+
EndProjectSection
10+
EndProject
611
Global
712
GlobalSection(SolutionConfigurationPlatforms) = preSolution
813
Debug|Win32 = Debug|Win32
@@ -19,6 +24,14 @@ Global
1924
{8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|Win32.Build.0 = Release|Win32
2025
{8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|x64.ActiveCfg = Release|x64
2126
{8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|x64.Build.0 = Release|x64
27+
{7B1F60CD-2A09-4514-937C-D9DD044428FB}.Debug|Win32.ActiveCfg = Debug|Win32
28+
{7B1F60CD-2A09-4514-937C-D9DD044428FB}.Debug|Win32.Build.0 = Debug|Win32
29+
{7B1F60CD-2A09-4514-937C-D9DD044428FB}.Debug|x64.ActiveCfg = Debug|x64
30+
{7B1F60CD-2A09-4514-937C-D9DD044428FB}.Debug|x64.Build.0 = Debug|x64
31+
{7B1F60CD-2A09-4514-937C-D9DD044428FB}.Release|Win32.ActiveCfg = Release|Win32
32+
{7B1F60CD-2A09-4514-937C-D9DD044428FB}.Release|Win32.Build.0 = Release|Win32
33+
{7B1F60CD-2A09-4514-937C-D9DD044428FB}.Release|x64.ActiveCfg = Release|x64
34+
{7B1F60CD-2A09-4514-937C-D9DD044428FB}.Release|x64.Build.0 = Release|x64
2235
EndGlobalSection
2336
GlobalSection(SolutionProperties) = preSolution
2437
HideSolutionNode = FALSE

VisualC/SDL_net_VS2008.vcproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
/>
7373
<Tool
7474
Name="VCLinkerTool"
75-
AdditionalDependencies="wsock32.lib SDL.lib"
75+
AdditionalDependencies="wsock32.lib iphlpapi.lib SDL.lib"
7676
OutputFile=".\Debug/SDL_net.dll"
7777
LinkIncremental="2"
7878
SuppressStartupBanner="true"
@@ -162,7 +162,7 @@
162162
/>
163163
<Tool
164164
Name="VCLinkerTool"
165-
AdditionalDependencies="ws2_32.lib SDL.lib"
165+
AdditionalDependencies="ws2_32.lib iphlpapi.lib SDL.lib"
166166
OutputFile=".\Debug/SDL_net.dll"
167167
LinkIncremental="2"
168168
SuppressStartupBanner="true"
@@ -250,7 +250,7 @@
250250
/>
251251
<Tool
252252
Name="VCLinkerTool"
253-
AdditionalDependencies="wsock32.lib SDL.lib"
253+
AdditionalDependencies="wsock32.lib iphlpapi.lib SDL.lib"
254254
OutputFile=".\Release/SDL_net.dll"
255255
LinkIncremental="1"
256256
SuppressStartupBanner="true"
@@ -337,7 +337,7 @@
337337
/>
338338
<Tool
339339
Name="VCLinkerTool"
340-
AdditionalDependencies="ws2_32.lib SDL.lib"
340+
AdditionalDependencies="ws2_32.lib iphlpapi.lib SDL.lib"
341341
OutputFile=".\Release/SDL_net.dll"
342342
LinkIncremental="1"
343343
SuppressStartupBanner="true"

0 commit comments

Comments
 (0)