简体   繁体   中英

Problems setting up DCHP server written in java on linux (Ubuntu 18.04)

I'm new to linux and kind of new to networking so I might have made some stupid mistake along the way.

This is current "topology" of the "network" I'm trying to setup.

So, I am setting up the DHCP server on linux machine and afaik, the DHCP works like this (from the point of the server).. it receives DHCPDISCOVER which client has broadcasted then it offers a client an address from the assigned "pool" (DHCPOFFER) also by broadcast, then client requests the address (DHCPREQUEST) and finally server acknowledges (DHCPACK or NACK if some err). Currently I am stuck at the DHCPOFFER step, because when I connect the linux laptop (server) with my windows laptop (client), it receives a DHCPDISCOVER and throws an exception when trying to broadcast DHCPOFFER.

I'm currently using this configuration for the network, but so far I've tried multiple variations

....DHCPMessage.BROADCAST_ADDR = InetAddress.getByName("255.255.255.255")....
....DHCPMessage.CLIENT_PORT = 68....

try {
        byte[] data = back.msgToByteArr();
        socket.setBroadcast(true);
        
        
        var pack = new DatagramPacket(data, data.length,
                DHCPMessage.BROADCAST_ADDR, DHCPMessage.CLIENT_PORT);
        
        var arr = new byte[]{1};
        var pack1 = new DatagramPacket(arr, arr.length,
                DHCPMessage.BROADCAST_ADDR, DHCPMessage.CLIENT_PORT);
        
        var pack2 = new DatagramPacket(arr, arr.length,
                InetAddress.getByName("192.168.0.255"), DHCPMessage.CLIENT_PORT);
        
        // after next line "java.io.IOException: Network is unreachable" is raised
        // regardless of trying to send pack, pack1 or pack2 (or many other variations I've tried so far)
        socket.send(pack2);

    } catch (IOException e) {
        return;
    }

Now, these are just a few of the last ones I've tried sending, after successfully receiving DHCPDISCOVER

pack  <<-- the real DHCPOFFER message, 
pack1 <<-- one of other attempts I've made, with just one byte sending  
pack2 <<-- another attempt where I tried to broadcast keeping in mind the mask

One more thing that might be worth mentioning is that once I turn wifi on on the server laptop (now both eth and wifi are up), the DHCPOFFER goes through, but now I never get DHCPREQUEST from client, its stuck on DHCPDISCOVER / DHCPOFFER looping few times.

Last thing is that while the wifi is on on the server laptop, when I connect with my phone, the whole communication goes through DHCPDISCOVER->DHCSPOFFER->DHCPREQUEST->DHCPACK (at which point I stop having internet access on my phone, but I'm guessing that's since the assigned private IP is not good for the public domain). This is the least important thing for me since I want to make the ethernet connected network work, not the wifi one, but maybe it carries some important info.

Ok so... even though I am still not 100% able to explain WHY the broadcast was throwing the said exception, BUT! I managed to find a way to make everything work . Turns out, there was no need for change in the code

but rather the setup of the network on linux itself.

I'm posting how I fixed it if some unlucky soul as myself stumbles upon something similar. Also, there sure are some knowledgeable people out there and some of them might take the time to explain what was happening.

MY GUESS: (and mind, its just a guess) the server laptop didn't have a way to communicate on network and was searching for the IP itself - (read - from another DHCP) so when it was auto assigned an address, that address wasn't good for the needed network, therefore the problem was in the network settings (possibly propagating to sockets) rather than in the code itself (or the receiving part - 255.255.255.255 BROADCAST).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM