Forum > General

How to get the Ethernet subnet mask

(1/1)

AnthonyTekatch:
Is there a way to get the subnet mask (netmask) of an Ethernet interface using the Ararat Synapse tools (or something else)?

I would like to make broadcast but some networks have a subnet mask of 255.255.255.0 and some have a netmask of 255.255.0.0 . Knowing the subnet would help me determine what the broadcast IP address should be.

Remy Lebeau:

--- Quote from: AnthonyTekatch on October 10, 2017, 12:14:43 pm ---I would like to make broadcast but some networks have a subnet mask of 255.255.255.0 and some have a netmask of 255.255.0.0 . Knowing the subnet would help me determine what the broadcast IP address should be.

--- End quote ---

I don't know if Synapse offers anything for this, but it is not hard to get the information using platform APIs directly.

Most *Nix platforms have either getifaddrs() or ioctl(SIOCGIFCONF), both of which report each interface's IP address, subnet mask (if any), and broadcast IP (if any).

On Windows, you have a few different options.  You can:

- create and bind an IPv4 UDP socket to the desired adapter and pass it to WSAIoctl(SIO_GET_BROADCAST_ADDRESS), which returns the bound adapter's broadcast IP (if any).

- create an IPv4 UDP socket and pass it to WSAIoctl(SIO_GET_INTERFACE_LIST/_EX), which reports each interface's IPv4 address, subnet mask, and broadcast IP (for IPv6, you can use SIO_ADDRESS_LIST_QUERY instead, but it only returns interface IP addresses, nothing else).

- use GetAdaptersInfo() to get each adapter's IPv4 address(es) and subnet mask(s) (IPv6 not supported).  You will have to calculate the broadcast IPs manually.

- on XP+, use GetAdaptersAddresses() to get each adapter's IPv4 and IPv6 address(es).  On Vista+, it also includes IPv4 subnet masks.  On XP, you can get IPv4 subnet masks by matching the reported IPv4 addresses to those reported by GetIpAddrTable().  You will have to calculate the broadcast IPs manually.

Indy has a TIdStack.GetLocalAddressList() class method that currently uses getifaddrs() and GetAdapters(Info|Addresses)() to retrieve IPv4/IPv6 interface addresses, and IPv4 subnet masks when possible (no broadcast IPs at this time).

AnthonyTekatch:
Great!! Thanks for all the leads.

Navigation

[0] Message Index

Go to full version