From 4a95946a9b634513494553808b31372060e3c045 Mon Sep 17 00:00:00 2001 From: "Jeremy T. Bouse" Date: Sat, 8 Mar 2003 02:26:22 +0000 Subject: AF indepedent routines introduced. Modifed process_request() & my_connect() parameters to make 'proto' type 'int' rather than 'char *' and use IPPROTO_* POSIX values. Removed is_dotted_quad() & my_inet_aton() functions Added is_addr(), is_inet_addr(), is_inet6_addr() and resolve_host_or_addr() functions to check whether it is a valid IP address Modified is_host() to call is_addr() and is_hostname() git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@384 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/plugins/netutils.c b/plugins/netutils.c index fc85a24..c0e82da 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -40,20 +40,18 @@ RETSIGTYPE socket_timeout_alarm_handler (int); int process_tcp_request2 (char *, int, char *, char *, int); int process_tcp_request (char *, int, char *, char *, int); int process_udp_request (char *, int, char *, char *, int); -int process_request (char *, int, char *, char *, char *, int); +int process_request (char *, int, int, char *, char *, int); int my_tcp_connect (char *, int, int *); int my_udp_connect (char *, int, int *); -int my_connect (char *, int, int *, char *); - -int my_inet_aton (register const char *, struct in_addr *); +int my_connect (char *, int, int *, int); /* handles socket timeouts */ void socket_timeout_alarm_handler (int sig) { - printf ("Socket timeout after %d seconds\n", socket_timeout); + printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout); exit (STATE_CRITICAL); } @@ -62,16 +60,13 @@ socket_timeout_alarm_handler (int sig) /* connects to a host on a specified TCP port, sends a string, and gets a response */ int -process_tcp_request (char *server_address, - int server_port, - char *send_buffer, char *recv_buffer, int recv_size) +process_tcp_request (char *server_address, int server_port, + char *send_buffer, char *recv_buffer, int recv_size) { int result; - char proto[4] = "tcp"; - result = process_request (server_address, - server_port, - proto, send_buffer, recv_buffer, recv_size); + result = process_request (server_address, server_port, + IPPROTO_TCP, send_buffer, recv_buffer, recv_size); return result; } @@ -80,16 +75,13 @@ process_tcp_request (char *server_address, /* connects to a host on a specified UDP port, sends a string, and gets a response */ int -process_udp_request (char *server_address, - int server_port, - char *send_buffer, char *recv_buffer, int recv_size) +process_udp_request (char *server_address, int server_port, + char *send_buffer, char *recv_buffer, int recv_size) { int result; - char proto[4] = "udp"; - result = process_request (server_address, - server_port, - proto, send_buffer, recv_buffer, recv_size); + result = process_request (server_address, server_port, + IPPROTO_UDP, send_buffer, recv_buffer, recv_size); return result; } @@ -100,9 +92,8 @@ process_udp_request (char *server_address, response. loops on select-recv until timeout or eof to get all of a multi-packet answer */ int -process_tcp_request2 (char *server_address, - int server_port, - char *send_buffer, char *recv_buffer, int recv_size) +process_tcp_request2 (char *server_address, int server_port, + char *send_buffer, char *recv_buffer, int recv_size) { int result; @@ -113,7 +104,7 @@ process_tcp_request2 (char *server_address, fd_set readfds; int recv_length = 0; - result = my_connect (server_address, server_port, &sd, "tcp"); + result = my_connect (server_address, server_port, &sd, IPPROTO_TCP); if (result != STATE_OK) return STATE_CRITICAL; @@ -146,25 +137,31 @@ process_tcp_request2 (char *server_address, } else { /* it has */ recv_result = - recv (sd, recv_buffer + recv_length, recv_size - recv_length - 1, 0); - if (recv_result == -1) { /* recv failed, bail out */ + recv (sd, recv_buffer + recv_length, + recv_size - recv_length - 1, 0); + if (recv_result == -1) { + /* recv failed, bail out */ strcpy (recv_buffer + recv_length, ""); result = STATE_WARNING; break; } - else if (recv_result == 0) { /* end of file ? */ + else if (recv_result == 0) { + /* end of file ? */ recv_buffer[recv_length] = 0; break; } else { /* we got data! */ recv_length += recv_result; - if (recv_length >= recv_size - 1) { /* buffer full, we're done */ + if (recv_length >= recv_size - 1) { + /* buffer full, we're done */ recv_buffer[recv_size - 1] = 0; break; } } - } /* end if(!FD_ISSET(sd,&readfds)) */ - } /* end while(1) */ + } + /* end if(!FD_ISSET(sd,&readfds)) */ + } + /* end while(1) */ close (sd); return result; @@ -173,10 +170,8 @@ process_tcp_request2 (char *server_address, /* connects to a host on a specified port, sends a string, and gets a response */ int -process_request (char *server_address, - int server_port, - char *proto, - char *send_buffer, char *recv_buffer, int recv_size) +process_request (char *server_address, int server_port, int proto, + char *send_buffer, char *recv_buffer, int recv_size) { int result; int send_result; @@ -216,7 +211,7 @@ process_request (char *server_address, recv_result = recv (sd, recv_buffer, recv_size - 1, 0); if (recv_result == -1) { strcpy (recv_buffer, ""); - if (!strcmp (proto, "tcp")) + if (proto != IPPROTO_TCP) printf ("recv() failed\n"); result = STATE_WARNING; } @@ -238,9 +233,8 @@ int my_tcp_connect (char *host_name, int port, int *sd) { int result; - char proto[4] = "tcp"; - result = my_connect (host_name, port, sd, proto); + result = my_connect (host_name, port, sd, IPPROTO_TCP); return result; } @@ -251,9 +245,8 @@ int my_udp_connect (char *host_name, int port, int *sd) { int result; - char proto[4] = "udp"; - result = my_connect (host_name, port, sd, proto); + result = my_connect (host_name, port, sd, IPPROTO_UDP); return result; } @@ -261,169 +254,53 @@ my_udp_connect (char *host_name, int port, int *sd) /* opens a tcp or udp connection to a remote host */ int -my_connect (char *host_name, int port, int *sd, char *proto) +my_connect (char *host_name, int port, int *sd, int proto) { - struct sockaddr_in servaddr; - struct hostent *hp; - struct protoent *ptrp; + struct addrinfo hints; + struct addrinfo *res; + struct addrinfo *ptrp; + char port_str[6]; int result; - bzero ((char *) &servaddr, sizeof (servaddr)); - servaddr.sin_family = AF_INET; - servaddr.sin_port = htons (port); + memset (&hints, 0, sizeof (hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_protocol = proto; - /* 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); - } - - /* map transport protocol name to protocol number */ - if ((ptrp = getprotobyname (proto)) == NULL) { - printf ("Cannot map \"%s\" to protocol number\n", proto); - return STATE_UNKNOWN; - } + snprintf (port_str, sizeof (port_str), "%d", port); + result = getaddrinfo (host_name, port_str, &hints, &res); - /* create a socket */ - *sd = - socket (PF_INET, (!strcmp (proto, "udp")) ? SOCK_DGRAM : SOCK_STREAM, - ptrp->p_proto); - if (*sd < 0) { - printf ("Socket creation failed\n"); + if (result != 0) { + printf ("%s\n", gai_strerror (result)); return STATE_UNKNOWN; } + else { + while (res) { + /* attempt to create a socket */ + *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ? + SOCK_DGRAM : SOCK_STREAM, res->ai_protocol); + + if (*sd < 0) { + printf ("Socket creation failed\n"); + freeaddrinfo (res); + return STATE_UNKNOWN; + } - /* open a connection */ - result = connect (*sd, (struct sockaddr *) &servaddr, sizeof (servaddr)); - if (result < 0) { - switch (errno) { - case ECONNREFUSED: - printf ("Connection refused by host\n"); - break; - case ETIMEDOUT: - printf ("Timeout while attempting connection\n"); - break; - case ENETUNREACH: - printf ("Network is unreachable\n"); - break; - default: - printf ("Connection refused or timed out\n"); - } - - return STATE_CRITICAL; - } - - return STATE_OK; -} - - - -/* This code was taken from Fyodor's nmap utility, which was originally - taken from the GLIBC 2.0.6 libraries because Solaris doesn't contain - the inet_aton() funtion. */ -int -my_inet_aton (register const char *cp, struct in_addr *addr) -{ - register unsigned int val; /* changed from u_long --david */ - register int base, n; - register char c; - u_int parts[4]; - register u_int *pp = parts; - - c = *cp; - - for (;;) { - - /* - * Collect number up to ``.''. - * Values are specified as for C: - * 0x=hex, 0=octal, isdigit=decimal. - */ - if (!isdigit ((int) c)) - return (0); - val = 0; - base = 10; - - if (c == '0') { - c = *++cp; - if (c == 'x' || c == 'X') - base = 16, c = *++cp; - else - base = 8; - } + /* attempt to open a connection */ + result = connect (*sd, res->ai_addr, res->ai_addrlen); - for (;;) { - if (isascii ((int) c) && isdigit ((int) c)) { - val = (val * base) + (c - '0'); - c = *++cp; - } - else if (base == 16 && isascii ((int) c) && isxdigit ((int) c)) { - val = (val << 4) | (c + 10 - (islower ((int) c) ? 'a' : 'A')); - c = *++cp; - } - else + if (result == 0) break; - } - if (c == '.') { - - /* - * Internet format: - * a.b.c.d - * a.b.c (with c treated as 16 bits) - * a.b (with b treated as 24 bits) - */ - if (pp >= parts + 3) - return (0); - *pp++ = val; - c = *++cp; + close (*sd); + res = res->ai_next; } - else - break; + freeaddrinfo (res); } - /* Check for trailing characters */ - if (c != '\0' && (!isascii ((int) c) || !isspace ((int) c))) - return (0); - - /* Concoct the address according to the number of parts specified */ - n = pp - parts + 1; - switch (n) { - - case 0: - return (0); /* initial nondigit */ - - case 1: /* a -- 32 bits */ - break; - - case 2: /* a.b -- 8.24 bits */ - if (val > 0xffffff) - return (0); - val |= parts[0] << 24; - break; - - case 3: /* a.b.c -- 8.8.16 bits */ - if (val > 0xffff) - return (0); - val |= (parts[0] << 24) | (parts[1] << 16); - break; - - case 4: /* a.b.c.d -- 8.8.8.8 bits */ - if (val > 0xff) - return (0); - val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); - break; + if (result == 0) + return STATE_OK; + else { + printf ("%s\n", strerror(errno)); + return STATE_CRITICAL; } - - if (addr) - addr->s_addr = htonl (val); - - return (1); } diff --git a/plugins/netutils.h.in b/plugins/netutils.h.in index 1907ab7..69cf278 100644 --- a/plugins/netutils.h.in +++ b/plugins/netutils.h.in @@ -34,6 +34,7 @@ #undef HAVE_STRINGS_H #undef HAVE_STRING_H +#include "config.h" #include "common.h" #include #include @@ -41,22 +42,16 @@ void socket_timeout_alarm_handler (int); int process_tcp_request2 (char *address, int port, char *sbuffer, - char *rbuffer, int rsize); + char *rbuffer, int rsize); int process_tcp_request (char *address, int port, char *sbuffer, - char *rbuffer, int rsize); + char *rbuffer, int rsize); int process_udp_request (char *address, int port, char *sbuffer, - char *rbuffer, int rsize); + char *rbuffer, int rsize); int process_request (char *address, int port, char *proto, char *sbuffer, - char *rbuffer, int rsize); + char *rbuffer, int rsize); int my_tcp_connect (char *address, int port, int *sd); int my_udp_connect (char *address, int port, int *sd); -int my_connect (char *address, int port, int *sd, char *proto); - -int my_inet_aton (register const char *cp, struct in_addr *addr); +int my_connect (char *address, int port, int *sd, int proto); -#ifndef DEFAULT_SOCKET_TIMEOUT -#include "config.h" -#include "common.h" -#endif int socket_timeout = DEFAULT_SOCKET_TIMEOUT; diff --git a/plugins/utils.c b/plugins/utils.c index aaa9fe5..0d25067 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -17,6 +17,8 @@ #include #include +#include + extern int timeout_interval; extern const char *progname; @@ -27,7 +29,10 @@ void terminate (int, const char *fmt, ...); RETSIGTYPE timeout_alarm_handler (int); int is_host (char *); -int is_dotted_quad (char *); +int is_addr (char *); +int resolve_host_or_addr (char *, int); +int is_inet_addr (char *); +int is_inet6_addr (char *); int is_hostname (char *); int is_integer (char *); @@ -58,7 +63,7 @@ char *strpcat (char *dest, const char *src, const char *str); #define TXTBLK 128 /* ************************************************************************** - * max_state(STATE_x, STATE_y) + /* max_state(STATE_x, STATE_y) * compares STATE_x to STATE_y and returns result based on the following * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL * @@ -167,28 +172,50 @@ timeout_alarm_handler (int signo) int is_host (char *address) { - if (is_dotted_quad (address) || is_hostname (address)) + if (is_addr (address) || is_hostname (address)) return (TRUE); + return (FALSE); } int -is_dotted_quad (char *address) +is_addr (char *address) { - int o1, o2, o3, o4; - char c[1]; + if (is_inet_addr (address) || is_inet6_addr (address)) + return (TRUE); - if (!address) - return FALSE; + return (FALSE); +} - if (sscanf (address, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, c) != 4) - return FALSE; - else if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255) - return FALSE; - else if (o1 < 0 || o2 < 0 || o3 < 0 || o4 < 0) +int +resolve_host_or_addr (char *address, int family) +{ + struct addrinfo hints; + struct addrinfo *res; + int retval; + + memset (&hints, 0, sizeof (hints)); + hints.ai_family = family; + retval = getaddrinfo (address, NULL, &hints, &res); + + if (retval != 0) return FALSE; - else + else { + freeaddrinfo (res); return TRUE; + } +} + +int +is_inet_addr (char *address) +{ + return resolve_host_or_addr (address, AF_INET); +} + +int +is_inet6_addr (char *address) +{ + return resolve_host_or_addr (address, AF_INET6); } /* from RFC-1035 @@ -201,22 +228,7 @@ is_dotted_quad (char *address) int is_hostname (char *s1) { - if (!s1 || strlen (s1) > 63) { - return FALSE; - } - if (strcspn (s1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") != 0) { - return FALSE; - } - if (strspn (s1, "0123456789-.") == 1) { - return FALSE; - } - while ((s1 = index (s1, '.'))) { - s1++; - if (strspn (s1, "0123456789-.") == 1) { - return FALSE; - } - } - return TRUE; + return resolve_host_or_addr (s1, AF_UNSPEC); } int diff --git a/plugins/utils.h.in b/plugins/utils.h.in index 317ec46..0d947f4 100644 --- a/plugins/utils.h.in +++ b/plugins/utils.h.in @@ -28,7 +28,9 @@ int timeout_interval = DEFAULT_SOCKET_TIMEOUT; /* Test input types */ int is_host (char *); -int is_dotted_quad (char *); +int is_addr (char *); +int is_inet_addr (char *); +int is_inet6_addr (char *); int is_hostname (char *); int is_integer (char *); -- cgit v0.10-9-g596f