Skip to content

Commit

Permalink
Fixed boundary IP cases. Fixed error with IPv6 address when using IPv…
Browse files Browse the repository at this point in the history
…4 BIN.
  • Loading branch information
ip2location committed Dec 3, 2024
1 parent 4423391 commit 5d078ee
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
51 changes: 45 additions & 6 deletions ip2location.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
-define(IF(Cond), (case (Cond) of true -> (0); false -> (1) end)).

apiversion() ->
"8.6.1".
"8.6.2".

getapiversion() ->
io:format("API Version: ~p~n", [apiversion()]).
Expand Down Expand Up @@ -369,18 +369,57 @@ query(Ip) ->
Result = case inet:parse_address(Ip) of
{ok, {X1, X2, X3, X4}} ->
Ipnum = (X1 bsl 24) + (X2 bsl 16) + (X3 bsl 8) + (X4),
search4(S, Ipnum, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
if
Ipnum == 4294967295 ->
Ipnum2 = Ipnum - 1;
true ->
Ipnum2 = Ipnum
end,
search4(S, Ipnum2, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
{ok, {X1, X2, X3, X4, X5, X6, X7, X8}} ->
Ipnum = (X1 bsl 112) + (X2 bsl 96) + (X3 bsl 80) + (X4 bsl 64) + (X5 bsl 48) + (X6 bsl 32) + (X7 bsl 16) + X8,
if
Ipnum >= Fromv4mapped andalso Ipnum =< Tov4mapped ->
search4(S, Ipnum - Fromv4mapped, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
Ipnum2 = Ipnum - Fromv4mapped,
if
Ipnum2 == 4294967295 ->
Ipnum3 = Ipnum2 - 1;
true ->
Ipnum3 = Ipnum2
end,
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
Ipnum >= From6to4 andalso Ipnum =< To6to4 ->
search4(S, (Ipnum bsr 80) band Last32bits, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
Ipnum2 = (Ipnum bsr 80) band Last32bits,
if
Ipnum2 == 4294967295 ->
Ipnum3 = Ipnum2 - 1;
true ->
Ipnum3 = Ipnum2
end,
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
Ipnum >= Fromteredo andalso Ipnum =< Toteredo ->
search4(S, ((bnot Ipnum) band Last32bits), Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
Ipnum2 = (bnot Ipnum) band Last32bits,
if
Ipnum2 == 4294967295 ->
Ipnum3 = Ipnum2 - 1;
true ->
Ipnum3 = Ipnum2
end,
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
true ->
search6(S, Ipnum, Databasetype, 0, Ipv6databasecount, Ipv6databaseaddr, Ipv6indexbaseaddr, Ipv6columnsize)
if
Ipv6databasecount > 0 ->
if
Ipnum == 340282366920938463463374607431768211455 ->
Ipnum2 = Ipnum - 1;
true ->
Ipnum2 = Ipnum
end,
search6(S, Ipnum2, Databasetype, 0, Ipv6databasecount, Ipv6databaseaddr, Ipv6indexbaseaddr, Ipv6columnsize);
true ->
io:format("Error: IPv6 address is missing in IPv4 BIN.~n", []),
{} % return empty
end
end;
{_, _} ->
io:format("Error: Invalid IP address.~n", []),
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule IP2LocationErlang.MixProject do
def project() do
[
app: :ip2location_erlang,
version: "8.6.1",
version: "8.6.2",
elixir: "~> 1.0",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand Down

0 comments on commit 5d078ee

Please sign in to comment.