[关闭]
@zwh8800 2017-08-23T02:02:55.000000Z 字数 5810 阅读 191266

网络管理中的ioctl

blog 归档 网络编程 unix Linux


ioctl函数传统上用于哪些不适合归入其他精细定义类别的特性的系统接口. 虽然POSIX一直在致力于创造特殊函数来取代ioctl函数, 但目前来说大多数网络编程相关的特性还需要用ioctl来实现. 特别是用于网络管理方面的相当之多(如设置ip, 获取接口, 访问路由表, 访问arp).


原型:

  1. #include <sys/ioctl.h>
  2. int ioctl(int d, int request, ... /* void *arg */);

ioctl的具体用法就不说了, 熟悉linux编程的都或多或少用过. 现在具体说下在网络编程下request的可选值和对应arg指向的的数据类型(在网络编程中arg是一个指针, 下面给出的数据类型都是这个指针要指向的类型).

image_1bl0lpcf75ua1ea638dn4f1oo89.png-184kB

总结一下的话就是:

下面重点学习一下接口请求:

接口请求除了上面图上的, 还有很多. 具体的可以去参考ioctl_list(2)中. 文章最下面也有一个表, 有所有的接口相关的request.

一般情况下, 进行网络配置时, 都会先获取所有网络接口列表, 从内核获取接口列表使用SIOCGIFCONF请求完成. 它会用到struct ifconf结构, 而ifconf结构又会用到ifreq结构:

  1. /* --<net/if.h>-- */
  2. struct ifreq
  3. {
  4. # define IFHWADDRLEN 6
  5. # define IFNAMSIZ IF_NAMESIZE
  6. union
  7. {
  8. char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */
  9. } ifr_ifrn;
  10. union
  11. {
  12. struct sockaddr ifru_addr;
  13. struct sockaddr ifru_dstaddr;
  14. struct sockaddr ifru_broadaddr;
  15. struct sockaddr ifru_netmask;
  16. struct sockaddr ifru_hwaddr;
  17. short int ifru_flags;
  18. int ifru_ivalue;
  19. int ifru_mtu;
  20. struct ifmap ifru_map;
  21. char ifru_slave[IFNAMSIZ]; /* Just fits the size */
  22. char ifru_newname[IFNAMSIZ];
  23. __caddr_t ifru_data;
  24. } ifr_ifru;
  25. };
  26. # define ifr_name ifr_ifrn.ifrn_name /* interface name */
  27. # define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */
  28. # define ifr_addr ifr_ifru.ifru_addr /* address */
  29. # define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */
  30. # define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
  31. # define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */
  32. # define ifr_flags ifr_ifru.ifru_flags /* flags */
  33. # define ifr_metric ifr_ifru.ifru_ivalue /* metric */
  34. # define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
  35. # define ifr_map ifr_ifru.ifru_map /* device map */
  36. # define ifr_slave ifr_ifru.ifru_slave /* slave device */
  37. # define ifr_data ifr_ifru.ifru_data /* for use by interface */
  38. # define ifr_ifindex ifr_ifru.ifru_ivalue /* interface index */
  39. # define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */
  40. # define ifr_qlen ifr_ifru.ifru_ivalue /* queue length */
  41. # define ifr_newname ifr_ifru.ifru_newname /* New name */
  42. struct ifconf
  43. {
  44. int ifc_len; /* Size of buffer. */
  45. union
  46. {
  47. __caddr_t ifcu_buf;
  48. struct ifreq *ifcu_req;
  49. } ifc_ifcu;
  50. };
  51. # define ifc_buf ifc_ifcu.ifcu_buf /* Buffer address. */
  52. # define ifc_req ifc_ifcu.ifcu_req /* Array of structures. */

另外给一个《unix网络编程》上的图:

image_1bl0ltenadtdr9f1ks21kiiiuqm.png-118.5kB

看起来很复杂, 实际上分开看就不很复杂.

先看ifconf结构, 其实只有两个成员, 一个是ifc_len, 一个是ifc_ifcu, 这是一个联合所以名字以u结尾, 但是我们不会直接用ifc_ifcu, 直接用的话会很长很麻烦, 而是用下面定义的两个宏, ifc_buf和ifc_req. 当想使用联合的ifcu_buf时, 只需要写ifc.ifc_buf即可, 否则需要写ifc.ifc_ifcu.ifcu_buf. 同理, ifreq也是这样设计的.

为什么要这样设计呢? 因为这个ifreq结构体需要供多个request使用, 当你请求获取ip地址时, 需要存在ifr_addr中, 当你获取子网掩码时需要存在ifr_netmask中, 但是因为需要保存在同一个结构中, 所以用联合比较方便(而且节省空间).

现在继续看ifconf结构, 可以看到, 两个成员**成员一**ifc_len为缓冲区长度, **成员二**ifc_buf(既ifc_req)是一个指针, 当调用ioctl(sock, SIOCGIFCONF, &ifc)之前, 需要把成员二当成ifc_buf, 既把它当成一个缓冲区, 所以我们需要自己申请出缓冲区空间, 并且把缓冲区的大小保存在ifc_len中. 当调用之后, 内核会把所有的接口信息保存在我们刚刚申请的ifc_buf缓冲区中. 此时, 缓冲区中的数据是有意义的了, 所以我们应当把成员二当成ifc_req(既当成一个指向struct ifreq的指针), 它指向一个ifreq结构数组, 保存着所有的接口信息. 同时, ifc_len也被更新, 保存着更改之后ifc_req的大小.

