选择套接字输出接口:SO_BINDTODEVICE vs bind before connect [英] Choosing socket output interface: SO_BINDTODEVICE vs bind before connect

查看:8
本文介绍了选择套接字输出接口:SO_BINDTODEVICE vs bind before connect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在具有两个接口的系统上设置套接字的输出接口.我用谷歌搜索了很多,我发现了不同的答案.有人说我可以在调用 connect 之前bind 一个套接字来选择特定的接口(如 套接字如何知道要使用哪个网络接口控制器?).但是其他人说这还不够(如 http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html如何在特定接口上打开套接字并接收 IPv4 和 IPv6 流量).

I'm trying to set a socket's output interface on a system with two interfaces. I've googled a lot and I found divergent answers. Some people say that I can bind a socket before calling connect to choose a specific interface (as in How does a socket know which network interface controller to use?). But other people say that this isn't enough (as in http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html or How to open a socket on a specific interface and receive both IPv4 and IPv6 traffic).

我有一个使用 SO_BINDTODEVICE 的工作实现.但是 connect 之前的 bind 解决方案不起作用.似乎源地址不影响路由,在这种情况下只考虑路由表.有人说这是Linux的弱端系统模型造成的.根据 http://wiki.treck.com/Appendix_C:_Strong_End_System_Model_/_Weak_End_System_Model源地址没有指定输出接口.

I have a working implementation using SO_BINDTODEVICE. However the bind before connect solution isn't working. It seems that the source address doesn't affect routing and only the routing table is considered in this case. Someone said that this is caused by Linux's Weak end system model. According to http://wiki.treck.com/Appendix_C:_Strong_End_System_Model_/_Weak_End_System_Model the Source Address does not specify the output interface.

如果可能的话,我希望有一个便携式解决方案.我知道 SO_BINDTODEVICE 仅在 Linux 中可用.

If possible I would like to have a portable solution. I know that SO_BINDTODEVICE is available only in Linux.

推荐答案

源地址不影响数据包的路由.将根据目标地址和主机的路由器表选择接口.可以通过 route 命令修改.

Source address doesn't affect routing of a packet. The interface will be selected based on the destination address and host's router table. You can modify it with route command.

route default gw [gateway IP]

无论socket绑定在哪个接口,数据包都是根据路由表进行路由的.套接字绑定的接口将决定源ip地址.

No matter which interface the socket is bound, the packet is to be routed based on route table. The interface to which the socket is bound will determine the source ip address.

现在,我运行了一些绑定到环回接口的测试并将套接字连接到 Internet 中的其他地址,在这种情况下,连接失败并显示 errno 22 (EINVAL).但是,在其他两个接口的测试中(没有一个是环回),并连接到互联网中的服务器,无论我将套接字绑定到哪个接口,数据包都是根据路由表发送出去的,在我的情况下遵循默认值规则.源地址取决于绑定.

Now, I run some tests binding to loopback interface and connecting the socket to other address in the Internet and in that case connect failed with errno 22 (EINVAL). However, in other tests with two interfaces (none was loopback), and connecting to a server in the Internet, no matter which interface I bound the socket to, the packet was sent out according to the routing table, in my case following the default rule. The sources address depended on the bind.

这篇关于选择套接字输出接口:SO_BINDTODEVICE vs bind before connect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