Get subnet mask from GetAdapterAddresses ()

I use the GetAdapterAddresses() method to get the IP addresses of all interfaces in the system.

I need to find the broadcast address of each interface. I can calculate this using the IP address and subnet mask, but I do not see the subnet mask in the IP_ADAPTER_ADDRESSES structure.

Is there a way to get the subnet mask using GetAdapterAddresses() ?

+7
c ++ c winapi
source share
1 answer

GetAdapterAddresses() provides subnet masks only for Vista and later.

When navigating through the unicast addresses pointed to by the FirstUnicastAddress field of the FirstUnicastAddress record, IP_ADAPTER_UNICAST_ADDRESS includes the OnLinkPrefixLength field. This field is not available on systems prior to Vista. This field is the length of the subnet mask in bits. For unicast IPv4 addresses, you can use ConvertLengthToIpv4Mask() to convert the OnLinkPrefixLength value to a subnet mask, which you can then use to mask a unicast IPv4 address as needed.

On systems prior to Vista, select GetIpAddrTable() for a list of available IPv4 interfaces. The MIB_IPADDRROW contains the dwAddr field for the IPv4 address, the dwMask field for the subnet mask, and the dwBCastAddr field for the broadcast address. You can scroll through this table by looking at each unicast IPv4 address specified by GetAdapterAddresses() , and then you will have associated subnet masks and broadcast IP addresses.

+10
source share

All Articles