/** * Calculates IPv6 from cidr * * @access private * @param mixed $cidr * @return void */ private function calculate_IPv6_calc_results($cidr) { # initialize subnets Class $Subnets = new Subnets($this->Database); # Initialize PEAR NET object $this->initialize_pear_net_IPv6(); # set ip address type $out['Type'] = 'IPv6'; # calculate network details $out['Host address'] = $cidr; $out['Host address'] = $this->Net_IPv6->compress($out['Host address'], 1); $out['Host address (uncompressed)'] = $this->Net_IPv6->uncompress($out['Host address']); $mask = $this->Net_IPv6->getNetmaskSpec($cidr); $subnet = $this->Net_IPv6->getNetmask($cidr); $out['Subnet prefix'] = $this->Net_IPv6->compress($subnet) . '/' . $mask; $out['Prefix length'] = $this->Net_IPv6->getNetmaskSpec($cidr); # get reverse DNS entries $out['Host Reverse DNS'] = $this->reverse_IPv6($out['Host address (uncompressed)']); $out['Subnet Reverse DNS'] = $this->reverse_IPv6($subnet, $mask); # if IP == subnet clear the Host fields and Host Reverse DNS if ($out['Host address'] == $out['Subnet prefix']) { $out['Host address'] = '/'; $out['Host address (uncompressed)'] = '/'; unset($out['Host Reverse DNS']); } # /min / max hosts $maxIp = gmp_strval(gmp_add(gmp_pow(2, 128 - $mask), $this->ip2long6($subnet))); $maxIp = gmp_strval(gmp_sub($maxIp, 1)); $out['Min host IP'] = $subnet; $out['Max host IP'] = $this->long2ip6($maxIp); $out['Number of hosts'] = $Subnets->get_max_hosts($mask, "IPv6"); # set address type $out['Address type'] = $this->get_ipv6_address_type($cidr); # result return $out; }
/** * Returns array of all addresses to be scanned inside subnet defined with subnetId * * @access public * @param mixed $subnetId * @return void */ public function prepare_addresses_to_discover_subnetId($subnetId, $die) { # initialize classes $Subnets = new Subnets($this->Database); //subnet ID is provided, fetch subnet $subnet = $Subnets->fetch_subnet(null, $subnetId); if ($subnet === false) { if ($die) { die(json_encode(array("status" => 1, "error" => "Invalid subnet ID provided"))); } else { return array(); } } // we should support only up to 4094 hosts! if ($Subnets->get_max_hosts($subnet->mask, "IPv4") > 4094 && php_sapi_name() != "cli") { if ($die) { die(json_encode(array("status" => 1, "error" => "Scanning from GUI is only available for subnets up to /20 or 4094 hosts!"))); } else { return array(); } } # set array of addresses to scan, exclude existing! $ip = $this->get_all_possible_subnet_addresses($subnet->subnet, $subnet->mask); # remove existing $ip = $this->remove_existing_subnet_addresses($ip, $subnetId); //none to scan? if (sizeof($ip) == 0) { if ($die) { die(json_encode(array("status" => 1, "error" => "Didn't find any address to scan!"))); } else { return array(); } } //return return $ip; }
// count usage foreach ($out as $k => $s) { //check if subnet has slaves and set slaves flag true/false $slaves = $Subnets->has_slaves($s->id) ? true : false; # fetch all addresses and calculate usage if ($slaves) { $addresses = $Addresses->fetch_subnet_addresses_recursive($s->id, false); $slave_subnets = (array) $Subnets->fetch_subnet_slaves($s->id); // save count $addresses_cnt = gmp_strval(sizeof($addresses)); # full ? if (sizeof($slave_subnets) > 0) { foreach ($slave_subnets as $ss) { if ($ss->isFull == 1) { # calculate max $max_hosts = $Subnets->get_max_hosts($ss->mask, $Subnets->identify_address($ss->subnet), true); # count $count_hosts = $Addresses->count_subnet_addresses($ss->id); # add $addresses_cnt = gmp_strval(gmp_add($addresses_cnt, gmp_sub($max_hosts, $count_hosts))); } } } $subnet_usage = $Subnets->calculate_subnet_usage($addresses_cnt, $s->mask, $s->subnet, $s->isFull); //Calculate free/used etc } else { # fetch addresses in subnet $addresses_cnt = $Addresses->count_subnet_addresses($s->id); # calculate usage $subnet_usage = $Subnets->calculate_subnet_usage($addresses_cnt, $s->mask, $s->subnet, $s->isFull); }
# calculate max mask $max_new_mask = $Subnets->identify_address($Subnets->transform_to_dotted($subnet->subnet)) == "IPv4" ? 32 : 128; # die if too small if ($max_new_mask < $subnet->mask) { $Result->show("danger", _("Subnet too small to be splitted"), true, true); } $n = 2; # step $m = 0; # array id //set mask options for ($mask = $subnet->mask + 1; $mask <= $max_new_mask; $mask++) { # set vars $opts[$m]['mask'] = $mask; $opts[$m]['number'] = $n; $opts[$m]['max'] = $Subnets->get_max_hosts($mask, $Subnets->identify_address($Subnets->transform_to_dotted($subnet->subnet))); # next $m++; $n = $n * 2; # max number = 16! if ($n > 256) { $mask = 1000; } } ?> <!-- header --> <div class="pHeader"><?php print _('Split subnet'); ?> </div>
/** * Search for unused address space between 2 IPv6 addresses * * Return unused address range or false if none available * * @access protected * @param int $address1 * @param int $address2 * @param int $netmask * @param bool $empty (default: false) * @param bool $is_subnet (default: false) * @param bool $is_broadcast (default: false) * @return void */ protected function find_unused_addresses_IPv6($address1, $address2, $netmask, $empty = false, $is_subnet = false, $is_broadcast = false) { # Initialize PEAR NET object $this->initialize_pear_net_IPv6(); if ($empty) { $Subnets = new Subnets($this->Database); return array("ip" => $this->transform_to_dotted(gmp_strval($address1)) . " - " . $this->transform_to_dotted(gmp_strval($address2)), "hosts" => $Subnets->get_max_hosts($netmask, "IPv6")); } else { # calculate diff $diff = $this->calculate_address_diff($address1, $address2); # /128 if ($netmask == 128) { if ($diff > 1) { return array("ip" => $this->transform_to_dotted(gmp_strval($address1)), "hosts" => 1); } } elseif ($netmask == 127) { if ($diff == 1 && $this->is_network($address1, $netmask)) { return array("ip" => $this->transform_to_dotted($address2), "hosts" => 1); } elseif ($diff == 1 && $this->is_broadcast($address2, $netmask)) { return array("ip" => $this->transform_to_dotted($address1), "hosts" => 1); } elseif ($diff == 0) { return false; } else { return array("ip" => $this->transform_to_dotted($address1), "hosts" => 2); } } elseif ($diff == 0) { return false; } elseif ($diff == 1) { if ($is_subnet) { return array("ip" => $this->transform_to_dotted(gmp_strval($address1)), "hosts" => 1); } elseif ($is_broadcast) { return array("ip" => $this->transform_to_dotted(gmp_strval($address2)), "hosts" => 1); } else { return false; } } elseif ($diff == 2 && !$is_subnet && !$is_broadcast) { return array("ip" => $this->transform_to_dotted(gmp_strval(gmp_add($address1, 1))), "hosts" => 1); } else { if ($is_subnet) { return array("ip" => $this->transform_to_dotted(gmp_strval($address1)) . " - " . $this->transform_to_dotted(gmp_strval(gmp_sub($address2, 1))), "hosts" => $this->reformat_number(gmp_strval(gmp_sub($diff, 0)))); } elseif ($is_broadcast) { return array("ip" => $this->transform_to_dotted(gmp_strval(gmp_add($address1, 1))) . " - " . $this->transform_to_dotted(gmp_strval($address2)), "hosts" => $this->reformat_number(gmp_strval(gmp_sub($diff, 0)))); } else { return array("ip" => $this->transform_to_dotted(gmp_strval(gmp_add($address1, 1))) . " - " . $this->transform_to_dotted(gmp_strval(gmp_sub($address2, 1))), "hosts" => $this->reformat_number(gmp_strval(gmp_strval(gmp_sub($diff, 1))))); } } # default false return false; } }