Skip to content

Commit b73b766

Browse files
committed
Iterable IPv4 & CI testing
1 parent ac51674 commit b73b766

File tree

5 files changed

+105
-1423
lines changed

5 files changed

+105
-1423
lines changed

.github/workflows/ci.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: CI
2+
3+
on: [push, pull-request]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
php-versions: [ '5.6', '7.4', '8.2' ]
15+
16+
steps:
17+
- name: Code Checkout
18+
uses: actions/checkout@v3
19+
run: echo "The ${{ github.repository }} repository has been cloned to the runner."
20+
- name: Install PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php-versions }}
24+
- name: Composer
25+
uses: php-actions/composer@v6
26+
run: echo "Composer dependencies have been installed"
27+
- name: PHPUnit
28+
run: vendor/bin/phpunit

LibreNMS/Util/IPv4.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727

2828
use LibreNMS\Exceptions\InvalidIpException;
2929

30-
class IPv4 extends IP
30+
class IPv4 extends IP implements \Iterator
3131
{
32+
private $current;
33+
private $networkLongIp;
34+
3235
/**
3336
* IPv4 constructor.
3437
* @param $ipv4
@@ -42,6 +45,8 @@ public function __construct($ipv4)
4245
if (!self::isValid($this->ip)) {
4346
throw new InvalidIpException("$ipv4 is not a valid ipv4 address");
4447
}
48+
49+
$this->networkLongIp = $this->calculateNetworkAddress($this->ip, $this->cidr);
4550
}
4651

4752
/**
@@ -113,11 +118,11 @@ public function inNetwork($network)
113118
*/
114119
public function getNetworkAddress($cidr = null)
115120
{
116-
if (is_null($cidr)) {
117-
$cidr = $this->cidr;
121+
if (is_null($cidr) || $cidr == $this->cidr) {
122+
return long2ip($this->networkLongIp);
118123
}
119124

120-
return long2ip(ip2long($this->ip) & $this->cidr2long($cidr));
125+
return long2ip($this->calculateNetworkAddress($this->ip, $cidr));
121126
}
122127

123128
/**
@@ -130,6 +135,19 @@ public function toSnmpIndex()
130135
return (string)$this->ip;
131136
}
132137

138+
139+
/**
140+
* Get the long of the network address for the given IP and cidr.
141+
*
142+
* @param string $ip
143+
* @param int $cidr
144+
* @return int
145+
*/
146+
protected function calculateNetworkAddress($ip, $cidr)
147+
{
148+
return ip2long($ip) & $this->cidr2long($cidr);
149+
}
150+
133151
/**
134152
* Extract an address from a cidr, assume a host is given if it does not contain /
135153
* Handle netmasks in addition to cidr
@@ -152,4 +170,44 @@ protected function extractCidr($ip)
152170

153171
return $parts;
154172
}
173+
174+
// --- Iterable Methods ---
175+
public function current()
176+
{
177+
return long2ip($this->current);
178+
}
179+
180+
public function next()
181+
{
182+
$this->current++;
183+
}
184+
185+
public function key()
186+
{
187+
return $this->current;
188+
}
189+
190+
public function valid()
191+
{
192+
if ($this->cidr == 32) {
193+
return $this->current === $this->networkLongIp;
194+
}
195+
196+
if ($this->cidr == 31) {
197+
198+
return $this->current === $this->networkLongIp || $this->current === ($this->networkLongIp + 1);
199+
}
200+
201+
$max = $this->networkLongIp - $this->cidr2long($this->cidr) - 1;
202+
203+
return $this->current > $this->networkLongIp && $this->current < $max;
204+
}
205+
206+
public function rewind()
207+
{
208+
$this->current = $this->cidr > 30
209+
? $this->networkLongIp
210+
: $this->networkLongIp + 1;
211+
212+
}
155213
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"php": ">=5.6.4"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "5.*",
19-
"squizlabs/php_codesniffer": "^2.9.1"
18+
"phpunit/phpunit": ">5.7",
19+
"squizlabs/php_codesniffer": ">=2.9.1"
2020
},
2121
"autoload": {
2222
"psr-4": {

0 commit comments

Comments
 (0)