<p>Consider the following setup:
A windows PC with a LAN interface and a WiFi interface (the standard for any new laptop). Each of the interfaces might be connected or disconnected from a network. I need a way to determine which one of the adapters is the one connected to the internet - specifically, in case they are both connected to different networks, one with connection to the internet and one without.</p>

<p>My current solution involves using IPHelper's "<a href="http://msdn.microsoft.com/en-us/library/aa365920(VS.85).aspx" rel="noreferrer">GetBestInterface</a>" function and supplying it with the IP address "0.0.0.0".</p>

<p>Do you have any other solutions you might suggest to this problem?</p>

<p>Following some of the answers, let me elaborate:</p>

<ul>
<li>I need this because I have a product that has to choose which adapter to bind to. I have no way of controlling the setup of the network or the host where the product will run and so I need a solution that is as robust as possible, with as few assumptions as possible.</li>
<li>I need to do this in code, since this is part of a product.</li>
</ul>

<p>@Chris Upchurch: This makes me dependent on google.com being up (usually not a problem) and on any personal firewall that might be installed to allow pinging.</p>

<p>@Till: Like Steve Moon said, relying on the adapter's address is kind of risky because you make a lot of assumptions on the internal network setup.</p>

<p>@Steve Moon: Looking at the routing table sounds like a good idea, but instead of applying the routing logic myself, I am trying to use "GetBestInterface" as described above. I believe what it should do is exactly what you outlined in your answer, but I am not really sure. The reason I'm reluctant to implement my own "routing logic" is that there's a better chance that I'll get it wrong than if I use a library/API written and tested by more "hard-core" network people.</p>

[{'answer_id': 38866, 'author': 'Chris Upchurch', 'author_id': 2600, 'author_profile': 'https://Stackoverflow.com/users/2600', 'pm_score': -1, 'selected': False, 'text': '<p>Ping google.com though each NIC.</p>\n'}, {'answer_id': 38872, 'author': 'Till', 'author_id': 2859, 'author_profile': 'https://Stackoverflow.com/users/2859', 'pm_score': 0, 'selected': False, 'text': '<p>Start > Run > cmd.exe (this works in XP and Vista): <code>ipconfig /all</code></p>\n\n<p>This displays all info about the interfaces in your computer. The "public" facing interface should have a public IP address. For starters, <strong>it should not</strong> be 192.168.x.x or 10.x.x.x :)</p>\n'}, {'answer_id': 38876, 'author': 'Steve Moon', 'author_id': 3660, 'author_profile': 'https://Stackoverflow.com/users/3660', 'pm_score': 0, 'selected': False, 'text': '<p>Look at the routing table? Generally, unless you\'re routing between the networks in windows (which is possible, but unusual for a client computer these days) the interface that holds the default route is going to have the Internet connection.</p>\n\n<p>Your question didn\'t detail why or what you\'re doing this with so I can\'t provide any specifics. The command line tool "route" may be of some help, but there are probably libraries for whatever programming language you\'re using to look at the routing table.</p>\n\n<p>You can\'t rely on the IP address of the interface (e.g., assuming an RFC-1918 address [192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8] isn\'t the internet) since most sites have some kind of NATed firewall or proxy setup and the "internet" interface is really on a "private" lan that gets you out to the Internet.</p>\n\n<p>UPDATE: Based on your further information, it sounds like you have a decent solution. I\'m not so sure about the choice of 0.0.0.0 since that\'s a boundary case for IP address -- might be OK on your particular mix of platform/language. Sounds (from the API description) like you could just specify an address, so why not some address known to be on the Internet, e.g. the IP address of your web site, or something more random like 65.66.67.68? Just make sure not to pick 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 resolves to a .mil or .gov (while it doesn\'t sound like getbestinterface sends any traffic, it would suck to find out by having the feds break your door down... :)</p>\n'}, {'answer_id': 38954, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 0, 'selected': False, 'text': '<p>running traceroute to some public site will show you.  Of course, there may be more than one interface that would get you there.</p>\n'}, {'answer_id': 39209, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 3, 'selected': True, 'text': '<p>Technically, there is no "connected to the Internet". The real question is, which interface is routeable to a desired address. Right now, you\'re querying for the "default route" - the one that applies if no specific route to destination exists. But, you\'re ignoring any specific routes.</p>\n\n<p>Fortunately, for 99.9% of home users, that\'ll do the trick. They\'re not likely to have much of a routing table, and GetBestInterface will automatically prefer wired over wireless - so you should be good. Throw in an override option for the .1% of cases you screw up, and call it a day. </p>\n\n<p>But, for corporate use, you should be using GetBestInterface for a specific destination - otherwise, you\'ll have issues if someone is on the same LAN as your destination (which means you should take the "internal" interface, not the "external") or has a specific route to your destination (my internal network could peer with your destination\'s network, for instance).</p>\n\n<p>Then again, I\'m not sure what you plan to do with this adapter "connected to the Internet", so it might not be a big deal.</p>\n'}, {'answer_id': 45932, 'author': 'Hershi', 'author_id': 1596, 'author_profile': 'https://Stackoverflow.com/users/1596', 'pm_score': 1, 'selected': False, 'text': '<p>Apparently, in Vista there are new interfaces that enable querying for internet connectivity and more. Take a look at the <a href="http://msdn.microsoft.com/en-us/library/aa370799(VS.85).aspx" rel="nofollow noreferrer">NLM Interfaces</a> and specifically at <a href="http://msdn.microsoft.com/en-us/library/aa370751(VS.85).aspx" rel="nofollow noreferrer">INetworkConnection</a> - you can specifically query if the network connection has internet connectivity using the GetConnectivity method.</p>\n\n<p>See also: <a href="http://msdn.microsoft.com/en-us/library/ms697388.aspx" rel="nofollow noreferrer">Network Awareness on Windows Vista</a>\nUnfortunately, this is only available on Vista, so for XP I\'d have to keep my original heuristic.</p>\n'}, {'answer_id': 48460, 'author': 'quux', 'author_id': 2383, 'author_profile': 'https://Stackoverflow.com/users/2383', 'pm_score': 1, 'selected': False, 'text': "<p>I'd look at the routing table. Whichever NIC has an 0.0.0.0 route AND is enabled AND has the lowest metric, is the nic that's currently sending packets to the internet.</p>\n\n<p>So in my case, the top one is the 'internet nic'.</p>\n\n<pre><code>IPv4 Route Table\n===========================================================================\nActive Routes:\nNetwork Destination        Netmask          Gateway       Interface  Metric\n          0.0.0.0          0.0.0.0        10.0.0.10        10.0.0.51     20\n          0.0.0.0          0.0.0.0        10.0.0.10        10.0.0.50     25\n(much other stuff deleted)\n</code></pre>\n\n<p>Another alternative is to ping or GetBestInterface 4.2.2.2 - this is an old and venerable DNS server, currently held by GTEI; formerly by Sprint if I remember right.</p>\n"}, {'answer_id': 71878, 'author': 'Tubs', 'author_id': 11924, 'author_profile': 'https://Stackoverflow.com/users/11924', 'pm_score': 0, 'selected': False, 'text': '<p>Looking at the network point of view, either could be routing to the "internet" at any time.  If things like spanning tree protocol are enabled on a switch then you may find that what may have been the routing card to begin with may not be anymore.</p>\n'}]
['https://Stackoverflow.com/questions/38864', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1596/']