Skip to content
Prev Previous commit
Next Next commit
Add comment with explanation
  • Loading branch information
simonrozsival committed Sep 29, 2022
commit 5d63e0380d5402001f98320975fbf5b185b90852
14 changes: 9 additions & 5 deletions src/native/libs/System.Native/pal_interfaceaddresses.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,16 @@ int32_t SystemNative_GetNetworkInterfaces(int32_t * interfaceCount, NetworkInter
// where we first write out NetworkInterfaceInfo entries immediately followed by
// IpAddressInfo list.
#if TARGET_ANDROID
int interfacesCount = count;
// Since Android API 30, getifaddrs returns only AP_INET and AF_INET6 addresses and
// we do not get any AP_PACKET addresses and so count == ip4count + ip6count.
// We need to make sure that the memoryBlock is large enough to hold all interfaces
// and all addresses without any overlap between interfaceList and addressList.
int interfaceCountUpperLimit = count;
#else
int interfacesCount = count - ip4count - ip6count;
int interfaceCountUpperLimit = count - ip4count - ip6count;
#endif
size_t interfacesAndAddressesCount = (size_t)(interfacesCount + ip4count + ip6count);
void * memoryBlock = calloc(interfacesAndAddressesCount, sizeof(NetworkInterfaceInfo));
size_t entriesCount = (size_t)(interfaceCountUpperLimit + ip4count + ip6count);
void * memoryBlock = calloc(entriesCount, sizeof(NetworkInterfaceInfo));
if (memoryBlock == NULL)
{
errno = ENOMEM;
Expand All @@ -370,7 +374,7 @@ int32_t SystemNative_GetNetworkInterfaces(int32_t * interfaceCount, NetworkInter
ifaddrsEntry = head;
*interfaceList = nii = (NetworkInterfaceInfo*)memoryBlock;
// address of first IpAddressInfo after all NetworkInterfaceInfo entries.
*addressList = ai = (IpAddressInfo*)(nii + interfacesCount);
*addressList = ai = (IpAddressInfo*)(nii + interfaceCountUpperLimit);

while (ifaddrsEntry != NULL)
{
Expand Down