if ($_POST['action'] == "add" && $_POST['masterSubnetId'] == 0) { /* first verify user input */ $errors = verifyCidr($_POST['subnet']); /* check for overlapping */ if ($section['strictMode'] == 1 && !$isFolder) { /* verify that no overlapping occurs if we are adding root subnet only check for overlapping if vrf is empty or not exists! */ if ($overlap = verifySubnetOverlapping($_POST['sectionId'], $_POST['subnet'], $_POST['vrfId'])) { $errors[] = $overlap; } } } else { if ($_POST['action'] == "add") { /* first verify user input */ $errors = verifyCidr($_POST['subnet']); /* verify that nested subnet is inside root subnet */ if ($section['strictMode'] == 1 && !$isFolder) { if (!($overlap = verifySubnetNesting($_POST['masterSubnetId'], $_POST['subnet']))) { $errors[] = _('Nested subnet not in root subnet!'); } } /* verify that no overlapping occurs if we are adding nested subnet */ if ($section['strictMode'] == 1 && !$isFolder) { if ($overlap = verifyNestedSubnetOverlapping($_POST['sectionId'], $_POST['subnet'], $_POST['vrfId'], $_POST['masterSubnetId'])) { $errors[] = $overlap; } } } else { if ($_POST['action'] == "edit") { if ($section['strictMode'] == 1 && !$isFolder) {
} } /* import each record to database */ foreach ($allSubnets as $subnet) { //get number $m = str_replace("subnet-", "", $subnet); //set subnet details $subnetDetails['action'] = "add"; $subnetDetails['subnet'] = $_POST['subnet-' . $m]; $subnetDetails['sectionId'] = $_POST['section-' . $m]; $subnetDetails['description'] = $_POST['description-' . $m]; $subnetDetails['VLAN'] = $_POST['vlan-' . $m]; $subnetDetails['masterSubnetId'] = 0; //cidr if (verifyCidr($subnetDetails['subnet'])) { $errors[] = verifyCidr($subnetDetails['subnet']); } else { if (verifySubnetOverlapping($subnetDetails['sectionId'], $subnetDetails['subnet'], "000")) { $errors[] = verifySubnetOverlapping($subnetDetails['sectionId'], $subnetDetails['subnet'], "000"); } else { if (verifyNestedSubnetOverlapping($subnetDetails['sectionId'], $subnetDetails['subnet'], "000")) { $errors[] = verifyNestedSubnetOverlapping($subnetDetails['sectionId'], $subnetDetails['subnet'], "000"); } else { $subnetInsert[] = $subnetDetails; } } } } /* print errors if they exist or success */ if (!isset($errors)) { $errors2 = 0;
<?php /** * * Script to calculate IP subnetting * */ /* include required scripts */ require_once '../../functions/functions.php'; # check referer and requested with CheckReferrer(); # get requested IP addresses $cidr = $_POST['cidr']; # verify input CIDR $errors = verifyCidr($cidr, 0); # die on errors if (sizeof($errors) != 0) { die('<div class="alert alert-danger alert-absolute">' . _('Invalid input') . ': ' . $errors[0] . '</div>'); } /* calculate results */ $ipCalcResults = calculateIpCalcResult($cidr); ?> <hr> <h4><?php print _('Subnetting details for'); ?> <?php print $cidr; ?> :</h4>
/** * verify ip address /mask 10.10.10.10./24 - CIDR * * if subnet == 0 we dont check if IP is subnet -> needed for ipCalc */ function getSubnetNetworkAddress($newSubnet) { $type = IdentifyAddress($cidr); /* IPv4 */ if ($type == "IPv4") { $resized = getIpv4NetworkAddress($newSubnet); if (verifyCidr($resized, 0)) { return false; } } else { // TODO: IPv6 not yet supported here return false; } return $resized; }
/** * Function, that calculates first possible subnet from provided subnet and number of next free IP addresses */ function getFirstPossibleSubnet($subnet, $free, $print = true) { // check IP address type (v4/v6) $type = IdentifyAddress($subnet); // set max possible mask for IP range if ($type == "IPv6") { $maxmask = 128; } else { $maxmask = 32; } // calculate maximum possible IP mask $mask = floor(log($free) / log(2)); $mask = $maxmask - $mask; // we have now maximum mask. We need to verify if subnet is valid // otherwise add 1 to $mask and go to $maxmask for ($m = $mask; $m <= $maxmask; $m++) { //validate $err = verifyCidr($subnet . "/" . $m, 1); if (sizeof($err) == 0) { //ok, it is possible! $result = $subnet . "/" . $m; break; } } //print or return? if ($print) { print $result; } else { return $result; } }