DNS lookup from Elixir iex

I wanted to see how to turn a network name into a network address.

Erlang’s had this function for a long time, via :inet_res.lookup/3.

It needs a few parameters via Erlang arrays, so looking up 'apple.com' as an internet address :in, as IPv4 :a looks like:

iex(1)> :inet_res.lookup('apple.com', :in, :a)
[{17, 253, 144, 10}]

which seems to be an array version of 17.253.144.10.

A ping of apple.com yields:

$ ping -c 3 -t 4 apple.com
PING apple.com (17.253.144.10): 56 data bytes
64 bytes from 17.253.144.10: icmp_seq=0 ttl=58 time=4.967 ms
64 bytes from 17.253.144.10: icmp_seq=1 ttl=58 time=4.691 ms
64 bytes from 17.253.144.10: icmp_seq=2 ttl=58 time=4.671 ms

… which matches nicely.

The third parameter of this function :a can be changed to :aaaa to query for IPv6, like this:

iex(2)> :inet_res.lookup('apple.com', :in, :aaaa)
[]  # no ipv6 for apple?... try cisco:
iex(3)> :inet_res.lookup('cisco.com', :in, :aaaa)
[{8193, 1056, 4353, 1, 0, 0, 0, 389}]

…which shows via ping6, although the actual ping times-out… maybe because there’s no public IPv6 ICMP listener?

$ ping6 cisco.com
PING6(56=40+8+8 bytes) fdfb:e984:3edc:1:1411:33fd:afd9:39a1 --> 2001:420:1101:1::185