Skip to content

Commit 45f034b

Browse files
committed
Fixed Teredo/6to4 matching
1 parent 53a0a44 commit 45f034b

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 ip2location
3+
Copyright (c) 2020 ip2location
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package = "ip2location"
2-
version = "8.3.0-1"
2+
version = "8.3.1-1"
33
source = {
44
url = "git://github.com/ip2location/ip2location-lua.git"
55
}

ip2location.lua

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ local from_6to4 = bn("42545680458834377588178886921629466624")
8989
local to_6to4 = bn("42550872755692912415807417417958686719")
9090
local from_teredo = bn("42540488161975842760550356425300246528")
9191
local to_teredo = bn("42540488241204005274814694018844196863")
92-
local last_32bits = bn(4294967295)
9392

9493
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}
9594
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}
@@ -111,7 +110,7 @@ local mobilebrand_position = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111110
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}
112111
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}
113112

114-
local api_version = "8.3.0"
113+
local api_version = "8.3.1"
115114

116115
local modes = {
117116
countryshort = 0x00001,
@@ -449,6 +448,12 @@ function ip2location:checkip(ip)
449448
end
450449
if chunks[hextets] == "" then chunks[hextets] = "0" end
451450
end
451+
452+
-- DEBUGGING CODE
453+
-- for key, value in pairs(chunks)
454+
-- do
455+
-- print(key, " -- " , value);
456+
-- end
452457

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

463468
local override = 0
464469

470+
-- DEBUGGING
471+
-- print("IPNUM BEFORE: " .. ipnum)
472+
465473
-- special cases which should convert to equivalent IPv4
466474
if ipnum >= from_v4mapped and ipnum <= to_v4mapped then -- ipv4-mapped ipv6
475+
-- print("IPv4-mapped") -- DEBUGGING
467476
override = 1
468477
ipnum = ipnum - from_v4mapped
469478
elseif ipnum >= from_6to4 and ipnum <= to_6to4 then -- 6to4
479+
-- print("6to4") -- DEBUGGING
470480
override = 1
471481
ipnum = ipnum >> 80
472-
ipnum = ipnum & last_32bits
482+
ipnum2 = ipnum:asnumber() & 0xffffffff
483+
ipnum = bn(ipnum2) -- convert back to bn
473484
elseif ipnum >= from_teredo and ipnum <= to_teredo then -- Teredo
485+
-- print("Teredo") -- DEBUGGING
474486
override = 1
475487
ipnum = ~ipnum
476-
ipnum = ipnum & last_32bits
488+
ipnum2 = ipnum:asnumber() & 0xffffffff
489+
-- print("ipnum2: " .. ipnum2) -- DEBUGGING
490+
ipnum = bn(ipnum2) -- convert back to bn
477491
end
478492

493+
-- DEBUGGING
494+
-- print("IPNUM AFTER: " .. ipnum)
495+
479496
local ipindex = 0;
480497
if override == 1 then
481498
if self.ipv4indexbaseaddr > 0 then

0 commit comments

Comments
 (0)