From fadc7900408b4e3034659e70e316b76f2a322e8a Mon Sep 17 00:00:00 2001 From: laf Date: Thu, 11 Jun 2015 19:34:11 +0100 Subject: [PATCH 1/3] Added function to check if ip already exists and updated addHost to use it --- includes/functions.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/includes/functions.php b/includes/functions.php index 9e666c2b0e..f36543aca0 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -285,6 +285,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0 // Test Database Exists if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') { + if (filter_var($host, FILTER_VALIDATE_IP) && ip_exists($host) === false) { // Test reachability if ($force_add == 1 || isPingable($host)) { @@ -369,6 +370,11 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0 // failed Reachability if($quiet == '0') { print_error("Could not ping $host"); } } + } else { + if ($quiet == 0) { + print_error("Already have host with this IP $host"); + } + } } else { // found in database if($quiet == '0') { print_error("Already got host $host"); } @@ -1210,3 +1216,18 @@ function fix_integer_value($value) { } return $return; } + +function ip_exists($ip) { + // Function to check if an IP exists in the DB already + $return = true; // Default to assuming it does. + if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE) { + if (!dbFetchRow("SELECT `ipv6_address_id` FROM `ipv6_addresses` WHERE `ipv6_address` = ? OR `ipv6_compressed` = ?", array($ip,$ip))) { + $return = false; + } + } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== FALSE) { + if (!dbFetchRow("SELECT `ipv4_address_id` FROM `ipv4_addresses` WHERE `ipv4_address` = ?", array($ip))) { + $return = false; + } + } + return $return; +} From 15cd6d5662584a567440a89e3c6117f8d973ad8f Mon Sep 17 00:00:00 2001 From: laf Date: Thu, 11 Jun 2015 19:42:54 +0100 Subject: [PATCH 2/3] Added fallback to false if it is not an IP --- includes/functions.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/functions.php b/includes/functions.php index f36543aca0..e555e711a0 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -285,7 +285,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0 // Test Database Exists if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') { - if (filter_var($host, FILTER_VALIDATE_IP) && ip_exists($host) === false) { + if (ip_exists($host) === false) { // Test reachability if ($force_add == 1 || isPingable($host)) { @@ -1228,6 +1228,8 @@ function ip_exists($ip) { if (!dbFetchRow("SELECT `ipv4_address_id` FROM `ipv4_addresses` WHERE `ipv4_address` = ?", array($ip))) { $return = false; } + } else { + $return = false; } return $return; } From e478a495f989a1d876ef595411178999d8b23d31 Mon Sep 17 00:00:00 2001 From: laf Date: Fri, 12 Jun 2015 16:24:02 +0100 Subject: [PATCH 3/3] Changed to return during function processing --- includes/functions.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index e555e711a0..d268667bd1 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1219,17 +1219,16 @@ function fix_integer_value($value) { function ip_exists($ip) { // Function to check if an IP exists in the DB already - $return = true; // Default to assuming it does. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE) { if (!dbFetchRow("SELECT `ipv6_address_id` FROM `ipv6_addresses` WHERE `ipv6_address` = ? OR `ipv6_compressed` = ?", array($ip,$ip))) { - $return = false; + return false; } } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== FALSE) { if (!dbFetchRow("SELECT `ipv4_address_id` FROM `ipv4_addresses` WHERE `ipv4_address` = ?", array($ip))) { - $return = false; + return false; } } else { - $return = false; + return false; } - return $return; + return true; }