self::CIDR_MIN_BITS || $segments[1] < self::CIDR_MAX_BITS), CidrOutOfRangeException::class); } $underlying = $data['allocation_ip']; try { $underlying = gethostbyname($data['allocation_ip']); $parsed = Network::parse($underlying); } catch (Exception $exception) { throw new DisplayException("Could not parse provided allocation IP address ({$underlying}): {$exception->getMessage()}", $exception); } DB::transaction(function () use ($node, $data, $parsed): void { foreach ($parsed as $ip) { foreach ($data['allocation_ports'] as $port) { // SAFETY: the allocation contract limits ports to integers and strings before this syntax parser. $portText = (string) $port; $isPort = ctype_digit($portText); $isRange = preg_match(self::PORT_RANGE_REGEX, $portText, $matches) === 1; throw_if(! $isPort && ! $isRange, InvalidPortMappingException::class, $port); if ($isRange) { // SAFETY: PORT_RANGE_REGEX captures two decimal integer strings. $start = (int) $matches[1]; // SAFETY: PORT_RANGE_REGEX captures two decimal integer strings. $end = (int) $matches[2]; $block = range($start, $end); throw_if(count($block) > self::PORT_RANGE_LIMIT, TooManyPortsInRangeException::class); throw_if($start <= self::PORT_FLOOR || $end > self::PORT_CEIL, PortOutOfRangeException::class); } else { // SAFETY: ctype_digit() above proves a decimal port string in the non-range branch. $portNumber = (int) $portText; throw_if($portNumber <= self::PORT_FLOOR || $portNumber > self::PORT_CEIL, PortOutOfRangeException::class); $block = [$portNumber]; } $insertData = []; foreach ($block as $unit) { $insertData[] = [ 'node_id' => $node->id, 'ip' => $ip->__toString(), 'port' => $unit, 'ip_alias' => $data['allocation_alias'] ?? null, 'server_id' => null, ]; } Allocation::query()->insertOrIgnore($insertData); } } }); } }