inet6 support in inet:getaddr()

Tony Rogvall <>
Sat Apr 10 14:13:04 CEST 2004


torsdagen den 1 april 2004 kl 11.35 skrev Fredrik Thulin:

> Hi
>
> I've managed to get IPv6 support working in the Erlang 
> SIP-server/stack called
> Yxa (http://www.stacken.kth.se/projekt/yxa/). To my delight, I found 
> out that
> inet:getaddr() does handle inet6 (I'm using R9C-0) even though the
> documentation says is doesn't ;)
>
> My question is, will this be the way to do it or am I better off 
> implementing
> my own DNS resolver functionality to obtain IPv6 as well as IPv4 
> addresses? I
> might have to do that anyways since I want to obtain all addresses for 
> a
> hostname, not just the first one.
>

Use

inet:gethostbyname/2

it will return {ok, HEnt}  where HEnt is a hostent record which is 
defined in kernel/include/inet.hrl

Please look around in the resolver code to find out more goodies ...
you may find the inet_dns/inet_res/inet_parse/inet_db and inet modules 
interesting.


Example:

4> inet:gethostbyname("www.stacken.kth.se", inet6).
{ok,{hostent,"www.stacken.kth.se",
              [],
              inet6,
              16,
              [{8193,1712,1,234,2560,8447,65167,28815}]}}

6> inet:gethostbyname("www.stacken.kth.se", inet).
{ok,{hostent,"www.stacken.kth.se",[],inet,4,[{130,237,234,41}]}}


7> inet_parse:ntoa({8193,1712,1,234,2560,8447,65167,28815}).
"2001:6B0:1:EA:A00:20FF:FE8F:708F"

8> inet_parse:ntoa({130,237,234,41}).
"130.237.234.41"

Example with ipv4/ipv6

23> inet:gethostbyname("www.google.com", inet6).
{ok,{hostent,"www.google.akadns.net",
              ["www.google.com"],
              inet6,
              16,
              
[{0,0,0,0,0,65535,16998,2915},{0,0,0,0,0,65535,16998,2920}]}}
24>
24>
24> inet_parse:ntoa({0,0,0,0,0,65535,16998,2915}).
"::FFFF:66.102.11.99"
25>

Regards

/Tony




More information about the erlang-questions mailing list