Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method IPv4::isNetmask() #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- New method `matches($ip, $mask)` to perform wildcard mask matching common in network Access Control Lists and OSPF dynamic routing [#51](https://github.com/rlanvin/php-ip/pull/51)
- IPv4Block: Allow to specify the prefix also as an old-style netmask [#53](https://github.com/rlanvin/php-ip/pull/53)
- New method `IPv4::isNetmask()` tests whether the IPv4 address is a valid subnet mask. [#55](https://github.com/rlanvin/php-ip/pull/55)

## [2.0.0] - 2019-09-01

Expand Down
27 changes: 27 additions & 0 deletions src/IPv4.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class IPv4 extends IP
*/
protected static $link_local_block = '169.254.0.0/16';

/**
* @var bool
*/
protected $is_netmask;

/**
* {@inheritdoc}
*/
Expand All @@ -73,4 +78,26 @@ public function reversePointer(): string

return implode('.', $octets).'.in-addr.arpa.';
}

/**
* Return true if the IP address is a valid sub network mask, for instance: '255.255.252.0'.
*
* @return bool
*/
public function isNetmask(): bool
{
if ($this->is_netmask !== null) {
return $this->is_netmask;
}

if (gmp_cmp($this->ip, 0) === 0) {
$this->is_netmask = true;
} else {
$y = $this->bit_negate()->plus(1);
$z = $this->bit_negate()->bit_and($y);
$this->is_netmask = gmp_cmp($z->ip, 0) === 0;
}

return $this->is_netmask;
}
}
32 changes: 32 additions & 0 deletions tests/IPv4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ public function getMatchesData(): array
return $data;
}

/**
* Data provider for testIsNetmask().
*
* @return array
*/
public function netmaskProvider(): array
{
return [
//IP address is valid netmask
['0.0.0.0', true],
['64.0.0.0', false],
['128.0.0.0', true],
['127.0.0.0', false],
['255.255.0.0', true],
['255.0.255.252', false],
['255.255.255.0', true],
['255.255.255.224', true],
['255.255.255.252', true],
];
}

/**
* @dataProvider validAddresses
*/
Expand Down Expand Up @@ -517,4 +538,15 @@ public function testDefaultMaskValueMatchesEntireIpAddress()
$this->assertTrue($ip->matches('172.16.3.5'));
$this->assertFalse($ip->matches('172.16.3.4'));
}

/**
* @dataProvider netmaskProvider
*
* @param $ip
* @param bool $expectation
*/
public function testIsNetMask($ip, bool $expectation)
{
$this->assertEquals($expectation, IPv4::create($ip)->isNetmask());
}
}