pcap_if_t* findBestAdapter(PIP_ADAPTER_INFO allIPAdaptersInfo, pcap_if_t* alldevs)
{
DWORD bestIfIndex;
IPAddr ipAddr = htonl(0x08080808); // "8.8.8.8" is well-knowon Google DNS Server IP.
DWORD res = GetBestInterface(ipAddr, &bestIfIndex);
if (res != NO_ERROR)
{
printf("GetBestInterface return %d", res);
return NULL;
}
PIP_ADAPTER_INFO p = allIPAdaptersInfo;
while (p != NULL)
{
if (bestIfIndex == p->Index) break;
p = p->Next;
}
if (p == NULL)
{
printf("Can not find appropriate system adapter\n");
return NULL;
}
pcap_if_t *d = alldevs;
while (d != NULL)
{
if (strstr(d->name, p->AdapterName) != NULL)
break;
}
if (d == NULL)
{
printf("Can not find appropriate winpcap adapter\n");
return NULL;
}
return d;
}