Skip to content

Commit

Permalink
Fixed Teredo/6to4 matching
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Nov 24, 2020
1 parent 53a0a44 commit 45f034b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 ip2location
Copyright (c) 2020 ip2location

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package = "ip2location"
version = "8.3.0-1"
version = "8.3.1-1"
source = {
url = "git://github.com/ip2location/ip2location-lua.git"
}
Expand Down
25 changes: 21 additions & 4 deletions ip2location.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ local from_6to4 = bn("42545680458834377588178886921629466624")
local to_6to4 = bn("42550872755692912415807417417958686719")
local from_teredo = bn("42540488161975842760550356425300246528")
local to_teredo = bn("42540488241204005274814694018844196863")
local last_32bits = bn(4294967295)

local country_position = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
local region_position = {0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}
Expand All @@ -111,7 +110,7 @@ local mobilebrand_position = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
local elevation_position = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19}
local usagetype_position = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20}

local api_version = "8.3.0"
local api_version = "8.3.1"

local modes = {
countryshort = 0x00001,
Expand Down Expand Up @@ -449,6 +448,12 @@ function ip2location:checkip(ip)
end
if chunks[hextets] == "" then chunks[hextets] = "0" end
end

-- DEBUGGING CODE
-- for key, value in pairs(chunks)
-- do
-- print(key, " -- " , value);
-- end

-- only support full IPv6 format for now
if #chunks == 8 then
Expand All @@ -462,20 +467,32 @@ function ip2location:checkip(ip)

local override = 0

-- DEBUGGING
-- print("IPNUM BEFORE: " .. ipnum)

-- special cases which should convert to equivalent IPv4
if ipnum >= from_v4mapped and ipnum <= to_v4mapped then -- ipv4-mapped ipv6
-- print("IPv4-mapped") -- DEBUGGING
override = 1
ipnum = ipnum - from_v4mapped
elseif ipnum >= from_6to4 and ipnum <= to_6to4 then -- 6to4
-- print("6to4") -- DEBUGGING
override = 1
ipnum = ipnum >> 80
ipnum = ipnum & last_32bits
ipnum2 = ipnum:asnumber() & 0xffffffff
ipnum = bn(ipnum2) -- convert back to bn
elseif ipnum >= from_teredo and ipnum <= to_teredo then -- Teredo
-- print("Teredo") -- DEBUGGING
override = 1
ipnum = ~ipnum
ipnum = ipnum & last_32bits
ipnum2 = ipnum:asnumber() & 0xffffffff
-- print("ipnum2: " .. ipnum2) -- DEBUGGING
ipnum = bn(ipnum2) -- convert back to bn
end

-- DEBUGGING
-- print("IPNUM AFTER: " .. ipnum)

local ipindex = 0;
if override == 1 then
if self.ipv4indexbaseaddr > 0 then
Expand Down

0 comments on commit 45f034b

Please sign in to comment.