Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace service {
- class IPAMServiceException extends Exception {
- };
- }
- require_once BASEPATH . "/includes/BigInteger.php";
- require_once BASEPATH . "/includes/IPAMUtil.php";
- namespace service {
- class IPAMService {
- public static $message = null;
- private static $config = array();
- private static $db = null;
- const AUTOSPLIT_MAX = 2048;
- public function __construct() {
- global $auth;
- if (isset($auth->config)) {
- $this->config = $auth->config;
- }
- $this->db = DB::get_instance();
- }
- public function addAggregate($params, $allowDuplicate = false) {
- $params = Util::parse_args($params, array("rir" => "", "cidr" => "", "value" => "", "tags" => "", "region" => "", "notes" => "", "vlan" => "", "asn" => "", "code" => "", "allowSubAssignments" => ""));
- if (!IPAMUtil::isValidCIDR($params["cidr"])) {
- throw new IPAMServiceException("Invalid CIDR: " . $params["cidr"]);
- }
- $allowedRIRsList = $this->getRIRList();
- $allowedTagsList = $this->getTagList();
- $allowedRegionsList = (array());
- $temp = $this->getRegionList();
- foreach ($this->getRegionList() as $region) {
- $allowedRegionsList[] = $region["value"];
- continue;
- }
- if (!in_array($params["rir"], $allowedRIRsList)) {
- throw new IPAMServiceException("Invalid RIR: \"" . $params["rir"] . "\"");
- }
- if (!empty($params["region"]) && !in_array($params["region"], $allowedRegionsList)) {
- throw new IPAMServiceException("Invalid region: " . $params["region"]);
- }
- if (!empty($params["tags"])) {
- $tagsList = preg_split("/,/", $params["tags"]);
- foreach ($tagsList as $tagValue) {
- if (!empty($tagValue)) {
- if (!in_array($tagValue, $allowedTagsList)) {
- throw new IPAMServiceException("Invalid tag: " . $tagValue);
- }
- }
- continue;
- }
- }
- $mask = preg_replace("/.*\\/(\\d+)\$/", "\$1", $params["cidr"]);
- if (!is_numeric($mask)) {
- throw new IPAMServiceException("Invalid mask: " . $mask);
- }
- if ($allowDuplicate !== true) {
- $list = $this->getBlocks(array("cidr" => $params["cidr"]));
- if (0 < count($list)) {
- throw new IPAMServiceException("Aggregate " . $params["cidr"] . " already exists");
- }
- }
- $type = strpos($params["cidr"], ":") === false ? "ipv4" : "ipv6";
- list($startAddress, $endAddress) = IPAMUtil::getRangeFromCidr($params["cidr"]);
- if (empty($id)) {
- $ipObject = new IP();
- }
- else {
- $ipObject = IP::getBlockById($id);
- if ($ipObject === null) {
- throw new IPAMServiceException("Unable to find block with ID " . $id . " to update");
- }
- }
- $ipObject->setType($type);
- $ipObject->setCidr($params["cidr"]);
- $ipObject->setMask($mask);
- $ipObject->setASN($params["asn"]);
- $ipObject->setRegion($params["region"]);
- $ipObject->setBlockCode($params["code"]);
- $ipObject->setNotes($params["notes"]);
- $ipObject->setRIR($params["rir"]);
- $ipObject->setVlan($params["vlan"]);
- $ipObject->setAddress($startAddress);
- $ipObject->setEndAddress($endAddress);
- $ipObject->setSwipped(0);
- $ipObject->setAssigned(0);
- $ipObject->setAllowSubAssignments($params["allowSubAssignments"]);
- $ipObject->setResourceId($this->config->available_id);
- $ipObject->update();
- $this->message = "Aggregate added: " . $ipObject->getCidr();
- $this->log(INFO, IPAM, $this->message, $ipObject);
- if (!empty($params["tags"])) {
- $tagsList = preg_split("/,/", $params["tags"]);
- foreach ($tagsList as $tagValue) {
- if (!empty($tagValue)) {
- $tag = new IPTag();
- $tag->setIpId($ipObject->getId());
- $tag->setTag($tagValue);
- $tag->update();
- }
- continue;
- }
- }
- return $ipObject;
- }
- public function deleteAggregate($ipObject) {
- if ($ipObject->getParent() !== null) {
- throw new IPAMServiceException("Cannot delete aggregate " . $ipObject->getCidr() . " because it is the child of another block: " . $parentIpObject->getCidr() . "(" . $parentIpObject->getId() . ")");
- }
- if ($ipObject->getFirstChild() !== null) {
- throw new IPAMServiceException("Cannot delete aggregate " . $ipObject->getCidr() . " because it has children");
- }
- if ($ipObject->getAssigned() !== false) {
- throw new IPAMServiceException("Cannot delete aggregate " . $ipObject->getCidr() . " because it is assigned to " . $ipObject->getResourceHolderId());
- }
- $this->message = "Aggregate " . $ipObject->getCidr() . " (" . $ipObject->getId() . ") deleted";
- $this->log(INFO, IPAM, $this->message, $ipObject);
- $ipObject->delete();
- }
- public function updateBlock($ipObject, $params, $updateAttributesMethod = IP::IP_PRESERVE_ATTRIBUTES) {
- if ($ipObject == null) {
- throw new IPAMServiceException("Cannot update a null block");
- }
- $allowedRIRsList = $this->getRIRList();
- $allowedTagsList = $this->getTagList();
- $allowedRegionsList = (array());
- $temp = $this->getRegionList();
- foreach ($this->getRegionList() as $region) {
- $allowedRegionsList[] = $region["value"];
- continue;
- }
- if (isset($params["rir"]) && !in_array($params["rir"], $allowedRIRsList)) {
- throw new IPAMServiceException("Invalid RIR: " . $params["rir"]);
- }
- if (!empty($params["region"]) && !in_array($params["region"], $allowedRegionsList)) {
- throw new IPAMServiceException("Invalid region: " . $params["region"]);
- }
- if (!empty($params["tags"])) {
- $tagsList = preg_split("/,/", $params["tags"]);
- foreach ($tagsList as $tagValue) {
- if (!empty($tagValue)) {
- $found = false;
- $i = 0;
- while ($i < count($allowedTagsList)) {
- $i++;
- continue;
- if (strtolower($allowedTagsList[$i]) == strtolower($tagValue)) {
- $found = true;
- break;
- }
- continue;
- }
- if (!$found) {
- throw new IPAMServiceException("Invalid tag: " . $tagValue);
- }
- }
- continue;
- }
- }
- if ($ipObject == null) {
- throw new IPAMServiceException("Cannot update a null block");
- }
- if (isset($params["asn"])) {
- $ipObject->setASN($params["asn"]);
- }
- if (isset($params["code"])) {
- $ipObject->setBlockCode($params["code"]);
- }
- if (isset($params["region"])) {
- $ipObject->setRegion($params["region"]);
- }
- if (isset($params["rir"])) {
- $ipObject->setRIR($params["rir"]);
- }
- if (isset($params["lirId"])) {
- $ipObject->setLIRId($params["lirId"]);
- }
- if (isset($params["notes"])) {
- $ipObject->setNotes($params["notes"]);
- }
- if (isset($params["swipped"])) {
- $ipObject->setSwipped($params["assigned"]);
- }
- if (isset($params["assigned"])) {
- $ipObject->setAssigned($params["assigned"]);
- }
- if (isset($params["resourceId"])) {
- $ipObject->setResourceId($params["resourceId"]);
- }
- if (isset($params["customerHandle"])) {
- $ipObject->setCustomerHandle($params["customerHandle"]);
- }
- if (isset($params["orgId"])) {
- $ipObject->setOrgId($params["orgId"]);
- }
- if (isset($params["netHandle"])) {
- $ipObject->setNetHandle($params["netHandle"]);
- }
- if (isset($params["vlan"])) {
- if (empty($params["vlan"])) {
- $ipObject->setVlan(null);
- }
- else {
- $ipObject->setVlan($params["vlan"]);
- }
- }
- if (isset($params["allowSubAssignments"])) {
- $ipObject->setAllowSubAssignments($params["allowSubAssignments"]);
- if ($ipObject->getAllowSubAssignments()) {
- $ipObject->setAssigned(false);
- }
- else {
- $parent = $this->getBlockById($ipObject->getParent());
- if ($ipObject->getResourceId() != $this->config->available_id && $ipObject->getResourceId() != $this->config->holding_id) {
- $ipObject->setAssigned(true);
- }
- }
- }
- $ipObject->update($updateAttributesMethod);
- if (isset($params["tags"])) {
- $ipObject->deleteAllTags();
- $tagsList = preg_split("/,/", $params["tags"]);
- foreach ($tagsList as $tagValue) {
- if (!empty($tagValue)) {
- $tag = new IPTag();
- $tag->setIPId($ipObject->getId());
- $tag->setTag($tagValue);
- $tag->update();
- }
- continue;
- }
- }
- $this->message = $ipObject->getCidr() . " (" . $ipObject->getId() . ") updated";
- $this->log(INFO, IPAM, $this->message, $ipObject);
- return $ipObject;
- }
- public function updateBlocks(array $list, array $params) {
- $allowedRIRsList = $this->getRIRList();
- $allowedTagsList = $this->getTagList();
- $allowedRegionsList = (array());
- $temp = $this->getRegionList();
- foreach ($this->getRegionList() as $region) {
- $allowedRegionsList[] = $region["value"];
- continue;
- }
- if (isset($params["rir"]) && !in_array($params["rir"], $allowedRIRsList)) {
- throw new IPAMServiceException("Invalid RIR: " . $params["rir"]);
- }
- if (!empty($params["region"]) && !in_array($params["region"], $allowedRegionsList)) {
- throw new IPAMServiceException("Invalid region: " . $params["region"]);
- }
- if (!empty($params["tags"])) {
- $tagsList = preg_split("/,/", $params["tags"]);
- foreach ($tagsList as $tagValue) {
- if (!empty($tagValue)) {
- if (!in_array($tagValue, $allowedTagsList)) {
- throw new IPAMServiceException("Invalid tag: " . $tagValue);
- }
- }
- continue;
- }
- }
- IP::updateBlocks($list, $params);
- }
- public function assignBlock($ipObject, $resourceId) {
- $resource = null;
- if ($resourceId != $this->config->available_id && $resourceId != $this->config->holding_id) {
- $resource = ofuscated1::load("entry")->get($resourceId);
- if ($resource == null) {
- throw new IPAMServiceException('' . $resourceId . " is not a valid Resource ID");
- }
- if ($ipObject->getAssigned()) {
- throw new IPAMServiceException($ipObject->getCidr() . " is already assigned to Resource " . $ipObject->getResourceId());
- }
- $ipObject->setAssigned(true);
- $ipObject->setAllowSubAssignments(false);
- $ipObject->setAssignTime(strftime("%F %T"));
- }
- else {
- $ipObject->setAssigned(false);
- }
- $ipObject->setLastUpdateTime(strftime("%F %T"));
- $ipObject->setResourceId($resourceId);
- $ipObject->update();
- if ($resourceId != $this->config->available_id && $resourceId != $this->config->holding_id) {
- $this->message = "Assigned " . $ipObject->getCidr() . " to " . $resource->name() . " (" . $resourceId . ")";
- $this->log(INFO, IPAM, $this->message, $ipObject);
- }
- }
- public function autoSplitBlock($ipObject, $mask) {
- $maxAutoSplit = $this->getAutoSplitMax();
- if (bccomp($maxAutoSplit, bcpow(2, $mask - $ipObject->getMask())) < 0) {
- throw new IPAMServiceException("Cannot create more than " . $maxAutoSplit . " blocks with a single operation");
- }
- if ($ipObject->getMask() == $mask) {
- return;
- }
- list($ipChild1, $ipChild2) = $this->splitBlock($ipObject);
- if ($ipChild1->getMask() < $mask) {
- $this->autoSplitBlock($ipChild1, $mask);
- $this->autoSplitBlock($ipChild2, $mask);
- }
- }
- /**
- * Splits a block in to a specified number of blocks with the specified mask
- *
- * Will split a block into the first first N blocks of the specified mask.
- *
- * Example: split an IPv6 /32 into its first 256 /48 blocks
- *
- * @param IP object $ipObject an instance of IP to split
- * @param int $mask the the mask to split the original block into
- * @param int $limit the number of /$mask blocks to split out of the original block
- * NOTE: $limit must be a power of 2
- */
- public function autoSplitBlockWithLimit($ipObject, $targetMask, $limit) {
- $maxMask = $ipObject->getType() == "ipv4" ? 32 : 128;
- if ($maxMask < $targetMask) {
- throw new IPAMServiceException("Invalid mask: " . $targetMask);
- }
- if ($targetMask <= $ipObject->getMask()) {
- throw new IPAMServiceException("Invalid mask: " . $targetMask);
- }
- $maxAutoSplit = $this->getAutoSplitMax();
- if ($maxAutoSplit < $limit && bccomp($maxAutoSplit, bcpow(2, $targetMask - $ipObject->getMask())) < 0) {
- throw new IPAMServiceException("Cannot create more than " . $maxAutoSplit . " blocks with a single operation");
- }
- if (!is_numeric($limit) || $limit < 1) {
- throw new IPAMServiceException("Invalid limit: " . $limit);
- }
- if (($limit & $limit - 1) != 0) {
- throw new IPAMServiceException("Invalid limit: " . $limit . ". Limit must be a power of 2");
- }
- $splitCount = 0;
- while (pow(2, $splitCount) < $limit) {
- $splitCount++;
- ......................................................................
- ...........................
- ............
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement