struct IFREQ structure with IP, subnet mask, gateway and other information

Source: Internet
Author: User
Tags sprintf

Summing up, learn today about through the Socket,ioctl to obtain ip,netmask and other information, many of which refer to a lot of information on the Internet, I will list the

I use this function, is the following function, which has some global variables, very understood, do not do more explanation
One. Here is a comment on this function:

int get_nic_ip_address ()//Get each NIC IP address, subnet mask
{
struct Ifreq ifreq; Declare a struct ifreq struct (there are many important parameters in this structure, which can be referred to the second supplement)
int sock;
int i;
int tmpint;
Read_dev (); The function is to obtain the NIC name (saved in the sys_nic_ip[][] array mentioned below) and calculate the total number of network cards (Sys_nic_count below)

for (i=0;i<sys_nic_count;i++)
{
if ((Sock=socket (af_inet,sock_stream,0)) <0) {//Create a socket
Perror ("socket");
return;
}
strcpy (Ifreq.ifr_name,sys_nic_name[i]); Copy the NIC name to the name variable in the ifreq struct (feel this place is necessary)
if (IOCTL (sock,siocgifaddr,&ifreq) <0) {//This involves the control of the IOCTL function for network files (described below)
sprintf (Sys_nic_ip[i], "not set");
} else {
sprintf (Sys_nic_ip[i], "%d.%d.%d.%d",//Extract the IP address, save (understand the relationship between Socketaddr_in and SOCKETADDR)
(unsigned char) ifreq.ifr_addr.sa_data[2],
(unsigned char) ifreq.ifr_addr.sa_data[3],
(unsigned char) ifreq.ifr_addr.sa_data[4],
(unsigned char) ifreq.ifr_addr.sa_data[5]);
}
if (IOCTL (sock,siocgifnetmask,&ifreq) <0) {//I understand this place with Siocgifnetmask, then the original IP address in Ifreq, which is now saved as a subnet mask.
sprintf (Sys_nic_mask[i], "not set"); The mesh mask is extracted (but the only way to get the/xx is to divide the network)
} else {
sprintf (Sys_nic_mask[i], "%d",
Count ((unsigned char) ifreq.ifr_netmask.sa_data[2]) +
Count ((unsigned char) ifreq.ifr_netmask.sa_data[3]) +
Count ((unsigned char) ifreq.ifr_netmask.sa_data[4]) +
Count ((unsigned char) ifreq.ifr_netmask.sa_data[5]);

}
}
}
List the last Call function (Count ()) and some full variables:
Char sys_nic_ip[20][20];//each NIC IP
Char sys_nic_mask[20][20];//each network clip netmask "/xx"

int counttable[256] =
{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
1 , 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, ,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4 , 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
};
int Count (int v)

  return counttable[v]; 
}
you should understand. It's very classic. But there seems to be a version on the internet ... It's annoying.

Two. To supplement the points of knowledge involved

1.struct Ifreq {
Char Ifr_name[ifnamsiz];
Union
{
struct SOCKADDR ifru_addr;
struct SOCKADDR ifru_dstaddr;
struct SOCKADDR ifru_broadaddr;
struct SOCKADDR ifru_netmask;
struct SOCKADDR ifru_hwaddr;
short int ifru_flags;
int ifru_ivalue;
int IFRU_MTU;
struct Ifmap ifru_map;
Char Ifru_slave[ifnamsiz]; /* Just fits the size */
Char Ifru_newname[ifnamsiz];
__caddr_t Ifru_data;
} Ifr_ifru;
};

# define IFR_NAME ifr_ifrn.ifrn_name/* Interface Name * *
# define IFR_HWADDR ifr_ifru.ifru_hwaddr/* MAC address */
# define IFR_ADDR IFR_IFRU.IFRU_ADDR/* address */
# define IFR_DSTADDR ifr_ifru.ifru_dstaddr/* Other end of P. lnk */
# define IFR_BROADADDR IFR_IFRU.IFRU_BROADADDR/* Broadcast address * *
# define Ifr_netmask ifr_ifru.ifru_netmask/* Interface Net mask */
# define IFR_FLAGS ifr_ifru.ifru_flags * * Flags * *
# define Ifr_metric ifr_ifru.ifru_ivalue/* metric */
# define IFR_MTU IFR_IFRU.IFRU_MTU/* MTU */
# define IFR_MAP IFR_IFRU.IFRU_MAP/* Device map */
# define Ifr_slave ifr_ifru.ifru_slave/* slave device */
# define Ifr_data ifr_ifru.ifru_data/* for use by interface */
# define IFR_IFINDEX ifr_ifru.ifru_ivalue/* Interface index */
# define Ifr_bandwidth ifr_ifru.ifru_ivalue/* Link Bandwidth */
# define Ifr_qlen ifr_ifru.ifru_ivalue */Queue Length */
# define Ifr_newname ifr_ifru.ifru_newname/* New name */
# define _iot_ifreq _iot (_iots (char), ifnamsiz,_iots (char), 16,0,0)
# define _iot_ifreq_short _iot (_iots (char), ifnamsiz,_iots (short), 1,0,0)
# define _iot_ifreq_int _iot (_iots (char), ifnamsiz,_iots (int), 1,0,0)


2.ioctl functions (function in the network)
Regarding this network related request, is the IOCTL in here the function and each parameter function ... can refer to this page, the explanation is very detailed:
http://www.iteye.com/topic/309442
The 2 IOCTL control functions in this example: It's been explained very clearly.

3. With regard to the relationship between Socketaddr_in and socketaddr, a specific definition is posted below:
struct SOCKADDR_IN
{
short int sin_family; /* Address family */
unsigned short int sin_port; /* Port number */
struct IN_ADDR sin_addr; /* IP Address */
unsigned char sin_zero[8]; /* Fill 0 to keep the same size as the struct sockaddr */
};

struct SOCKADDR
{
unsigned short sa_family; /* Address family, AF_XXX */
Char sa_data[14]; /* 14-byte protocol address */
};

Compare, will find the same length, so these 2 can be common, but to do the type conversion, the comparison is drawn to why the above program can be used:
(unsigned char) ifreq.ifr_addr.sa_data[2], this form, or explain it: This ifr_addr is a struct sockaddr struct. Its sa_date[2] is not according to the above sockaddr_in in the Sin_add (that is, IP address), it is clear ....

Summary: Through this function, you can understand how to get the IP and subnet mask process ....

struct IFREQ structure with IP, subnet mask, gateway and other information

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.