Exemplo n.º 1
0
  /**
   * Receives a datagram packet containing a DHCP Message into a DHCPMessage object.
   *
   * @return true if message is received, false if timeout occurs.
   * @param outMessage DHCPMessage object to receive new message into
   */
  public synchronized boolean receive(DHCPMessage outMessage) {
    try {
      DatagramPacket incoming = new DatagramPacket(new byte[packetSize], packetSize);
      // gSocket.
      receive(incoming); // block on receive for SO_TIMEOUT

      outMessage.internalize(incoming.getData());
    } catch (java.io.IOException e) {
      return false;
    } // end catch
    return true;
  }
Exemplo n.º 2
0
  private Object[] acquireAddress(int xid, byte hwaddr[], int sleepTime) {
    Object results[] = new Object[5];
    results[0] = "n/a";
    results[1] = String.valueOf(0);
    results[2] = "error";
    results[3] = "no data";
    results[4] = "no data";
    int releaseTime = -1;
    DHCPSocket socket = null;
    DHCPMessage outMessage = new DHCPMessage();
    DHCPMessage replyMessage = new DHCPMessage();
    byte offeredAddr[] = new byte[4];
    byte backupAddr[] = new byte[4];

    Random r = new Random();

    if ((socket = openSocket(socket)) == null) {
      results[3] = results[4] = lastError;
      return results;
    }
    outMessage.setChaddr(hwaddr);
    if (IP.length() > 0) {
      outMessage.setOption(50, stringToIP(IP));
    }
    for (int i = 0; i < 10; i++) {
      if (!sendMessage(lastError, outMessage, socket, xid, 1)) {
        results[3] = results[4] = lastError;
        socket.close();
        return results;
      }

      try {
        Thread.sleep(sleepTime);
      } catch (InterruptedException e) {

      }

      if (socket.receive(replyMessage)) {
        if (checkOffer(replyMessage, xid)) {
          offeredAddr = replyMessage.getYiaddr();
          System.arraycopy(offeredAddr, 0, backupAddr, 0, 4);
          break;
        }
        if (unavailable) {
          results[3] = results[4] = lastError;
          socket.close();
          return results;
        }
      }
      if (i == 9) {
        results[3] = results[4] = "Timed out waiting for DHCPOFFER";
        socket.close();
        return results;
      }
    }

    releaseTime = 1;
    outMessage.setOption(54, replyMessage.getOption(54));
    sendMessage(lastError, outMessage, socket, xid, 3, offeredAddr);
    if (!socket.receive(replyMessage)) {
      results[3] = results[4] = "Timed out waiting for DHCPACK";
      socket.close();
      return results;
    }
    if (replyMessage.getXid() != xid) {
      results[3] = results[4] = "Received invalid DHCPACK from server";
      socket.close();
      return results;
    }

    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {

    }

    outMessage.setCiaddr(backupAddr);
    outMessage.setOption(50, new byte[4]);
    outMessage.setOption(51, new byte[4]);
    outMessage.setOption(54, replyMessage.getOption(54));
    sendMessage(lastError, outMessage, socket, r.nextInt(), 7);
    socket.close();
    long totalDuration = System.currentTimeMillis() - startTime;
    String stateString =
        (new StringBuilder()).append("leased address ").append(ipToString(backupAddr)).toString();
    String duration = (float) totalDuration / 1000F + " sec";
    results[0] = results[1] = duration;
    results[2] = "ok";
    results[3] =
        (new StringBuilder()).append("leased address ").append(ipToString(backupAddr)).toString();
    results[4] = stateString;
    return results;
  }