简体   繁体   English

路由是否影响具有绑定源地址的套接字?

[英]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: 使用routeip route add我已将其设置为route:

  • All addresses to 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.

相关问题 跟踪套接字(文件)描述符返回绑定地址 - Tracing socket (file) descriptor back to bound address 当套接字未指定源IP地址时,Linux如何选择IP地址? - How does Linux choose a IP address when a socket didn't specify the source IP address? 哪个函数告诉我内核将本地套接字绑定到哪个地址? - Which function tells me what address the kernel bound the local socket to? 如果许多套接字连接处于TIMEWAIT状态,是否会影响服务器的性能? - Does it affect the performance of server if many socket connections are in TIMEWAIT state 使用和UDP套接字时如何设置源地址 - How to set source address when sending using and UDP socket 在分叉进程中连接()绑定()的套接字如何影响父级中的套接字? - How does connect()'ing a bind()'ed socket in a forked process affect the socket in the parent? 如何检查套接字绑定到的 IP 地址是否仍然是有效机器地址之一? - How to check if the IP address that a socket is bound to is still one of the valid machine's addresses? 如何查找和取消绑定的套接字 - How to Find and Unlink a Bound Socket 如何在Java中对UDP数据包进行IP欺骗/更改源地址/原始套接字编程? - How to do ip spoofing/change source address/raw socket programming for UDP packets in java? 使用Linux网络设备别名时,指定TCP套接字的源IP地址 - Specify source IP address for TCP socket when using Linux network device aliases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM