Search in sources :

Example 1 with DHCPSocket

use of org.opennms.jdhcp.DHCPSocket in project opennms by OpenNMS.

the class DhcpDetectorTest method testJdhcp.

@Test(timeout = 90000)
public void testJdhcp() throws IOException {
    assumeTrue(m_extendedTests);
    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.getDestination());
        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(org.opennms.jdhcp.DHCPMessage) DHCPSocket(org.opennms.jdhcp.DHCPSocket) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 DHCPMessage (org.opennms.jdhcp.DHCPMessage)1 DHCPSocket (org.opennms.jdhcp.DHCPSocket)1