Exemplo n.º 1
0
  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;
  }
Exemplo n.º 2
0
 private DHCPSocket openSocket(DHCPSocket s) {
   try {
     s = new DHCPSocket(68);
   } catch (SocketException se) {
     if (s != null) s.close();
     lastError = se.getMessage();
     return null;
   }
   return s;
 }
Exemplo n.º 3
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;
  }