Http://blog.chinaunix.net/space.php? Uid = 20357359 & Do = Blog & cuid = 1798479
Checks the connection status of the network adapter and network adapter in Linux, and sends it to the socket using IOCTL.Siocethtool
Command.
Link_stat.c
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <fcntl. h>
# Include <errno. h>
# Include <sys/IOCTL. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <Linux/If. h>
# Include <Linux/sockios. h>
# Include <Linux/ethtool. h>
Int get_netlink_status (const char * if_name );
Int main ()
{
If (getuid ()! = 0)
{
Fprintf (stderr, "Netlink status check need root power. \ n ");
Return 1;
}
Printf ("net link status: % d \ n", get_netlink_status ("eth0 "));
Return 0;
}
// If_name like "ATH0", "eth0". Notice: Call this function
// Need root privilege.
// Return value:
//-1 -- error, details can check errno
// 1 -- interface link up
// 0 -- interface link down.
Int get_netlink_status (const char * if_name)
{
Int skfd;
Struct ifreq IFR;
Struct ethtool_value edata;
Edata. cmd = ethtool_glink;
Edata. Data = 0;
Memset (& IFR, 0, sizeof (IFR ));
Strncpy (IFR. ifr_name, if_name, sizeof (IFR. ifr_name)-1 );
IFR. ifr_data = (char *) & edata;
If (skfd = socket (af_inet, sock_dgram, 0) <0)
Return-1;
If (IOCTL (skfd, siocethtool, & IFR) =-1)
{
Close (skfd );
Return-1;
}
Close (skfd );
Return edata. Data;
}
-----------------------------------------------------------
# Ifconfig eth0
Eth0 link encap: Ethernet hwaddr 00: 25: 35: 68: CC: D6
Inet ADDR: 192.168.1.168 bcast: 192.168.1.255 mask: 255.255.255.0
Inet6 ADDR: fe80: 215: c5ff: fe18: ccd6/64 scope: Link
Up broadcast running Multicast MTU: 1500 Metric: 1
RX packets: 130722 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 112560 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 FIG: 1000
RX Bytes: 104371099 (99.5 MIB) TX Bytes: 20518584 (19.5 MIB)
Interrupt: 16
The "running" command indicates that the network card is normally connected to the network card. If you unplug the network card and run this command, you will find that the "running" command is no longer running.
My goal is to use C LanguageProgramIn Linux, the popen/pclose process pipeline is provided to allow C and shell to easily interact. However, you must set permissions to avoid security risks. Let's talk a little bit about it. Refer to C below.CodeUse shell commands to check the connectivity between the nic and the Network:
Netstat. c
# Include <unistd. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <string. h>
/*************************************** *******************************
* Function name: getnetstat
* Function Description: checks whether the network connection is disconnected.
* Input parameters:
* Output parameter: None
* Return value: normal link 1, disconnection returns-1
* Other Instructions: This program requires the superuser permission to successfully call the ifconfig command.
* Modification date version number modifier modification content
*---------------------------------------------------------------------
* 2010/04/02 V1.0 eden_mgqw
**************************************** *******************************/
Int getnetstat ()
{
Char buffer [bufsiz];
File * read_fp;
Int chars_read;
Int ret;
Memset (buffer, 0, bufsiz );
Read_fp = popen ("ifconfig eth0 | grep running", "R ");
If (read_fp! = NULL)
{
Chars_read = fread (buffer, sizeof (char), BUFSIZ-1, read_fp );
If (chars_read> 0)
{
Ret = 1;
}
Else
{
Ret =-1;
}
Pclose (read_fp );
}
Else
{
Ret =-1;
}
Return ret;
}
Int main ()
{
Int I = 0;
I = getnetstat ();
Printf ("\ nnetstat = % d \ n", I );
Return 0;
}