[英]Does routing affect a socket with a bound source address?
Let's say I have two network interfaces: 假设我有两个网络接口:
eth0
with address 10.0.0.1
eth0
,地址10.0.0.1
eth1
with address 192.168.0.1
eth1
,地址为192.168.0.1
Using route
or ip route add
I have set it to route: 使用
route
或ip route add
我已将其设置为route:
eth0
eth0
1.2.3.4
only to eth1
1.2.3.4
仅限eth1
So packets to 1.2.3.4
should be routed to eth1
, and everything else to eth0
. 所以
1.2.3.4
数据包应该路由到eth1
,其他一切都路由到eth0
。
I then create a UDP socket and use bind()
to set its local address to 192.168.0.1
. 然后我创建一个UDP套接字并使用
bind()
将其本地地址设置为192.168.0.1
。 Then I send a packet to 1.2.3.4
. 然后我发送一个数据包到
1.2.3.4
。
Will it be be sent over eth1
per the routing table or eth0
because it is bound to that IP address? 是否会通过路由表或
eth0
通过eth1
发送,因为它绑定到该IP地址? I tried, and it seems to be sent on eth1
. 我试过了,似乎是在
eth1
上发送的。
Is there a way I can force a socket to use eth0
, which has a valid route to the destination, but not the most specific rule? 有没有办法可以强制套接字使用
eth0
,它有一个到目的地的有效路由,但不是最具体的规则? I know about SO_BINDTODEVICE, but prefer to avoid using interface names in C code. 我知道SO_BINDTODEVICE,但更喜欢避免在C代码中使用接口名称。
For sockets if you want the the Kernel and its routing table to pick the best interface for you using any available port you don't have to call bind()
before sending datagram socket. 对于套接字,如果您希望内核及其路由表使用任何可用端口为您选择最佳接口,则在发送数据报套接字之前不必调用
bind()
。
If you do bind a socket, it will be bound to a network device with that specific IP address. 如果绑定套接字,它将绑定到具有该特定IP地址的网络设备。 But does it make sense if packet can't reach destination address from that network device?
但是,如果数据包无法从该网络设备到达目标地址,这是否有意义?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.