| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- namespace IPLib\Range;
- use IPLib\Address\AddressInterface;
- use IPLib\Address\IPv4;
- use IPLib\Address\Type as AddressType;
- use IPLib\Factory;
- use IPLib\ParseStringFlag;
- use IPLib\Service\BinaryMath;
- /**
- * Represents an address range in subnet format (eg CIDR).
- *
- * @example 127.0.0.1/32
- * @example ::/8
- */
- class Subnet extends AbstractRange
- {
- /**
- * Starting address of the range.
- *
- * @var \IPLib\Address\AddressInterface
- */
- protected $fromAddress;
- /**
- * Final address of the range.
- *
- * @var \IPLib\Address\AddressInterface
- */
- protected $toAddress;
- /**
- * Number of the same bits of the range.
- *
- * @var int
- */
- protected $networkPrefix;
- /**
- * The type of the range of this IP range.
- *
- * @var int|null
- *
- * @since 1.5.0
- */
- protected $rangeType;
- /**
- * The 6to4 address IPv6 address range.
- *
- * @var self|null
- */
- private static $sixToFour;
- /**
- * Initializes the instance.
- *
- * @param \IPLib\Address\AddressInterface $fromAddress
- * @param \IPLib\Address\AddressInterface $toAddress
- * @param int $networkPrefix
- *
- * @internal
- */
- public function __construct(AddressInterface $fromAddress, AddressInterface $toAddress, $networkPrefix)
- {
- $this->fromAddress = $fromAddress;
- $this->toAddress = $toAddress;
- $this->networkPrefix = $networkPrefix;
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::__toString()
- */
- public function __toString()
- {
- return $this->toString();
- }
- /**
- * @deprecated since 1.17.0: use the parseString() method instead.
- * For upgrading:
- * - if $supportNonDecimalIPv4 is true, use the ParseStringFlag::IPV4_MAYBE_NON_DECIMAL flag
- *
- * @param string|mixed $range
- * @param bool $supportNonDecimalIPv4
- *
- * @return static|null
- *
- * @see \IPLib\Range\Subnet::parseString()
- * @since 1.10.0 added the $supportNonDecimalIPv4 argument
- */
- public static function fromString($range, $supportNonDecimalIPv4 = false)
- {
- return static::parseString($range, ParseStringFlag::MAY_INCLUDE_PORT | ParseStringFlag::MAY_INCLUDE_ZONEID | ($supportNonDecimalIPv4 ? ParseStringFlag::IPV4_MAYBE_NON_DECIMAL : 0));
- }
- /**
- * Try get the range instance starting from its string representation.
- *
- * @param string|mixed $range
- * @param int $flags A combination or zero or more flags
- *
- * @return static|null
- *
- * @see \IPLib\ParseStringFlag
- * @since 1.17.0
- */
- public static function parseString($range, $flags = 0)
- {
- if (!is_string($range)) {
- return null;
- }
- $parts = explode('/', $range);
- if (count($parts) !== 2) {
- return null;
- }
- $flags = (int) $flags;
- if (strpos($parts[0], ':') === false && $flags & ParseStringFlag::IPV4SUBNET_MAYBE_COMPACT) {
- $missingDots = 3 - substr_count($parts[0], '.');
- if ($missingDots > 0) {
- $parts[0] .= str_repeat('.0', $missingDots);
- }
- }
- $address = Factory::parseAddressString($parts[0], $flags);
- if ($address === null) {
- return null;
- }
- if (!preg_match('/^[0-9]{1,9}$/', $parts[1])) {
- return null;
- }
- $networkPrefix = (int) $parts[1];
- $addressBytes = $address->getBytes();
- $totalBytes = count($addressBytes);
- $numDifferentBits = $totalBytes * 8 - $networkPrefix;
- if ($numDifferentBits < 0) {
- return null;
- }
- $numSameBytes = $networkPrefix >> 3;
- $sameBytes = array_slice($addressBytes, 0, $numSameBytes);
- $differentBytesStart = ($totalBytes === $numSameBytes) ? array() : array_fill(0, $totalBytes - $numSameBytes, 0);
- $differentBytesEnd = ($totalBytes === $numSameBytes) ? array() : array_fill(0, $totalBytes - $numSameBytes, 255);
- $startSameBits = $networkPrefix % 8;
- if ($startSameBits !== 0) {
- $varyingByte = $addressBytes[$numSameBytes];
- $differentBytesStart[0] = $varyingByte & bindec(str_pad(str_repeat('1', $startSameBits), 8, '0', STR_PAD_RIGHT));
- $differentBytesEnd[0] = $differentBytesStart[0] + bindec(str_repeat('1', 8 - $startSameBits));
- }
- return new static(
- Factory::addressFromBytes(array_merge($sameBytes, $differentBytesStart)),
- Factory::addressFromBytes(array_merge($sameBytes, $differentBytesEnd)),
- $networkPrefix
- );
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::toString()
- */
- public function toString($long = false)
- {
- return $this->fromAddress->toString($long) . '/' . $this->networkPrefix;
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getAddressType()
- */
- public function getAddressType()
- {
- return $this->fromAddress->getAddressType();
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getStartAddress()
- */
- public function getStartAddress()
- {
- return $this->fromAddress;
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getEndAddress()
- */
- public function getEndAddress()
- {
- return $this->toAddress;
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getComparableStartString()
- */
- public function getComparableStartString()
- {
- return $this->fromAddress->getComparableString();
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getComparableEndString()
- */
- public function getComparableEndString()
- {
- return $this->toAddress->getComparableString();
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::asSubnet()
- */
- public function asSubnet()
- {
- return $this;
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::asPattern()
- * @since 1.8.0
- */
- public function asPattern()
- {
- $address = $this->getStartAddress();
- $networkPrefix = $this->getNetworkPrefix();
- switch ($address->getAddressType()) {
- case AddressType::T_IPv4:
- return $networkPrefix % 8 === 0 ? new Pattern($address, $address, 4 - $networkPrefix / 8) : null;
- case AddressType::T_IPv6:
- return $networkPrefix % 16 === 0 ? new Pattern($address, $address, 8 - $networkPrefix / 16) : null;
- }
- }
- /**
- * Get the 6to4 address IPv6 address range.
- *
- * @return self
- *
- * @since 1.5.0
- */
- public static function get6to4()
- {
- if (self::$sixToFour === null) {
- self::$sixToFour = self::parseString('2002::/16');
- }
- return self::$sixToFour;
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getNetworkPrefix()
- * @since 1.7.0
- */
- public function getNetworkPrefix()
- {
- return $this->networkPrefix;
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getSubnetMask()
- */
- public function getSubnetMask()
- {
- if ($this->getAddressType() !== AddressType::T_IPv4) {
- return null;
- }
- $bytes = array();
- $prefix = $this->getNetworkPrefix();
- while ($prefix >= 8) {
- $bytes[] = 255;
- $prefix -= 8;
- }
- if ($prefix !== 0) {
- $bytes[] = bindec(str_pad(str_repeat('1', $prefix), 8, '0'));
- }
- $bytes = array_pad($bytes, 4, 0);
- return IPv4::fromBytes($bytes);
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getReverseDNSLookupName()
- */
- public function getReverseDNSLookupName()
- {
- switch ($this->getAddressType()) {
- case AddressType::T_IPv4:
- $unitSize = 8; // bytes
- $maxUnits = 4;
- $isHex = false;
- $rxUnit = '\d+';
- break;
- case AddressType::T_IPv6:
- $unitSize = 4; // nibbles
- $maxUnits = 32;
- $isHex = true;
- $rxUnit = '[0-9A-Fa-f]';
- break;
- }
- $totBits = $unitSize * $maxUnits;
- $prefixUnits = (int) ($this->networkPrefix / $unitSize);
- $extraBits = ($totBits - $this->networkPrefix) % $unitSize;
- if ($extraBits !== 0) {
- $prefixUnits += 1;
- }
- $numVariants = 1 << $extraBits;
- $result = array();
- $unitsToRemove = $maxUnits - $prefixUnits;
- $initialPointer = preg_replace("/^(({$rxUnit})\.){{$unitsToRemove}}/", '', $this->getStartAddress()->getReverseDNSLookupName());
- $chunks = explode('.', $initialPointer, 2);
- for ($index = 0; $index < $numVariants; $index++) {
- if ($index !== 0) {
- $chunks[0] = $isHex ? dechex(1 + hexdec($chunks[0])) : (string) (1 + (int) $chunks[0]);
- }
- $result[] = implode('.', $chunks);
- }
- return $result;
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getSize()
- */
- public function getSize()
- {
- $fromAddress = $this->fromAddress;
- $maxPrefix = $fromAddress::getNumberOfBits();
- $prefix = $this->getNetworkPrefix();
- return pow(2, $maxPrefix - $prefix);
- }
- /**
- * {@inheritdoc}
- *
- * @see \IPLib\Range\RangeInterface::getExactSize()
- */
- public function getExactSize()
- {
- $fromAddress = $this->fromAddress;
- $maxPrefix = $fromAddress::getNumberOfBits();
- $prefix = $this->getNetworkPrefix();
- return BinaryMath::getInstance()->pow2string($maxPrefix - $prefix);
- }
- }
|