/** * Returns first available subnet address, false if none * * @access public * @param int $subnetId * @param obj $Subnets * @return int / false */ public function get_first_available_address($subnetId, $Subnets) { # fetch all addresses in subnet and subnet $addresses = $this->fetch_subnet_addresses($subnetId, "ip_addr", "asc"); $subnet = (array) $Subnets->fetch_subnet(null, $subnetId); # get max hosts $max_hosts = $Subnets->get_max_hosts($subnet['mask'], $this->identify_address($subnet['subnet'])); # full subnet? if (sizeof($addresses) >= $max_hosts) { return false; } //full subnet # set type $ip_version = $this->identify_address($subnet['subnet']); # get first diff > 1 if (sizeof($addresses) > 0) { foreach ($addresses as $k => $ipaddress) { # check subnet and first IP if ($k == 0) { # /31 fix if ($subnet['mask'] == 31) { if (gmp_strval(gmp_sub($addresses[$k]->ip_addr, $subnet['subnet'])) > 0) { return gmp_strval($subnet['subnet']); } } else { if (gmp_strval(gmp_sub($addresses[$k]->ip_addr, $subnet['subnet'])) > 1) { return gmp_strval(gmp_add($subnet['subnet'], 1)); } elseif ($ip_version == "IPv6") { if (sizeof($addresses) == 1) { if (gmp_strval(gmp_sub($addresses[$k]->ip_addr, $subnet['subnet'])) == 0) { return gmp_strval(gmp_add($subnet['subnet'], 1)); } } } } } else { if (gmp_strval(gmp_sub($addresses[$k]->ip_addr, $addresses[$k - 1]->ip_addr)) > 1) { return gmp_strval(gmp_add($addresses[$k - 1]->ip_addr, 1)); } } } return gmp_strval(gmp_add($addresses[$k]->ip_addr, 1)); } else { # /32, /31 if ($subnet['mask'] == 32 || $subnet['mask'] == 31 || $ip_version == "IPv6") { return $subnet['subnet']; } else { return gmp_strval(gmp_add($subnet['subnet'], 1)); } } }