Linux Get network interface information

Source: Internet
Author: User
Tags sin

Linux acquires network interface information needed for the function of the IOCTL (), struct struct ifreq,struct ifconf

1.ioctl () function prototypes and functions

1 #include <sys/ioctl.h>2  3int ioctl (intint  Request, ...); 4 5 // Parameters 6 // int D: is a file descriptor 7 // int Request: Represents the information to be requested. such as IP address, netmask, etc 8//...: variable parameters, depending on request

Here is the request parameter for the IOCTL requests and the data type that the ARG address must point to:

2.struct Ifreq Structural Body

This structure is defined in include/net/if.h, which is used to configure IP addresses, activate interfaces, configure interface information such as MTU

1  /*INTERFACE request structure used for socket IOCTL ' s. All Interface2 The IOCTL ' s must has parameter definitions which begin with Ifr_name.3 The remainder may interface specific. */4  5 structIfreq6 {7# define Ifhwaddrlen68 # define Ifnamsiz if_namesize9 UnionTen        { One      CharIfrn_name[ifnamsiz];/*Interface name, e.g. "En0". */ A } IFR_IFRN; -   - Union the        { -      structsockaddr ifru_addr; -      structsockaddr ifru_dstaddr; -      structsockaddr ifru_broadaddr; +      structsockaddr Ifru_netmask; -      structsockaddr ifru_hwaddr; +       Short intifru_flags; A      intIfru_ivalue; at      intIfru_mtu; -      structIfmap Ifru_map; -      CharIfru_slave[ifnamsiz];/*Just fits the size*/ -      CharIfru_newname[ifnamsiz]; - __caddr_t Ifru_data; - } Ifr_ifru; in    }; -# define Ifr_name Ifr_ifrn.ifrn_name/*Interface Name*/ to# 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*/ the# 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*/Panax Notoginseng# define Ifr_metric Ifr_ifru.ifru_ivalue/*Metric*/ -# define IFR_MTU IFR_IFRU.IFRU_MTU/*MTU*/ the# define IFR_MAP Ifr_ifru.ifru_map/*Device Map*/ +# define Ifr_slave Ifr_ifru.ifru_slave/*slave Device*/ A# define Ifr_data Ifr_ifru.ifru_data/*For use by interface*/ the# 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), -,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)

3.struct ifconf

The ifreq is included in the ifconf structure. The ifconf structure is usually used to hold information about all interfaces.

1  /*Structure used in siocgifconf request. Used to retrieve interface2 configuration for machine (useful-programs which must know all3 networks accessible). */4  5  structifconf6    {7      intIfc_len;/*Size of buffer. */8 Union9        {Ten __caddr_t ifcu_buf; One      structIfreq *Ifcu_req; A } IFC_IFCU; -    }; -# define IFC_BUF Ifc_ifcu.ifcu_buf/*Buffer address. */ the# define Ifc_req Ifc_ifcu.ifcu_req/*Array of structures. */ -# define _IOT_IFCONF _iot (_iots (structifconf),1,0,0,0,0)/* Not right*/ -  #endif/* Misc. */

The following program is to get the IP address of this machine

1#include <stdio.h>2#include <sys/socket.h>3#include <string.h>4#include <sys/types.h>5#include <netinet/inch.h>6#include <sys/ioctl.h>7#include <net/if.h>8 9 //#define ETH_NAME "eth0"Ten //#define NET_NAME "Wlan1" One  A intMainintargcChar*argv[]) - { -     if(ARGC! =2) { theprintf"using%s <eth_name>\n", argv[0]); -         return 0; -     } -  +     intSock =0; -     structsockaddr_in sin; +     structIfreq IFR;//information to save the interface A  atSock = socket (af_inet, SOCK_DGRAM,0); -     if(Sock = =-1) { -Perror ("Socket"); -         return 1; -     } -  instrcpy (Ifr.ifr_name, argv[1]); -     intRET = IOCTL (sock, SIOCGIFADDR, &AMP;IFR);//siocgifaddr Get interface address to     if(Ret <0) { +Perror ("IOCTL"); -         return 1; the     }     *  $memcpy (&sin, &AMP;IFR.IFR_ADDR,sizeof(sin));//Copy the address to the SOCKADDR_IN structurePanax Notoginsengprintf"%s\n", Inet_ntoa (SIN.SIN_ADDR));//Show -     return 0;  the      +}

The results of the operation are as follows:

Linux Get network interface 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.