Obtain Network Interface Information-IOCTL () function and struct ifreq and struct ifconf

Source: Internet
Author: User

Reprinted please indicate the source: windeal Column


In Linux, you can use IOCTL () function and struct ifreq struct ifconf to obtain various information about network interfaces.


IOCTL first, The IOCTL () usage IOCTL () prototype is as follows:
       #include <sys/ioctl.h>       int ioctl(int fd, int request, ...);
Parameter: FD: file descriptor request: indicates the information to be requested. Such as IP address and network mask...: variable parameters following the request are determined according to the request.
For example, we request a list of all network interfaces:
struct ifconf IoCtlReq;...ioctl( Sock, SIOCGIFCONF, &IoCtlReq )

Ioctlreq is
Interface-related requests are shown in the following table (Source: >):
ConnectPort Siocgifconfsiocsifaddrsiocgifaddrsiocsifflagssiocgifflagssiocsifdstaddrsiocgifdstaddrsiocgifbrdaddrsiocsifbrdaddrsiocgifnetmask siocsifnetmasksiocgifmetricsiocsifmetricsiocgifmtusiocxxx Get list of all interfaces set interface address get interface address set interface flag get interface flag set point-to-point address get broadcast address set broadcast address get subnet mask set subnet mask get interface measure settings obtain the MTU of an interface based on the system implementation) Struct ifconfstruct ifreqstruct ifre

For more information about ioctl, see other blog posts in this blog.
Struct ifreq is used to save information about an interface.
// if.h/* * Interface request structure used for socket * ioctl's.  All interface ioctl's must have parameter * definitions which begin with ifr_name.  The * remainder may be interface specific. */struct ifreq {#define IFHWADDRLEN6union{charifrn_name[IFNAMSIZ];/* if name, e.g. "en0" */} ifr_ifrn;union {structsockaddr ifru_addr;structsockaddr ifru_dstaddr;structsockaddr ifru_broadaddr;structsockaddr ifru_netmask;struct  sockaddr ifru_hwaddr;shortifru_flags;intifru_ivalue;intifru_mtu;struct  ifmap ifru_map;charifru_slave[IFNAMSIZ];/* Just fits the size */charifru_newname[IFNAMSIZ];void __user *ifru_data;structif_settings ifru_settings;} ifr_ifru;};#define ifr_nameifr_ifrn.ifrn_name/* interface name */#define ifr_hwaddrifr_ifru.ifru_hwaddr/* MAC address */#defineifr_addrifr_ifru.ifru_addr/* address*/#defineifr_dstaddrifr_ifru.ifru_dstaddr/* other end of p-p lnk*/#defineifr_broadaddrifr_ifru.ifru_broadaddr/* broadcast address*/#defineifr_netmaskifr_ifru.ifru_netmask/* interface net mask*/#defineifr_flagsifr_ifru.ifru_flags/* flags*/#defineifr_metricifr_ifru.ifru_ivalue/* metric*/#defineifr_mtuifr_ifru.ifru_mtu/* mtu*/#define ifr_mapifr_ifru.ifru_map/* device map*/#define ifr_slaveifr_ifru.ifru_slave/* slave device*/#defineifr_dataifr_ifru.ifru_data/* for use by interface*/#define ifr_ifindexifr_ifru.ifru_ivalue/* interface index*/#define ifr_bandwidthifr_ifru.ifru_ivalue    /* link bandwidth*/#define ifr_qlenifr_ifru.ifru_ivalue/* Queue length */#define ifr_newnameifr_ifru.ifru_newname/* New name*/#define ifr_settingsifr_ifru.ifru_settings/* Device/proto settings*/



Ifr_name identifies an interface.
You can use IOCTL to obtain information about this interface. For example:
IOCTL (sock, siocgifnetmask, & ifreq); // obtain the network interface address mask
This code first needs to assign values to ifreq-> ifr_name, and then obtain the address mask of the network interface that matches ifreq-> ifr_name.


