1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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.
|