Using the IOCTL function to obtain network card information under Linux

Source: Internet
Author: User
Tags fread function prototype get ip sin nameserver

 

The IOCTL function prototype under Linux is as follows:

#include <sys/ioctl.h>

int ioctl (int handle, int cmd, [int *argc, int argv])

The function returned 0 successfully, and the failure returned-1.

The relevant command interfaces are as follows:

/tr>
Category Request Description Data type
Socket interface Siocatmarksiocspgrpsiocgpgrp Whether the process ID or process group ID that is located in the out-of-band flag set interface gets the process ID or process group ID of the socket interface Intintint
File Fionbiofioasyncfionreadfiosetownfiogetown Set/clear non-blocking I/O flag set/clear signal-driven asynchronous I/O flag gets the number of bytes in the receive buffer set the process ID or process group ID of the file to get the process ID or process group ID of the file Intintintintint
interface Siocgifconfsiocsifaddrsiocgifaddrsiocsifflagssiocgifflagssiocsifdstaddrsiocgifdstaddrsiocgifbrdaddrsiocsifbrdaddrsiocgifn Etmasksiocsifnetmasksiocgifmetricsiocsifmetricsiocgifmtusiocxxx Get the manifest of all interfaces set interface address Get interface address set interface flag Get interface flag set point-to address get to point address get broadcast address set broadcast address get subnet mask set subnet mask get interface measure set interface measure get interface MTU (there are many more depending on the implementation of the system) struct ifconfstruct ifreqstruct ifreqstruct ifreqstruct Ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreq
Arp Siocsarpsiocgarpsiocdarp Create/modify ARP table entry get ARP table entry delete ARP table entry struct Arpreqstruct arpreqstruct arpreq
Routing Siocaddrtsiocdelrt Increase path Delete path struct Rtentrystruct rtentry

Here we need to use the structure of the body

#include <netinet/in.h>

struct SOCKADDR_IN {short sin_family;/* Address family */unsigned short sin_port;/* Port number */struct in_addr Sin_add R /* Internet address */unsigned char sin_zero[8]; /* Same size as struct sockaddr */};

#include <net/if.h>

struct IFREQ
{
#define Ifhwaddrlen 6
Union
{
Charifrn_name[ifnamsiz];
} 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];
Charifru_newname[ifnamsiz];
void __user * IFRU_DATA;
Structif_settings ifru_settings;
} Ifr_ifru;
};

#define IFR_NAME Ifr_ifrn.ifrn_name
#define IFR_HWADDR IFR_IFRU.IFRU_HWADDR
#defineifr_addr ifr_ifru.ifru_addr
#defineifr_dstaddr ifr_ifru.ifru_dstaddr
#defineifr_broadaddr ifr_ifru.ifru_broadaddr
#defineifr_netmask Ifr_ifru.ifru_netmask
#defineifr_flags Ifr_ifru.ifru_flags
#defineifr_metric Ifr_ifru.ifru_ivalue
#defineifr_mtu IFR_IFRU.IFRU_MTU
#define IFR_MAP Ifr_ifru.ifru_map
#define Ifr_slave Ifr_ifru.ifru_slave
#defineifr_data Ifr_ifru.ifru_data
#define IFR_IFINDEX Ifr_ifru.ifru_ivalue
#define Ifr_bandwidth Ifr_ifru.ifru_ivalue
#define Ifr_qlen Ifr_ifru.ifru_ivalue
#define IFR_NEWNAME Ifr_ifru.ifru_newname
#define Ifr_settings ifr_ifru.ifru_setting

The IOCTL function can obtain information such as IP address, subnet mask, broadcast address, hardware MAC address, and so on, the gateway and routing table are complex, not discussed here.

The specific code is as follows: (Test pass)

#include <stdio.h>

#include <stdlib.h>

#include <net/if.h>

#include <unistd.h>

#include <sys/ioctl.h>

#include <arpa/inet.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <errno.h>

