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
moving common methods from IPAddressV[46] to AbstractIpAddress
Signed-off-by: Oliver Wegner <[email protected]>
  • Loading branch information
olivermg committed Nov 12, 2018
commit af610cd4d168695d6d712237b4532f29749a0fd9
43 changes: 32 additions & 11 deletions lib/private/Net/AbstractIpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,49 @@
abstract class AbstractIpAddress implements IIpAddress {
abstract public function getMaxBitlength(): int;
abstract protected function getCidrRegex(): string;

abstract protected function setOriginal(string $original);
abstract public function getOriginal(): string;
abstract protected function setAddress(string $address);
abstract public function getAddress(): string;
abstract protected function setNetmaskBits(int $bits);
abstract public function getNetmaskBits(): int;
abstract protected function matchCidr(IIpAddress $other): bool;

private $original = '';
private $netPart = '';
private $netmaskBits = 0;

public function __construct(string $address) {
$this->setOriginal($address);

if (preg_match($this->getCidrRegex(), $address, $match)) {
$this->setAddress($match[1]);
$this->setNetPart($match[1]);
$this->setNetmaskBits(max(0, min($this->getMaxBitlength(), intval($match[2]))));
} else {
$this->setAddress($address);
$this->setNetPart($address);
$this->setNetmaskbits($this->getMaxBitlength());
}
}

protected function matchLiteral(IIpAddress $other): bool {
protected function setOriginal(string $original) {
$this->original = $original;
}

protected function getOriginal(): string {
return $this->original;
}

protected function setNetPart(string $netPart) {
$this->netPart = $netPart;
}

protected function getNetPart(): string {
return $this->netPart;
}

protected function setNetmaskBits(int $bits) {
$this->netmaskBits = $bits;
}

protected function getNetmaskBits(): int {
return $this->netmaskBits;
}

protected function matchOriginal(IIpAddress $other): bool {
return $other->getOriginal() === $this->getOriginal();
}

Expand All @@ -40,7 +61,7 @@ public function isRange(): bool {
public function containsAddress(IIpAddress $other): bool {
return $this->isRange()
? $this->matchCidr($other)
: $this->matchLiteral($other);
: $this->matchOriginal($other);
}
}

32 changes: 2 additions & 30 deletions lib/private/Net/IpAddressV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class IpAddressV4 extends AbstractIpAddress {
private $address = '';
private $cidrBits = 0;

public function __construct(string $address) {
parent::__construct($address);
}

public function getMaxBitlength(): int {
return 32;
}
Expand All @@ -23,34 +19,10 @@ 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).

}

protected function setOriginal(string $original) {
$this->original = $original;
}

public function getOriginal(): string {
return $this->original;
}

protected function setAddress(string $address) {
$this->address = $address;
}

public function getAddress(): string {
return $this->address;
}

protected function setNetmaskBits(int $bits) {
$this->cidrBits = $bits;
}

public function getNetmaskBits(): int {
return $this->cidrBits;
}

protected function matchCidr(IIpAddress $other): bool {
$shiftbits = $this->getMaxBitlength() - $this->getNetmaskBits();
$thisnum = ip2long($this->getAddress()) >> $shiftbits;
$othernum = ip2long($other->getAddress()) >> $shiftbits;
$thisnum = ip2long($this->getNetPart()) >> $shiftbits;
$othernum = ip2long($other->getNetPart()) >> $shiftbits;

return $othernum === $thisnum;
}
Expand Down
34 changes: 3 additions & 31 deletions lib/private/Net/IpAddressV6.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class IpAddressV6 extends AbstractIpAddress {
private $address = '';
private $cidrBits = 0;

public function __construct(string $address) {
parent::__construct($address);
}

public function getMaxBitlength(): int {
return 128;
}
Expand All @@ -23,36 +19,12 @@ protected function getCidrRegex(): string {
return '/^([0-9a-fA-F:]+[0-9a-fA-F:]+)\/([0-9]{1,3})$/';
}

protected function setOriginal(string $original) {
$this->original = $original;
}

public function getOriginal(): string {
return $this->original;
}

protected function setAddress(string $address) {
$this->address = $address;
}

public function getAddress(): string {
return $this->address;
}

protected function setNetmaskBits(int $bits) {
$this->cidrBits = $bits;
}

public function getNetmaskBits(): int {
return $this->cidrBits;
}

protected function matchCidr(IIpAddress $other): bool {
$thisaddrn = inet_pton($this->getAddress());
$otheraddrn = inet_pton($other->getAddress());
$thisaddrn = inet_pton($this->getNetPart());
$otheraddrn = inet_pton($other->getNetPart());
if ($thisaddrn === false || $otheraddrn === false) {
// if we can't handle ipV6 addresses, simply compare strings:
return $this->matchLiteral($other);
return $this->matchOriginal($other);
}

$netbits = $this->getNetmaskBits();
Expand Down