Java 上的 DHCP 客户端,无法从 IP 0.0.0.0 发送

发布于 2024-11-26 04:22:01 字数 712 浏览 1 评论 0原文

我正在尝试使用 Java 创建 DHCP 客户端。 客户端将发送 Discover、接收 Offer、发送 Request 并接收 Ack。 但是,当发送发现器和请求 UDP DatagramPackets 时,我需要从 IP 0.0.0.0 端口 68 发送它们,因为 DHCP 的目的是从 DHCP 服务器接收可用的 IP 地址。 但是,如果我将套接字绑定到 0.0.0.0,根据 http://download.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html Java Reference,内核会给socket分配一个IP地址。

如果我通过强制将以太网卡的 IP 地址设置为 0.0.0.0

$ sudo ifconfig en0 0.0.0.0  

并发送我的数据包;我收到

java.io.IOException: Can't assign requested address

异常。

有没有办法从 0.0.0.0 发送 UDP 数据包,并通过 System.exec() 设置(如果需要)使用 Java 从 DHCP 服务器接收的 IP 地址?

I'm trying to create a DHCP Client using Java.
The client will send Discover, receive a Offer, send a Request, and receive an Ack.
However, when sending the discver and the request UDP DatagramPackets, I need to send them from the IP 0.0.0.0 port 68 because, the point of DHCP is is to receive a usable IP address from the DHCP Server.
However, If I bind my socket to a 0.0.0.0, according to the http://download.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html Java Reference, the kernel will assign a IP address to the socket.

If I force set the IP address of my ethernet card to 0.0.0.0 via the

$ sudo ifconfig en0 0.0.0.0  

and send my Packet; I receive a

java.io.IOException: Can't assign requested address

Exception.

Is there a way to send UDP packets from a 0.0.0.0 and set through System.exec() (if required) the IP address received from the DHCP server using Java?

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

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

发布评论

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

评论(2

过潦 2024-12-03 04:22:01

您可能无法使用此库来执行 DHCP。该库假设您不想使用 0.0.0.0 作为发件人地址,即使 DHCP 指定您不应在此处放置 IP 地址。只是扫描 RFC,它似乎表明您“不应该填写发件人字段”。有没有可能它会被忽略?可能因为 DHCP 都是广播的,所以没关系,你可以填写任何内容。您是否尝试过在 Wireshark 或其他数据包嗅探器上监听 DHCP 请求,看看它是否真的是 0.0.0.0?

您可能必须在较低级别构建数据报。

如果您使用 DatagramSocket,我认为如何设置您自己的 IP 地址并不重要,因为如果您尝试分配 0.0.0.0,则库将更改它。

编辑:
我扫描了 dhclient 源代码的一部分,它很大,我不明白它,但看起来他们正在使用 shell 脚本使用 ifconfig 配置接口。所以这可以回答这部分问题。所以你可以像现在一样使用 ifconfig 来更改你的 IP 地址。

It may be that you won't be able to use this library to do DHCP. The library assumes that you won't want to use 0.0.0.0 as the sender address, even though DHCP specifies that you shouldn't put an ip address there. Just scanning the RFC, it seems to indicate that you "shouldn't fill out the sender field." Is it possible that it'll just be ignored? It may be that since DHCP is all broadcast, it won't matter and you can just fill out anything. Have you tried listening to DHCP requests on Wireshark or another packet sniffer to see if its really 0.0.0.0?

You may have to build your datagrams at a lower level.

I don't think it will matter how your own IP address is set if you're using DatagramSocket, because the library is going to change it if you try and assign 0.0.0.0.

Edit:
I scanned through parts of the dhclient source, and its huge and I don't understand it, but it looks like they are using shell scripts to configure the interfaces using ifconfig. So that may answer that part of the question. So you can just use your ifconfig to change your IP address as you are now.

街角卖回忆 2024-12-03 04:22:01

0.0.0.0是代表整个本地网络的ip。你不能绑定你的网卡,你只能发送数据包到它的广播ip 255.255.255.255

0.0.0.0 is the ip that represents the whole local network. You can't bind your network card to it, you can only send packets to it's broadcast ip 255.255.255.255

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