Linux ioctl function to obtain the local IP Address

Source: Internet
Author: User

// Ifconf is usually used to save all interface information
// If. h
Struct ifconf
{
Int ifc_len;/* size of buffer */
Union
{
Char * ifcu_buf;/* input from user-> kernel */
Struct ifreq * ifcu_req;/* return from kernel-> User */
} Ifc_ifcu;
};
# Define ifc_buf ifc_ifcu.ifcu_buf/* buffer address */
# Define ifc_req ifc_ifcu.ifcu_req/* array of structures */

// Ifreq is used to save the information of an interface.
// If. h
Struct ifreq {
Char ifr_name [ifnamsiz];
Union {
Struct sockaddr ifru_addr;
Struct sockaddr ifru_dstaddr;
Struct sockaddr ifru_broadaddr;
Short ifru_flags;
Int ifru_metric;
Caddr_t ifru_data;
} Ifr_ifru;
};
# Define ifr_addr ifr_ifru.ifru_addr
# Define ifr_dstaddr ifr_ifru.ifru_dstaddr
# Define ifr_broadaddr ifr_ifru.ifru_broadaddr

The above two structures seem complicated, so we can simplify them now:
For example, we want to implement the function of obtaining a local IP address.

Our practice is:
1. Obtain information about all local interfaces through IOCTL and save the information in ifconf.
2. Then retrieve the IP address information in each ifreq from ifconf.

For specific use, we can think that ifconf has two members:
Ifc_len and ifc_buf, as shown in Figure 1:

Ifc_len: indicates the buffer length used to store all interface information.
Ifc_buf: buffer zone for storing Interface Information

So we needProgramInitialize ifc_led and ifc_buf of ifconf at the beginning.
Next, use IOCTL to obtain information about all interfaces. Then, put the ifc_len memory into the total length of the provided information.
And the information is stored in ifc_buf.
As shown in: (assume that two interfaces are read)

Next, we only need to obtain IP address information from one interface.

The following is a simple reference:

# Include
# Include
# Include
# Include
# Include in. h>
# Include <string. h>
# Include if. h>
# Include

Int main ()
{
Int I = 0;
Int sockfd;
Struct ifconf;
Unsigned char Buf [512];
Struct ifreq * ifreq;

// Initialize ifconf
Ifconf. ifc_len = 512;
Ifconf. ifc_buf = Buf;

If (sockfd = socket (af_inet, sock_dgram, 0) <0)
{
Perror ("socket ");
Exit (1 );
}
IOCTL (sockfd, siocgifconf, & ifconf); // obtain all interface information

// obtain the IP address one by one
ifreq = (struct ifreq *) BUF;
for (I = (ifconf. ifc_len/sizeof (struct ifreq); I> 0; I --)
{< br> // If (ifreq-> ifr_flags = af_inet) {// for IPv4
printf ("name = [% s] \ n", ifreq-> ifr_name );
printf ("Local ADDR = [% s] \ n",
inet_ntoa (struct sockaddr_in *) & (ifreq-> ifr_addr )) -> sin_addr);
ifreq ++;
/}< BR >}< br> return 0;
}

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.