从 Elixir 调用时 gen_tcp 中的 IPv6 解析连接
IPv6 Resolution in gen_tcp connect when called from Elixir
当使用 gen_tcp
的 connect
方法时,自动解析 IPv4 域,即:
:gen_tcp.connect('google.com', 443, [:binary, active: false])
{:ok, #Port<0.116>}
但是,如果解析 IPv6 域,则无法正确解析
iex(production@b7726c04)3> :gen_tcp.connect('ipv6.google.com', 443, [:binary, active: false])
{:error, :nxdomain}
我是否遗漏了 :gen_tcp.connect
的套接字选项,可以启用 IPv6 解析,或者我是否在使用前自己手动解析了域?即类似:
:inet_res.resolve('ipv6.google.com', :in, :aaaa)
{:ok,
{:dns_rec, {:dns_header, 1, true, :query, false, false, true, true, false, 0},
[{:dns_query, 'ipv6.google.com', :aaaa, :in, false}],
[
{:dns_rr, 'ipv6.google.com', :cname, :in, 0, 604606, 'ipv6.l.google.com',
:undefined, [], false},
{:dns_rr, 'ipv6.l.google.com', :aaaa, :in, 0, 300,
{9220, 26624, 16387, 3072, 0, 0, 0, 101}, :undefined, [], false},
{:dns_rr, 'ipv6.l.google.com', :aaaa, :in, 0, 300,
{9220, 26624, 16387, 3072, 0, 0, 0, 102}, :undefined, [], false},
{:dns_rr, 'ipv6.l.google.com', :aaaa, :in, 0, 300,
{9220, 26624, 16387, 3072, 0, 0, 0, 113}, :undefined, [], false},
{:dns_rr, 'ipv6.l.google.com', :aaaa, :in, 0, 300,
{9220, 26624, 16387, 3072, 0, 0, 0, 100}, :undefined, [], false}
], [], []}}
然后直接使用 IPv6 元组:
:gen_tcp.connect({9220, 26624, 16387, 3072, 0, 0, 0, 101}, 443, [:binary, active: false])
{:ok, #Port<0.138>}
您可以使用 inet6
:
{ok, P} = gen_tcp:connect("ipv6.google.com", 80, [inet6]).
{ok,#Port<0.14>}
当使用 gen_tcp
的 connect
方法时,自动解析 IPv4 域,即:
:gen_tcp.connect('google.com', 443, [:binary, active: false])
{:ok, #Port<0.116>}
但是,如果解析 IPv6 域,则无法正确解析
iex(production@b7726c04)3> :gen_tcp.connect('ipv6.google.com', 443, [:binary, active: false])
{:error, :nxdomain}
我是否遗漏了 :gen_tcp.connect
的套接字选项,可以启用 IPv6 解析,或者我是否在使用前自己手动解析了域?即类似:
:inet_res.resolve('ipv6.google.com', :in, :aaaa)
{:ok,
{:dns_rec, {:dns_header, 1, true, :query, false, false, true, true, false, 0},
[{:dns_query, 'ipv6.google.com', :aaaa, :in, false}],
[
{:dns_rr, 'ipv6.google.com', :cname, :in, 0, 604606, 'ipv6.l.google.com',
:undefined, [], false},
{:dns_rr, 'ipv6.l.google.com', :aaaa, :in, 0, 300,
{9220, 26624, 16387, 3072, 0, 0, 0, 101}, :undefined, [], false},
{:dns_rr, 'ipv6.l.google.com', :aaaa, :in, 0, 300,
{9220, 26624, 16387, 3072, 0, 0, 0, 102}, :undefined, [], false},
{:dns_rr, 'ipv6.l.google.com', :aaaa, :in, 0, 300,
{9220, 26624, 16387, 3072, 0, 0, 0, 113}, :undefined, [], false},
{:dns_rr, 'ipv6.l.google.com', :aaaa, :in, 0, 300,
{9220, 26624, 16387, 3072, 0, 0, 0, 100}, :undefined, [], false}
], [], []}}
然后直接使用 IPv6 元组:
:gen_tcp.connect({9220, 26624, 16387, 3072, 0, 0, 0, 101}, 443, [:binary, active: false])
{:ok, #Port<0.138>}
您可以使用 inet6
:
{ok, P} = gen_tcp:connect("ipv6.google.com", 80, [inet6]).
{ok,#Port<0.14>}