* * *

Author Topic: How to get the Ethernet subnet mask  (Read 258 times)

AnthonyTekatch

  • New member
  • *
  • Posts: 20
How to get the Ethernet subnet mask
« on: October 10, 2017, 12:14:43 pm »
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

  • Sr. Member
  • ****
  • Posts: 333
    • Lebeau Software
Re: How to get the Ethernet subnet mask
« Reply #1 on: October 10, 2017, 09:41:31 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.

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).
« Last Edit: October 10, 2017, 11:19:22 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) open source project - Admin, Developer

AnthonyTekatch

  • New member
  • *
  • Posts: 20
Re: How to get the Ethernet subnet mask
« Reply #2 on: October 10, 2017, 11:27:17 pm »
Great!! Thanks for all the leads.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus