ioctl相当于一个杂物箱,它一直作为那些不适合归入其他精细定义类别的特性的系统接口。

本章笔记先放着,到时候有需要再看

 

 

ioctl函数

#include <unistd.h>
int ioctl(int fd,int request,.../* void *arg */);

其中第三个参数总是一个指针,但指针的类型依赖于request参数。

我们可以把网络相关的请求(request)划分为6类:

1.套接字操作

2.文件操作

3.接口操作

4.ARP高速缓存操作

5.路由表操作

6.流系统

下图列出了网络相关ioctl请求的request参数以及arg地址必须指向的数据类型:

 

UNP学习笔记(第十七章 ioctl操作) 

 

 

 

套接字操作

SIOCATMARK    如果本套接字的读指针当前位于带外标记,那就通过由第三个参数指向的整数返回一个非0值,否则返回一个0值

SIOCGPGRP       通过由第三个参数指向的整数返回本套接字的进程ID或进程组ID

SIOCSGRP         把本进程进程ID或进程组ID设置成由第三个参数指向的整数

 

 

文件操作

FIONBIO       根据ioctl的第三个参数指向一个0值或非0值,可清除或设置本套接字的非阻塞式I/O标志

FIOASYNC     根据ioctl的第三个参数指向一个0值或非0值,可清除或设置本套接字的信号驱动异步I/O标志,它决定是否收取针对本套接字的异步I/O信号(SIGIO)

FIONREAD     通过由ioctl的第三个参数指向的整数返回当前本套接字接收缓冲区中的字节数

FIOSETOWN   对于套接字和SIOCSPGRP等效

FIOGETOWN   对于套接字和SIOCGPGRP等效

 

 

接口配置

需处理网络接口的许多程序的初始步骤之一就是从内核获取配置在系统中的所有接口。本任务由SIOCGIFCONF请求完成,它使用ifconf结构,ifconf又使用ifreq结构。这两个结构定义如下:

struct ifconf 
{
    int    ifc_len;            /* size of buffer    */
    union 
    {
        char *ifcu_buf;                        /* input from user->kernel*/
        struct ifreq *ifcu_req;        /* return from kernel->user*/
    } ifc_ifcu;
};
#define    ifc_buf    ifc_ifcu.ifcu_buf        /* buffer address    */
#define    ifc_req    ifc_ifcu.ifcu_req        /* array of structures    */
 
//ifreq用来保存某个接口的信息
//if.h
struct ifreq {
    char ifr_name[IFNAMSIZ];
    union {
        struct sockaddr ifru_addr;
        struct sockaddr ifru_dstaddr;
        struct sockaddr ifru_broadaddr;
        short ifru_flags;
        int ifru_metric;
        caddr_t ifru_data;
    } ifr_ifru;
};
#define ifr_addr ifr_ifru.ifru_addr
#define ifr_dstaddr ifr_ifru.ifru_dstaddr
#define ifr_broadaddr ifr_ifru.ifru_broadaddr

在调用ioctl前我们先分配一个缓冲区和一个ifconf结构,然后初始化后者。下面展示这个ifconf结构的初始化结果,其中缓冲区的大小为1024字节

UNP学习笔记(第十七章 ioctl操作)

假设内核返回2个ifreq结构,在ioctl返回时通过同一个ifconf结构所返回的值如下图。缓冲区被填入两个ifreq结构,而且ifconf结构的ifc_len成员也被更新

UNP学习笔记(第十七章 ioctl操作)

 

 

 

get_ifi_info函数(暂时不看)

我们使用ioctl开发一个名为get_ifi_info的函数,它返回一个结构链表,其中每个结构对应一个当前处于“up”状态的接口。

头文件

 1 /* Our own header for the programs that need interface configuration info.
 2    Include this file, instead of "unp.h". */
 3 
 4 #ifndef    __unp_ifi_h
 5 #define    __unp_ifi_h
 6 
 7 #include    "unp.h"
 8 #include    <net/if.h>
 9 
10 #define    IFI_NAME    16            /* same as IFNAMSIZ in <net/if.h> */
11 #define    IFI_HADDR     8            /* allow for 64-bit EUI-64 in future */
12 
13 struct ifi_info {
14   char    ifi_name[IFI_NAME];    /* interface name, null-terminated */
15   short   ifi_index;            /* interface index */
16   short   ifi_mtu;                /* interface MTU */
17   u_char  ifi_haddr[IFI_HADDR];    /* hardware address */
18   u_short ifi_hlen;                /* # bytes in hardware address: 0, 6, 8 */
19   short   ifi_flags;            /* IFF_xxx constants from <net/if.h> */
20   short   ifi_myflags;            /* our own IFI_xxx flags */
21   struct sockaddr  *ifi_addr;    /* primary address */
22   struct sockaddr  *ifi_brdaddr;/* broadcast address */
23   struct sockaddr  *ifi_dstaddr;/* destination address */
24   struct ifi_info  *ifi_next;    /* next of these structures */
25 };
26 
27 #define    IFI_ALIAS    1            /* ifi_addr is an alias */
28 
29                     /* function prototypes */
30 struct ifi_info    *get_ifi_info(int, int);
31 struct ifi_info    *Get_ifi_info(int, int);
32 void             free_ifi_info(struct ifi_info *);
33 
34 #endif    /* __unp_ifi_h */
View Code

相关文章:

  • 2021-09-07
  • 2021-12-05
  • 2022-12-23
  • 2021-08-30
  • 2021-05-21
  • 2022-02-10
  • 2022-01-02
  • 2022-02-26
猜你喜欢
  • 2021-08-04
  • 2021-08-09
  • 2021-09-10
  • 2021-12-24
  • 2021-05-21
  • 2021-10-04
  • 2022-03-02
相关资源
相似解决方案