aboutsummaryrefslogtreecommitdiff
path: root/shard/lib/net/addr.ex
diff options
context:
space:
mode:
Diffstat (limited to 'shard/lib/net/addr.ex')
-rw-r--r--shard/lib/net/addr.ex18
1 files changed, 18 insertions, 0 deletions
diff --git a/shard/lib/net/addr.ex b/shard/lib/net/addr.ex
index c1d2f05..b92ae70 100644
--- a/shard/lib/net/addr.ex
+++ b/shard/lib/net/addr.ex
@@ -1,4 +1,10 @@
defmodule SNet.Addr do
+ @moduledoc"""
+ Helper module for getting our IP addresses.
+
+ Runs an agent that gets our public IPv4 address on the internet and stores it.
+ """
+
use Agent
require Logger
@@ -21,6 +27,9 @@ defmodule SNet.Addr do
end
end
+ @doc"""
+ Reteurn the list of IPv4 address for our network interfaces.
+ """
def get_if_inet4 do
{:ok, ifs} = :inet.getifaddrs
for {_, opts} <- ifs,
@@ -32,15 +41,24 @@ defmodule SNet.Addr do
end
end
+ @doc"""
+ Return our public IPv4 address as observed by an external API provider (`ipify.org`)
+ """
def get_pub_inet4 do
Agent.get(__MODULE__, &(&1))
end
+ @doc"""
+ Get all our IPv4 addresses.
+ """
def get_all_inet4 do
addrset = for x <- get_if_inet4() ++ get_pub_inet4(), into: %MapSet{}, do: x
MapSet.to_list addrset
end
+ @doc"""
+ Determines if an IP address is ours or not.
+ """
def is_local?({:inet, ip, port}) do
port == Application.get_env(:shard, :port) and (ip == {127,0,0,1} or ip in get_all_inet4())
end