[monitoring-plugins] check_icmp: Do not overwrite -4,-6 on lookup

Jacob Hansen git at monitoring-plugins.org
Tue Feb 19 15:50:17 CET 2019


 Module: monitoring-plugins
 Branch: master
 Commit: 4a4ef0d6898cd1590561cd7685d1b8b02b757823
 Author: Jacob Hansen <jhansen at op5.com>
   Date: Mon Dec 10 14:00:43 2018 +0000
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=4a4ef0d

check_icmp: Do not overwrite -4,-6 on lookup

In case we needed to do a lookup, we previously overwrote the
address_family to IPv6, even if we supplied -4 as a cmd line argument.

This commit should ensure the cmd line argument is always followed.

Signed-off-by: Jacob Hansen <jhansen at op5.com>

---

 plugins-root/check_icmp.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/plugins-root/check_icmp.c b/plugins-root/check_icmp.c
index 98891f0..e45fdf6 100644
--- a/plugins-root/check_icmp.c
+++ b/plugins-root/check_icmp.c
@@ -1336,7 +1336,7 @@ add_target(char *arg)
 
 	switch (address_family) {
 	case -1:
-		// -4 and -6 are not specified on cmdline
+		/* -4 and -6 are not specified on cmdline */
 		address_family = AF_INET;
 		sin = (struct sockaddr_in *)&ip;
 		result = inet_pton(address_family, arg, &sin->sin_addr);
@@ -1347,6 +1347,10 @@ add_target(char *arg)
 			result = inet_pton(address_family, arg, &sin6->sin6_addr);
 		}
 #endif
+		/* If we don't find any valid addresses, we still don't know the address_family */
+		if ( result != 1) {
+			address_family = -1;
+		}
 		break;
 	case AF_INET:
 		sin = (struct sockaddr_in *)&ip;
@@ -1367,7 +1371,11 @@ add_target(char *arg)
 	else {
 		errno = 0;
 		memset(&hints, 0, sizeof(hints));
-		hints.ai_family = AF_UNSPEC;
+		if (address_family == -1) {
+			hints.ai_family = AF_UNSPEC;
+		} else {
+			hints.ai_family = address_family == AF_INET ? PF_INET : PF_INET6;
+		}
 		hints.ai_socktype = SOCK_RAW;
 		if((error = getaddrinfo(arg, NULL, &hints, &res)) != 0) {
 			errno = 0;



More information about the Commits mailing list