Skip to content

Commit b03a14f

Browse files
Merge pull request #1189 from rabbitmq/rabbitmq-server-1171
Add IPv6 support for DNS discovery
2 parents 261efd9 + 0bb59f8 commit b03a14f

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/rabbit_peer_discovery_dns.erl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,25 @@ discover_nodes(SeedHostname, LongNamesUsed) ->
7575
H <- discover_hostnames(SeedHostname, LongNamesUsed)].
7676

7777
discover_hostnames(SeedHostname, LongNamesUsed) ->
78-
%% TODO: IPv6 support
79-
IPs = inet_res:lookup(SeedHostname, in, a),
80-
rabbit_log:info("Addresses discovered via A records of ~s: ~s",
81-
[SeedHostname, string:join([inet_parse:ntoa(IP) || IP <- IPs], ", ")]),
78+
lookup(SeedHostname, LongNamesUsed, ipv4) ++
79+
lookup(SeedHostname, LongNamesUsed, ipv6).
80+
81+
decode_record(ipv4) ->
82+
a;
83+
decode_record(ipv6) ->
84+
aaaa.
85+
86+
lookup(SeedHostname, LongNamesUsed, IPv) ->
87+
IPs = inet_res:lookup(SeedHostname, in, decode_record(IPv)),
88+
rabbit_log:info("Addresses discovered via ~s records of ~s: ~s",
89+
[string:to_upper(atom_to_list(decode_record(IPv))),
90+
SeedHostname,
91+
string:join([inet_parse:ntoa(IP) || IP <- IPs], ", ")]),
8292
Hosts = [extract_host(inet:gethostbyaddr(A), LongNamesUsed, A) ||
83-
A <- IPs],
93+
A <- IPs],
8494
lists:filter(fun(E) -> E =/= error end, Hosts).
8595

96+
8697
%% long node names are used
8798
extract_host({ok, {hostent, FQDN, _, _, _, _}}, true, _Address) ->
8899
FQDN;

test/peer_discovery_dns_SUITE.erl

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ groups() ->
3333
hostname_discovery_with_long_node_names,
3434
hostname_discovery_with_short_node_names,
3535
node_discovery_with_long_node_names,
36-
node_discovery_with_short_node_names
36+
node_discovery_with_short_node_names,
37+
test_aaaa_record_hostname_discovery
3738
]}
3839
].
3940

@@ -54,7 +55,9 @@ suite() ->
5455
%% * One does not resolve to a [typically] non-reachable IP
5556
%% * One does not support reverse lookup queries
5657

57-
-define(DISCOVERY_ENDPOINT, "peer_discovery.tests.rabbitmq.net").
58+
-define(DISCOVERY_ENDPOINT_RECORD_A, "peer_discovery.tests.rabbitmq.net").
59+
60+
-define(DISCOVERY_ENDPOINT_RECORD_AAAA, "www.v6.facebook.com").
5861

5962
init_per_suite(Config) ->
6063
rabbit_ct_helpers:log_environment(),
@@ -63,17 +66,26 @@ init_per_suite(Config) ->
6366
end_per_suite(Config) ->
6467
rabbit_ct_helpers:run_teardown_steps(Config).
6568

69+
70+
init_per_testcase(test_aaaa_record, Config) ->
71+
case inet_res:lookup(?DISCOVERY_ENDPOINT_RECORD_AAAA, in, aaaa) of
72+
[] ->
73+
{skip, "pre-configured AAAA record does not resolve, skipping"};
74+
[_ | _] ->
75+
Config
76+
end;
77+
6678
init_per_testcase(_Testcase, Config) ->
67-
%% TODO: support IPv6-only environments
68-
case inet_res:lookup(?DISCOVERY_ENDPOINT, in, a) of
79+
case inet_res:lookup(?DISCOVERY_ENDPOINT_RECORD_A, in, a) of
6980
[] ->
7081
{skip, "pre-configured *.rabbitmq.net record does not resolve, skipping"};
7182
[_ | _] ->
7283
Config
7384
end.
7485

86+
7587
end_per_testcase(_Testcase, Config) ->
76-
case inet_res:lookup(?DISCOVERY_ENDPOINT, in, a) of
88+
case inet_res:lookup(?DISCOVERY_ENDPOINT_RECORD_A, in, a) of
7789
[] ->
7890
{skip, "pre-configured *.rabbitmq.net record does not resolve, skipping"};
7991
[_ | _] ->
@@ -85,18 +97,22 @@ end_per_testcase(_Testcase, Config) ->
8597
%% Test cases
8698
%% -------------------------------------------------------------------
8799

100+
test_aaaa_record_hostname_discovery(_) ->
101+
Result = rabbit_peer_discovery_dns:discover_hostnames(?DISCOVERY_ENDPOINT_RECORD_AAAA, true),
102+
?assert(string:str(lists:flatten(Result), "facebook.com") > 0).
103+
88104
hostname_discovery_with_long_node_names(_) ->
89-
Result = rabbit_peer_discovery_dns:discover_hostnames(?DISCOVERY_ENDPOINT, true),
105+
Result = rabbit_peer_discovery_dns:discover_hostnames(?DISCOVERY_ENDPOINT_RECORD_A, true),
90106
?assert(lists:member("www.rabbitmq.com", Result)).
91107

92108
hostname_discovery_with_short_node_names(_) ->
93-
Result = rabbit_peer_discovery_dns:discover_hostnames(?DISCOVERY_ENDPOINT, false),
109+
Result = rabbit_peer_discovery_dns:discover_hostnames(?DISCOVERY_ENDPOINT_RECORD_A, false),
94110
?assert(lists:member("www", Result)).
95111

96112
node_discovery_with_long_node_names(_) ->
97-
Result = rabbit_peer_discovery_dns:discover_nodes(?DISCOVERY_ENDPOINT, true),
113+
Result = rabbit_peer_discovery_dns:discover_nodes(?DISCOVERY_ENDPOINT_RECORD_A, true),
98114
?assert(lists:member('[email protected]', Result)).
99115

100116
node_discovery_with_short_node_names(_) ->
101-
Result = rabbit_peer_discovery_dns:discover_nodes(?DISCOVERY_ENDPOINT, false),
117+
Result = rabbit_peer_discovery_dns:discover_nodes(?DISCOVERY_ENDPOINT_RECORD_A, false),
102118
?assert(lists:member(ct_rabbit@www, Result)).

0 commit comments

Comments
 (0)