如何在Linux下从特定接口发送UDP数据包? [英] How to send a UDP packet from a specific interface under Linux?

查看:83
本文介绍了如何在Linux下从特定接口发送UDP数据包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个具有IPv4本地链接地址的以太网接口,这意味着我在不同接口上有多个通往 169.254.0.0/16 的路由.(这些接口不一定在同一第2层网络上,因此该范围内的相同IP可以由不同接口上的不同系统使用.)

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.)

在将UDP数据包发送到 169.254.0.0/16 中的地址时,我无法使用Rust的标准库 UdpSocket 来选择将哪个接口用于发送数据包,但未指定接口,则很可能使用错误的接口.

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.

如何从特定接口发送UDP数据包?

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

(IPv6本地链接 fe80 ::/64 也有类似的问题,但是 SocketAddrV6 有一个 scope_id 可用于设置要使用的接口的索引).

(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).

推荐答案

假定您知道哪个本地IP地址属于哪个网络,则可以使用 bind 将套接字绑定到该IP地址.

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

这篇关于如何在Linux下从特定接口发送UDP数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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