Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4dd4379
Adding IpAddress handling utility classes in OC\Net namespace
olivermg Oct 31, 2018
5efc12f
Working on IpAddress classes for CIDR
olivermg Oct 31, 2018
54c3694
Merge branch 'master' of github.com:nextcloud/server into feature/655…
olivermg Nov 4, 2018
9c98e43
Adding test for IpAddressFactory
olivermg Nov 4, 2018
fc7b744
adding tests to IpAddressV4Test.php
olivermg Nov 5, 2018
9674d3b
Merge branch 'master' of github.com:nextcloud/server into feature/655…
olivermg Nov 12, 2018
7b9f5bc
adding tests for IpAddressV4
olivermg Nov 12, 2018
587025a
renaming tests
olivermg Nov 12, 2018
8d35b0e
adding tests for IpAddressV6
olivermg Nov 12, 2018
5fa3396
adding tests for localhost
olivermg Nov 12, 2018
af610cd
moving common methods from IPAddressV[46] to AbstractIpAddress
olivermg Nov 12, 2018
a5503ad
adding author to Request.php
olivermg Nov 12, 2018
cf554e0
removing empty setUp & tearDown methods from tests
olivermg Nov 12, 2018
0920691
shrinking IIpAddress interface to sensible minimum
olivermg Nov 12, 2018
b26e862
Merge branch 'master' of github.com:nextcloud/server into feature/655…
olivermg Nov 19, 2018
b4a5d8f
remove matchesTrustedProxy function from Request.php
olivermg Nov 19, 2018
02c6ed1
adding license headers to new source files
olivermg Nov 19, 2018
ad76224
updating config.sample.php to reflect IPv6 CIDR notation
olivermg Nov 19, 2018
361871d
adding function header comments
olivermg Nov 19, 2018
efc02c6
adding license file headers to test files
olivermg Nov 19, 2018
29488ec
Merge branch 'master' of github.com:nextcloud/server into feature/655…
olivermg Nov 19, 2018
caf3df2
updating license statements in files of OC\Net
olivermg Nov 19, 2018
4ad17fc
pushing @since in \OC\Net\IIpAddress to 16.0.0
olivermg Nov 19, 2018
9b66b4f
moving IIpAddress and IpAddressFactory to OCP namespace
olivermg Nov 20, 2018
36aec3f
adding @author lines
olivermg Nov 20, 2018
811befa
removing obsolete members from concrete IpAddressV[46] classes
olivermg Nov 25, 2018
f82f4fb
introduce IIpAddressFactory and move IpAddressFactory to OC namespace
olivermg Nov 25, 2018
733b416
Merge branch 'master' of github.com:olivermg/server into feature/6550…
olivermg Nov 27, 2018
638d876
Merge branch 'master' of github.com:nextcloud/server into feature/655…
olivermg Nov 27, 2018
bc413ea
inject IIpAddressFactory into Request
olivermg Nov 27, 2018
a8f7ea4
check if an IIpAddressFactory exists in Request
olivermg Nov 27, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding function header comments
Signed-off-by: Oliver Wegner <[email protected]>
  • Loading branch information
olivermg committed Nov 19, 2018
commit 361871d5c3e2ad3b07dcdc10c36cf0da2bf4f8d9
61 changes: 61 additions & 0 deletions lib/private/Net/AbstractIpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ abstract protected function matchCidr(IIpAddress $other): bool;
private $netPart = '';
private $netmaskBits = 0;

/**
* Constructor that takes an IP address in string form and
* initializes this instance to represent that address
*
* @param string $address
*/
public function __construct(string $address) {
$this->setOriginal($address);

Expand All @@ -46,38 +52,93 @@ public function __construct(string $address) {
}
}

/**
* Sets the literal address string that this instance
* represents
*
* @param string $original
*/
protected function setOriginal(string $original) {
$this->original = $original;
}

/**
* Returns the literal address string that this instance
* represents
*
* @return string
*/
protected function getOriginal(): string {
return $this->original;
}

/**
* Sets the network part of the
* address/range represented by this instance
*
* @param string $netPart
*/
protected function setNetPart(string $netPart) {
$this->netPart = $netPart;
}

/**
* Returns the network part of the
* address/range represented by this instance
*
* @return string
*/
protected function getNetPart(): string {
return $this->netPart;
}

