3

I'm planning on developing a desktop application that can provide various features depending on which networks a system is connected to, such as setting up routing.

So I'm looking for a way to identify and assign a human-friendly label to the network that an interface / adapter is connected to. By that I mean labels like "Home Wi-Fi", "XYZ's Wi-Fi", "Office ethernet", or in the case of virtual interfaces, things like "XYZ Company VPN", "VPN service X's New York servers", "VPN service Y's Paris server", and so on. I expect users of this app to be able to label the networks themselves. I just need a way of reliably identifying them and telling them apart.

To that end, what kind of information can I get from a network interface to recognize a specific network, and distinguish multiple networks from each other?

In terms of things that don't require making outgoing requests, off the top of my head there's:

  • Physical interface type (ethernet, Wi-Fi, etc.)
  • Local IP address & netmask
  • Gateway
  • DNS servers

...though those don't seem like a reliable way of accomplishing this. Different unrelated networks can share the same local IP address range, gateway IP, and even DNS servers.

That leaves things that require making some outgoing requests, like:

  • public IP address
  • local domain name (if any)
  • seeing what other systems that are connected on the same network (perhaps identified by MAC address)

Those don't strike me as super reliable either, since public IP addresses can change, and not every local network has its own domain name or servers that are reliably connected to it.

What I'd prefer to avoid is any OS or application specific approach, like looking at system settings or the settings for a third party VPN client, since there's simply too many different ways to configure this sort of thing across all the major operating systems and their networking clients.

Is there an approach that works reliably? Perhaps some combination of all of these?

4
  • ipconfig /all (ifconfig) is a good starting point and gives me what I need.
    – John
    May 20, 2022 at 17:30
  • where would you expect to see these labels appear? it sounds like you would need an custom application that would store your custom labels and display them to you. that application could allow you to create labels and associate them with IP ranges or adapters. so how do you expect this all to manifest? May 20, 2022 at 17:38
  • @FrankThomas I've updated my question to provide some more context about what I'm looking to do (develop an app)
    – Bri Bri
    May 20, 2022 at 18:08
  • 2
    In addition to the information you listed, you can also get the gateway's MAC address, the DHCP server's (or DHCP relay agent's) IP address and MAC address, and if it's a Wi-Fi network, the SSID (name) of the network, and the BSSID (MAC address) of the AP. All of these will be known to the appropriate subsystems of the device you're on, without generating any queries. However, there might not be widely-deployed cross-platform APIs for some of these things, even among the generally POSIX-y, Sockets-y OSes your question seems to presume. You might also want to look at LLDP and CDP.
    – Spiff
    May 20, 2022 at 19:14

1 Answer 1

4

The best example of this I've seen is Cisco Jabber's (instant messaging/phone software) feature called Network Location.

Locations are defined as each unique pair of subnet and default gateway MAC address (not local MAC).

Jabber locations

Using the gateway's MAC address lets you see if someone is on VPN or at the office for example, even if the subnet is the same.

5
  • That's a great idea, though I think not one I can utilize for VPNs. So far when I try to query the MAC address of a VPN's gateway, there's no entry for it in my system's address translation table, which makes sense since my system isn't actually communicating directly with it.
    – Bri Bri
    May 20, 2022 at 19:04
  • @BriBri On the contrary, your system must know the MAC address if the vpn interface is up, based on how packets are routed at layer 2. You should be able to see the gateway mac with arp -a. Or in powershell, list all the gateway macs with (Get-NetIPConfiguration).IPv4DefaultGateway.NextHop | foreach { Get-NetNeighbor -IPAddress $_ }
    – Cpt.Whale
    May 20, 2022 at 19:25
  • 1
    @Bri: Your system is "directly" communicating with the gateway (it's literally the job of the VPN to make it look like you're directly connected), the lack of a MAC entry is because of a different reason – most corporate and commercial VPNs create a "layer 3" tunnel (which behaves like a point-to-point serial connection) instead of a "layer 2" one (which would be Ethernet-like). There are both kinds, though, L2 (MAC) tunnels are more commonly seen in "personal" VPNs such as Zerotier or OpenVPN that link your own devices rather than providing Internet access. May 20, 2022 at 19:33
  • 1
    (Point-to-point links don't use explicit layer2 addresses since there's only one possible destination: the other end of the link. Physical links using PPP or similar used to be more common in the past before Ethernet took over.) But I'd also caution about the gateway MAC addresses even when they are present; every network that uses VRRP for redundancy has the same "virtual" gateway MAC (± last digit or two), similarly gateways using HSRP also have fixed MACs, etc. May 20, 2022 at 19:40
  • @user1686 I've learned something new! In a case like that, a pair of subnet and null gateway mac is probably still enough to identify a device location
    – Cpt.Whale
    May 20, 2022 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .