How to send a UDP packet from a specific interface

2019-07-07 06:38发布

I have several Ethernet interfaces with IPv4 Link-Local addresses, which means I have multiple routes to 169.254.0.0/16 on different interfaces. (Those interfaces are not necessarily on the same layer 2 network, so the same IP in this range can be used by different systems on different interfaces.)

When sending a UDP packet to an address in 169.254.0.0/16 I can't pick which interface will be used to send the packet using Rust's standard library UdpSocket, but without specifying the interface it is very likely to use the wrong one.

How can I send a UDP packet from a specific interface?

(IPv6 Link-Local fe80::/64 has a similar problem, but SocketAddrV6 has a scope_id which can be used to set the index of the interface to use).

1条回答
太酷不给撩
2楼-- · 2019-07-07 06:47

Assuming you know which local IP address belongs to which network, you can use bind to bind the socket to that IP address.

查看更多
登录 后发表回答