当前位置:   article > 正文

Linux 套接字编程 套接字选项SO_BINDTODEVICE 绑定接口 示例

so_bindtodevice

man socket(7)里对该选项的描述:

  1. SO_BINDTODEVICE
  2. Bind this socket to a particular device like “eth0”, as speci‐
  3. fied in the passed interface name. If the name is an empty
  4. string or the option length is zero, the socket device binding
  5. is removed. The passed option is a variable-length null-ter‐
  6. minated interface name string with the maximum size of IFNAM‐
  7. SIZ. If a socket is bound to an interface, only packets
  8. received from that particular interface are processed by the
  9. socket. Note that this works only for some socket types, par‐
  10. ticularly AF_INET sockets. It is not supported for packet
  11. sockets (use normal bind(2) there).
  12. Before Linux 3.8, this socket option could be set, but could
  13. not retrieved with getsockopt(2). Since Linux 3.8, it is
  14. readable. The optlen argument should contain the buffer size
  15. available to receive the device name and is recommended to be
  16. IFNAMSIZ bytes. The real device name length is reported back
  17. in the optlen argument.
  18. 将套接字绑定到指定接口,例如eth0等。如果绑定了接口,这个套接字只能处理由该接口收到的数据。
  19. 注意,并不是所有套接字类型都有这个选项。AF_INET套接字支持,但是packet 套接字不支持(不过,可以使用bind函数绑定地址)

如果有多个接口,例如eth0, eth1, ethx......,就可以在创建套接字的时候绑定相应的接口发送数据,例如我的电脑里有两个接口 :

在创建套接字的时候就可以绑定相应的接口发送数据,demo:

  1. /*
  2. * Desrciption : 套接字选项SO_BINDTODEVICE使用,需要root权限执行
  3. * Author : mason
  4. * Date : 201809
  5. */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13. #include <net/if.h>
  14. #include <errno.h>
  15. int main()
  16. {
  17. int sock;
  18. struct sockaddr_in addr, raddr;
  19. char buffer[] = "hello world";
  20. /* 指定接口 */
  21. struct ifreq nif;
  22. char *inface = "eth0";
  23. strcpy(nif.ifr_name, inface);
  24. /* 创建套接字 */
  25. sock = socket(AF_INET, SOCK_DGRAM, 0);
  26. if (-1 == sock)
  27. {
  28. printf("create socket fail \r\n");
  29. return;
  30. }
  31. else
  32. {
  33. printf("create socket success \r\n");
  34. }
  35. if (inet_aton("192.168.80.129", &addr.sin_addr) != 1)
  36. {
  37. printf("addr convert fail \r\n");
  38. close(sock);
  39. return;
  40. }
  41. addr.sin_family = AF_INET;
  42. addr.sin_port = htons(8000);
  43. if (inet_aton("192.168.80.1", &raddr.sin_addr) != 1)
  44. {
  45. printf("addr convert fail \r\n");
  46. close(sock);
  47. return;
  48. }
  49. raddr.sin_family = AF_INET;
  50. raddr.sin_port = htons(8000);
  51. #if 0 //绑定地址
  52. if (bind(sock, (struct sockadd *)&addr, sizeof(addr)) != 0)
  53. {
  54. printf("bind fail \r\n");
  55. close(sock);
  56. return ;
  57. }
  58. else
  59. {
  60. printf("bind success \r\n");
  61. }
  62. #endif
  63. /* 绑定接口 */
  64. if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&nif, sizeof(nif)) < 0)
  65. {
  66. close(sock);
  67. printf("bind interface fail, errno: %d \r\n", errno);
  68. return ;
  69. }
  70. else
  71. {
  72. printf("bind interface success \r\n");
  73. }
  74. /* 发送 */
  75. sendto(sock, buffer, sizeof(buffer), 0, ((struct sockadd *)&raddr),sizeof(raddr));
  76. close(sock);
  77. return;
  78. }

分别绑定eth0, eth1发送数据,抓包如下图,可以看到有不同的源地址发送。

程序执行的时候需要使用sudo权限,不然会提示绑定接口失败。

感觉类似的功能完全可以由bind接口实现,即绑定指定IP地址。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/317122?site
推荐阅读
相关标签
  

闽ICP备14008679号