#include <fcntl.h>

#include <netinet/in.h>

#include <net/route.h>

#include <string.h>

#include <net/if_arp.h>

int main ()

{

struct sockaddr_in *sin;

struct Ifreq IFR;

FILE *dns;

FILE *GW;

Char *ip = new char (16);

Char *netmask = new char (16);

Char *broadcast = new char (16);

Char *ip = (char *) malloc (16);

Char *mac = new char (32);

Char *mac = (char *) malloc (32);

int socket_fd;

if ((SOCKET_FD = socket (af_inet, SOCK_DGRAM, 0)) < 0) {

Perror ("socket");

Exit (1);

}

memset (&IFR, 0, sizeof (IFR));

strcpy (Ifr.ifr_name, "eth0");

memset (&sin, 0, sizeof (sin));

Get IP Address

if (IOCTL (SOCKET_FD, SIOCGIFADDR, &IFR)! =-1) {

Sin = (struct sockaddr_in *) &ifr.ifr_addr;

strcpy (IP, Inet_ntoa (sin->sin_addr));

printf ("IP address is%s\n", IP);

}

Get broadcast Address

if (IOCTL (SOCKET_FD, SIOCGIFBRDADDR, &IFR)! =-1) {

Sin = (struct sockaddr_in *) &ifr.ifr_broadaddr;

strcpy (broadcast, Inet_ntoa (SIN->SIN_ADDR));

printf ("Broadcast is%s\n", broadcast);

}

Get Subnet mask

if (IOCTL (SOCKET_FD, Siocgifnetmask, &IFR)! =-1) {

Sin = (struct sockaddr_in *) &ifr.ifr_broadaddr;

strcpy (netmask, Inet_ntoa (SIN->SIN_ADDR));

printf ("Net-mask is%s\n", netmask);

}

Get Hardware MAC Address

if (IOCTL (SOCKET_FD, SIOCGIFHWADDR, &IFR)! =-1) {

Sin = (struct sockaddr_in *) &ifr.ifr_netmask;

sprintf (Mac, "%02x:%02x:%02x:%02x:%02x:%02x",

(unsigned char) ifr.ifr_netmask.sa_data[0],

(unsigned char) ifr.ifr_netmask.sa_data[1],

(unsigned char) ifr.ifr_netmask.sa_data[2],

(unsigned char) ifr.ifr_netmask.sa_data[3],

(unsigned char) ifr.ifr_netmask.sa_data[4],

(unsigned char) ifr.ifr_netmask.sa_data[5]);

printf ("MAC address is%s\n", Mac);

}

return 0;

}

As for obtaining the gateway and DNS, I obtained it through the relevant commands.

The main code is as follows:

Get the gateway and use the ROUTE-N command to see the associated gateway. Connection flag is ' UG '

if (gw_fd = Popen ("route-n | grep ' UG ' "," R ")) {

Fread (temp,1,128, GW_FD);

SSCANF (temp, "%*s%s", sznetgate);

printf ("Gateway is%s\n", sznetgate);

}

Get DNS; General DNS is saved in the/etc/reslov.conf file. The specific method of obtaining depends on the actual situation.

This is the case in my config file.

[Email protected]:/home/arm-none-linux# cat/etc/resolv.conf

# Dynamic resolv.conf (5) file for glibc resolver (3) generated by resolvconf (8)

# do not EDIT this FILE by HAND--YOUR changes'll be overwritten

NameServer 202.96.134.133

The 202.96.134.133 above is the primary DNS I need to get, no alternate DNS

if (dns_fd = Popen ("cat/etc/reslov.conf | grep ' nameserver ' "," R ")) {

Fread (temp,1,128, GW_FD);

SSCANF (temp, "%*s%s%*s%s", szdns1,szdns2);

printf ("DNS1 is%s", szDNS1);

printf ("Dns2is%s", szDNS2);

}

Using the IOCTL function to obtain network card information under Linux

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.