Replace GetBestRoute with UWP-compatible API

Everything should still work with Windows XP as minimum version.
Those API are, according to Microsoft docs, compatible with XP.
This commit is contained in:
Fabio Alessandrelli 2018-09-21 14:56:59 +02:00
parent bde31cd4f1
commit a31c86b8db
1 changed files with 74 additions and 50 deletions

View File

@ -494,7 +494,6 @@ ssdpDiscoverDevices(const char * const deviceTypes[],
struct addrinfo hints, *servinfo, *p; struct addrinfo hints, *servinfo, *p;
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
MIB_IPFORWARDROW ip_forward;
unsigned long _ttl = (unsigned long)ttl; unsigned long _ttl = (unsigned long)ttl;
#endif #endif
int linklocal = 1; int linklocal = 1;
@ -539,70 +538,95 @@ ssdpDiscoverDevices(const char * const deviceTypes[],
/* Get IP associated with the index given in the ip_forward struct /* Get IP associated with the index given in the ip_forward struct
* in order to give this ip to setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF) */ * in order to give this ip to setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF) */
if(!ipv6) { if(!ipv6) {
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA DWORD ifbestidx;
IN_ADDR addr; SOCKADDR_IN destAddr;
InetPtonA(AF_INET, "223.255.255.255", &addr); memset(&destAddr, 0, sizeof(destAddr));
#else destAddr.sin_family = AF_INET;
struct in_addr addr; destAddr.sin_addr.s_addr = inet_addr("223.255.255.255");
addr.s_addr = inet_addr("223.255.255.255"); destAddr.sin_port = 0;
#endif if (GetBestInterfaceEx((struct sockaddr *)&destAddr, &ifbestidx) == NO_ERROR) {
if (GetBestRoute(addr.s_addr, 0, &ip_forward) == NO_ERROR) {
DWORD dwRetVal = 0; DWORD dwRetVal = 0;
PMIB_IPADDRTABLE pIPAddrTable; unsigned int i = 0;
DWORD dwSize = 0; PIP_ADAPTER_ADDRESSES pAddresses = NULL;
#ifdef DEBUG ULONG outBufLen = 0;
IN_ADDR IPAddr; ULONG Iterations = 0;
#endif PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
int i; PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
#ifdef DEBUG PIP_ADAPTER_ANYCAST_ADDRESS pAnycast = NULL;
printf("ifIndex=%lu nextHop=%lx \n", ip_forward.dwForwardIfIndex, ip_forward.dwForwardNextHop); PIP_ADAPTER_MULTICAST_ADDRESS pMulticast = NULL;
#endif
pIPAddrTable = (MIB_IPADDRTABLE *) malloc(sizeof (MIB_IPADDRTABLE)); outBufLen = 15360;
if(pIPAddrTable) { do {
if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { pAddresses = (IP_ADAPTER_ADDRESSES *) HeapAlloc(GetProcessHeap(), 0, outBufLen);
free(pIPAddrTable); if (pAddresses == NULL) {
pIPAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize); break;
} }
dwRetVal = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufLen);
if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
HeapFree(GetProcessHeap(), 0, pAddresses);
pAddresses = NULL;
} else {
break;
} }
if(pIPAddrTable) { Iterations++;
dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 ); } while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < 3));
if (dwRetVal == NO_ERROR) { if (dwRetVal == NO_ERROR) {
pCurrAddresses = pAddresses;
while (pCurrAddresses) {
#ifdef DEBUG #ifdef DEBUG
printf("\tNum Entries: %lu\n", pIPAddrTable->dwNumEntries); printf("\tIfIndex (IPv4 interface): %u\n", pCurrAddresses->IfIndex);
#endif printf("\tAdapter name: %s\n", pCurrAddresses->AdapterName);
for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) { pUnicast = pCurrAddresses->FirstUnicastAddress;
#ifdef DEBUG if (pUnicast != NULL) {
char buffer[16]; for (i = 0; pUnicast != NULL; i++) {
printf("\n\tInterface Index[%d]:\t%lu\n", i, pIPAddrTable->table[i].dwIndex); IPAddr.S_un.S_addr = (u_long) pUnicast->Address;
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr; printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
InetNtopA(AF_INET, &IPAddr, buffer, sizeof(buffer)); pUnicast = pUnicast->Next;
printf("\tIP Address[%d]: \t%s\n", i, buffer ); }
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask; printf("\tNumber of Unicast Addresses: %d\n", i);
InetNtopA(AF_INET, &IPAddr, buffer, sizeof(buffer)); }
printf("\tSubnet Mask[%d]: \t%s\n", i, buffer ); pAnycast = pCurrAddresses->FirstAnycastAddress;
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr; if (pAnycast) {
InetNtopA(AF_INET, &IPAddr, buffer, sizeof(buffer)); for (i = 0; pAnycast != NULL; i++) {
printf("\tBroadCast[%d]: \t%s (%lu)\n", i, buffer, pIPAddrTable->table[i].dwBCastAddr); IPAddr.S_un.S_addr = (u_long) pAnyCast->Address;
printf("\tReassembly size[%d]:\t%lu\n", i, pIPAddrTable->table[i].dwReasmSize); printf("\tAnycast Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
printf("\tType and State[%d]:", i); pAnycast = pAnycast->Next;
}
printf("\tNumber of Anycast Addresses: %d\n", i);
}
pMulticast = pCurrAddresses->FirstMulticastAddress;
if (pMulticast) {
for (i = 0; pMulticast != NULL; i++) {
IPAddr.S_un.S_addr = (u_long) pMultiCast->Address;
printf("\tMulticast Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
}
}
printf("\n"); printf("\n");
#endif #endif
if (pIPAddrTable->table[i].dwIndex == ip_forward.dwForwardIfIndex) { pUnicast = pCurrAddresses->FirstUnicastAddress;
if (pCurrAddresses->IfIndex == ifbestidx && pUnicast != NULL) {
SOCKADDR_IN *ipv4 = (SOCKADDR_IN *)(pUnicast->Address.lpSockaddr);
/* Set the address of this interface to be used */ /* Set the address of this interface to be used */
struct in_addr mc_if; struct in_addr mc_if;
memset(&mc_if, 0, sizeof(mc_if)); memset(&mc_if, 0, sizeof(mc_if));
mc_if.s_addr = pIPAddrTable->table[i].dwAddr; mc_if.s_addr = ipv4->sin_addr.s_addr;
if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0) { if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0) {
PRINT_SOCKET_ERROR("setsockopt"); PRINT_SOCKET_ERROR("setsockopt");
} }
((struct sockaddr_in *)&sockudp_r)->sin_addr.s_addr = pIPAddrTable->table[i].dwAddr; ((struct sockaddr_in *)&sockudp_r)->sin_addr.s_addr = ipv4->sin_addr.s_addr;
#ifndef DEBUG #ifndef DEBUG
break; break;
#endif #endif
} }
pCurrAddresses = pCurrAddresses->Next;
} }
} }
free(pIPAddrTable); if (pAddresses != NULL) {
HeapFree(GetProcessHeap(), 0, pAddresses);
pAddresses = NULL;
} }
} }
} }