Skip to content

Commit 936d506

Browse files
committed
Add isNetwork() method
1 parent 1396492 commit 936d506

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

LibreNMS/Util/IP.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ public function inNetworks($networks)
155155
*/
156156
abstract public function inNetwork($network);
157157

158+
/**
159+
* Checks if this IP is a network.
160+
* This generally means hostmask is < 32 or < 128
161+
*
162+
* @return bool
163+
*/
164+
public function isNetwork()
165+
{
166+
return $this->cidr < $this->host_bits;
167+
}
168+
158169
/**
159170
* Check if this IP is in the reserved range.
160171
* @return bool

tests/IpTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public function testNetworkParse()
8282
$this->assertEquals('::1', IP::parse('::1/128'));
8383
}
8484

85+
public function testIsNetwork()
86+
{
87+
$this->assertFalse(IP::parse('192.168.3.0')->isNetwork());
88+
$this->assertFalse(IP::parse('192.168.3.0/32')->isNetwork());
89+
$this->assertTrue(IP::parse('192.168.3.0/24')->isNetwork());
90+
$this->assertFalse(IPv6::parse('2001:db8:85a3::8a2e:370:7334')->isNetwork());
91+
$this->assertFalse(IPv6::parse('2001:db8:85a3::8a2e:370:7334/128')->isNetwork());
92+
$this->assertTrue(IPv6::parse('2001:db8:85a3::8a2e:370:7334/64')->isNetwork());
93+
}
94+
8595
/**
8696
* @expectedException \LibreNMS\Exceptions\InvalidIpException
8797
*/

0 commit comments

Comments
 (0)