aboutsummaryrefslogtreecommitdiff
path: root/shard/lib/net/addr.ex
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-11-06 15:03:22 +0100
committerAlex Auvolat <alex@adnab.me>2018-11-06 15:03:22 +0100
commit2973cf99c5b677c71717d916f83212bc2e6b36dc (patch)
tree8651b326786c62fc4e0bc438a625a13bf5df1844 /shard/lib/net/addr.ex
parentc4f6cbab20b0b1d08755073d93365e5bd00dc755 (diff)
downloadshard-2973cf99c5b677c71717d916f83212bc2e6b36dc.tar.gz
shard-2973cf99c5b677c71717d916f83212bc2e6b36dc.zip
Document
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