在 Linux 2.6.21 (glibc 2.3.5) 上进行 ARP 和反向 ARP

发布于 2024-07-13 07:22:07 字数 551 浏览 15 评论 0原文

我需要在任意 IP 网络上存储对第三方设备的持久引用,其中设备的 IP 地址可能是静态的或由 DHCP 随机分配。 我不控制网络上的设备,也不能依赖 DNS 和其他现有的或与设备一起使用的临时网络协议。

所以我被指示使用硬件地址和 ARP 进行调查。 这会起作用,但我不想重复代码。 内核必须管理 ARP 表。 在 Windows 上,您可以使用 GetIpNetTable 等访问它 我希望

有一个API可以回答这两个问题:

  • 如何从IP地址转换为MAC地址? (ARP)
  • 如何将 MAC 地址转换为 IP 地址? (InARP)

如果没有,那么我可能需要更多手动操作:

  • How do I read the kernel's ARP table?
  • 如果我自己确定了映射,如何添加条目?

I need to store persistent reference to third party device on an arbitrary IP network where the IP address of the devices may be static or randomly assigned by DHCP. I don't control the devices on the network and I can't rely on DNS and other ad-hoc networking protocols existing or working with the devices.

So I have been instructed to investigate using hardware addresses and ARP. This will work but I don't want to duplicate code. The kernel must manage an ARP table. On Windows you can access it using GetIpNetTable etc.

I am hoping there is an API to answer these two questions:

  • How do I translate from IP to MAC address? (ARP)
  • How do I translate from MAC to IP address? (InARP)

If not then I may have to do it more manually:

  • How do I read the kernel's ARP table?
  • How do I add an entry if I have the determined a mapping myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生活了然无味 2024-07-20 07:22:09

至于ARP:
您可以使用 system("/usr/bin/arp -option_of_choice"); 并解析输出,但这是一个丑陋的黑客。 ——不是我的推荐。

查看 /usr/include/linux/sockios.h —— SIOCGARP、SIOCDARP 和 SIOCSARP 详细信息。 您可以执行这些 ioctl 来管理 Linux 上的 ARP 表。 当然,您必须在套接字 fd 上执行这些 ioctl。
以下是一些示例:SIOCGARP 示例
我相信您也可以找到其他几种语言的其他示例。 我假设您使用的是 C。

至于 RARP:
linux rarp 联机帮助页中的引用:
" 该程序已过时。从 2.3 版本开始,Linux 内核不再
包含 RARP 支持。 有关替代 RARP 守护程序,请参阅 ftp://ftp.demen-
tia.org/pub/net-tools"
因此,您必须在目标系统上安装 rarpd。

As for ARP:
You could use system("/usr/bin/arp -option_of_choice"); and parse the output, but that's an ugly hack. -- Not my recommendation.

Take a look at /usr/include/linux/sockios.h -- At the SIOCGARP, SIOCDARP, and SIOCSARP details. Those are ioctls that you can perform to manage the ARP table on linux. Of course, you'll have to perform these ioctls on a socket fd.
Here's some examples: SIOCGARP examples
I'm sure you can find many other examples in several other languages as well. As I'm assuming that you're using C.

As for RARP:
A quote from the linux rarp manpage:
" This program is obsolete. From version 2.3, the Linux kernel no longer
contains RARP support. For a replacement RARP daemon, see ftp://ftp.demen-
tia.org/pub/net-tools"
So you'll have to install rarpd on the target system.

暗恋未遂 2024-07-20 07:22:08

/proc/net/arp

K

/proc/net/arp

K

淡笑忘祈一世凡恋 2024-07-20 07:22:08

ARP 表往往相当本地且短暂。 如果您检查协议,通常仅当给定的 IP 地址位于本地子网中时才会提供真实的 MAC 地址。
否则,数据包将转发到本地路由器,然后由本地路由器负责转发它。

如果您在 Windows 上执行“arp -g”或在 UNIX 上执行“arp -a”,您会看到该表,但我认为这不会对您有帮助由于上述原因,任何好处都没有。 该命令

实际上就是 DNS 的用途,但正如您所说,它可能不适合您。

您可能必须在应用程序级别编写自己的“ARP​​”数据库。

ARP tables tend to be fairly local and short-lived. If you examine the protocol, the real MAC addresses are generally only provided when the given IP address is in the local subnet.
Otherwise, the packet is forwarded to the local router, which is then responsible for forwarding it.

If you do "arp -g" on Windows or "arp -a" on UNIX, you'll see the table, but I don't think it will do you any good, due to the reasons mentioned above. That command and

That's really what DNS is for but, as you say, it may not be an option for you.

You may well have to write your own 'ARP' database at your application level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文