1. 程式人生 > >Interface request structure used for socket ioctl's

Interface request structure used for socket ioctl's

mem ble short 激活 pri specific ring trie 類型

1. 結構體定義

/*
 * 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 IFHWADDRLEN    6
    union
    {
        char    ifrn_name[IFNAMSIZ];        /*
if name, e.g. "en0" */ } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue;
int ifru_mtu; struct ifmap ifru_map; char ifru_slave[IFNAMSIZ]; /* Just fits the size */ char ifru_newname[IFNAMSIZ]; void * ifru_data; struct if_settings ifru_settings; } 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-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 ifr_settings ifr_ifru.ifru_settings /* Device/proto settings*/ /* * Structure used in SIOCGIFCONF request. * Used to retrieve interface configuration * for machine (useful for programs which * must know all networks accessible). */ struct ifconf { int ifc_len; /* size of buffer */ union { char *ifcu_buf; struct ifreq *ifcu_req; } ifc_ifcu; }; #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ #define ifc_req ifc_ifcu.ifcu_req /* array of structures */

ifreq結構定義在/usr/include/net/if.h;用來配置ip地址,激活接口,配置MTU等接口信息的。

獲取本機ip:

#include <string.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


void get_local_ip(char *g_localIP )
{
    int sock_get_ip;
    struct sockaddr_in *sin;
    struct ifreq ifr_ip;

    if ((sock_get_ip = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        //todo:
    }
    
    memset(&ifr_ip, 0, sizeof(ifr_ip));
    strncpy(ifr_ip.ifr_name, "eth0", sizeof(ifr_ip.ifr_name)-1);
    if (ioctl(sock_get_ip, SIOCGIFADDR, &ifr_ip) < 0) {
        //todo:
    }
    sin = (struct sockaddr_in*)&ifr_ip.ifr_addr;
    printf("host: %s\r\n", inet_ntoa(sin->sin_addr));
    strcpy(g_localIP, inet_ntoa(sin->sin_addr));
    close(sock_get_ip);
}

int main()
{
    char g_localIP[20]="";
    get_local_ip(g_localIP);
    printf("%s\r\n", g_localIP);
    return 0;
}

2. ioctl聲明

int ioctl( int filedes, int request, ... )
網絡相關的ioctl請求的request參數及arg地址必須指向的數據類型如下表所示:
接口

SIOCGIFCONF

SIOCSIFADDR

SIOCGIFADDR

SIOCSIFBRDADDR

SIOCGIFBRDADDR

SIOCSIFNETMASK

SIOCGIFNETMASK

獲取所有接口列表

設置接口地址

獲取接口地址

設置廣播地址

獲取廣播地址

設置子網掩碼

獲取子網掩碼

Struct ifconf

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq




Interface request structure used for socket ioctl's