diff options
Diffstat (limited to 'src/fib_util.erl')
-rw-r--r-- | src/fib_util.erl | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/fib_util.erl b/src/fib_util.erl new file mode 100644 index 0000000..b495f25 --- /dev/null +++ b/src/fib_util.erl @@ -0,0 +1,41 @@ +-module(fib_util). +-export([module/0, parse_ip_address_with_netmask/1]). + +module() -> + case os:type() of + {unix, linux} -> + {ok, fib_linux}; + {unix, darwin} -> + {ok, fib_macos}; + {unix, freebsd} -> + {ok, fib_freebsd}; + unsupported -> + {error, {unsupported_platform, unsupported}} + end. + +-spec parse_ip_address_with_netmask(string()) -> {inet:ip_address(), non_neg_integer()}. +parse_ip_address_with_netmask(S) -> + logger:warning("IPNets ~p", [S]), + case re:split(S, "/", [{return, list}, {parts, 2}]) of + [Prefix, LenStr] -> + {Prefix1, Scope} = ipv6_scope(Prefix), + {ok, Addr} = inet:parse_address(Prefix1), + {PrefixLen, _} = string:to_integer(LenStr), + {Addr, Scope, PrefixLen}; + [Prefix] -> + {Prefix1, Scope} = ipv6_scope(Prefix), + {ok, Addr} = inet:parse_address(Prefix1), + PrefixLen = case inet_cidr:is_ipv6(Addr) of + true -> 128; + false -> 32 + end, + {Addr, Scope, PrefixLen} + end. + +ipv6_scope(Addr) -> + case re:split(Addr, "%", [{return, list}, {parts, 2}]) of + [IP, Scope] -> + {IP, Scope}; + [IP] -> + {IP, undefined} + end. |