Simple usage of ioctl() function

Get local IP, MAC

#include <sys/ioctl.h>

int ioctl(int d, int request, ...);

/* Socket configuration controls. */

#define SIOCGIFADDR 0x8915    /* get PA address */

#define SIOCSIFADDR 0x8916    /* set PA address */

#define SIOCGIFHWADDR 0x8927  /* Get hardware address */

struct ifreq, Interface request structure, in the header file <net/if.h>

#include <stdio.h>  

#include <unistd.h>  

#include <sys/socket.h>  

#include <arpa/inet.h>  

#include <string.h>  

#include <sys/ioctl.h>  

#include <net/if.h>  

  

int main()  

{  

    int sock;  

    int res;  

    struct ifreq ifr;  

  

    sock = socket(AF_INET, SOCK_STREAM, 0);  

    strcpy(ifr.ifr_name, "eth0");  

    res = ioctl(sock, SIOCGIFADDR, &ifr);  

  

    printf("IP: %s\n",inet_ntoa(((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr));  

  

    strcpy(ifr.ifr_name, "eth0");  

    res = ioctl(sock, SIOCGIFHWADDR, &ifr);  

  

    int i;  

    char mac[32];  

    for(i = 0; i < 6; ++i)  

    {  

        sprintf(mac + 3*i, "%02x:", (unsigned char)ifr.ifr_hwaddr.sa_data[i]);  

    }  

    printf("MAC: %s\n",mac);  

  

    return 0;  

}  

The ifreq structure is defined in /usr/include/net/if.h, which is used to configure ip address, activate interface, configure MTU and other interface information.

It contains the name and specific content of an interface - (it is a union, it may be IP address, broadcast address, subnet mask, MAC number, MTU or other content).

ifreq is included in the ifconf structure. The ifconf structure is usually used to save the information of all interfaces.

Structural relationship between struct ifconf and struct ifreq in ioctl function

Usually the first step in using the ioctl function is to obtain all the interfaces of the system from the kernel, and then obtain its address information for each interface. Get all interfaces implemented by SIOCGIFCONF request:

 In fact, there is nothing difficult to understand about ioctl. The key is to understand how the cmd command code is generated in the user program and parsed in the driver program. The main workload of the programmer is in the switch{case} structure, because the I/O control of the device is realized through this part of the code.

**************************************************************************************************************

    Generally speaking, the IOCTL system call in user space is as follows: ioctl(int fd, int command, (char *) argstruct); Because this call has network-related code, the file descriptor fd is what the socket() system call returns, and the command parameter can be any one in the /usr/include/linux/sockios.h header file. These commands are divided into various types according to the aspects of the problem it can solve. for example:

  Alter routing table (SIOCADDRT, SIOCDELRT)  

  Read or update ARP/RARP cache (SIOCDARP, SIOCSRARP)

  General network-related functions (SIOCGIFNAME, SIOCSIFADDR, etc.)

Guess you like

Origin blog.csdn.net/qq_20853741/article/details/126839422