zoukankan      html  css  js  c++  java
  • struct ifcfg, struct ifreq的使用

     #include <sys/ioctl.h>

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

    可以查看man手册看ioctl()函数的使用说明,man手册已经说的很清楚了.根据ioctl()函数第二个参数"request"的不同,ioctl()具有不同的形式。

    man 2 ioctl.

    man 2 ioctl_list可以列出i386所支持的所有的ioctl可以使用的宏。

    这些宏的定义有一定的规律,例如对Socket进行操作的宏都以'S'开头,

    // <include/linux/sockios.h>
    
           0x0000890B   SIOCADDRT           const struct rtentry *   // MORE
           0x0000890C   SIOCDELRT           const struct rtentry *   // MORE
           0x00008910   SIOCGIFNAME         char []
           0x00008911   SIOCSIFLINK         void
           0x00008912   SIOCGIFCONF         struct ifconf *          // MORE // I-O
           0x00008913   SIOCGIFFLAGS        struct ifreq *           // I-O
           0x00008914   SIOCSIFFLAGS        const struct ifreq *
           0x00008915   SIOCGIFADDR         struct ifreq *           // I-O
           0x00008916   SIOCSIFADDR         const struct ifreq *
           0x00008917   SIOCGIFDSTADDR      struct ifreq *           // I-O
           0x00008918   SIOCSIFDSTADDR      const struct ifreq *
           0x00008919   SIOCGIFBRDADDR      struct ifreq *           // I-O
           0x0000891A   SIOCSIFBRDADDR      const struct ifreq *
           0x0000891B   SIOCGIFNETMASK      struct ifreq *           // I-O
           0x0000891C   SIOCSIFNETMASK      const struct ifreq *
           0x0000891D   SIOCGIFMETRIC       struct ifreq *           // I-O
           0x0000891E   SIOCSIFMETRIC       const struct ifreq *
           0x0000891F   SIOCGIFMEM          struct ifreq *           // I-O
           0x00008920   SIOCSIFMEM          const struct ifreq *
           0x00008921   SIOCGIFMTU          struct ifreq *           // I-O
           0x00008922   SIOCSIFMTU          const struct ifreq *

    对文件操作的宏都以'F'开头。

    // <include/linux/fd.h>
    
           0x00000000   FDCLRPRM         void
           0x00000001   FDSETPRM         const struct floppy_struct *
           0x00000002   FDDEFPRM         const struct floppy_struct *
           0x00000003   FDGETPRM         struct floppy_struct *
           0x00000004   FDMSGON          void
           0x00000005   FDMSGOFF         void
           0x00000006   FDFMTBEG         void
           0x00000007   FDFMTTRK         const struct format_descr *
           0x00000008   FDFMTEND         void
           0x0000000A   FDSETEMSGTRESH   int
           0x0000000B   FDFLUSH          void
           0x0000000C   FDSETMAXERRS     const struct floppy_max_errors *
           0x0000000E   FDGETMAXERRS     struct floppy_max_errors *
           0x00000010   FDGETDRVTYP      struct { char [16]; } *
           0x00000014   FDSETDRVPRM      const struct floppy_drive_params *
           0x00000015   FDGETDRVPRM      struct floppy_drive_params *

    并且这些信息指出了ioctl() 函数所需要的argement参数的类型, 可以看到对socket进行操作的SIOCxx的宏大部分都需要struct ifreq结构体作为参数。struct ifreq结构体的定义在头文件<net/if.h>中,可以打开看一下。

    #include <net/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 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 __user *    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*/

     struct ifcfg结构体的定义在同一个头文件中<net/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 
    {
        int    ifc_len;            /* size of buffer    */
        union 
        {
            char __user *ifcu_buf;
            struct ifreq __user *ifcu_req;
        } ifc_ifcu;
    };
    #define    ifc_buf    ifc_ifcu.ifcu_buf        /* buffer address    */
    #define    ifc_req    ifc_ifcu.ifcu_req        /* array of structures    */
  • 相关阅读:
    Can't remove netstandard folder from output path (.net standard)
    website项目的reference问题
    The type exists in both DLLs
    git常用配置
    Map dependencies with code maps
    How to check HTML version of any website
    Bootstrap UI 编辑器
    网上职位要求对照
    Use of implicitly declared global variable
    ResolveUrl in external JavaScript file in asp.net project
  • 原文地址:https://www.cnblogs.com/dimonchen/p/4378146.html
Copyright © 2011-2022 走看看