Skip to content

Commit

Permalink
Add isNetwork() method
Browse files Browse the repository at this point in the history
  • Loading branch information
murrant committed May 29, 2018
1 parent 1396492 commit 936d506
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions LibreNMS/Util/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ public function inNetworks($networks)
*/
abstract public function inNetwork($network);

/**
* Checks if this IP is a network.
* This generally means hostmask is < 32 or < 128
*
* @return bool
*/
public function isNetwork()
{
return $this->cidr < $this->host_bits;
}

/**
* Check if this IP is in the reserved range.
* @return bool
Expand Down
10 changes: 10 additions & 0 deletions tests/IpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public function testNetworkParse()
$this->assertEquals('::1', IP::parse('::1/128'));
}

public function testIsNetwork()
{
$this->assertFalse(IP::parse('192.168.3.0')->isNetwork());
$this->assertFalse(IP::parse('192.168.3.0/32')->isNetwork());
$this->assertTrue(IP::parse('192.168.3.0/24')->isNetwork());
$this->assertFalse(IPv6::parse('2001:db8:85a3::8a2e:370:7334')->isNetwork());
$this->assertFalse(IPv6::parse('2001:db8:85a3::8a2e:370:7334/128')->isNetwork());
$this->assertTrue(IPv6::parse('2001:db8:85a3::8a2e:370:7334/64')->isNetwork());
}

/**
* @expectedException \LibreNMS\Exceptions\InvalidIpException
*/
Expand Down

0 comments on commit 936d506

Please sign in to comment.