How to find out which network adapter is connected to the Internet?

Consider the following setup: Windows PC with LAN and WiFi (standard for any new laptop). Each of the interfaces can be connected or disconnected from the network. I need a way to determine which of the adapters is the one that is connected to the Internet - in particular, if they are both connected to different networks, one with and without an Internet connection.

My current solution involves using the IPHelper GetBestInterface "function and giving it the IP address" 0.0.0.0 ".

Do you have any other solutions that you could offer to this problem?

Following some answers, let me clarify:

  • I need this because I have a product that I have to choose which adapter to bind to. I don’t have the ability to control the configuration of the network or host on which the product will run, and therefore I need the most reliable solution with the highest possible assumptions.
  • I need to do this in code, as this is part of the product.

@Chris Upchurch: It makes me depend on how google.com works (usually not a problem) and on any personal firewall that can be set to allow ping.

@ It must be, as Steve Moon said, relying on the adapter address, this is pretty risky because you make a lot of assumptions about setting up the internal network.

@Steve Moon: looking at the routing table sounds like a good idea, but instead of applying the routing logic myself, I'm trying to use "GetBestInterface" as described above. I believe that he should do exactly what you stated in your answer, but I'm not entirely sure. The reason that I am reluctant to implement my own “routing logic” is because I will be more likely to be mistaken if I use a library / API written and tested by more “tough” network users.

+4
source share
8 answers

Technically, there is no "Internet connection." The real question is which interface is routed to the desired address. Right now you are requesting a “default route” - one that applies if there is no specific route to the destination. But you are ignoring any specific routes.

Fortunately, for 99.9% of home users this will do the trick. They are unlikely to have much of the routing table, and GetBestInterface will automatically prefer wired wireless communications — so you should be good. Throw the override option for .1% of the cases you mess up and call it day.

But for corporate use, you must use GetBestInterface for a specific destination - otherwise you will have problems if someone is on the same local network as your destination (which means that you must use the "internal" interface, and not “external”) or has a specific route to your destination (for example, my internal network can connect to your target network).

And again, I'm not sure what you plan to do with this “Internet-connected” adapter, so this may not be very.

+4
source

I would look at the routing table. No matter which NIC has a route of 0.0.0.0 and is included and has the lowest metric, it is nic, which currently sends packets to the Internet.

So, in my case, the top one is an "internet nickname."

IPv4 Route Table =========================================================================== Active Routes: Network Destination Netmask Gateway Interface Metric 0.0.0.0 0.0.0.0 10.0.0.10 10.0.0.51 20 0.0.0.0 0.0.0.0 10.0.0.10 10.0.0.50 25 (much other stuff deleted) 

Another alternative is ping or GetBestInterface 4.2.2.2 - this is an old and respectable DNS server, which is currently supported by GTEI; Sprint earlier, if I remember correctly.

+2
source

Vista appears to have new interfaces that allow you to request an Internet connection and much more. Take a look at the NLM interfaces and, in particular, at the INetworkConnection - you can ask if the network connection has an Internet connection using the GetConnectivity method.

See also: Network knowledge in Windows Vista Unfortunately, this is only available for Vista, so for XP I will have to keep the original heuristic.

+1
source

Ping google.com although every network card.

0
source

Start> Run> cmd.exe (this works in XP and Vista): ipconfig /all

Displays all the information about the interfaces of your computer. The "open" interface must have a public IP address. For starters, it should not be 192.168.xx or 10.xxx :)

0
source

Look at the routing table? Typically, if you do not route between networks on Windows (which is possible, but unusual for a client computer these days), an interface that has a default route will have an Internet connection.

There was no detail in your question about why or what you are doing, so I cannot provide any features. The "route" command line tool may be of some help, but there are probably libraries for any programming language that you use to view the routing table.

You cannot rely on the IP address of the interface (for example, if the address RFC-1918 [192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8] is not the Internet), since most sites have some kind of NAT a firewall or proxy installation, and the Internet interface is really located on the private server, which takes you to the Internet.

UPDATE: Based on your additional information, it looks like you have a decent solution. I'm not sure about 0.0.0.0, as this is a borderline case for an IP address - it might be ok on your particular platform / language combination. Sounds (from the API description), how could you just indicate an address, so why not name some address on the Internet, for example? Is the IP address of your website or something more random, like 65.66.67.68? Just remember to choose one of the rfc-1918 addresses or the localhost range (127.0.0.0/8) or multicast, any other reserved range and any address that allows .mil or .gov (as long as it doesn't work) It sounds like getbestinterface sends any traffic, it suck to find out if the feds will break your door ... :)

0
source

running traceroute on some public site will show you. Of course, there may be more than one interface that takes you there.

0
source

Looking at the network point of view, there can be routing to the Internet at any time. If things like the spanning tree protocol are enabled on the switch, you may find that what might have been started from the routing map might not be anymore.

0
source

Source: https://habr.com/ru/post/1276515/


All Articles