在 Linux 上使用 C++ 解析 IP 地址的 MAC 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4480689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:46:30  来源:igfitidea点击:

Resolving MAC address for IP address using C++ on Linux

linuxarp

提问by kagali-san

I need to generate an Ethernet header that includes the destination MAC address, (since libnfnetlink gives only the IP header before prerouting takes place), the outgoing interface number is also known, so the lookup can be made in the correct network.

我需要生成一个包含目标 MAC 地址的以太网标头(因为 libnfnetlink 在预路由之前只给出 IP 标头),传出接口编号也是已知的,因此可以在正确的网络中进行查找。

What's the library/function to resolve the MAC address from an IP address?

从 IP 地址解析 MAC 地址的库/函数是什么?

采纳答案by Mr.Ree

It's unclear why you need the MAC address, since that's usually handled for you at a lower level.

不清楚为什么需要 MAC 地址,因为通常在较低级别为您处理。

However, assuming your target is on your local Ethernet segment, you can use the arpcommand to look up values in the local cache. If the value is not cached... Well, that's a problem. Perhaps arpingwould help...

但是,假设您的目标位于本地以太网段上,您可以使用arp命令在本地缓存中查找值。如果该值没有被缓存......好吧,这是一个问题。也许arping会有所帮助...

(Normally you'd send a packet to, for example, IP address 10.10.10.10, and your system would send an ARP packet out querying who-has 10.10.10.10, and a response would come back from that target system with its MAC address, and then it would be cached. (You can watch this happening with tcpdump.) Or when a system comes on line it would send out a broadcast message informing everyone else of its MAC address. Naturally, if your destination is on another Ethernet segment, you're routing to a gateway rather than directly to the destination, and no destination-MAC address is available.)

(通常,您会向 IP 地址 10.10.10.10 发送一个数据包,您的系统会发送一个 ARP 数据包,查询 who-has 10.10.10.10,并且响应将从该目标系统返回其 MAC 地址, 然后它会被缓存。(你可以用tcpdump观察这种情况。)或者当一个系统上线时,它会发出一个广播消息,通知其他人它的 MAC 地址。当然,如果你的目的地在另一个以太网段上,您正在路由到网关而不是直接到目的地,并且没有可用的目的地 MAC 地址。)

You might read further at:

您可以在以下位置进一步阅读:

回答by Don Roby

You cannot in general get the MAC address from the IP address, and in fact as IP can run on data link protocols other than ethernet, some IP addresses have no corresponding MAC address.

一般不能从IP地址中获取MAC地址,实际上由于IP可以运行在以太网以外的数据链路协议上,所以有些IP地址没有对应的MAC地址。

The MAC address is only available and only relevant on the same ethernet segment. On that segment, it can be retrieved by an ARP request.

MAC 地址仅可用且仅与同一以太网段相关。在该段上,它可以通过 ARP 请求检索。

回答by eater

Obviously you can only find the MAC address for directly connected IP addresses, but there's no platform-independent way of doing it. On Linux, you can look in /proc/net/arpafter sending something to the target to trigger the kernel to send the ARP.

显然,您只能找到直接连接的 IP 地址的 MAC 地址,但没有独立于平台的方法。在 Linux 上,您可以在/proc/net/arp向目标发送一些内容以触发内核发送 ARP 后查看。

Edit to add you could also use the SIOCGARPioctl()though that just looks in the ARP cache, so it won't send an ARP if there isn't one already there.

编辑添加你也可以使用SIOCGARPioctl()虽然只是在 ARP 缓存中查找,因此如果没有 ARP 则它不会发送 ARP。

Otherwise, you would have to craft your own ARP request packet. You could probably reuse a bunch of code from arpingif you go that route.

否则,您将不得不制作自己的 ARP 请求数据包。如果你走那条路,你可能会重用来自arping的一堆代码。