Skip to content

Commit 9b6941c

Browse files
committed
Fixes exceptions during interface discovery
Fix #38
1 parent edacccd commit 9b6941c

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

lib/core/tools/ConvertTo-IcingaIPBinaryString.psm1

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,16 @@
2222
function ConvertTo-IcingaIPBinaryString()
2323
{
2424
param(
25-
[string]$IP
25+
$IP
2626
);
2727

28-
$IPArray = $IP.Split(' ');
29-
$IPList = @();
30-
31-
foreach ($entry in $IPArray) {
32-
if ($entry -like '*.*') {
33-
$IPList += ConvertTo-IcingaIPv4BinaryString -IP $entry;
34-
} elseif ($entry -like '*:*') {
35-
$IPList += ConvertTo-IcingaIPv6BinaryString -IP $entry;
36-
} else {
37-
return 'Invalid IP was provided!';
38-
}
28+
if ($IP -like '*.*') {
29+
$IP = ConvertTo-IcingaIPv4BinaryString -IP $IP;
30+
} elseif ($IP -like '*:*') {
31+
$IP = ConvertTo-IcingaIPv6BinaryString -IP $IP;
32+
} else {
33+
return 'Invalid IP was provided!';
3934
}
4035

41-
return $IPList;
36+
return $IP;
4237
}

lib/core/tools/ConvertTo-IcingaIPv4BinaryString.psm1

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,24 @@
2121

2222
function ConvertTo-IcingaIPv4BinaryString()
2323
{
24-
param(
25-
[string]$IP
26-
);
24+
param(
25+
[string]$IP
26+
);
2727

28-
$IP = $IP -split '\.' | ForEach-Object {
29-
[System.Convert]::ToString($_, 2).PadLeft(8, '0');
30-
}
31-
$IP = $IP -join '';
32-
$IP = $IP -replace '\s','';
33-
34-
return @{
35-
'value' = $IP;
36-
'name' = 'IPv4'
28+
try {
29+
$IP = $IP -split '\.' | ForEach-Object {
30+
[System.Convert]::ToString($_, 2).PadLeft(8, '0');
31+
}
32+
$IP = $IP -join '';
33+
$IP = $IP -replace '\s','';
34+
} catch {
35+
# Todo: Should we handle errors? It might happen due to faulty routes or unhandled route config
36+
# we throw errors which should have no effect at all
37+
return $null;
38+
}
39+
40+
return @{
41+
'value' = $IP;
42+
'name' = 'IPv4'
3743
}
3844
}

lib/core/tools/Get-IcingaNetworkInterface.psm1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,21 @@ function Get-IcingaNetworkInterface()
113113
foreach ($destinationIP in $IPBinStringMaster) {
114114
[string]$RegexPattern = [string]::Format("^.{{{0}}}", $Route.Value.Mask);
115115
[string]$ToBeMatched = $Route.Value."Binary IP String";
116+
if ($null -eq $ToBeMatched) {
117+
continue;
118+
}
119+
116120
$Match1=[regex]::Matches($ToBeMatched, $RegexPattern).Value;
117121
$Match2=[regex]::Matches($destinationIP.Value, $RegexPattern).Value;
118122

119123
If ($Match1 -like $Match2) {
120-
$ExternalInterface = ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $destinationIP.Name).IPAddress);
124+
$ExternalInterface = ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $destinationIP.Name -ErrorAction SilentlyContinue).IPAddress);
125+
126+
# If no interface was found -> skip this entry
127+
if ($null -eq $ExternalInterface) {
128+
continue;
129+
}
130+
121131
if ($ExternalInterfaces.ContainsKey($ExternalInterface)) {
122132
$ExternalInterfaces[$ExternalInterface].count += 1;
123133
} else {

0 commit comments

Comments
 (0)