send_nsca ipv6 patch

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
customdesigned
Posts: 3
Joined: Thu May 05, 2016 10:43 am

send_nsca ipv6 patch

Post by customdesigned »

When the client is on an IPv6 only network, or you just don't want to deal with all the IPv4 NAT garbage. The nsca service is normally used with xinetd or systemd.socket, but it should eventually get fixed also just for completeness.

nsca-2.9-ipv6.patch

Code: Select all

diff -up ./src/netutils.c.ipv6 ./src/netutils.c
--- ./src/netutils.c.ipv6	2016-03-11 15:57:55.838503833 -0500
+++ ./src/netutils.c	2016-03-11 17:00:10.891199928 -0500
@@ -31,8 +31,9 @@
 
 #include "../include/common.h"
 #include "../include/netutils.h"
-
-
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
 
 /* opens a connection to a remote host/tcp port */
 int my_tcp_connect(char *host_name,int port,int *sd){
@@ -50,23 +51,12 @@ int my_connect(char *host_name,int port,
 	struct hostent *hp;
 	struct protoent *ptrp;
 	int result;
-
-	bzero((char *)&servaddr,sizeof(servaddr));
-	servaddr.sin_family=AF_INET;
-	servaddr.sin_port=htons(port);
-
-	/* try to bypass using a DNS lookup if this is just an IP address */
-	if(!my_inet_aton(host_name,&servaddr.sin_addr)){
-
-		/* else do a DNS lookup */
-		hp=gethostbyname((const char *)host_name);
-		if(hp==NULL){
-			printf("Invalid host name '%s'\n",host_name);
-			return STATE_UNKNOWN;
-		        }
-
-		memcpy(&servaddr.sin_addr,hp->h_addr,hp->h_length);
-	        }
+	struct addrinfo *aip = 0;
+	struct addrinfo hints = {
+	  (AI_V4MAPPED | AI_ADDRCONFIG),
+	  AF_UNSPEC,
+	};
+	char server_port[32];
 
 	/* map transport protocol name to protocol number */
 	if(((ptrp=getprotobyname(proto)))==NULL){
@@ -74,15 +64,26 @@ int my_connect(char *host_name,int port,
 		return STATE_UNKNOWN;
 	        }
 
+	hints.ai_protocol = ptrp->p_proto;
+	hints.ai_socktype = (!strcmp(proto,"udp"))?SOCK_DGRAM:SOCK_STREAM;
+	sprintf(server_port,"%d",port);
+	result = getaddrinfo(host_name,server_port,&hints,&aip);
+	if (result < 0) {
+		printf("Host lookup failed: '%s'\n",host_name);
+		return STATE_UNKNOWN;
+		}
+
 	/* create a socket */
-	*sd=socket(PF_INET,(!strcmp(proto,"udp"))?SOCK_DGRAM:SOCK_STREAM,ptrp->p_proto);
+	*sd=socket(aip->ai_family,aip->ai_socktype,aip->ai_protocol);
 	if(*sd<0){
 		printf("Socket creation failed\n");
+		freeaddrinfo(aip);
 		return STATE_UNKNOWN;
 	        }
 
 	/* open a connection */
-	result=connect(*sd,(struct sockaddr *)&servaddr,sizeof(servaddr));
+	result=connect(*sd,aip->ai_addr,aip->ai_addrlen);
+	freeaddrinfo(aip);
 	if(result<0){
 		switch(errno){  
 		case ECONNREFUSED:

ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: send_nsca ipv6 patch

Post by ssax »

Thank you for posted that, I'm sure future visitors will appreciate it.
customdesigned
Posts: 3
Joined: Thu May 05, 2016 10:43 am

Re: send_nsca ipv6 patch

Post by customdesigned »

I'm going to repost on the general forum. I just saw that that is the successor to the devel mailing list.
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: send_nsca ipv6 patch

Post by tmcdonald »

Actually a better way to get it on the dev radar would be to submit this as a pull request if possible: https://github.com/NagiosEnterprises/nsca
Former Nagios employee
customdesigned
Posts: 3
Joined: Thu May 05, 2016 10:43 am

Re: send_nsca ipv6 patch

Post by customdesigned »

tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: send_nsca ipv6 patch

Post by tmcdonald »

Thanks for the contribution! Going to link back to this post for clarity, and then close this up.
Former Nagios employee
Locked