/**
* Sets the number of bits of the net part of the IP
* address/range represented by this instance
*
* @param int $bits
*/
protected function setNetmaskBits(int $bits) {
$this->netmaskBits = $bits;
}

/**
* Returns the number of bits of the net part of the IP
* address/range represented by this instance
*
* @return int
*/
protected function getNetmaskBits(): int {
return $this->netmaskBits;
}

/**
* Returns whether $other is literally equivalent to this instance
*
* @return bool
*/
protected function matchOriginal(IIpAddress $other): bool {
return $other->getOriginal() === $this->getOriginal();
}

/**
* Returns whether this instance represents an IP range (vs.
* a single IP address)
*
* @return bool
*/
public function isRange(): bool {
return $this->getNetmaskBits() < $this->getMaxBitlength();
}

/**
* Returns whether given $other address is either
* - equal to this instance regarding its IP address or
* - is contained in the IP address range represented by this instance
*
* @param IIpAddress $other
* @return bool
*/
public function containsAddress(IIpAddress $other): bool {
return $this->isRange()
? $this->matchCidr($other)
Expand Down
16 changes: 15 additions & 1 deletion lib/private/Net/IpAddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@

namespace OC\Net;

use OC\Net\IIpAddress;
use OC\Net\IpAddressV4;
use OC\Net\IpAddressV6;

class IpAddressFactory {
/**
* Returns whether $address represents an IPv6 address
*
* @param string $address
* @return bool
*/
public static function isIpv6(string $address): bool {
return strpos($address, ':') !== false;
}

public static function new($address) {
/**
* Creates a new instance conforming to IIpAddress and
* representing the given $address.
*
* @param string $address
* @return IIpAddress
*/
public static function new($address): IIpAddress {
if (self::isIpv6($address)) {
return new IpAddressV6($address);
} else {
Expand Down
18 changes: 18 additions & 0 deletions lib/private/Net/IpAddressV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,32 @@ class IpAddressV4 extends AbstractIpAddress {
private $address = '';
private $cidrBits = 0;

/**
* Returns the length of the represented IP address format in bits.
*
* @return int
*/
public function getMaxBitlength(): int {
return 32;
}

/**
* Returns the regular expression for recognizing CIDR notation.
*
* @return string
*/
protected function getCidrRegex(): string {
return '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/([0-9]{1,2})$/';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex matches invalid IPv4 subnets: https://regex101.com/r/tjtE95/1 (same for IPv6 in the IpAddressV6 class).

As IP address are numbers, I think you could use numerical comparison and avoid relying on regexes (that will most certainly be incorrect in a way or another). I'm not really into PHP so I'm not sure what's the best way to do that (to be honest it bugs me that this is not part of the standard library).

Also, I think it would be useful to warn the administrator when they put a wrong value in the configuration (thus using another "parsing" method).

}

/**
* Returns whether given $other address is either
* - equal to this instance regarding its IP address or
* - is contained in the IP address range represented by this instance
*
* @param IIpAddress $other
* @return bool
*/
protected function matchCidr(IIpAddress $other): bool {
$shiftbits = $this->getMaxBitlength() - $this->getNetmaskBits();
$thisnum = ip2long($this->getNetPart()) >> $shiftbits;
Expand Down
18 changes: 18 additions & 0 deletions lib/private/Net/IpAddressV6.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,32 @@ class IpAddressV6 extends AbstractIpAddress {
private $address = '';
private $cidrBits = 0;

/**
* Returns the length of the represented IP address format in bits.
*
* @return int
*/
public function getMaxBitlength(): int {
return 128;
}

/**
* Returns the regular expression for recognizing CIDR notation.
*
* @return string
*/
protected function getCidrRegex(): string {
return '/^([0-9a-fA-F:]+[0-9a-fA-F:]+)\/([0-9]{1,3})$/';
}

/**
* Returns whether given $other address is either
* - equal to this instance regarding its IP address or
* - is contained in the IP address range represented by this instance
*
* @param IIpAddress $other
* @return bool
*/
protected function matchCidr(IIpAddress $other): bool {
$thisaddrn = inet_pton($this->getNetPart());
$otheraddrn = inet_pton($other->getNetPart());
Expand Down