Skip to content

Commit 5d078ee

Browse files
Fixed boundary IP cases. Fixed error with IPv6 address when using IPv4 BIN.
1 parent 4423391 commit 5d078ee

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

ip2location.erl

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
-define(IF(Cond), (case (Cond) of true -> (0); false -> (1) end)).
3131

3232
apiversion() ->
33-
"8.6.1".
33+
"8.6.2".
3434

3535
getapiversion() ->
3636
io:format("API Version: ~p~n", [apiversion()]).
@@ -369,18 +369,57 @@ query(Ip) ->
369369
Result = case inet:parse_address(Ip) of
370370
{ok, {X1, X2, X3, X4}} ->
371371
Ipnum = (X1 bsl 24) + (X2 bsl 16) + (X3 bsl 8) + (X4),
372-
search4(S, Ipnum, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
372+
if
373+
Ipnum == 4294967295 ->
374+
Ipnum2 = Ipnum - 1;
375+
true ->
376+
Ipnum2 = Ipnum
377+
end,
378+
search4(S, Ipnum2, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
373379
{ok, {X1, X2, X3, X4, X5, X6, X7, X8}} ->
374380
Ipnum = (X1 bsl 112) + (X2 bsl 96) + (X3 bsl 80) + (X4 bsl 64) + (X5 bsl 48) + (X6 bsl 32) + (X7 bsl 16) + X8,
375381
if
376382
Ipnum >= Fromv4mapped andalso Ipnum =< Tov4mapped ->
377-
search4(S, Ipnum - Fromv4mapped, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
383+
Ipnum2 = Ipnum - Fromv4mapped,
384+
if
385+
Ipnum2 == 4294967295 ->
386+
Ipnum3 = Ipnum2 - 1;
387+
true ->
388+
Ipnum3 = Ipnum2
389+
end,
390+
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
378391
Ipnum >= From6to4 andalso Ipnum =< To6to4 ->
379-
search4(S, (Ipnum bsr 80) band Last32bits, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
392+
Ipnum2 = (Ipnum bsr 80) band Last32bits,
393+
if
394+
Ipnum2 == 4294967295 ->
395+
Ipnum3 = Ipnum2 - 1;
396+
true ->
397+
Ipnum3 = Ipnum2
398+
end,
399+
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
380400
Ipnum >= Fromteredo andalso Ipnum =< Toteredo ->
381-
search4(S, ((bnot Ipnum) band Last32bits), Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
401+
Ipnum2 = (bnot Ipnum) band Last32bits,
402+
if
403+
Ipnum2 == 4294967295 ->
404+
Ipnum3 = Ipnum2 - 1;
405+
true ->
406+
Ipnum3 = Ipnum2
407+
end,
408+
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
382409
true ->
383-
search6(S, Ipnum, Databasetype, 0, Ipv6databasecount, Ipv6databaseaddr, Ipv6indexbaseaddr, Ipv6columnsize)
410+
if
411+
Ipv6databasecount > 0 ->
412+
if
413+
Ipnum == 340282366920938463463374607431768211455 ->
414+
Ipnum2 = Ipnum - 1;
415+
true ->
416+
Ipnum2 = Ipnum
417+
end,
418+
search6(S, Ipnum2, Databasetype, 0, Ipv6databasecount, Ipv6databaseaddr, Ipv6indexbaseaddr, Ipv6columnsize);
419+
true ->
420+
io:format("Error: IPv6 address is missing in IPv4 BIN.~n", []),
421+
{} % return empty
422+
end
384423
end;
385424
{_, _} ->
386425
io:format("Error: Invalid IP address.~n", []),

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule IP2LocationErlang.MixProject do
44
def project() do
55
[
66
app: :ip2location_erlang,
7-
version: "8.6.1",
7+
version: "8.6.2",
88
elixir: "~> 1.0",
99
build_embedded: Mix.env == :prod,
1010
start_permanent: Mix.env == :prod,

0 commit comments

Comments
 (0)