Skip to content

Commit

Permalink
fix: Don't scan network and broadcast addresses when using P2P
Browse files Browse the repository at this point in the history
Closes #804
  • Loading branch information
g-bougard committed Nov 13, 2024
1 parent 7088dc3 commit c6ecb9f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ deploy:
* Fix checks on command run and clarify reason of success or failure. This fixes
"return code is not" and "command output doesn't contain" checks which was always
failing.
* fix #804: Don't scan network and broadcast addresses when using P2P

esx:
* Support reporting of ESX virtualmachines ip and operating system. It requires
Expand Down
14 changes: 14 additions & 0 deletions lib/GLPI/Agent/Task/Deploy/P2P.pm
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,16 @@ sub _getPotentialPeers {
push @end, $ip_bytes[$idx] | (255 - $mask_bytes[$idx]);
}

# ipEnd must not be the broadcast ip
$end[3]--;

my $ipStart = join('.', @start);
my $ipEnd = join('.', @end);
return if $ipStart eq $ipEnd;

# ipStart is here the network address to avoid
my $ipNetwork = $ipStart;

# Get ip interval before this interface ip
my $ipIntervalBefore = Net::IP->new($ipStart.' - '.$address->{ip})
or die Net::IP::Error();
Expand Down Expand Up @@ -230,6 +236,14 @@ sub _getPotentialPeers {
$ipStart = $ipIntervalBefore->ip();
$beforeCount = $ipIntervalBefore->size() - 1;

# Still skip first address if it's the network address
if ($ipStart eq $ipNetwork) {
++$ipIntervalBefore;
$beforeCount--;
$afterCount++;
$ipStart = $ipIntervalBefore->ip() if defined($ipIntervalBefore);
}

# Now add ips before
my @peers;
while (defined($ipIntervalBefore) && $beforeCount-->0) {
Expand Down
22 changes: 16 additions & 6 deletions t/tasks/deploy/p2p.t
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,24 @@ my @tests = (
name => '192.168.1.2/24',
address => { ip => '192.168.1.2', mask => '255.255.255.0' },
result => [
'192.168.1.0',
'192.168.1.1',
'192.168.1.3',
'192.168.1.4',
'192.168.1.5',
'192.168.1.6'
'192.168.1.6',
'192.168.1.7'
]
},
{
name => '192.168.2.254/24',
address => { ip => '192.168.2.254', mask => '255.255.255.0' },
address => { ip => '192.168.2.253', mask => '255.255.255.0' },
result => [
'192.168.2.248',
'192.168.2.249',
'192.168.2.250',
'192.168.2.251',
'192.168.2.252',
'192.168.2.253',
'192.168.2.255'
'192.168.2.254'
]
},
{
Expand All @@ -99,6 +99,15 @@ my @tests = (
'192.168.5.1'
]
},
{
name => '192.168.5.100/24',
address => { ip => '192.168.5.100', mask => '255.255.255.0' },
max => 512,
result => [
(map { '192.168.5.' . $_ } (1..99)),
(map { '192.168.5.' . $_ } (101..254)),
]
},
);

my %find_tests = (
Expand All @@ -124,7 +133,8 @@ my $p2p = GLPI::Agent::Task::Deploy::P2P->new(
);

foreach my $test (@tests) {
my @peers = $p2p->_getPotentialPeers($test->{address}, 6);
my $max = $test->{max} // 6;
my @peers = $p2p->_getPotentialPeers($test->{address}, $max);
cmp_deeply(\@peers, $test->{result}, $test->{name});
}

Expand Down

0 comments on commit c6ecb9f

Please sign in to comment.