tags:

views:

722

answers:

6

I have a C# application that should only be used when the network is down, but am afraid users will just unplug the network cable in order to use it.

Is there a way to detect if the network cable has been unplugged?

Thanks

A: 

The network card will report this as a state. Tools like ethtool can display this (Link up), but that is only available for Linux/Unix.

If you can enumerate the installed network cards with a Windows API, I'm sure you'll find the flag for "link up" somewhere in there.

Aaron Digulla
A: 

If I really wanted to use your application and whether it will work depends on something like this, I would always be able to find a way to trick your application. Are you sure there's no better solution?

Martijn
I don't think the users of this app would go to the trouble to try and 'trick' it as using it is the quicker option to another program. The point of this app is for it to only be used when the network is down, which is very rare. I just need a way to ensure that the users will not simply pull out the network cable and use this constantly. These are not people with a high level knowledge of computers - I just need an indication that they have removed the network cable to give the illusion the network is down.
halz
+4  A: 

In my humble opinion, there is no certain way to distinguish between a network down and an unplugged cable. And even if there is a way, there is also a way to work around it.

Let's assume that you have a solution and let's look at some situations:

  • There is no network traffic, the cable is not unplugged from the computer: it may be unplugged at the other end.
  • There is no network traffic, the cable is unplugged: but this has always been the case, the laptop is connected via Wi-Fi, which is down at the moment.
  • There are several network interfaces, only the one connected to WAN is down: should your app work?
  • The network is actually down, in the sense you mean: someone has managed to reboot the router continuously for using your app.
mouviciel
Exactly. So many things can make your network go down manually.
Ólafur Waage
+2  A: 

Some network drivers are able to detect this. However you'd need to use unmanaged code to access them from C# (which may be very difficult/impossible) and the solution may not be reliable for all network adapters.

Alex Angas
Interesting... got any links for such H/W, APIs etc ?
timday
I don't sorry, but I've seen it on major brand NICs such as Intel.
Alex Angas
+2  A: 

You could use IsNetworkAlive(). Although technically it doesn't check link state, it's probably better since it can detect wireless and dialup connectivity as well. Here's an example:

using System.Runtime.InteropServices;
class Program
{
    [DllImport("sensapi.dll")]
    static extern bool IsNetworkAlive(out int flags);

    static void Main(string[] args)
    {
        int flags;
        bool connected = IsNetworkAlive(out flags);

    }
}

The flags param returns whether the connection is to the internet or just a LAN. I'm not 100% sure how it knows, but I'd bet it just looks to see if there is a default gateway set.

Jeff Mc
Thank you, I think this is the closest it is going to get considering all of the ways to work around this.
halz
A: 

How about pinging the default gateway?

There is some code here that gets the default gateway from the registry.

Harry Lime