Search in sources :

Example 1 with DHCPSocket

use of edu.bucknell.net.JDHCP.DHCPSocket in project opennms by OpenNMS.

the class DhcpDetectorTest method testJdhcp.

@Test(timeout = 90000)
@Ignore
public void testJdhcp() throws IOException {
    DHCPSocket mySocket = new DHCPSocket(68);
    try {
        DHCPMessage messageOut = new DHCPMessage(InetAddressUtils.addr(DHCP_SERVER_IP));
        // fill DHCPMessage object 
        messageOut.setOp((byte) 1);
        messageOut.setHtype((byte) 1);
        messageOut.setHlen((byte) 6);
        messageOut.setHops((byte) 0);
        // should be a random int
        messageOut.setXid(191991743);
        messageOut.setSecs((short) 0);
        messageOut.setFlags((short) 0);
        byte[] hw = new byte[16];
        hw[0] = (byte) 0x00;
        hw[1] = (byte) 0x60;
        hw[2] = (byte) 0x97;
        hw[3] = (byte) 0xC6;
        hw[4] = (byte) 0x76;
        hw[5] = (byte) 0x64;
        messageOut.setChaddr(hw);
        // set message type option to DHCPDISCOVER
        byte[] opt = new byte[1];
        opt[0] = (byte) DHCPMessage.DISCOVER;
        messageOut.setOption(53, opt);
        mySocket.send(messageOut);
        DHCPMessage messageIn = new DHCPMessage();
        mySocket.receive(messageIn);
        messageIn.printMessage();
        System.out.println("Destination Address:  " + messageIn.getDestinationAddress());
        System.out.println("Ch Address:  " + Arrays.toString(messageIn.getChaddr()));
        System.out.println("Siaddr:  " + Arrays.toString(messageIn.getSiaddr()));
        System.out.println("Ciaddr: " + Arrays.toString(messageIn.getCiaddr()));
        System.out.println("Option54: " + Arrays.toString(messageIn.getOption(54)));
        System.out.println(InetAddress.getByAddress(messageIn.getOption(54)));
    } finally {
        IOUtils.closeQuietly(mySocket);
    }
}
Also used : DHCPMessage(edu.bucknell.net.JDHCP.DHCPMessage) DHCPSocket(edu.bucknell.net.JDHCP.DHCPSocket) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DHCPMessage (edu.bucknell.net.JDHCP.DHCPMessage)1 DHCPSocket (edu.bucknell.net.JDHCP.DHCPSocket)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1