Functions for obtaining the IP address and NIC status of a local machine in Linux

Source: Internet
Author: User
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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.