Skip to content

Commit c6ecb9f

Browse files
committed
fix: Don't scan network and broadcast addresses when using P2P
Closes #804
1 parent 7088dc3 commit c6ecb9f

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Changes

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ deploy:
2323
* Fix checks on command run and clarify reason of success or failure. This fixes
2424
"return code is not" and "command output doesn't contain" checks which was always
2525
failing.
26+
* fix #804: Don't scan network and broadcast addresses when using P2P
2627

2728
esx:
2829
* Support reporting of ESX virtualmachines ip and operating system. It requires

lib/GLPI/Agent/Task/Deploy/P2P.pm

+14
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,16 @@ sub _getPotentialPeers {
187187
push @end, $ip_bytes[$idx] | (255 - $mask_bytes[$idx]);
188188
}
189189

190+
# ipEnd must not be the broadcast ip
191+
$end[3]--;
192+
190193
my $ipStart = join('.', @start);
191194
my $ipEnd = join('.', @end);
192195
return if $ipStart eq $ipEnd;
193196

197+
# ipStart is here the network address to avoid
198+
my $ipNetwork = $ipStart;
199+
194200
# Get ip interval before this interface ip
195201
my $ipIntervalBefore = Net::IP->new($ipStart.' - '.$address->{ip})
196202
or die Net::IP::Error();
@@ -230,6 +236,14 @@ sub _getPotentialPeers {
230236
$ipStart = $ipIntervalBefore->ip();
231237
$beforeCount = $ipIntervalBefore->size() - 1;
232238

239+
# Still skip first address if it's the network address
240+
if ($ipStart eq $ipNetwork) {
241+
++$ipIntervalBefore;
242+
$beforeCount--;
243+
$afterCount++;
244+
$ipStart = $ipIntervalBefore->ip() if defined($ipIntervalBefore);
245+
}
246+
233247
# Now add ips before
234248
my @peers;
235249
while (defined($ipIntervalBefore) && $beforeCount-->0) {

t/tasks/deploy/p2p.t

+16-6
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,24 @@ my @tests = (
5555
name => '192.168.1.2/24',
5656
address => { ip => '192.168.1.2', mask => '255.255.255.0' },
5757
result => [
58-
'192.168.1.0',
5958
'192.168.1.1',
6059
'192.168.1.3',
6160
'192.168.1.4',
6261
'192.168.1.5',
63-
'192.168.1.6'
62+
'192.168.1.6',
63+
'192.168.1.7'
6464
]
6565
},
6666
{
6767
name => '192.168.2.254/24',
68-
address => { ip => '192.168.2.254', mask => '255.255.255.0' },
68+
address => { ip => '192.168.2.253', mask => '255.255.255.0' },
6969
result => [
70+
'192.168.2.248',
7071
'192.168.2.249',
7172
'192.168.2.250',
7273
'192.168.2.251',
7374
'192.168.2.252',
74-
'192.168.2.253',
75-
'192.168.2.255'
75+
'192.168.2.254'
7676
]
7777
},
7878
{
@@ -99,6 +99,15 @@ my @tests = (
9999
'192.168.5.1'
100100
]
101101
},
102+
{
103+
name => '192.168.5.100/24',
104+
address => { ip => '192.168.5.100', mask => '255.255.255.0' },
105+
max => 512,
106+
result => [
107+
(map { '192.168.5.' . $_ } (1..99)),
108+
(map { '192.168.5.' . $_ } (101..254)),
109+
]
110+
},
102111
);
103112

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

126135
foreach my $test (@tests) {
127-
my @peers = $p2p->_getPotentialPeers($test->{address}, 6);
136+
my $max = $test->{max} // 6;
137+
my @peers = $p2p->_getPotentialPeers($test->{address}, $max);
128138
cmp_deeply(\@peers, $test->{result}, $test->{name});
129139
}
130140

0 commit comments

Comments
 (0)