Struct ifconf is usually used to save all interface information
// if.h/* * Structure used in SIOCGIFCONF request. * Used to retrieve interface configuration * for machine (useful for programs which * must know all networks accessible). */struct ifconf  {intifc_len;/* size of buffer*/union {char __user *ifcu_buf;struct ifreq __user *ifcu_req;} ifc_ifcu;};#defineifc_bufifc_ifcu.ifcu_buf/* buffer address*/#defineifc_reqifc_ifcu.ifcu_req/* array of structures*/


The struct can be used to obtain the name and information of the network interface yo (not all information, is the IP address) (picture from: http://tech.sunplusedu.com/space/post-4064.aspx)


Example:
# Include <sys/types. h> # include <sys/IOCTL. h> # include <sys/socket. h> # include <net/if. h> # include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <netdb. h> # include <string. h> # include <fcntl. h> # include <string. h> # include <errno. h> typedef uint32_t uint32; # define max_if 10int main () {struct ifreq ifvec [max_if]; // used to save all interfaces int sock =-1; if (sock = socket (af_inet, sock_dgram, 0) <0) Fprintf (stderr, "error: % d, cannot open Ram; \ n"); // get If struvector CT ifconf ioifconf; ioifconf. ifc_buf = (void *) ifvec; ioifconf. ifc_len = sizeof (ifvec); printf ("Len: % d \ n", ioifconf. ifc_len); If (IOCTL (sock, siocgifconf, & ioifconf) <0) // obtain all network interface information fprintf (stderr, "error: % d IOCTL ifconf \ n "); printf ("Len: % d \ n", ioifconf. ifc_len); // compare with the previous one to Len, and find that the len to the size in the ioctl modification // print each network interface to the Information cyclically {struct ifreq * ifpt; ST Ruct ifreq * ifendpt; ifpt = ifvec; ifendpt = (void *) (char *) ifvec + ioifconf. ifc_len); For (ifpt = ifvec; ifpt <ifendpt; ifpt ++) {struct ifreq; If (ifpt-> ifr_addr.sa_family! = Af_inet) {continue;} // temp keepers of interface Params... uint32 u32_addr, u32_mask;/* print IP Address */Char ipdotbuf [16], subnetdotbuf [16], maskdotbuf [16]; // Save the vertex decimal to the IP address u32_addr = (struct sockaddr_in *) & ifpt-> ifr_addr)-> metadata; inet_ntop (af_inet, & u32_addr, ipdotbuf) sizeof (ipdotbuf); printf ("IP Address: % s \ n", ipdotbuf);/* print Address Mask */bzero (& ifreq, sizeof (struct ifreq )); memcpy (ifreq. ifr_name, ifpt-> ifr_name, sizeof (ifreq. ifr_name); If (IOCTL (sock, siocgifnetmask, & ifreq) <0) {fprintf (stderr, "error: % d, cannot get mask \ n", errno );} else {u32_mask = (struct sockaddr_in *) & ifreq. ifr_addr)-> signature; inet_ntop (af_inet, & u32_mask, maskdotbuf, (socklen_t) sizeof (maskdotbuf); printf ("Mask: % s \ n", signature );} /* print MTU */bzero (& ifreq, sizeof (struct ifreq); memcpy (ifreq. ifr_name, ifpt-> ifr_name, sizeof (ifreq. ifr_name); If (IOCTL (sock, siocgifmtu, & ifreq) <0) {fprintf (stderr, "error: % d, cannot get MTU \ n", errno );} else {printf ("siocgifmtu: % d \ n", ifreq. ifr_mtu);}/* Other information is printed in the same way as the mask and MTU */}}}



Running result:
[email protected]:~/Windeal/apue$ ./exe Len:320Len:64IP Address: 127.0.0.1Mask: 255.0.0.0SIOCGIFMTU:16436IP Address: 172.17.92.198Mask: 255.255.254.0SIOCGIFMTU:1500[email protected]:~/Windeal/apue$ 



















Obtain Network Interface Information-IOCTL () function and struct ifreq and struct ifconf

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.