# Include <stdlib. h>
# Include <stdio. h>
# Include
<String. h>
# Include <unistd. h>
# Include
<Sys/IOCTL. h>
# Include <net/If. h>
# Include
<ARPA/inet. h>
Int main (INT argc, char ** argv)
{
Int fd_recv =-1;
Struct ifreq IFR;
Fd_recv =
Socket (af_inet, sock_packet, htons (0x0003 ));
If (fd_recv <0 ){
Fprintf (stderr, "PACKET socket error/N ");
Exit (-1 );
}
Strcpy (IFR. ifr_name, "eth0 ");
If
(IOCTL (fd_recv, siocgifflags, & IFR) <0 ){
Fprintf (stderr, "IOCTL siocgifflags error/N ");
}
Else {
/* Network cable plug-in, iff_running set, else not */
Fprintf (stderr, "iff_running-> % d/N", IFR. ifr_flags &
Iff_running? 1: 0 );
/* Ifconfig eth0 up, iff_up
Set, else not */
Fprintf (stderr, "iff_up-> % d/N ",
IFR. ifr_flags & iff_up? 1: 0 );
}
Close (fd_recv );
Return 0;
}
Of course, this Code also requires driver support, if
The iff_running and iff_up flag will be set for a complete driver.
If ifconfig eth0 down,
Iff_running and iff_up will be unset; otherwise, iff_up will be set.
If the network cable is disconnected, iff_running will be unset and plugged in
The network will be set.