Skip to content

Commit

Permalink
Rewrite naked local.db entries into a /32 CIDR.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertHauwaerts committed Apr 23, 2019
1 parent 98709b4 commit f7a201a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ExaBGP/VoIPBL/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ private function updateBlacklistLocal()
}

$this->localbl['data'] = array_map('chop', $this->localbl['data']);
$this->localbl['data'] = array_map([$this->validator, 'makeCIDR'], $this->localbl['data']);

$this->localbl['data'] = array_filter($this->localbl['data'], [$this->validator, 'isCIDR']);
$this->localbl['data'] = array_filter($this->localbl['data'], [$this->validator, 'isNotReservedIP']);

Expand Down
16 changes: 16 additions & 0 deletions src/ExaBGP/VoIPBL/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ public function isRegularExpression($expr)
return @preg_match($expr, '') !== false;
}

/**
* Change an IP into an IP/CIDR.
*
* @param string $expr An IP address.
*
* @return bool
*/
public function makeCIDR($ip)
{
if ($this->isIP($ip)) {
return $ip . '/32';
}

return $ip;
}

/**
* Ensure the given file is readable.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/ExaBGP/VoIPBL/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,20 @@ public function testValidatorIsRegularExpression()
$this->assertFalse($validator->isRegularExpression('string'));
$this->assertFalse($validator->isRegularExpression(true));
}

/**
* Test the makeCIDR() function.
*
* @return void
*/
public function testValidatorMakeCIDR()
{
$validator = new Validator();

$this->assertEquals($validator->makeCIDR('10.0.0.1'), '10.0.0.1/32');
$this->assertEquals($validator->makeCIDR('10.0.0.1/32'), '10.0.0.1/32');

$this->assertEquals($validator->makeCIDR('string'), 'string');
$this->assertEquals($validator->makeCIDR(true), true);
}
}

0 comments on commit f7a201a

Please sign in to comment.