Skip to content

Commit 53a0a44

Browse files
Fixed matching for IPv4-mapped IPv6
1 parent 896a99f commit 53a0a44

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

ip2location.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,24 @@ function ip2location:checkip(ip)
401401
return R.IPV4, ipnum, ipindex
402402
end
403403

404+
-- check for format ::FFFF:1.11.111.111 for ipv4
405+
local chunks = {ip:match("^%:%:[fF][fF][fF][fF]%:(%d+)%.(%d+)%.(%d+)%.(%d+)$")}
406+
if #chunks == 4 then
407+
local ipnum = bn.ZERO
408+
local octet = 0
409+
for x,v in pairs(chunks) do
410+
octet = tonumber(v)
411+
if octet > 255 then return R.ERROR end
412+
ipnum = ipnum + (bn(octet) << (8 * (4 - x)))
413+
end
414+
415+
local ipindex = 0;
416+
if self.ipv4indexbaseaddr > 0 then
417+
ipindex = ((ipnum >> 16) << 3):asnumber() + self.ipv4indexbaseaddr
418+
end
419+
return R.IPV4, ipnum, ipindex
420+
end
421+
404422
-- check for ipv6 format, should be 8 'chunks' of numbers/letters
405423
-- without leading/trailing chars
406424
-- or fewer than 8 chunks, but with only one `::` group

0 commit comments

Comments
 (0)