Error getting WinPcap address information
Frage
Hello i am having some problem getting network card address information from the WinPcap driver. I have noticed that whenever IPv6 is enabled for a particular NIC, the address information for it becomes 0.0.0.0. How can i fix this?
pcap_addr* address = GetDeviceAddress(1);
cout<<"IP address "<<iptos(((struct sockaddr_in *)address->addr)->sin_addr.s_addr)<<endl;
/////////////
pcap_addr* GetDeviceAddress(int index)//Gets the name of a device using the zero based index of its location in the list
{
int i=0;
if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&AllDevices,errbuf) != -1)
for(Iterator=AllDevices;Iterator!=NULL;Iterator=Iterator->next)
{
if(i==index)
{
return Iterator->addresses;
}
i++;
}
return NULL;
}
Lösung
What does your code that's looking at the address information look like? Perhaps it has a bug that causes it not to properly handle IPv6 addresses.
The address information may have more than one IP address - libpcap/WinPcap's pcap_findalldevs()
call and WinPcap's pcap_findalldevs_ex()
call supply a pointer to a list of pcap_if_t
structures, one for each interface that it found, and each of those structures includes a list of address information items of type struct pcap_addr
. Each of those address information items includes a network address, which is of type struct sockaddr
; that structure includes a member named sa_family
, which will have the value AF_INET
for IPv4 and AF_INET6
for IPv6. If the member has the value AF_INET6
, the address data will be 128 bits of IPv6 address, NOT 32 bits of IPv4 address.