Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ilsanbao committed Nov 9, 2018
1 parent ef67793 commit 1908ba5
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/ipip/db/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function find($ip, $language)
try
{
$node = $this->findNode($ip);

if ($node > 0)
{
$data = $this->resolve($node);
Expand Down Expand Up @@ -133,24 +134,24 @@ public function findMap($ip, $language)
return array_combine($this->meta['fields'], $array);
}

private $v4offset = 0;
private $v6offsetCache = [];

/**
* @param $ip
* @return int
* @throws \Exception
*/
private function findNode($ip)
{
static $v4offset = 0;
static $v6offsetCache = [];

$binary = inet_pton($ip);
$bitCount = strlen($binary) * 8; // 32 | 128
$key = substr($binary, 0, 2);
$node = 0;
$index = 0;
if ($bitCount === 32)
{
if ($v4offset === 0)
if ($this->v4offset === 0)
{
for ($i = 0; $i < 96 && $node < $this->nodeCount; $i++)
{
Expand All @@ -168,19 +169,19 @@ private function findNode($ip)
return 0;
}
}
$v4offset = $node;
$this->v4offset = $node;
}
else
{
$node = $v4offset;
$node = $this->v4offset;
}
}
else
{
if (isset($v6offsetCache[$key]))
if (isset($this->v6offsetCache[$key]))
{
$index = 16;
$node = $v6offsetCache[$key];
$node = $this->v6offsetCache[$key];
}
}

Expand All @@ -195,7 +196,7 @@ private function findNode($ip)

if ($i == 15)
{
$v6offsetCache[$key] = $node;
$this->v6offsetCache[$key] = $node;
}
}

Expand Down

0 comments on commit 1908ba5

Please sign in to comment.