Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F11726392
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/Network/IPv4Range.php b/src/Network/IPv4Range.php
index ae2b674..218302b 100644
--- a/src/Network/IPv4Range.php
+++ b/src/Network/IPv4Range.php
@@ -1,105 +1,104 @@
<?php
namespace Keruald\OmniTools\Network;
-use Countable;
use InvalidArgumentException;
class IPv4Range extends IPRange {
/**
* @var string
*/
private $base;
/**
* @var int
*/
private $networkBits;
///
/// Constructors
///
public function __construct (string $base, int $networkBits) {
$this->setBase($base);
$this->setNetworkBits($networkBits);
}
///
/// Getters and setters
///
/**
* @return string
*/
public function getBase () : string {
return $this->base;
}
/**
* @param string $base
*/
public function setBase (string $base) : void {
if (!IP::isIPv4($base)) {
throw new InvalidArgumentException;
}
$this->base = $base;
}
/**
* @return int
*/
public function getNetworkBits () : int {
return $this->networkBits;
}
/**
* @param int $networkBits
*/
public function setNetworkBits (int $networkBits) : void {
if ($networkBits < 0 || $networkBits > 32) {
throw new InvalidArgumentException;
}
$this->networkBits = $networkBits;
}
///
/// Helper methods
///
public function getFirst () : string {
return $this->base;
}
public function getLast () : string {
return long2ip(ip2long($this->base) + 2 ** $this->count() - 1);
}
public function contains (string $ip) : bool {
if (!IP::isIP($ip)) {
throw new InvalidArgumentException;
}
if (!IP::isIPv4($ip)) {
return false;
}
$ipAsLong = ip2long($ip);
$baseAsLong = ip2long($this->base);
return $ipAsLong >= $baseAsLong
&& $ipAsLong <= $baseAsLong + $this->count() - 1;
}
///
/// Countable interface
///
public function count () : int {
return 32 - $this->networkBits;
}
}
diff --git a/src/Network/IPv6Range.php b/src/Network/IPv6Range.php
index ea4a79e..8979818 100644
--- a/src/Network/IPv6Range.php
+++ b/src/Network/IPv6Range.php
@@ -1,119 +1,118 @@
<?php
namespace Keruald\OmniTools\Network;
-use Countable;
use InvalidArgumentException;
class IPv6Range extends IPRange {
/**
* @var string
*/
private $base;
/**
* @var int
*/
private $networkBits;
///
/// Constructors
///
public function __construct (string $base, int $networkBits) {
$this->setBase($base);
$this->setNetworkBits($networkBits);
}
///
/// Getters and setters
///
/**
* @return string
*/
public function getBase () : string {
return $this->base;
}
/**
* @param string $base
*/
public function setBase (string $base) : void {
if (!IP::isIPv6($base)) {
throw new InvalidArgumentException;
}
$this->base = $base;
}
/**
* @return int
*/
public function getNetworkBits () : int {
return $this->networkBits;
}
/**
* @param int $networkBits
*/
public function setNetworkBits (int $networkBits) : void {
if ($networkBits < 0 || $networkBits > 128) {
throw new InvalidArgumentException;
}
$this->networkBits = $networkBits;
}
///
/// Helper methods
///
public function getFirst () : string {
return $this->base;
}
public function getLast () : string {
if ($this->count() === 0) {
return $this->base;
}
$base = inet_pton($this->getFirst());
$mask = inet_pton($this->getInversedMask());
return inet_ntop($base | $mask);
}
private function getInversedMask () : string {
$bits = array_fill(0, $this->networkBits, 0) + array_fill(0, 128, 1);
return (string)IPv6::fromBinaryBits($bits);
}
public function contains (string $ip) : bool {
if (!IP::isIP($ip)) {
throw new InvalidArgumentException;
}
if (IP::isIPv4($ip)) {
$ip = "::ffff:" . $ip; // IPv4-mapped IPv6 address
}
$baseAsNumericBinary = inet_pton($this->getFirst());
$lastAsNumericBinary = inet_pton($this->getLast());
$ipAsNumericBinary = inet_pton($ip);
return strlen($ipAsNumericBinary) == strlen($baseAsNumericBinary)
&& $ipAsNumericBinary >= $baseAsNumericBinary
&& $ipAsNumericBinary <= $lastAsNumericBinary;
}
///
/// Countable interface
///
public function count () : int {
return 128 - $this->networkBits;
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Sep 19, 03:09 (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2992129
Default Alt Text
(4 KB)
Attached To
Mode
rKERUALD Keruald libraries development repository
Attached
Detach File
Event Timeline
Log In to Comment