Skip to content

Commit

Permalink
Mark reverseIp as @internal, lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jbboehr committed Sep 28, 2024
1 parent b87f346 commit 41f240f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
23 changes: 15 additions & 8 deletions src/DNSBL.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,30 +215,37 @@ protected function buildLookUpHost(string $ip, string $blacklist): string
/**
* Reverse the order of an IP address to PTR
* IPv4: 127.0.0.1 -> 1.0.0.127
* IPv6: 2001:db8::567:89ab -> b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2 // Example taken from https://en.wikipedia.org/wiki/Reverse_DNS_lookup#IPv6_reverse_resolution
* IPv6: 2001:db8::567:89ab -> b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2
* Example taken from https://en.wikipedia.org/wiki/Reverse_DNS_lookup#IPv6_reverse_resolution
*
* @param string $ipAddress IP address to reverse.
*
* @internal
* @access protected
* @return string Reversed IP address
* @throws \Exception
*/
public function reverseIp(string $ipAddress): string
{
$ipAddress = trim($ipAddress, '[]'); // IPv6 addresses are sometimes enclosed in square brackets. Removing them here for IPv4 too :-)
// IPv6 addresses are sometimes enclosed in square brackets. Removing them here for IPv4 too
$ipAddress = trim($ipAddress, '[]');

if(filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
if (false !== filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return implode('.', array_reverse(explode('.', $ipAddress)));
}
elseif (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
} elseif (false !== filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
// Code is taken from https://stackoverflow.com/a/6621473/1668200
$in_addr = inet_pton($ipAddress);
if (false === $in_addr) {
throw new \Exception("Failed to parse IP address: " . $ipAddress);
}
$unpack = unpack('H*hex', $in_addr);
if (!isset($unpack['hex']) || !is_string($unpack['hex'])) {
throw new \Exception("Failed to parse IP address: " . $ipAddress);
}
$hex = $unpack['hex'];
return implode('.', array_reverse(str_split($hex)));
}
else {
throw new \Exception();
} else {
throw new \Exception("Failed to parse IP address: " . $ipAddress);
}
}

Expand Down
10 changes: 8 additions & 2 deletions tests/DNSBLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,14 @@ public function testReverseIp(): void
$this->assertEquals('1.0.0.127', $this->rbl->reverseIp('[127.0.0.1]'));

// IPv6:
$this->assertEquals('b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2', $this->rbl->reverseIp('2001:db8::567:89ab'));
$this->assertEquals('b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2', $this->rbl->reverseIp('[2001:db8::567:89ab]'));
$this->assertEquals(
'b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2',
$this->rbl->reverseIp('2001:db8::567:89ab')
);
$this->assertEquals(
'b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2',
$this->rbl->reverseIp('[2001:db8::567:89ab]')
);

// Invalid IP address:
$this->expectException(\Exception::class);
Expand Down

0 comments on commit 41f240f

Please sign in to comment.