Search in sources :

Example 1 with DHCPMessage

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

the class Message method readObject.

private void readObject(ObjectInputStream in) throws MalformedPacketException, ClassNotFoundException, IOException {
    m_target = (InetAddress) in.readObject();
    byte[] buf = new byte[in.readInt()];
    in.readFully(buf, 0, buf.length);
    m_dhcpmsg = new DHCPMessage(buf);
}
Also used : DHCPMessage(org.opennms.jdhcp.DHCPMessage)

Example 2 with DHCPMessage

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

the class Poller method getPollingRequest.

/**
 * Returns a DHCP DISCOVER, INFORM, or REQUEST message that can be sent to
 * the DHCP server. DHCP server should respond with a DHCP OFFER, ACK, or
 * NAK message in response..
 *
 * @param (InetAddress) addr The address to poll
 * @param (byte) mType The type of DHCP message to send (DISCOVER, INFORM,
 *        or REQUEST)
 * @return The message to send to the DHCP server.
 */
private static Message getPollingRequest(InetAddress addr, byte mType) {
    int xid = 0;
    synchronized (Poller.class) {
        xid = ++m_nextXid;
    }
    DHCPMessage messageOut = new DHCPMessage();
    byte[] rawIp = addr.getAddress();
    // and the broadcast address.
    if (targetOffset) {
        if (rawIp[3] % 2 == 0 && rawIp[3] != 0) {
            --rawIp[3];
        } else {
            ++rawIp[3];
        }
    }
    // fill DHCPMessage object
    // 
    messageOut.setOp((byte) 1);
    messageOut.setHtype((byte) 1);
    messageOut.setHlen((byte) 6);
    messageOut.setXid(xid);
    messageOut.setSecs((short) 0);
    // set hardware address
    messageOut.setChaddr(s_hwAddress);
    if (relayMode) {
        messageOut.setHops((byte) 1);
        // set relay address for replies
        messageOut.setGiaddr(s_myIpAddress);
    } else {
        messageOut.setHops((byte) 0);
        messageOut.setFlags(BROADCAST_FLAG);
    }
    messageOut.setOption(MESSAGE_TYPE, new byte[] { mType });
    if (mType == DHCPMessage.REQUEST) {
        if (reqTargetIp) {
            messageOut.setOption(REQUESTED_IP, rawIp);
            messageOut.setCiaddr(rawIp);
        } else {
            messageOut.setOption(REQUESTED_IP, s_requestIpAddress);
            messageOut.setCiaddr(s_requestIpAddress);
        }
    }
    if (mType == DHCPMessage.INFORM) {
        messageOut.setOption(REQUESTED_IP, s_myIpAddress);
        messageOut.setCiaddr(s_myIpAddress);
    }
    return new Message(addr, messageOut);
}
Also used : DHCPMessage(org.opennms.jdhcp.DHCPMessage) DHCPMessage(org.opennms.jdhcp.DHCPMessage)

Example 3 with DHCPMessage

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

the class Receiver method run.

/**
 * <p>run</p>
 */
