/** * Gets the next available address in this address pool. * * @return the next available address */ public InetAddress getNextAvailableAddress() { if (freeList != null) { BigInteger next = freeList.getNextFree(); if (next != null) { try { InetAddress ip = InetAddress.getByAddress(next.toByteArray()); int pingCheckTimeout = DhcpServerPolicies.globalPolicyAsInt(Property.V4_PINGCHECK_TIMEOUT); if (pingCheckTimeout > 0) { try { if (ip.isReachable(pingCheckTimeout)) { log.warn("Next free address answered ping check: " + ip.getHostAddress()); setUsed(ip); return getNextAvailableAddress(); // try again } } catch (IOException ex) { log.error("Failed to perform v4 ping check: " + ex); } } return ip; } catch (UnknownHostException ex) { log.error("Unable to build IPv4 address from next free: " + ex); } } } return null; }
@Override public void setFree(InetAddress addr) { if (contains(addr)) { BigInteger prefix = new BigInteger(addr.getAddress()); BigInteger start = new BigInteger(subnet.getSubnetAddress().getAddress()); freeList.setFree(prefix.subtract(start).divide(calculatePrefix())); } }
/** Returns the next random double between 0 and 1 */ public static double nextDouble() { Random random = getRandom(); double value = random.nextDouble(); if (!_isTest) _freeRandomList.free(random); return value; }
/** Returns the next random int. */ public static int nextInt(int n) { Random random = getRandom(); int value = random.nextInt(n); if (!_isTest) _freeRandomList.free(random); return value; }
/** Returns the next random long. */ public static long getRandomLong() { Random random = getRandom(); long value = random.nextLong(); if (!_isTest) _freeRandomList.free(random); return value; }
/** Returns the random generator. */ private static Random getRandom() { if (_isTest) { return _testRandom; } Random random = _freeRandomList.allocate(); if (random == null) random = new SecureRandom(); return random; }
@Override public InetAddress getNextAvailableAddress() { InetAddress result = null; if (freeList != null) { BigInteger start = new BigInteger(subnet.getSubnetAddress().getAddress()); BigInteger next = freeList.getNextFree(); if (next != null) { // TODO: if there are no more free addresses, then find one // that has been released or has been expired try { BigInteger prefix = next.multiply(calculatePrefix()); result = InetAddress.getByAddress(start.add(prefix).toByteArray()); } catch (Exception ex) { log.error("Unable to build IPv6 prefix from next free: " + ex); } } } return result; }
public String freeListToString() { return freeList.toString(); }
/** * Sets an IP address in this address pool as free. * * @param addr the address to set free */ public void setFree(InetAddress addr) { if (contains(addr)) { freeList.setFree(new BigInteger(addr.getAddress())); } }