Programmatically detect network cable unpluged

Programmatically detect network cable unpluged

Post by Marc Vacho » Fri, 06 Jun 2003 23:58:01



I'm trying to programmatically determine when my Linux box (more
specifically RedHat 7.3) has been disconnected from the network (ie cable
unplugged).

I don't want to use command line tools like ifconfig or similar. Is there a
library I can use to query a state or register for system events? Is there a
file I can parse?

Cheers,

Marc

 
 
 

Programmatically detect network cable unpluged

Post by Grant Edwar » Sat, 07 Jun 2003 00:30:13



> I'm trying to programmatically determine when my Linux box
> (more specifically RedHat 7.3) has been disconnected from the
> network (ie cable unplugged).

> I don't want to use command line tools like ifconfig or
> similar. Is there a library I can use to query a state or
> register for system events? Is there a file I can parse?

I don't know the answer specifically, but here's a general
hint:  If you know ifconfig can display the info you need, look
at the source for ifconfig and see how gets that info.

--
Grant Edwards                   grante             Yow!  Why was I BORN?
                                  at              
                               visi.com            

 
 
 

Programmatically detect network cable unpluged

Post by braintrus » Sat, 07 Jun 2003 08:46:10


#include <sys/ioctl.h>
#include <net/if.h>

struct ifreq ifr;
strcpy(ifr.ifr_name, "eth0"); // <== Interface name goes here
if(ioctl(sock,SIOCGIFFLAGS,&ifr) == 0)
{
 if(ifr.ifr_ifru.ifru_flags & IFF_UP)
   printf("eth0 is up\n");
  else
   printf("eth0 is down\n");

Quote:}

sock is any open socket. You can just...

struct protoent *proto = getprotobyname("TCP");
int sock = socket(PF_INET,SOCK_STREAM,proto->p_proto);

Nice code to look at is part of net-tools in lib/interface.c

Gary


> I'm trying to programmatically determine when my Linux box (more
> specifically RedHat 7.3) has been disconnected from the network (ie cable
> unplugged).

> I don't want to use command line tools like ifconfig or similar. Is there
> a library I can use to query a state or register for system events? Is
> there a file I can parse?

> Cheers,

> Marc

 
 
 

Programmatically detect network cable unpluged

Post by Gary Desrosier » Sat, 07 Jun 2003 08:47:16


Correction...You wanted to know if it was unplugged. Right? Okay, same
check but replace IFF_UP with IFF_RUNNING. The interface could be up even
if it's unplugged. I just checked the code here with IFF_RUNNING and it
works.

> #include <sys/ioctl.h>
> #include <net/if.h>

> struct ifreq ifr;
> strcpy(ifr.ifr_name, "eth0"); // <== Interface name goes here
> if(ioctl(sock,SIOCGIFFLAGS,&ifr) == 0)
> {
>  if(ifr.ifr_ifru.ifru_flags & IFF_UP)
>    printf("eth0 is up\n");
>   else
>    printf("eth0 is down\n");
> }

> sock is any open socket. You can just...

> struct protoent *proto = getprotobyname("TCP");
> int sock = socket(PF_INET,SOCK_STREAM,proto->p_proto);

> Nice code to look at is part of net-tools in lib/interface.c

> Gary


>> I'm trying to programmatically determine when my Linux box (more
>> specifically RedHat 7.3) has been disconnected from the network (ie cable
>> unplugged).

>> I don't want to use command line tools like ifconfig or similar. Is there
>> a library I can use to query a state or register for system events? Is
>> there a file I can parse?

>> Cheers,

>> Marc

 
 
 

Programmatically detect network cable unpluged

Post by Kevin Easto » Sat, 07 Jun 2003 18:57:24



> Correction...You wanted to know if it was unplugged. Right? Okay, same
> check but replace IFF_UP with IFF_RUNNING. The interface could be up even
> if it's unplugged. I just checked the code here with IFF_RUNNING and it
> works.

Not all drivers for 10mbit cards do this, because it wasn't possible to
detect whether you were unplugged, or plugged into a 10base2 network (if
the NIC had such an interface).  Other than the lack of traffic, that
is.

        - Kevin.

 
 
 

Programmatically detect network cable unpluged

Post by zeljk » Sun, 08 Jun 2003 05:10:29



> Correction...You wanted to know if it was unplugged. Right? Okay, same
> check but replace IFF_UP with IFF_RUNNING. The interface could be up even
> if it's unplugged. I just checked the code here with IFF_RUNNING and it
> works.

hm...this works ok with my eepro100 (intel) card but I cannot get result
with rtl8139too (realtek) , with or without cable it always returns same
result (cannot check IFF_RUNNING, it always returns that cable is plugged)