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.ex27
1 files changed, 27 insertions, 0 deletions
diff --git a/shard/lib/net/addr.ex b/shard/lib/net/addr.ex
new file mode 100644
index 0000000..645e109
--- /dev/null
+++ b/shard/lib/net/addr.ex
@@ -0,0 +1,27 @@
+defmodule SNet.Addr do
+
+ def get_if_inet4 do
+ {:ok, ifs} = :inet.getifaddrs
+ for {_, opts} <- ifs,
+ {:addr, addr} <- opts,
+ tuple_size(addr) == 4,
+ addr != {127,0,0,1}
+ do
+ addr
+ end
+ end
+
+ def get_pub_inet4 do
+ Application.ensure_all_started(:inets)
+ Application.ensure_all_started(:ssl)
+ {:ok, {_, _, body}} = :httpc.request('http://api.ipify.org')
+ {:ok, addr} = :inet.parse_address body
+ addr
+ end
+
+ def get_all_inet4 do
+ addrset = for x <- get_if_inet4(), into: %MapSet{}, do: x
+ addrset = MapSet.put(addrset, get_pub_inet4())
+ MapSet.to_list addrset
+ end
+end