UNIX网络编程卷一:第十七章 ioctl

来源:互联网 发布:爱剪辑软件 编辑:程序博客网 时间:2024/06/06 07:17

ioctl函数传统上一直作为不适合归入其它精细定义类别的特性的系统借口。

POSIX致力于摆脱ioctl借口,办法是创造新的函数来取代ioctl,例如:

Unix终端接口传统上使用ioctl访问,而POSIX为终端创造了12个新函数:tcgetattr  tcflush  ....

但是,ioctl依然保留了不少功能,用于:获取接口信息、访问路由表、访问ARP高速缓存等。


网络程序(特别是服务器程序)经常在启动后使用ioctl获取所在主机全部网络接口的信息:接口地址、是否支持广播、是否支持多播等。


NAME       ioctl - control deviceSYNOPSIS       #include <sys/ioctl.h>       int ioctl(int d, unsigned long request, ...);DESCRIPTION       The  ioctl()  function  manipulates  the  underlying device parameters of special files.  In particular, many operating characteristics of character special files (e.g., terminals) may be con‐       trolled with ioctl() requests.  The argument d must be an open file descriptor.       The second argument is a device-dependent request code.  The third argument is an untyped pointer to memory.  It's traditionally char *argp (from the days before void * was valid C), and  will       be so named for this discussion.       An  ioctl()  request  has  encoded in it whether the argument is an in parameter or out parameter, and the size of the argument argp in bytes.  Macros and defines used in specifying an ioctl()       request are located in the file <sys/ioctl.h>.RETURN VALUE       Usually, on success zero is returned.  A few ioctl() requests use the return value as an output parameter and return a nonnegative value on success.  On error, -1 is returned, and errno is set       appropriately.ERRORS       EBADF  d is not a valid descriptor.       EFAULT argp references an inaccessible memory area.       EINVAL Request or argp is not valid.       ENOTTY d is not associated with a character special device.       ENOTTY The specified request does not apply to the kind of object that the descriptor d references.

ioctl用于操作设备,该设备的描述符由d指定。第三个参数是一个指针,指针的类型依赖于request。其中与网络相关的request如下表所示:





0 0