Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
+125
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace IPLib\Range;
|
||||
|
||||
use IPLib\Address\AddressInterface;
|
||||
use IPLib\Address\IPv4;
|
||||
use IPLib\Address\IPv6;
|
||||
use IPLib\Address\Type as AddressType;
|
||||
use IPLib\Factory;
|
||||
|
||||
/**
|
||||
* Base class for range classes.
|
||||
*/
|
||||
abstract class AbstractRange implements RangeInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getRangeType()
|
||||
*/
|
||||
public function getRangeType()
|
||||
{
|
||||
if ($this->rangeType === null) {
|
||||
$addressType = $this->getAddressType();
|
||||
if ($addressType === AddressType::T_IPv6 && Subnet::get6to4()->containsRange($this)) {
|
||||
$this->rangeType = Factory::getRangeFromBoundaries($this->fromAddress->toIPv4(), $this->toAddress->toIPv4())->getRangeType();
|
||||
} else {
|
||||
switch ($addressType) {
|
||||
case AddressType::T_IPv4:
|
||||
$defaultType = IPv4::getDefaultReservedRangeType();
|
||||
$reservedRanges = IPv4::getReservedRanges();
|
||||
break;
|
||||
case AddressType::T_IPv6:
|
||||
$defaultType = IPv6::getDefaultReservedRangeType();
|
||||
$reservedRanges = IPv6::getReservedRanges();
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('@todo'); // @codeCoverageIgnore
|
||||
}
|
||||
$rangeType = null;
|
||||
foreach ($reservedRanges as $reservedRange) {
|
||||
$rangeType = $reservedRange->getRangeType($this);
|
||||
if ($rangeType !== null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->rangeType = $rangeType === null ? $defaultType : $rangeType;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->rangeType === false ? null : $this->rangeType;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getAddressAtOffset()
|
||||
*/
|
||||
public function getAddressAtOffset($n)
|
||||
{
|
||||
if (!is_int($n)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$address = null;
|
||||
if ($n >= 0) {
|
||||
$start = Factory::parseAddressString($this->getComparableStartString());
|
||||
$address = $start->getAddressAtOffset($n);
|
||||
} else {
|
||||
$end = Factory::parseAddressString($this->getComparableEndString());
|
||||
$address = $end->getAddressAtOffset($n + 1);
|
||||
}
|
||||
|
||||
if ($address === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->contains($address) ? $address : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::contains()
|
||||
*/
|
||||
public function contains(AddressInterface $address)
|
||||
{
|
||||
$result = false;
|
||||
if ($address->getAddressType() === $this->getAddressType()) {
|
||||
$cmp = $address->getComparableString();
|
||||
$from = $this->getComparableStartString();
|
||||
if ($cmp >= $from) {
|
||||
$to = $this->getComparableEndString();
|
||||
if ($cmp <= $to) {
|
||||
$result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::containsRange()
|
||||
*/
|
||||
public function containsRange(RangeInterface $range)
|
||||
{
|
||||
$result = false;
|
||||
if ($range->getAddressType() === $this->getAddressType()) {
|
||||
$myStart = $this->getComparableStartString();
|
||||
$itsStart = $range->getComparableStartString();
|
||||
if ($itsStart >= $myStart) {
|
||||
$myEnd = $this->getComparableEndString();
|
||||
$itsEnd = $range->getComparableEndString();
|
||||
if ($itsEnd <= $myEnd) {
|
||||
$result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
+322
@@ -0,0 +1,322 @@
|
||||
<?php
|
||||
|
||||
namespace IPLib\Range;
|
||||
|
||||
use IPLib\Address\AddressInterface;
|
||||
use IPLib\Address\IPv4;
|
||||
use IPLib\Address\IPv6;
|
||||
use IPLib\Address\Type as AddressType;
|
||||
use IPLib\ParseStringFlag;
|
||||
|
||||
/**
|
||||
* Represents an address range in pattern format (only ending asterisks are supported).
|
||||
*
|
||||
* @example 127.0.*.*
|
||||
* @example ::/8
|
||||
*/
|
||||
class Pattern 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 ending asterisks.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $asterisksCount;
|
||||
|
||||
/**
|
||||
* The type of the range of this IP range.
|
||||
*
|
||||
* @var int|false|null false if this range crosses multiple range types, null if yet to be determined
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
protected $rangeType;
|
||||
|
||||
/**
|
||||
* Initializes the instance.
|
||||
*
|
||||
* @param \IPLib\Address\AddressInterface $fromAddress
|
||||
* @param \IPLib\Address\AddressInterface $toAddress
|
||||
* @param int $asterisksCount
|
||||
*/
|
||||
public function __construct(AddressInterface $fromAddress, AddressInterface $toAddress, $asterisksCount)
|
||||
{
|
||||
$this->fromAddress = $fromAddress;
|
||||
$this->toAddress = $toAddress;
|
||||
$this->asterisksCount = $asterisksCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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\Pattern::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
|
||||
*
|
||||
* @since 1.17.0
|
||||
* @see \IPLib\ParseStringFlag
|
||||
*/
|
||||
public static function parseString($range, $flags = 0)
|
||||
{
|
||||
if (!is_string($range) || strpos($range, '*') === false) {
|
||||
return null;
|
||||
}
|
||||
if ($range === '*.*.*.*') {
|
||||
return new static(IPv4::parseString('0.0.0.0'), IPv4::parseString('255.255.255.255'), 4);
|
||||
}
|
||||
if ($range === '*:*:*:*:*:*:*:*') {
|
||||
return new static(IPv6::parseString('::'), IPv6::parseString('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 8);
|
||||
}
|
||||
$matches = null;
|
||||
if (strpos($range, '.') !== false && preg_match('/^[^*]+((?:\.\*)+)$/', $range, $matches)) {
|
||||
$asterisksCount = strlen($matches[1]) >> 1;
|
||||
if ($asterisksCount > 0) {
|
||||
$missingDots = 3 - substr_count($range, '.');
|
||||
if ($missingDots > 0) {
|
||||
$range .= str_repeat('.*', $missingDots);
|
||||
$asterisksCount += $missingDots;
|
||||
}
|
||||
}
|
||||
$fromAddress = IPv4::parseString(str_replace('*', '0', $range), $flags);
|
||||
if ($fromAddress === null) {
|
||||
return null;
|
||||
}
|
||||
$fixedBytes = array_slice($fromAddress->getBytes(), 0, -$asterisksCount);
|
||||
$otherBytes = array_fill(0, $asterisksCount, 255);
|
||||
$toAddress = IPv4::fromBytes(array_merge($fixedBytes, $otherBytes));
|
||||
|
||||
return new static($fromAddress, $toAddress, $asterisksCount);
|
||||
}
|
||||
if (strpos($range, ':') !== false && preg_match('/^[^*]+((?::\*)+)$/', $range, $matches)) {
|
||||
$asterisksCount = strlen($matches[1]) >> 1;
|
||||
$fromAddress = IPv6::parseString(str_replace('*', '0', $range));
|
||||
if ($fromAddress === null) {
|
||||
return null;
|
||||
}
|
||||
$fixedWords = array_slice($fromAddress->getWords(), 0, -$asterisksCount);
|
||||
$otherWords = array_fill(0, $asterisksCount, 0xffff);
|
||||
$toAddress = IPv6::fromWords(array_merge($fixedWords, $otherWords));
|
||||
|
||||
return new static($fromAddress, $toAddress, $asterisksCount);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::toString()
|
||||
*/
|
||||
public function toString($long = false)
|
||||
{
|
||||
if ($this->asterisksCount === 0) {
|
||||
return $this->fromAddress->toString($long);
|
||||
}
|
||||
switch (true) {
|
||||
case $this->fromAddress instanceof \IPLib\Address\IPv4:
|
||||
$chunks = explode('.', $this->fromAddress->toString());
|
||||
$chunks = array_slice($chunks, 0, -$this->asterisksCount);
|
||||
$chunks = array_pad($chunks, 4, '*');
|
||||
$result = implode('.', $chunks);
|
||||
break;
|
||||
case $this->fromAddress instanceof \IPLib\Address\IPv6:
|
||||
if ($long) {
|
||||
$chunks = explode(':', $this->fromAddress->toString(true));
|
||||
$chunks = array_slice($chunks, 0, -$this->asterisksCount);
|
||||
$chunks = array_pad($chunks, 8, '*');
|
||||
$result = implode(':', $chunks);
|
||||
} elseif ($this->asterisksCount === 8) {
|
||||
$result = '*:*:*:*:*:*:*:*';
|
||||
} else {
|
||||
$bytes = $this->toAddress->getBytes();
|
||||
$bytes = array_slice($bytes, 0, -$this->asterisksCount * 2);
|
||||
$bytes = array_pad($bytes, 16, 1);
|
||||
$address = IPv6::fromBytes($bytes);
|
||||
$before = substr($address->toString(false), 0, -strlen(':101') * $this->asterisksCount);
|
||||
$result = $before . str_repeat(':*', $this->asterisksCount);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('@todo'); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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()
|
||||
* @since 1.8.0
|
||||
*/
|
||||
public function asSubnet()
|
||||
{
|
||||
return new Subnet($this->getStartAddress(), $this->getEndAddress(), $this->getNetworkPrefix());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::asPattern()
|
||||
*/
|
||||
public function asPattern()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getSubnetMask()
|
||||
*/
|
||||
public function getSubnetMask()
|
||||
{
|
||||
if ($this->getAddressType() !== AddressType::T_IPv4) {
|
||||
return null;
|
||||
}
|
||||
switch ($this->asterisksCount) {
|
||||
case 0:
|
||||
$bytes = array(255, 255, 255, 255);
|
||||
break;
|
||||
case 4:
|
||||
$bytes = array(0, 0, 0, 0);
|
||||
break;
|
||||
default:
|
||||
$bytes = array_pad(array_fill(0, 4 - $this->asterisksCount, 255), 4, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
return IPv4::fromBytes($bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getReverseDNSLookupName()
|
||||
*/
|
||||
public function getReverseDNSLookupName()
|
||||
{
|
||||
return $this->asterisksCount === 0 ? array($this->getStartAddress()->getReverseDNSLookupName()) : $this->asSubnet()->getReverseDNSLookupName();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getSize()
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
$fromAddress = $this->fromAddress;
|
||||
$maxPrefix = $fromAddress::getNumberOfBits();
|
||||
$prefix = $this->getNetworkPrefix();
|
||||
|
||||
return pow(2, ($maxPrefix - $prefix));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float|int
|
||||
*/
|
||||
private function getNetworkPrefix()
|
||||
{
|
||||
switch ($this->getAddressType()) {
|
||||
case AddressType::T_IPv4:
|
||||
return 8 * (4 - $this->asterisksCount);
|
||||
case AddressType::T_IPv6:
|
||||
return 16 * (8 - $this->asterisksCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace IPLib\Range;
|
||||
|
||||
use IPLib\Address\AddressInterface;
|
||||
|
||||
/**
|
||||
* Interface of all the range types.
|
||||
*/
|
||||
interface RangeInterface
|
||||
{
|
||||
/**
|
||||
* Get the short string representation of this address.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString();
|
||||
|
||||
/**
|
||||
* Get the string representation of this address.
|
||||
*
|
||||
* @param bool $long set to true to have a long/full representation, false otherwise
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @example If $long is true, you'll get '0000:0000:0000:0000:0000:0000:0000:0001/128', '::1/128' otherwise.
|
||||
*/
|
||||
public function toString($long = false);
|
||||
|
||||
/**
|
||||
* Get the type of the IP addresses contained in this range.
|
||||
*
|
||||
* @return int One of the \IPLib\Address\Type::T_... constants
|
||||
*/
|
||||
public function getAddressType();
|
||||
|
||||
/**
|
||||
* Get the type of range of the IP address.
|
||||
*
|
||||
* @return int One of the \IPLib\Range\Type::T_... constants
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public function getRangeType();
|
||||
|
||||
/**
|
||||
* Get the address at a certain offset of this range.
|
||||
*
|
||||
* @param int $n the offset of the address (support negative offset)
|
||||
*
|
||||
* @return \IPLib\Address\AddressInterface|null return NULL if $n is not an integer or if the offset out of range
|
||||
*
|
||||
* @since 1.15.0
|
||||
*
|
||||
* @example passing 256 to the range 127.0.0.0/16 will result in 127.0.1.0
|
||||
* @example passing -1 to the range 127.0.1.0/16 will result in 127.0.255.255
|
||||
* @example passing 256 to the range 127.0.0.0/24 will result in NULL
|
||||
*/
|
||||
public function getAddressAtOffset($n);
|
||||
|
||||
/**
|
||||
* Check if this range contains an IP address.
|
||||
*
|
||||
* @param \IPLib\Address\AddressInterface $address
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function contains(AddressInterface $address);
|
||||
|
||||
/**
|
||||
* Check if this range contains another range.
|
||||
*
|
||||
* @param \IPLib\Range\RangeInterface $range
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public function containsRange(RangeInterface $range);
|
||||
|
||||
/**
|
||||
* Get the initial address contained in this range.
|
||||
*
|
||||
* @return \IPLib\Address\AddressInterface
|
||||
*
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public function getStartAddress();
|
||||
|
||||
/**
|
||||
* Get the final address contained in this range.
|
||||
*
|
||||
* @return \IPLib\Address\AddressInterface
|
||||
*
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public function getEndAddress();
|
||||
|
||||
/**
|
||||
* Get a string representation of the starting address of this range than can be used when comparing addresses and ranges.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getComparableStartString();
|
||||
|
||||
/**
|
||||
* Get a string representation of the final address of this range than can be used when comparing addresses and ranges.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getComparableEndString();
|
||||
|
||||
/**
|
||||
* Get the subnet mask representing this range (only for IPv4 ranges).
|
||||
*
|
||||
* @return \IPLib\Address\IPv4|null return NULL if the range is an IPv6 range, the subnet mask otherwise
|
||||
*
|
||||
* @since 1.8.0
|
||||
*/
|
||||
public function getSubnetMask();
|
||||
|
||||
/**
|
||||
* Get the subnet/CIDR representation of this range.
|
||||
*
|
||||
* @return \IPLib\Range\Subnet
|
||||
*
|
||||
* @since 1.13.0
|
||||
*/
|
||||
public function asSubnet();
|
||||
|
||||
/**
|
||||
* Get the pattern/asterisk representation (if applicable) of this range.
|
||||
*
|
||||
* @return \IPLib\Range\Pattern|null return NULL if this range can't be represented by a pattern notation
|
||||
*
|
||||
* @since 1.13.0
|
||||
*/
|
||||
public function asPattern();
|
||||
|
||||
/**
|
||||
* Get the Reverse DNS Lookup Addresses of this IP range.
|
||||
*
|
||||
* @return string[]
|
||||
*
|
||||
* @since 1.13.0
|
||||
*
|
||||
* @example for IPv4 it returns something like array('x.x.x.x.in-addr.arpa', 'x.x.x.x.in-addr.arpa') (where the number of 'x.' ranges from 1 to 4)
|
||||
* @example for IPv6 it returns something like array('x.x.x.x..x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.ip6.arpa', 'x.x.x.x..x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.ip6.arpa') (where the number of 'x.' ranges from 1 to 32)
|
||||
*/
|
||||
public function getReverseDNSLookupName();
|
||||
|
||||
/**
|
||||
* Get the count of addresses this IP range contains.
|
||||
*
|
||||
* @return int|float Return float as for huge IPv6 networks, int is not enough
|
||||
*
|
||||
* @since 1.16.0
|
||||
*/
|
||||
public function getSize();
|
||||
}
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
|
||||
namespace IPLib\Range;
|
||||
|
||||
use IPLib\Address\AddressInterface;
|
||||
use IPLib\Address\IPv4;
|
||||
use IPLib\Address\Type as AddressType;
|
||||
use IPLib\Factory;
|
||||
use IPLib\ParseStringFlag;
|
||||
|
||||
/**
|
||||
* Represents a single address (eg a range that contains just one address).
|
||||
*
|
||||
* @example 127.0.0.1
|
||||
* @example ::1
|
||||
*/
|
||||
class Single extends AbstractRange
|
||||
{
|
||||
/**
|
||||
* @var \IPLib\Address\AddressInterface
|
||||
*/
|
||||
protected $address;
|
||||
|
||||
/**
|
||||
* Initializes the instance.
|
||||
*
|
||||
* @param \IPLib\Address\AddressInterface $address
|
||||
*/
|
||||
protected function __construct(AddressInterface $address)
|
||||
{
|
||||
$this->address = $address;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::__toString()
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->address->__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\Single::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)
|
||||
{
|
||||
$result = null;
|
||||
$flags = (int) $flags;
|
||||
$address = Factory::parseAddressString($range, $flags);
|
||||
if ($address !== null) {
|
||||
$result = new static($address);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the range instance starting from an address instance.
|
||||
*
|
||||
* @param \IPLib\Address\AddressInterface $address
|
||||
*
|
||||
* @return static
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public static function fromAddress(AddressInterface $address)
|
||||
{
|
||||
return new static($address);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::toString()
|
||||
*/
|
||||
public function toString($long = false)
|
||||
{
|
||||
return $this->address->toString($long);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getAddressType()
|
||||
*/
|
||||
public function getAddressType()
|
||||
{
|
||||
return $this->address->getAddressType();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getRangeType()
|
||||
*/
|
||||
public function getRangeType()
|
||||
{
|
||||
return $this->address->getRangeType();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::contains()
|
||||
*/
|
||||
public function contains(AddressInterface $address)
|
||||
{
|
||||
$result = false;
|
||||
if ($address->getAddressType() === $this->getAddressType()) {
|
||||
if ($address->toString(false) === $this->address->toString(false)) {
|
||||
$result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getStartAddress()
|
||||
*/
|
||||
public function getStartAddress()
|
||||
{
|
||||
return $this->address;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getEndAddress()
|
||||
*/
|
||||
public function getEndAddress()
|
||||
{
|
||||
return $this->address;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getComparableStartString()
|
||||
*/
|
||||
public function getComparableStartString()
|
||||
{
|
||||
return $this->address->getComparableString();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getComparableEndString()
|
||||
*/
|
||||
public function getComparableEndString()
|
||||
{
|
||||
return $this->address->getComparableString();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::asSubnet()
|
||||
*/
|
||||
public function asSubnet()
|
||||
{
|
||||
$networkPrefixes = array(
|
||||
AddressType::T_IPv4 => 32,
|
||||
AddressType::T_IPv6 => 128,
|
||||
);
|
||||
|
||||
return new Subnet($this->address, $this->address, $networkPrefixes[$this->address->getAddressType()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::asPattern()
|
||||
*/
|
||||
public function asPattern()
|
||||
{
|
||||
return new Pattern($this->address, $this->address, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getSubnetMask()
|
||||
*/
|
||||
public function getSubnetMask()
|
||||
{
|
||||
if ($this->getAddressType() !== AddressType::T_IPv4) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return IPv4::fromBytes(array(255, 255, 255, 255));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getReverseDNSLookupName()
|
||||
*/
|
||||
public function getReverseDNSLookupName()
|
||||
{
|
||||
return array($this->getStartAddress()->getReverseDNSLookupName());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \IPLib\Range\RangeInterface::getSize()
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
+354
@@ -0,0 +1,354 @@
|
||||
<?php
|
||||
|
||||
namespace IPLib\Range;
|
||||
|
||||
use IPLib\Address\AddressInterface;
|
||||
use IPLib\Address\IPv4;
|
||||
use IPLib\Address\Type as AddressType;
|
||||
use IPLib\Factory;
|
||||
use IPLib\ParseStringFlag;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subnet prefix.
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @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));
|
||||
}
|
||||
}
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace IPLib\Range;
|
||||
|
||||
/**
|
||||
* Types of IP address classes.
|
||||
*/
|
||||
class Type
|
||||
{
|
||||
/**
|
||||
* Unspecified/unknown address.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_UNSPECIFIED = 1;
|
||||
|
||||
/**
|
||||
* Reserved/internal use only.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_RESERVED = 2;
|
||||
|
||||
/**
|
||||
* Refer to source hosts on "this" network.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_THISNETWORK = 3;
|
||||
|
||||
/**
|
||||
* Internet host loopback address.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_LOOPBACK = 4;
|
||||
|
||||
/**
|
||||
* Relay anycast address.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_ANYCASTRELAY = 5;
|
||||
|
||||
/**
|
||||
* "Limited broadcast" destination address.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_LIMITEDBROADCAST = 6;
|
||||
|
||||
/**
|
||||
* Multicast address assignments - Indentify a group of interfaces.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_MULTICAST = 7;
|
||||
|
||||
/**
|
||||
* "Link local" address, allocated for communication between hosts on a single link.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_LINKLOCAL = 8;
|
||||
|
||||
/**
|
||||
* Link local unicast / Linked-scoped unicast.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_LINKLOCAL_UNICAST = 9;
|
||||
|
||||
/**
|
||||
* Discard-Only address.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_DISCARDONLY = 10;
|
||||
|
||||
/**
|
||||
* Discard address.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_DISCARD = 11;
|
||||
|
||||
/**
|
||||
* For use in private networks.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_PRIVATENETWORK = 12;
|
||||
|
||||
/**
|
||||
* Public address.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const T_PUBLIC = 13;
|
||||
|
||||
/**
|
||||
* Carrier-grade NAT address.
|
||||
*
|
||||
* @var int
|
||||
*
|
||||
* @since 1.10.0
|
||||
*/
|
||||
const T_CGNAT = 14;
|
||||
|
||||
/**
|
||||
* Get the name of a type.
|
||||
*
|
||||
* @param int $type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName($type)
|
||||
{
|
||||
switch ($type) {
|
||||
case static::T_UNSPECIFIED:
|
||||
return 'Unspecified/unknown address';
|
||||
case static::T_RESERVED:
|
||||
return 'Reserved/internal use only';
|
||||
case static::T_THISNETWORK:
|
||||
return 'Refer to source hosts on "this" network';
|
||||
case static::T_LOOPBACK:
|
||||
return 'Internet host loopback address';
|
||||
case static::T_ANYCASTRELAY:
|
||||
return 'Relay anycast address';
|
||||
case static::T_LIMITEDBROADCAST:
|
||||
return '"Limited broadcast" destination address';
|
||||
case static::T_MULTICAST:
|
||||
return 'Multicast address assignments - Indentify a group of interfaces';
|
||||
case static::T_LINKLOCAL:
|
||||
return '"Link local" address, allocated for communication between hosts on a single link';
|
||||
case static::T_LINKLOCAL_UNICAST:
|
||||
return 'Link local unicast / Linked-scoped unicast';
|
||||
case static::T_DISCARDONLY:
|
||||
return 'Discard only';
|
||||
case static::T_DISCARD:
|
||||
return 'Discard';
|
||||
case static::T_PRIVATENETWORK:
|
||||
return 'For use in private networks';
|
||||
case static::T_PUBLIC:
|
||||
return 'Public address';
|
||||
case static::T_CGNAT:
|
||||
return 'Carrier-grade NAT';
|
||||
default:
|
||||
return $type === null ? 'Unknown type' : sprintf('Unknown type (%s)', $type);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user