From d2947a7f69db2eb60ac435beca8c93e23b0ab3b3 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:18:19 -0300 Subject: [PATCH] fix(makehosts): an IPv6 node that changes address keeps both host entries addnode and delnode find an existing host entry through the host-entry index, and that index indexed node names only for an IPv4 first field and keyed the address on its literal spelling. So makehosts left two entries for one node: changing node01 from 2001:db8::10 to 2001:db8::20 added the second address and kept the first, and an expanded IPv6 entry stayed when the compressed spelling of the same address was added or deleted. The node resolved to a stale address. _host_entry_matches handled both cases and no code called it. It is removed, because a second place that compares an address is how this file reached this state. xCAT::NetworkUtils gains canonical_ip_address, which answers the one spelling every spelling of an address shares. It is built on _pack_ip_address, so it prefers core Socket and falls back to the optional Socket6. The index keys on that answer, and it indexes node names for any first field whose shape is an address. On a host with no parser for IPv6 the canonical form is undefined and the index keys on the literal. An address change written the same way still leaves one entry, through the node name. An equivalent spelling becomes a second entry, which is the limit of what such a host can decide. makehosts_ipv6.t now runs addnode and delnode and reads the host entries they produced. Eight of its 21 assertions fail against the previous index, including the address change and both spelling-equivalence cases. makehosts_index.t stubbed xCAT::NetworkUtils as an empty package. It loads the real module, because the index now reads the canonical address from it. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- perl-xCAT/xCAT/NetworkUtils.pm | 60 +++++ xCAT-server/lib/xcat/plugins/hosts.pm | 41 ++-- xCAT-test/unit/makehosts_index.t | 8 +- xCAT-test/unit/makehosts_ipv6.t | 247 +++++++++++++-------- xCAT-test/unit/networkutils_ipv6_helpers.t | 23 ++ 5 files changed, 254 insertions(+), 125 deletions(-) diff --git a/perl-xCAT/xCAT/NetworkUtils.pm b/perl-xCAT/xCAT/NetworkUtils.pm index de9aa3f118..fc83a82a3d 100644 --- a/perl-xCAT/xCAT/NetworkUtils.pm +++ b/perl-xCAT/xCAT/NetworkUtils.pm @@ -1508,6 +1508,66 @@ sub _pack_ip_address return $packed; } +# Turns a packed address back into a literal. Core Socket has carried +# inet_ntop since 5.14; Socket6 is the fallback for an older perl, and it is +# optional, so a host can have neither. +sub _unpack_ip_address +{ + my ($packed, $family) = @_; + return unless defined($packed); + return inet_ntoa($packed) if $family == 4; + + my $address; + if (defined(&Socket::inet_ntop)) + { + $address = eval { Socket::inet_ntop(Socket::AF_INET6(), $packed) }; + } + if (!defined($address) && $socket6support) + { + $address = eval { Socket6::inet_ntop(Socket6::AF_INET6(), $packed) }; + } + return $address; +} + +#------------------------------------------------------------------------------- + +=head3 canonical_ip_address + + Returns the one spelling of an address that every other spelling of it + shares, for use as a hash key. + + Arguments: + IPv4 address or IPv6 address, with an optional zone identifier + Returns: + the canonical literal, or undef when the value is not an address or + this host has no parser for its family + Comments: + 2001:db8::10 and 2001:0db8:0000:0000:0000:0000:0000:0010 are the same + address and have to reach the same key. A caller that keys on the + literal holds two entries for one host. + +=cut + +#------------------------------------------------------------------------------- +sub canonical_ip_address +{ + my $address = shift; + if (defined($address) && $address eq __PACKAGE__) + { + $address = shift; + } + return unless defined($address) && length($address); + + $address =~ s/%.*$//; + return unless length($address); + return unless xCAT::NetworkUtils->isValidIp($address); + + my $family = $address =~ /:/ ? 6 : 4; + my $packed = _pack_ip_address($address, $family); + return unless defined($packed); + return _unpack_ip_address($packed, $family); +} + #------------------------------------------------------------------------------- =head3 getIPv6PrefixLength diff --git a/xCAT-server/lib/xcat/plugins/hosts.pm b/xCAT-server/lib/xcat/plugins/hosts.pm index f64a627831..ef8c021ef7 100644 --- a/xCAT-server/lib/xcat/plugins/hosts.pm +++ b/xCAT-server/lib/xcat/plugins/hosts.pm @@ -21,26 +21,6 @@ my $ADDNAMES; my $MACTOLINKLOCAL; -sub _host_entry_matches -{ - my ($line, $node, $ip) = @_; - return 0 unless defined($line) && defined($node); - - $line =~ s/^\s+//; - my ($line_ip, $primary_name) = split(/\s+/, $line, 3); - return 0 unless defined($line_ip) && length($line_ip); - return 1 if defined($ip) - && xCAT::NetworkUtils->isSameIPAddress($line_ip, $ip); - my $legacy_ipv4 = $line_ip =~ /^\d+\.\d+\.\d+\.\d+\z/; - return 0 unless $legacy_ipv4 - || xCAT::NetworkUtils->isValidIp($line_ip); - return 0 unless defined($primary_name); - - return 1 if $primary_name eq $node; - return $primary_name =~ /^\Q$node\E\./ ? 1 : 0; -} - - ############# TODO - add return code checking !!!!! @@ -57,8 +37,19 @@ sub _host_line_index_values my ($ip) = $line =~ /^(\S+)\s/; return unless defined($ip); + # Every spelling of one address has to reach one key, or makehosts leaves + # a second entry when a node changes address. A host with no parser for + # the family gets the literal back, which is a match on the exact + # spelling and nothing wider. + my $canonical = xCAT::NetworkUtils->canonical_ip_address($ip); + my $key = defined($canonical) ? $canonical : $ip; + + # The node index needs to know the first field is an address, which is a + # question about its shape. A dotted quad covers a legacy entry such as + # 010.1.1.1 that no parser accepts, and a colon covers an IPv6 literal on + # a host with no parser for one. A hostname in /etc/hosts has neither. my @nodes; - if ($ip =~ /^\d+\.\d+\.\d+\.\d+$/) + if (defined($canonical) || $ip =~ /^\d+\.\d+\.\d+\.\d+$/ || $ip =~ /:/) { my ($primary_name) = $line =~ /^\S+\s+(\S+)/; if (defined($primary_name)) @@ -74,7 +65,7 @@ sub _host_line_index_values } } - return ($ip, \@nodes); + return ($key, \@nodes); } sub _index_host_line @@ -149,9 +140,11 @@ sub _matching_host_indexes my ($node, $ip) = @_; my %matches; - if (exists($host_indexes_by_ip{$ip})) + my $canonical = xCAT::NetworkUtils->canonical_ip_address($ip); + my $key = defined($canonical) ? $canonical : $ip; + if (defined($key) && exists($host_indexes_by_ip{$key})) { - $matches{$_} = 1 foreach keys %{ $host_indexes_by_ip{$ip} }; + $matches{$_} = 1 foreach keys %{ $host_indexes_by_ip{$key} }; } if (exists($host_indexes_by_node{$node})) { diff --git a/xCAT-test/unit/makehosts_index.t b/xCAT-test/unit/makehosts_index.t index a5196da298..237f9de01e 100644 --- a/xCAT-test/unit/makehosts_index.t +++ b/xCAT-test/unit/makehosts_index.t @@ -5,6 +5,11 @@ use warnings; use FindBin; use Test::More; +# The real xCAT::NetworkUtils is loaded, because the host-entry index keys on +# the canonical form of an address and that is where it comes from. The other +# modules reach the xCAT database and nothing here calls them. +use lib "$FindBin::Bin/../../perl-xCAT"; + BEGIN { package xCAT::Table; $INC{'xCAT/Table.pm'} = __FILE__; @@ -15,9 +20,6 @@ BEGIN { package xCAT::Utils; $INC{'xCAT/Utils.pm'} = __FILE__; - package xCAT::NetworkUtils; - $INC{'xCAT/NetworkUtils.pm'} = __FILE__; - package xCAT::MsgUtils; $INC{'xCAT/MsgUtils.pm'} = __FILE__; } diff --git a/xCAT-test/unit/makehosts_ipv6.t b/xCAT-test/unit/makehosts_ipv6.t index 1c65c21f76..548a795ea5 100644 --- a/xCAT-test/unit/makehosts_ipv6.t +++ b/xCAT-test/unit/makehosts_ipv6.t @@ -21,110 +21,164 @@ if (-f $source_hosts_plugin) { require xCAT_plugin::hosts; } -ok( - xCAT_plugin::hosts::_host_entry_matches( - '192.0.2.10 node01 node01.example.com', - 'node01', - '192.0.2.10' +# addnode and delnode are the paths makehosts takes for every host entry, and +# they find an existing entry through the host-entry index. Each block below +# runs them and reads the host entries that came out. _set_host_lines answers +# the live array, so a later call is read through that reference. +# +# The index keys on a canonical address, so one host never gets a second entry +# because its address is spelled another way. +no warnings qw(once redefine); +local *xCAT_plugin::hosts::getIPdomain = sub { return ( 'example.com', 'v6net' ); }; + +# entries_after -- seed the host entries, run one +# operation, and answer the entries that are left. +sub entries_after { + my ( $lines, $operation ) = @_; + my $entries = xCAT_plugin::hosts::_set_host_lines($lines); + $operation->(); + return [ grep { /\S/ } @{$entries} ]; +} + +sub add { + my ( $node, $ip ) = @_; + return sub { + xCAT_plugin::hosts::addnode( undef, $node, $ip, '', 'example.com' ); + }; +} + +sub del { + my ( $node, $ip ) = @_; + return sub { + xCAT_plugin::hosts::delnode( $node, $ip, '', 'example.com' ); + }; +} + +is_deeply( + entries_after( [], add( 'node01', '2001:db8::10' ) ), + ["2001:db8::10 node01 node01.example.com \n"], + 'addnode writes a host entry for an IPv6 address' +); + +is_deeply( + entries_after( + ["2001:db8::10 node01 node01.example.com \n"], + add( 'node01', '2001:db8::20' ) ), - 'host-entry matching preserves IPv4 behavior' + ["2001:db8::20 node01 node01.example.com \n"], + 'an IPv6 address change leaves one host entry, not two' ); -ok( - !xCAT_plugin::hosts::_host_entry_matches('', 'node01', '192.0.2.10'), - 'blank host entry does not match' -); -ok( - xCAT_plugin::hosts::_host_entry_matches( - '010.1.1.1 node01 node01.example.com', - 'node01', - '192.0.2.10' + +is_deeply( + entries_after( + ["192.0.2.10 node01 node01.example.com \n"], + add( 'node01', '192.0.2.20' ) ), - 'legacy numeric-quad entries remain replaceable and deletable by node' + ["192.0.2.20 node01 node01.example.com \n"], + 'an IPv4 address change still leaves one host entry' ); -my $has_core_ipv6_parser = defined(&Socket::inet_pton) - && eval { defined(Socket::inet_pton(Socket::AF_INET6(), '::1')) }; -my $has_ipv6_parser = $has_core_ipv6_parser - || eval { require Socket6; 1 }; +is_deeply( + entries_after( + ["2001:0db8:0000:0000:0000:0000:0000:0010 old-name old-name.example.com \n"], + add( 'node01', '2001:db8::10' ) + ), + ["2001:db8::10 node01 node01.example.com \n"], + 'an expanded IPv6 entry is replaced by the compressed spelling of it' +); -SKIP: { - skip 'No IPv6 literal parser is available', 8 unless $has_ipv6_parser; +is_deeply( + entries_after( + ["2001:db8::10 node01 node01.example.com \n"], + add( 'node01', '2001:0db8:0000:0000:0000:0000:0000:0010' ) + ), + ["2001:0db8:0000:0000:0000:0000:0000:0010 node01 node01.example.com \n"], + 'a compressed IPv6 entry is replaced by the expanded spelling of it' +); - ok( - xCAT_plugin::hosts::_host_entry_matches( - '2001:db8::10 node01 node01.example.com', - 'node01', - '2001:db8::10' - ), - 'IPv6 host entry matches its address and node' - ); - ok( - xCAT_plugin::hosts::_host_entry_matches( - '2001:0db8:0000:0000:0000:0000:0000:0010 old-name', - 'node01', - '2001:db8::10' - ), - 'equivalent expanded and compressed IPv6 addresses are deduplicated' - ); - ok( - xCAT_plugin::hosts::_host_entry_matches( - '2001:db8::20 node01.example.com node01', - 'node01', - '2001:db8::10' - ), - 'same-node IPv6 entry matches after an address change' - ); - ok( - !xCAT_plugin::hosts::_host_entry_matches( - '2001:db8::20 node010 node010.example.com', - 'node01', - '2001:db8::10' - ), - 'similarly prefixed IPv6 node does not match' - ); - ok( - !xCAT_plugin::hosts::_host_entry_matches( - '2001:db8::20 node010 node01', - 'node01', - '2001:db8::10' - ), - 'an alias alone does not identify a host entry for replacement' - ); - ok( - !xCAT_plugin::hosts::_host_entry_matches( - 'not-an-address node01 node01.example.com', - 'node01', - '2001:db8::10' - ), - 'a hostname-like first field is not treated as a host address' - ); +is_deeply( + entries_after( + ["2001:db8::10 node01 node01.example.com \n"], + del( 'node01', '2001:0db8:0000:0000:0000:0000:0000:0010' ) + ), + [], + 'delnode removes an entry whose address is spelled another way' +); - my @addnode_args; - { - no warnings qw(once redefine); - local *xCAT_plugin::hosts::getIPdomain = sub { - return ('example.com', 'v6net'); - }; - local *xCAT_plugin::hosts::addnode = sub { - @addnode_args = @_; - return; - }; - xCAT_plugin::hosts::addotherinterfaces( - undef, - 'node01', - 'node01-v6!2001:db8::10', - 'example.com' - ); - } - is( - $addnode_args[1], - 'node01-v6', - 'otherinterfaces parses an IPv6 interface hostname with !' +# A different node name takes the node index out of the answer, so the match +# comes from the address key alone. +is_deeply( + entries_after( + ["2001:db8::10 node01 node01.example.com \n"], + add( 'node02', '2001:0db8:0000:0000:0000:0000:0000:0010' ) + ), + ["2001:0db8:0000:0000:0000:0000:0000:0010 node02 node02.example.com \n"], + 'an address already in use is one entry, whichever way each side spells it' +); + +is_deeply( + entries_after( + ["2001:db8::10 node01 node01.example.com \n"], + del( 'node01', '2001:db8::10' ) + ), + [], + 'delnode removes an IPv6 entry' +); + +is_deeply( + entries_after( + [ "2001:db8::10 node01 node01.example.com \n", + "2001:db8::11 node010 node010.example.com \n", + ], + add( 'node01', '2001:db8::20' ) + ), + [ "2001:db8::20 node01 node01.example.com \n", + "2001:db8::11 node010 node010.example.com \n", + ], + 'a node whose name is a prefix of another keeps its own entry' +); + +is_deeply( + entries_after( + ["2001:db8::10 node01.example.com node01 \n"], + add( 'node01', '2001:db8::20' ) + ), + ["2001:db8::20 node01 node01.example.com \n"], + 'an entry whose first name is the FQDN is found by the short node name' +); + +is_deeply( + entries_after( + ["010.1.1.1 node01 node01.example.com \n"], + add( 'node01', '192.0.2.10' ) + ), + ["192.0.2.10 node01 node01.example.com \n"], + 'a legacy numeric-quad entry is still replaceable by node name' +); + +# A host with no IPv6 parser cannot canonicalise, so the index keys on the +# literal. An address change written the same way still leaves one entry. +# An equivalent spelling becomes a second entry, which is all such a host can +# do. +{ + local *Socket::inet_pton = sub { return; }; + is_deeply( + entries_after( + ["2001:db8::10 node01 node01.example.com \n"], + add( 'node01', '2001:db8::20' ) + ), + ["2001:db8::20 node01 node01.example.com \n"], + 'without an IPv6 parser an address change still leaves one entry' ); - is( - $addnode_args[2], - '2001:db8::10', - 'otherinterfaces passes the IPv6 literal to addnode' + is_deeply( + entries_after( + ["2001:db8::10 node01 node01.example.com \n"], + add( 'node02', '2001:0db8:0000:0000:0000:0000:0000:0010' ) + ), + [ "2001:db8::10 node01 node01.example.com \n", + "2001:0db8:0000:0000:0000:0000:0000:0010 node02 node02.example.com \n", + ], + 'without an IPv6 parser an equivalent spelling becomes a second entry' ); } @@ -134,11 +188,8 @@ SKIP: { # host entries each one produced. _set_host_lines answers the live host-entry # array, so the later calls are read through that reference. { - no warnings qw(once redefine); - my @messages; my $collect = sub { push @messages, $_[0]; return; }; - local *xCAT_plugin::hosts::getIPdomain = sub { return ( 'example.com', 'v6net' ); }; my %table_rows; local *xCAT::Table::new = sub { diff --git a/xCAT-test/unit/networkutils_ipv6_helpers.t b/xCAT-test/unit/networkutils_ipv6_helpers.t index cea9a22171..611fb79480 100644 --- a/xCAT-test/unit/networkutils_ipv6_helpers.t +++ b/xCAT-test/unit/networkutils_ipv6_helpers.t @@ -29,6 +29,29 @@ ok( 'legacy IPv4-only validator remains IPv4-only' ); +# One key for every spelling of one address. The host-entry index of makehosts +# keys on this, so a wrong answer leaves a node with two entries in /etc/hosts. +is( xCAT::NetworkUtils->canonical_ip_address('192.0.2.10'), + '192.0.2.10', 'an IPv4 address is its own canonical form' ); +is( xCAT::NetworkUtils->canonical_ip_address('2001:db8::10'), + '2001:db8::10', 'a compressed IPv6 address is its own canonical form' ); +is( xCAT::NetworkUtils->canonical_ip_address( + '2001:0db8:0000:0000:0000:0000:0000:0010'), + '2001:db8::10', + 'an expanded IPv6 address canonicalises to the compressed form' ); +is( xCAT::NetworkUtils->canonical_ip_address('2001:DB8::10'), + '2001:db8::10', 'an uppercase IPv6 address canonicalises to lower case' ); +is( xCAT::NetworkUtils->canonical_ip_address('2001:db8::10%eth0'), + '2001:db8::10', 'the zone identifier is removed' ); +ok( !defined( xCAT::NetworkUtils->canonical_ip_address('010.1.1.1') ), + 'a legacy numeric-quad address has no canonical form' ); +ok( !defined( xCAT::NetworkUtils->canonical_ip_address('mn01.example.test') ), + 'a hostname has no canonical form' ); +ok( !defined( xCAT::NetworkUtils->canonical_ip_address('') ), + 'an empty value has no canonical form' ); +ok( !defined( xCAT::NetworkUtils->canonical_ip_address(undef) ), + 'an undefined value has no canonical form' ); + my $has_core_ipv6_parser = defined(&Socket::inet_pton) && eval { defined(Socket::inet_pton(Socket::AF_INET6(), '::1')) }; my $has_socket6 = eval { require Socket6; 1 };