在Linux上使用C ++解析IP地址的MAC地址 [英] Resolving MAC address for IP address using C++ on Linux

查看:254
本文介绍了在Linux上使用C ++解析IP地址的MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

目前尚不清楚为什么需要MAC地址,因为通常是在较低级别为您处理的.

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

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

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

(通常,您会将数据包发送到IP地址10.10.10.10,然后系统会发送ARP数据包,询问谁拥有10.10.10.10,然后从目标系统返回响应其MAC地址,然后将其缓存(您可以通过 tcpdump 观看此情况.)或者,当系统联机时,它将发出广播消息,通知当然,如果您的目的地在另一个以太网段上,那么您将路由到网关而不是直接路由到目的地,并且没有目标MAC地址可用.)

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

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

You might read further at:

  • http://linux.die.net/man/8/arp
  • http://linux.die.net/man/8/arping
  • http://linux.die.net/man/7/arp
  • http://www.kernel.org/doc/man-pages/online/pages/man7/arp.7.html

这篇关于在Linux上使用C ++解析IP地址的MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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