aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md6
-rw-r--r--lib/routing_table.ex61
-rw-r--r--test/routing_table_test.exs26
3 files changed, 47 insertions, 46 deletions
diff --git a/README.md b/README.md
index 2a6df43..f9dcc59 100644
--- a/README.md
+++ b/README.md
@@ -8,7 +8,7 @@ The tables covers both IPv4 and IPv6, and values are any erlang term, stored in
table = RoutingTable.new()
RoutingTable.add(table, {10, 69, 0, 0}, 16, :vpn)
RoutingTable.add(table, {10, 69, 1, 0}, 24, :lan)
-:vpn = RoutingTable.longest_match(table, {10, 69, 2, 1})
-:lan = RoutingTable.longest_match(table, {10, 69, 1, 1})
-nil = RoutingTable.longest_match(table, {10, 68, 1, 1})
+:vpn = RoutingTable.lookup(table, {10, 69, 2, 1})
+:lan = RoutingTable.lookup(table, {10, 69, 1, 1})
+nil = RoutingTable.lookup(table, {10, 68, 1, 1})
```
diff --git a/lib/routing_table.ex b/lib/routing_table.ex
index 90b0160..93bb94f 100644
--- a/lib/routing_table.ex
+++ b/lib/routing_table.ex
@@ -12,9 +12,10 @@ defmodule RoutingTable do
table = RoutingTable.new()
RoutingTable.add(table, {10, 69, 0, 0}, 16, :vpn)
RoutingTable.add(table, {10, 69, 1, 0}, 24, :lan)
- :vpn = RoutingTable.longest_match(table, {10, 69, 2, 1})
- :lan = RoutingTable.longest_match(table, {10, 69, 1, 1})
- nil = RoutingTable.longest_match(table, {10, 68, 1, 1})
+ :vpn = RoutingTable.lookup(table, {10, 69, 2, 1})
+ :lan = RoutingTable.lookup(table, {10, 69, 1, 1})
+ nil = RoutingTable.lookup(table, {10, 68, 1, 1})
+ false = RoutingTable.reachable?(table, {10, 68, 1, 1})
```
"""
@@ -44,47 +45,47 @@ defmodule RoutingTable do
remove(tree, tree.i6, {:inet6, a, b, c, d, e, f, g, h}, masklen)
end
- @spec longest_match(t(), :inet.ip_address()) :: map() | nil
- def longest_match(tree, ip)
+ @spec lookup(t(), :inet.ip_address()) :: map() | nil
+ def lookup(tree, ip)
- def longest_match(tree, {a, b, c, d}) do
+ def lookup(tree, {a, b, c, d}) do
longest_match(tree, tree.i4, {:inet4, a, b, c, d})
end
- def longest_match(tree, {a, b, c, d, e, f, g, h}) do
+ def lookup(tree, {a, b, c, d, e, f, g, h}) do
longest_match(tree, tree.i6, {:inet6, a, b, c, d, e, f, g, h})
end
- @spec longest_match?(t(), :inet.ip_address()) :: boolean()
- def longest_match?(tree, ip)
+ @spec match(t(), :inet.ip_address(), masklen()) :: map() | nil
+ def match(tree, ip, masklen)
- def longest_match?(tree, {a, b, c, d}) do
- longest_match?(tree, tree.i4, {:inet4, a, b, c, d})
+ def match(tree, {a, b, c, d}, masklen) do
+ exact_match(tree, tree.i4, {:inet4, a, b, c, d}, masklen)
end
- def longest_match?(tree, {a, b, c, d, e, f, g, h}) do
- longest_match?(tree, tree.i6, {:inet6, a, b, c, d, e, f, g, h})
+ def match(tree, {a, b, c, d, e, f, g, h}, masklen) do
+ exact_match(tree, tree.i6, {:inet6, a, b, c, d, e, f, g, h}, masklen)
end
- @spec exact_match(t(), :inet.ip_address(), masklen()) :: map() | nil
- def exact_match(tree, ip, masklen)
+ @spec reachable?(t(), :inet.ip_address()) :: boolean()
+ def reachable?(tree, ip)
- def exact_match(tree, {a, b, c, d}, masklen) do
- exact_match(tree, tree.i4, {:inet4, a, b, c, d}, masklen)
+ def reachable?(tree, {a, b, c, d}) do
+ longest_match?(tree, tree.i4, {:inet4, a, b, c, d})
end
- def exact_match(tree, {a, b, c, d, e, f, g, h}, masklen) do
- exact_match(tree, tree.i6, {:inet6, a, b, c, d, e, f, g, h}, masklen)
+ def reachable?(tree, {a, b, c, d, e, f, g, h}) do
+ longest_match?(tree, tree.i6, {:inet6, a, b, c, d, e, f, g, h})
end
- @spec exact_match?(t(), :inet.ip_address(), masklen()) :: boolean()
- def exact_match?(tree, ip, masklen)
+ @spec reachable?(t(), :inet.ip_address(), masklen()) :: boolean()
+ def reachable?(tree, ip, masklen)
- def exact_match?(tree, {a, b, c, d}, masklen) do
+ def reachable?(tree, {a, b, c, d}, masklen) do
exact_match?(tree, tree.i4, {:inet4, a, b, c, d}, masklen)
end
- def exact_match?(tree, {a, b, c, d, e, f, g, h}, masklen) do
+ def reachable?(tree, {a, b, c, d, e, f, g, h}, masklen) do
exact_match?(tree, tree.i6, {:inet6, a, b, c, d, e, f, g, h}, masklen)
end
@@ -140,13 +141,6 @@ defmodule RoutingTable do
end
end
- defp longest_match?(_, tbm, ip) do
- case TreeBitmap.longest_match(tbm, ip) do
- {:ok, nil} -> false
- {:ok, _, _, _} -> true
- end
- end
-
defp exact_match(tree, tbm, ip, masklen) do
case TreeBitmap.exact_match(tbm, ip, masklen) do
{:ok, nil} ->
@@ -157,6 +151,13 @@ defmodule RoutingTable do
end
end
+ defp longest_match?(_, tbm, ip) do
+ case TreeBitmap.longest_match(tbm, ip) do
+ {:ok, nil} -> false
+ {:ok, _, _, _} -> true
+ end
+ end
+
defp exact_match?(_, tbm, ip, masklen) do
case TreeBitmap.exact_match(tbm, ip, masklen) do
{:ok, nil} -> false
diff --git a/test/routing_table_test.exs b/test/routing_table_test.exs
index 730ed0d..a5ba3fd 100644
--- a/test/routing_table_test.exs
+++ b/test/routing_table_test.exs
@@ -7,26 +7,26 @@ defmodule RoutingTableTest do
assert nil == RoutingTable.add(t, {192, 168, 1, 0}, 24, :lan)
assert nil == RoutingTable.add(t, {8193, 3512, 34211, 0, 0, 35374, 880, 1}, 64, :lan)
- assert %{value: :lan} = RoutingTable.longest_match(t, {192, 168, 1, 2})
- assert true = RoutingTable.longest_match?(t, {192, 168, 1, 2})
- assert %{value: :lan} = RoutingTable.longest_match(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492})
- assert true = RoutingTable.longest_match?(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492})
+ assert %{value: :lan} = RoutingTable.lookup(t, {192, 168, 1, 2})
+ assert true = RoutingTable.reachable?(t, {192, 168, 1, 2})
+ assert %{value: :lan} = RoutingTable.lookup(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492})
+ assert true = RoutingTable.reachable?(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492})
- assert :lan = RoutingTable.exact_match(t, {192, 168, 1, 1}, 24)
- assert true = RoutingTable.exact_match?(t, {192, 168, 1, 1}, 24)
- assert :lan = RoutingTable.exact_match(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492}, 64)
- assert true = RoutingTable.exact_match?(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492}, 64)
+ assert :lan = RoutingTable.match(t, {192, 168, 1, 1}, 24)
+ assert true = RoutingTable.reachable?(t, {192, 168, 1, 1}, 24)
+ assert :lan = RoutingTable.match(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492}, 64)
+ assert true = RoutingTable.reachable?(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492}, 64)
- assert nil == RoutingTable.longest_match(t, {8, 8, 8, 8})
- assert false == RoutingTable.longest_match?(t, {8, 8, 8, 8})
- assert nil == RoutingTable.exact_match(t, {8, 8, 8, 8}, 32)
- assert false == RoutingTable.exact_match?(t, {8, 8, 8, 8}, 32)
+ assert nil == RoutingTable.lookup(t, {8, 8, 8, 8})
+ assert false == RoutingTable.reachable?(t, {8, 8, 8, 8})
+ assert nil == RoutingTable.match(t, {8, 8, 8, 8}, 32)
+ assert false == RoutingTable.reachable?(t, {8, 8, 8, 8}, 32)
assert %{ets: 330, inet4: {1248, 1168}, inet6: {1344, 1168}} = RoutingTable.memory(t)
assert %{ets: 2, inet4: 1, inet6: 1} = RoutingTable.length(t)
assert :lan = RoutingTable.remove(t, {8193, 3512, 34211, 0, 0, 35374, 880, 1}, 64)
- assert nil == RoutingTable.longest_match(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492})
+ assert nil == RoutingTable.lookup(t, {8193, 3512, 34211, 0, 0, 35374, 880, 29492})
assert %{ets: 2, inet4: 1, inet6: 0} = RoutingTable.length(t)
assert :lan == RoutingTable.remove(t, {192, 168, 1, 0}, 24)
assert %{ets: 1, inet4: 0, inet6: 0} = RoutingTable.length(t)