Page 1 of 1

send_nsca ipv6 patch

Posted: Thu May 05, 2016 10:59 am
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:


Re: send_nsca ipv6 patch

Posted: Thu May 05, 2016 11:06 am
by ssax
Thank you for posted that, I'm sure future visitors will appreciate it.

Re: send_nsca ipv6 patch

Posted: Thu May 05, 2016 11:09 am
by customdesigned
I'm going to repost on the general forum. I just saw that that is the successor to the devel mailing list.

Re: send_nsca ipv6 patch

Posted: Thu May 05, 2016 11:15 am
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

Re: send_nsca ipv6 patch

Posted: Thu May 05, 2016 12:23 pm
by customdesigned

Re: send_nsca ipv6 patch

Posted: Thu May 05, 2016 12:27 pm
by tmcdonald
Thanks for the contribution! Going to link back to this post for clarity, and then close this up.