Created
October 3, 2018 11:03
-
-
Save DJm00n/29d51bc32ec48551c77fa02e51f082e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iphlpapi.h> | |
struct iflist { | |
wchar_t name[50]; | |
struct sockaddr_in sin; | |
int ifidx; | |
}; | |
void getiflist(struct iflist *list, int *listLen) | |
{ | |
DWORD dwSize = *listLen; | |
DWORD dwRetVal = 0; | |
ULONG outBufLen = 15 * 1024; // Allocate a 15 KB buffer to start with. | |
PIP_ADAPTER_ADDRESSES pAddresses = nullptr; | |
ULONG Iterations = 0; | |
do | |
{ | |
if (!(pAddresses = (IP_ADAPTER_ADDRESSES *)HeapAlloc(GetProcessHeap(), 0, outBufLen))) | |
{ | |
printf("Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n"); | |
return; | |
} | |
dwRetVal = ::GetAdaptersAddresses(AF_UNSPEC, 0, nullptr, pAddresses, &outBufLen); | |
if (dwRetVal == ERROR_BUFFER_OVERFLOW) | |
{ | |
HeapFree(GetProcessHeap(), 0, pAddresses); | |
pAddresses = nullptr; | |
} | |
else | |
{ | |
break; | |
} | |
Iterations++; | |
} while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < 3)); | |
if (dwRetVal == NO_ERROR) | |
{ | |
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr; | |
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr; | |
// If successful, output some information from the data we received | |
for (*listLen = 0, pCurrAddresses = pAddresses; pCurrAddresses; pCurrAddresses = pCurrAddresses->Next) | |
{ | |
if (pCurrAddresses->IfType == IF_TYPE_TUNNEL | |
|| pCurrAddresses->IfType == IF_TYPE_SOFTWARE_LOOPBACK | |
|| pCurrAddresses->OperStatus != IfOperStatusUp | |
|| pCurrAddresses->Flags & IP_ADAPTER_NO_MULTICAST | |
) continue; | |
for (pUnicast = pCurrAddresses->FirstUnicastAddress; pUnicast; pUnicast = pUnicast->Next) { | |
if (pUnicast->Address.lpSockaddr->sa_family == AF_INET) { | |
memset(&list[*listLen], 0, sizeof(struct iflist)); | |
wcsncpy_s(list[*listLen].name, pCurrAddresses->FriendlyName, sizeof(list[*listLen].name) - 1); | |
memcpy(&list[*listLen].sin, pUnicast->Address.lpSockaddr, pUnicast->Address.iSockaddrLength); | |
list[*listLen].ifidx = pCurrAddresses->IfIndex; | |
(*listLen)++; | |
if ((DWORD)*listLen >= dwSize) | |
break; | |
} | |
} | |
} | |
} | |
else | |
{ | |
printf("Call to GetAdaptersAddresses failed with error: %d\n", | |
dwRetVal); | |
if (dwRetVal == ERROR_NO_DATA) | |
printf("\tNo addresses were found for the requested parameters\n"); | |
else | |
{ | |
LPSTR lpMsgBuf = nullptr; | |
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | |
nullptr, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
// Default language | |
lpMsgBuf, 0, nullptr)) | |
{ | |
printf("\tError: %s", lpMsgBuf); | |
LocalFree(lpMsgBuf); | |
if (pAddresses) HeapFree(GetProcessHeap(), 0, pAddresses); | |
return; | |
} | |
} | |
} | |
if (pAddresses) HeapFree(GetProcessHeap(), 0, pAddresses); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment