inet とか inet_parse とか

よく忘れるのでメモ

> inet:getaddr("ipv6.google.com", inet6).
{ok,{8193,18528,49155,0,0,0,0,104}}
> inet_parse:ipv6_address("3ae3:90a0:bd05:01d2:288a:1fc0:0001:10ee").
{ok,{15075,37024,48389,466,10378,8128,1,4334}}
> inet_parse:address("3ae3:90a0:bd05:01d2:288a:1fc0:0001:10ee").
{ok,{15075,37024,48389,466,10378,8128,1,4334}}

> inet_parse:ntoa({8193,18528,49155,0,0,0,0,104}).           
"2001:4860:C003::68"
> inet_parse:ntoa({15075,37024,48389,466,10378,8128,1,4334}).
"3AE3:90A0:BD05:1D2:288A:1FC0:1:10EE"

> {ok, {A, B, C, D, E, F, G, H}} = inet_parse:address("3ae3:90a0:bd05:01d2:288a:1fc0:0001:10ee").
{ok,{15075,37024,48389,466,10378,8128,1,4334}}
> <<A:16, B:16, C:16, D:16, E:16, F:16, G:16, H:16>>.
<<58,227,144,160,189,5,1,210,40,138,31,192,0,1,16,238>>
> erlang:byte_size(<<A:16, B:16, C:16, D:16, E:16, F:16, G:16, H:16>>).
16
> << <<X:16>> || X <- erlang:tuple_to_list({15075,37024,48389,466,10378,8128,1,4334})>>.
<<58,227,144,160,189,5,1,210,40,138,31,192,0,1,16,238>>

string to binary

case inet_parse:address(Value) of
  {ok, {A, B, C, D}} ->
    <<A:8, B:8, C:8, D:8>>;
  {ok, {A, B, C, D ,E ,F, G, H}} ->
    <<A:16, B:16, C:16, D:16, E:16, F:16, G:16, H:16>>;
  {error,einval} ->
    error
end.