diff --git a/LibreNMS/Util/IP.php b/LibreNMS/Util/IP.php index 48d44b9..f376cf2 100644 --- a/LibreNMS/Util/IP.php +++ b/LibreNMS/Util/IP.php @@ -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 diff --git a/tests/IpTest.php b/tests/IpTest.php index f1802a9..1439fca 100644 --- a/tests/IpTest.php +++ b/tests/IpTest.php @@ -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 */