Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top