上代码:

  1. #include <net/if.h>
  2. #include <sys/ioctl.h>
  3. #include <sys/socket.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #define BUF_SIZE 1024
  9. int main()
  10. {
  11. int sock;
  12. struct ifconf ifc;
  13. struct ifreq* pifr;
  14. int n, i;
  15. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  16. {
  17. perror("socket");
  18. exit(errno);
  19. }
  20. bzero(&ifc, sizeof(ifc));
  21. if ((ifc.ifc_buf = malloc(BUF_SIZE)) == NULL)
  22. {
  23. perror("malloc");
  24. exit(errno);
  25. }
  26. ifc.ifc_len = BUF_SIZE;
  27. if (ioctl(sock, SIOCGIFCONF, &ifc) == -1)
  28. {
  29. perror("ioctl");
  30. exit(errno);
  31. }
  32. n = ifc.ifc_len / sizeof(struct ifreq);
  33. printf("%d interfaces on your computer\n", n);
  34. for (i = 0, pifr = ifc.ifc_req; i < n; ++i, ++pifr)
  35. printf("\tinterface %d : %s\n", i, pifr->ifr_name);
  36. free(ifc.ifc_buf);
  37. return 0;
  38. }

不用解释了吧, 先申请ifc.ifc_buf的空间, 调用ioctl之后, ifc.ifc_buf被填充, 然后把它当做ifc_req来读取.

看图会更清晰:

ioctl前: image_1bl0lvsc41v0a13lv6un1pt810eo13.png-21.1kB

ioctl后: image_1bl0m0e19ragkr17lcv8dt9s1g.png-90.1kB

剩下的使用ifreq的用法就简单了, 直接上代码吧:

这个是获取接口ip地址的函数, 可以和上面那个配合使用(在循环中输出接口ip)

  1. #include <arpa/inet.h>
  2. char* getifaddr(int sock, const char* ifname, char* addr, size_t n)
  3. {
  4. struct ifreq ifr;
  5. struct sockaddr_in *paddr;
  6. bzero(&ifr, sizeof(ifr));
  7. strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  8. if (ioctl(sock, SIOCGIFADDR, &ifr) == -1)
  9. {
  10. perror("ioctl");
  11. return NULL;
  12. }
  13. paddr = (struct sockaddr_in*)&ifr.ifr_addr;
  14. if (inet_ntop(AF_INET, &paddr->sin_addr, addr, n) == NULL)
  15. {
  16. perror("inet_ntop");
  17. return NULL;
  18. }
  19. return addr;
  20. }

这个是设置ip的实用程序(需要root)

  1. /* file: setip.c */
  2. #include <net/if.h>
  3. #include <sys/ioctl.h>
  4. #include <sys/socket.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #define BUF_SIZE 1024
  10. #include <arpa/inet.h>
  11. int main(int argc, char* argv[])
  12. {
  13. int sock;
  14. struct ifreq ifr;
  15. struct sockaddr_in *sin;
  16. if (argc != 3)
  17. {
  18. exit(1);
  19. }
  20. bzero(&ifr, sizeof(ifr));
  21. strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
  22. sin = (struct sockaddr_in*)&ifr.ifr_addr;
  23. sin->sin_family = AF_INET;
  24. if (inet_pton(AF_INET, argv[2], &sin->sin_addr) == -1)
  25. {
  26. perror("inet_pton");
  27. exit(errno);
  28. }
  29. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  30. {
  31. perror("socket");
  32. exit(errno);
  33. }
  34. if (ioctl(sock, SIOCSIFADDR, &ifr) == -1)
  35. {
  36. perror("ioctl");
  37. exit(errno);
  38. }
  39. return 0;
  40. }

附:所有接口相关request

  1. 0x00008910 SIOCGIFNAME char []
  2. 0x00008911 SIOCSIFLINK void
  3. 0x00008912 SIOCGIFCONF struct ifconf * // MORE // I-O
  4. 0x00008913 SIOCGIFFLAGS struct ifreq * // I-O
  5. 0x00008914 SIOCSIFFLAGS const struct ifreq *
  6. 0x00008915 SIOCGIFADDR struct ifreq * // I-O
  7. 0x00008916 SIOCSIFADDR const struct ifreq *
  8. 0x00008917 SIOCGIFDSTADDR struct ifreq * // I-O
  9. 0x00008918 SIOCSIFDSTADDR const struct ifreq *
  10. 0x00008919 SIOCGIFBRDADDR struct ifreq * // I-O
  11. 0x0000891A SIOCSIFBRDADDR const struct ifreq *
  12. 0x0000891B SIOCGIFNETMASK struct ifreq * // I-O
  13. 0x0000891C SIOCSIFNETMASK const struct ifreq *
  14. 0x0000891D SIOCGIFMETRIC struct ifreq * // I-O
  15. 0x0000891E SIOCSIFMETRIC const struct ifreq *
  16. 0x0000891F SIOCGIFMEM struct ifreq * // I-O
  17. 0x00008920 SIOCSIFMEM const struct ifreq *
  18. 0x00008921 SIOCGIFMTU struct ifreq * // I-O
  19. 0x00008922 SIOCSIFMTU const struct ifreq *
  20. 0x00008923 OLD_SIOCGIFHWADDR struct ifreq * // I-O
  21. 0x00008924 SIOCSIFHWADDR const struct ifreq * // MORE
  22. 0x00008925 SIOCGIFENCAP int *
  23. 0x00008926 SIOCSIFENCAP const int *
  24. 0x00008927 SIOCGIFHWADDR struct ifreq * // I-O
  25. 0x00008929 SIOCGIFSLAVE void
  26. 0x00008930 SIOCSIFSLAVE void
  27. 0x00008970 SIOCGIFMAP struct ifreq * // I-O
  28. 0x00008971 SIOCSIFMAP const struct ifreq *
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注