@Override
public void run() {
    // 
    synchronized (this) {
        m_status = RUNNING;
    }
    byte[] dgbuf = new byte[2048];
    // 
    for (; ; ) {
        try {
            DatagramPacket pkt = new DatagramPacket(dgbuf, dgbuf.length);
            m_receiver.receive(pkt);
            LOG.debug("got a DHCP response.");
            Message msg = new Message(pkt.getAddress(), new DHCPMessage(pkt.getData()));
            synchronized (m_clients) {
                Iterator<Client> iter = m_clients.iterator();
                if (!iter.hasNext()) {
                    LOG.debug("No client waiting for response.");
                }
                while (iter.hasNext()) {
                    Client c = iter.next();
                    if (c.getStatus() == RUNNING) {
                        try {
                            LOG.debug("sending DHCP response pkt to client {}", c.getName());
                            c.sendMessage(msg);
                        } catch (IOException ex) {
                            LOG.warn("Error sending response to client {}", c.getName());
                        }
                    } else if (c.getStatus() == STOPPED) {
                        LOG.debug("Removing stale client {}", c.getName());
                        iter.remove();
                    }
                }
            }
        } catch (InterruptedIOException ex) {
        // ignore
        } catch (ArrayIndexOutOfBoundsException ex) {
            LOG.warn("An error occurred when reading DHCP response. Ignoring exception: ", ex);
        } catch (IOException ex) {
            synchronized (this) {
                if (m_status == RUNNING)
                    LOG.warn("Failed to read message, I/O error", ex);
            }
            break;
        } catch (Throwable t) {
            synchronized (this) {
                if (m_status == RUNNING)
                    LOG.warn("Undeclared throwable caught", t);
            }
            break;
        }
        synchronized (this) {
            if (m_status != RUNNING)
                break;
        }
    }
    synchronized (this) {
        m_status = STOP_PENDING;
    }
    // close the datagram socket
    // 
    m_receiver.close();
    synchronized (this) {
        m_status = STOPPED;
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) DHCPMessage(org.opennms.jdhcp.DHCPMessage) DatagramPacket(java.net.DatagramPacket) DHCPMessage(org.opennms.jdhcp.DHCPMessage) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 4 with DHCPMessage

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

the class Receiver2 method run.

/**
 * <p>run</p>
 */
@Override
public void run() {
    // 
    synchronized (this) {
        m_status = RUNNING;
    }
    byte[] dgbuf = new byte[2048];
    // 
    for (; ; ) {
        try {
            DatagramPacket pkt = new DatagramPacket(dgbuf, dgbuf.length);
            m_receiver.receive(pkt);
            LOG.debug("got a DHCP response.");
            Message msg = new Message(pkt.getAddress(), new DHCPMessage(pkt.getData()));
            synchronized (m_clients) {
                Iterator<Client> iter = m_clients.iterator();
                if (!iter.hasNext()) {
                    LOG.debug("No client waiting for response.");
                }
                while (iter.hasNext()) {
                    Client c = iter.next();
                    if (c.getStatus() == RUNNING) {
                        try {
                            LOG.debug("sending DHCP response pkt to client {}", c.getName());
                            c.sendMessage(msg);
                        } catch (IOException ex) {
                            LOG.warn("Error sending response to client {}", c.getName());
                        }
                    } else if (c.getStatus() == STOPPED) {
                        LOG.debug("Removing stale client {}", c.getName());
                        iter.remove();
                    }
                }
            }
        } catch (InterruptedIOException ex) {
        // ignore
        } catch (ArrayIndexOutOfBoundsException ex) {
            LOG.warn("An error occurred when reading DHCP response. Ignoring exception: ", ex);
        } catch (NegativeArraySizeException ex) {
            // Ignore cases where the target returns a badly-formatted DHCP response
            // Fixes http://bugzilla.opennms.org/show_bug.cgi?id=3445
            LOG.warn("An error occurred when reading DHCP response. Ignoring exception: ", ex);
        } catch (IOException ex) {
            synchronized (this) {
                if (m_status == RUNNING)
                    LOG.warn("Failed to read message, I/O error", ex);
            }
            break;
        } catch (Throwable t) {
            synchronized (this) {
                if (m_status == RUNNING)
                    LOG.warn("Undeclared throwable caught", t);
            }
            break;
        }
        synchronized (this) {
            if (m_status != RUNNING)
                break;
        }
    }
    synchronized (this) {
        m_status = STOP_PENDING;
    }
    // close the datagram socket
    // 
    m_receiver.close();
    synchronized (this) {
        m_status = STOPPED;
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) DHCPMessage(org.opennms.jdhcp.DHCPMessage) DatagramPacket(java.net.DatagramPacket) DHCPMessage(org.opennms.jdhcp.DHCPMessage) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 5 with DHCPMessage

use of org.opennms.jdhcp.DHCPMessage 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

DHCPMessage (org.opennms.jdhcp.DHCPMessage)5 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 DatagramPacket (java.net.DatagramPacket)2 Test (org.junit.Test)1 DHCPSocket (org.opennms.jdhcp.DHCPSocket)1