I am tasked with listing all unicast interfaces (IP-address & subnet via netmask (IPv4) or prefix length (IPv6)) on the local machine. Being new to win32 API, I am reading GetAdaptersAddress docs. There is an example which is quite clear:
- call
GetAdaptersAddresses
with increasing buffer size until the results fit into the allocated memory chunk - iterate over returned linked list of PIP_ADAPTER_ADDRESSES (
pCurrAddresses
):- iterate over PIP_ADAPTER_UNICAST_ADDRESS*
pCurrAddresses->FirstUnicastAddresses
linked list to retrieve all IP addresses (SOCKET_ADDRESS and its LPSOCKADDRAddress->lpSockAddr
member); it is asockaddr_in*
orsockaddr_in6*
depending onlpSockAddr.sa_family
(AF_INET
orAF_INET6
)
- iterate over PIP_ADAPTER_UNICAST_ADDRESS*
At this moment, I have IP addresses.
How about netmask/prefix? This seems to be what PIP_ADAPTER_PREFIX_XP (and its SOCKET_ADDRESS Address
field) is about. That is another linked list, accessible from PIP_ADAPTER_ADDRESSES
through ->FirstPrefix
.
Unhelpfully, the docs state that prefixes may not be ordered the same as addresses:
In addition, the linked IP_ADAPTER_UNICAST_ADDRESS structures pointed to by the FirstUnicastAddress member and the linked IP_ADAPTER_PREFIX structures pointed to by the FirstPrefix member are maintained as separate internal linked lists by the operating system. As a result, the order of linked IP_ADAPTER_UNICAST_ADDRESS structures pointed to by the FirstUnicastAddress member does not have any relationship with the order of linked IP_ADAPTER_PREFIX structures pointed to by the FirstPrefix member.
What is the solution? Am I missing something obvious?
Some code (e.g. zeroMQ) only uses, for each adapter, the first unicast address and the first prefix. Is that approach safe? Am I going to miss interfaces?