Skip to content

Add support for compressed form of IPv6#3

Merged
ip2location-com merged 1 commit intomasterfrom
unknown repository
Nov 23, 2020
Merged

Add support for compressed form of IPv6#3
ip2location-com merged 1 commit intomasterfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Nov 22, 2020

Resolves issue: #2

@ip2location-com
Copy link
Copy Markdown
Owner

Something seems wrong with your code. For IP 2001:4860:4860::8844, I am getting 9 chunks when we expect a max of 8 chunks for IPv6.

@ghost
Copy link
Copy Markdown
Author

ghost commented Nov 23, 2020

I do not get that in a test script that makes use of my changes posted in the commit. I get 8 chunks. Is there an extra :?

@ip2location-com
Copy link
Copy Markdown
Owner

ip2location-com commented Nov 23, 2020

My test code below:

bn = require("nums.bn")

function checkip(ip)
local R = {ERROR = 0, IPV4 = 4, IPV6 = 6}
if type(ip) ~= "string" then return R.ERROR end

-- check for format 1.11.111.111 for ipv4
local chunks = {ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")}
if #chunks == 4 then
local ipnum = bn.ZERO
local octet = 0
for x,v in pairs(chunks) do
octet = tonumber(v)
if octet > 255 then return R.ERROR end
ipnum = ipnum + (bn(octet) << (8 * (4 - x)))
end

local ipindex = 0;
if self.ipv4indexbaseaddr > 0 then
  ipindex = ((ipnum >> 16) << 3):asnumber() + self.ipv4indexbaseaddr
end
return R.IPV4, ipnum, ipindex

end

local isIPv6 = false;
local hextets = 8;

if #chunks == 0 then
-- parse the ipv6 string using the expected pattern
for hextet in ip:gmatch("[a-fA-F0-9]*") do
print("hextet: " .. hextet)
table.insert(chunks, hextet)
end
if #chunks > 0 then isIPv6 = true; end
end

-- expand the ipv6 address and add zeroes
if isIPv6 == true then
for i = 1, #chunks do
if chunks[i] == "" then
for j = 1 , hextets - (#chunks - 1) - 1 do
if j == 1 then chunks[i] = "0" end
table.insert(chunks, i, "0")
end
end
end
if chunks[hextets] == "" then chunks[hextets] = "0" end
end

for i = 1, #chunks do
print("chunks " .. i .. ": " .. chunks[i])
end

print("no. of chunks: " .. #chunks)
end

checkip("2001:4860:4860::8844")

The output I'm seeing is:

hextet: 2001
hextet:
hextet: 4860
hextet:
hextet: 4860
hextet:
hextet:
hextet: 8844
hextet:
chunks 1: 2001
chunks 2:
chunks 3: 4860
chunks 4:
chunks 5: 4860
chunks 6:
chunks 7:
chunks 8: 8844
chunks 9:
no. of chunks: 9

Is that how it's supposed to appear?

@ghost
Copy link
Copy Markdown
Author

ghost commented Nov 23, 2020

No. The output of mine looks like this:

Address =       2001:4860:4860::8844
Parsed chunks =         5
Total chunks =  8
1        --     2001
2        --     4860
3        --     4860
4        --     0
5        --     0
6        --     0
7        --     0
8        --     8844

The only difference I see between your test and mine is that I am using single quotes in a variable assignment in the script instead of this function:

checkip("2001:4860:4860::8844")

We have also tested this on our server and it appears to be working properly.

@ip2location-com
Copy link
Copy Markdown
Owner

Can you please share your test script? We need to check if there is any issue with our Lua version.

@ghost
Copy link
Copy Markdown
Author

ghost commented Nov 23, 2020

-- provide test ipv6 address, edit here for testing
ip = '2001:4860:4860::8844';
print("Address = ", ip);

-- define empty table and ipv6 expanded address size
local chunks = {};
local hextets = 8;

-- insert data into a table when it represents 'xxxx', null when it doesn't 
for hextet in ip:gmatch("[a-fA-F0-9]*")
do
    table.insert(chunks, hextet);
end

print("Parsed chunks = ", #chunks);

-- iterate through the table
for i = 1, #chunks
do
    -- '::' is added to the table as a null
    if (chunks[i] == "") then
        -- count the remaining spaces in the table to add '0'
        -- and insert with padding
        for j = 1 , hextets - (#chunks - 1) - 1
        do
            if (j == 1) then
                chunks[i] = "0";
            end
            table.insert(chunks, i, "0");
        end
    end
end

print("Total chunks = ", #chunks);

if (chunks[hextets] == "") then
    chunks[hextets] = "0";
end

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

I am using 5.4.1. It has also been tested in version 5.3 on our server.

Thanks for the help. :)

@ip2location-com
Copy link
Copy Markdown
Owner

I'm getting the below:

Address = 2001:4860:4860::8844
Parsed chunks = 9
Total chunks = 9
1 -- 2001
2 --
3 -- 4860
4 --
5 -- 4860
6 --
7 --
8 -- 8844
9 --

Starting to look like an issue with my Lua 5.3 running on ZeroBrane Studio.

@ghost
Copy link
Copy Markdown
Author

ghost commented Nov 23, 2020

I am running it in a standard terminal on Manjaro Linux. You might try that just to confirm.

@ip2location-com
Copy link
Copy Markdown
Owner

Works just fine in Debian. So I'll merge this pull request first then check on the ZeroBrane issue.

Address = 2001:4860:4860::8844
Parsed chunks = 5
Total chunks = 8
1 -- 2001
2 -- 4860
3 -- 4860
4 -- 0
5 -- 0
6 -- 0
7 -- 0
8 -- 8844

@ip2location-com ip2location-com merged commit 896a99f into ip2location-com:master Nov 23, 2020
@ghost
Copy link
Copy Markdown
Author

ghost commented Nov 23, 2020

Thank you for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant