Article title: functions for obtaining the local IP address and NIC status in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
My only requirement is to determine whether a certain network is available. The system has the LAN eth0 and 3G dial-up links ppp0, and different sockets need to be created on their respective networks. write the following functions, the network name can be used to determine whether the network is available.
// Detect network connections
// RouteName: Network Connection name, such as ppp0 and eth0
// Return value: 0 is returned if the network is normal, and-1 is returned if an exception occurs.
Int CheckNetLink (const char * routeName)
{
Register int fd, intrface;
Struct ifreq buf [16];
Struct ifconf ifc;
If (fd = socket (AF_INET, SOCK_DGRAM, 0)> = 0)
{
Ifc. ifc_len = sizeof (buf );
Ifc. ifc_buf = (caddr_t) buf;
If (! Ioctl (fd, SIOCGIFCONF, (char *) & ifc ))
{
Intrface = ifc. ifc_len/sizeof (struct ifreq );
While (intrface --> 0)
{
If (strcmp (buf [intrface]. ifr_name, routeName) = 0)
{
Ioctl (fd, SIOCGIFFLAGS, (char *) & buf [intrface]);
If (buf [intrface]. ifr_flags & IFF_UP) & (buf [intrface]. ifr_flags & IFF_RUNNING ))
{
// Printf ("\ n % s is UP & RUNNING \ n", routeName );
Return OK;
}
Else
{
// Printf ("\ n % s is DOWN \ n", routeName );
Return ERROR;
}
}
}
// Printf ("\ n % s is not exist \ n", routeName );
Return ERROR;
}
}
Return ERROR;
}