Permalink
Browse files

add parse function

  • Loading branch information...
1 parent 230e160 commit 693c13caf6da85e6be50d2c0378bfc7dcd836e5d @oujinliang committed Dec 1, 2012
Showing with 12 additions and 1 deletion.
  1. +12 −1 src/utils_inet.erl
View
@@ -3,15 +3,19 @@
-type(ip4_address() :: {0..255, 0..255, 0..255, 0..255}).
--spec get_addresses() -> [ip4_address()].
%% Get all ipv4 addresses
+-spec get_addresses() -> [ip4_address()].
get_addresses() ->
{ok, Addresses} = inet:getif(),
[Addr || {Addr,_,_} <- Addresses].
+%% Get all ipv4 addresses filtered by Filter
+-spec get_addresses(fun()) -> [ip4_address()].
get_addresses(Filter) ->
lists:filter(Filter, get_addresses()).
+%% Get all site local addresses
+-spec get_sitelocal_addrs() -> [ip4_address()].
get_sitelocal_addrs() ->
get_addresses(fun is_sitelocal_addr/1).
@@ -26,6 +30,13 @@ is_sitelocal_addr({172,16,_,_}) -> true;
is_sitelocal_addr({192,168,_,_}) -> true;
is_sitelocal_addr(_) -> false.
+%% format a ipv4 address to string
+-spec format_address(ip4_address()) -> string().
format_address({A,B,C,D}) ->
lists:flatten(io_lib:format("~b.~b.~b.~b", [A,B,C,D])).
+parse_address(Addr) when is_list(Addr) ->
+ inet_parse:ipv4_address(Addr);
+parse_address(Addr) when is_binary(Addr) ->
+ parse_address(binary_to_list(Addr)).
+

0 comments on commit 693c13c

Please sign in to comment.