private boolean sendMessage(
      String status, DHCPMessage outMessage, DHCPSocket socket, int xid, int type) {
    outMessage.setOp((byte) 1);
    outMessage.setHtype((byte) 1);
    outMessage.setHlen((byte) 6);
    outMessage.setHops((byte) 0);
    outMessage.setXid(xid);
    outMessage.setSecs((short) 0);
    if (type == 1 || type == 3) {
      outMessage.setFlags((short) -32768);
    } else {
      outMessage.setFlags((short) 0);
    }
    byte initAddr[] = new byte[4];
    for (int i = 0; i < 4; i++) initAddr[i] = 0;

    outMessage.setCiaddr(initAddr);
    outMessage.setYiaddr(initAddr);
    outMessage.setSiaddr(initAddr);
    outMessage.setGiaddr(initAddr);
    byte mtype[] = new byte[1];
    mtype[0] = (byte) type;
    outMessage.setOption(53, mtype);
    outMessage.setOption(51, intToBytes(1));
    try {
      socket.send(outMessage);
    } catch (IOException ie) {
      status = ie.getMessage();
      return false;
    }
    return true;
  }
Esempio n. 2
0
  /**
   * 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();
    // if targetOffset = true, we don't want to REQUEST the DHCP server's
    // own IP, so change it by 1, trying to avoid the subnet address
    // 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);
    messageOut.setChaddr(s_hwAddress); // set hardware address
    if (relayMode) {
      messageOut.setHops((byte) 1);
      messageOut.setGiaddr(s_myIpAddress); // set relay address for replies
    } 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);
  }