X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fnetutils.c;h=a3a241d254a81494d8259a33a97ecce851d139a5;hb=0c2be6a608135d8a45c82a1c0ada5e8020bc2409;hp=5017eb9e9651fe754aadc4473b7cd82b213d8bdb;hpb=dc8f5c0f658b5e060fc17a4beb4a0a2b195ba470;p=nagiosplug.git diff --git a/plugins/netutils.c b/plugins/netutils.c index 5017eb9..a3a241d 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -27,8 +27,12 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * +* $Id$ +* ****************************************************************************/ +#define LOCAL_TIMEOUT_ALARM_HANDLER + #include "common.h" #include "netutils.h" @@ -37,51 +41,19 @@ int econn_refuse_state = STATE_CRITICAL; int was_refused = FALSE; int address_family = AF_UNSPEC; -static int my_connect(const char *address, int port, int *sd, int proto); /* handles socket timeouts */ void socket_timeout_alarm_handler (int sig) { if (sig == SIGALRM) - printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout); + printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout); else - printf ("CRITICAL - Abnormal timeout after %d seconds\n", socket_timeout); + printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout); exit (STATE_CRITICAL); } -/* connects to a host on a specified TCP port, sends a string, - and gets a response */ -int -process_tcp_request (const char *server_address, int server_port, - const char *send_buffer, char *recv_buffer, int recv_size) -{ - int result; - - result = process_request (server_address, server_port, - IPPROTO_TCP, send_buffer, recv_buffer, recv_size); - - return result; -} - - -/* connects to a host on a specified UDP port, sends a string, and gets a - response */ -int -process_udp_request (const char *server_address, int server_port, - const char *send_buffer, char *recv_buffer, int recv_size) -{ - int result; - - result = process_request (server_address, server_port, - IPPROTO_UDP, send_buffer, recv_buffer, recv_size); - - return result; -} - - - /* connects to a host on a specified tcp port, sends a string, and gets a response. loops on select-recv until timeout or eof to get all of a multi-packet answer */ @@ -98,13 +70,13 @@ process_tcp_request2 (const char *server_address, int server_port, fd_set readfds; int recv_length = 0; - result = my_connect (server_address, server_port, &sd, IPPROTO_TCP); + result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP); if (result != STATE_OK) return STATE_CRITICAL; send_result = send (sd, send_buffer, strlen (send_buffer), 0); if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) { - printf ("send() failed\n"); + printf ("%s\n", _("Send failed")); result = STATE_WARNING; } @@ -121,7 +93,7 @@ process_tcp_request2 (const char *server_address, int server_port, if (!FD_ISSET (sd, &readfds)) { /* it hasn't */ if (!recv_length) { strcpy (recv_buffer, ""); - printf ("No data was received from host!\n"); + printf ("%s\n", _("No data was received from host!")); result = STATE_WARNING; } else { /* this one failed, but previous ones worked */ @@ -161,6 +133,7 @@ process_tcp_request2 (const char *server_address, int server_port, return result; } + /* connects to a host on a specified port, sends a string, and gets a response */ int @@ -172,7 +145,7 @@ process_request (const char *server_address, int server_port, int proto, result = STATE_OK; - result = my_connect (server_address, server_port, &sd, proto); + result = np_net_connect (server_address, server_port, &sd, proto); if (result != STATE_OK) return STATE_CRITICAL; @@ -184,65 +157,46 @@ process_request (const char *server_address, int server_port, int proto, } -/* opens a connection to a remote host/tcp port */ -int -my_tcp_connect (const char *host_name, int port, int *sd) -{ - int result; - - result = my_connect (host_name, port, sd, IPPROTO_TCP); - - return result; -} - - -/* opens a connection to a remote host/udp port */ +/* opens a tcp or udp connection to a remote host or local socket */ int -my_udp_connect (const char *host_name, int port, int *sd) -{ - int result; - - result = my_connect (host_name, port, sd, IPPROTO_UDP); - - return result; -} - - -/* opens a tcp or udp connection to a remote host */ -static int -my_connect (const char *host_name, int port, int *sd, int proto) +np_net_connect (const char *host_name, int port, int *sd, int proto) { struct addrinfo hints; - struct addrinfo *res; + struct addrinfo *r, *res; + struct sockaddr_un su; char port_str[6]; - int result; + int socktype, result; - memset (&hints, 0, sizeof (hints)); - hints.ai_family = address_family; - hints.ai_protocol = proto; - hints.ai_socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; + socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; - snprintf (port_str, sizeof (port_str), "%d", port); - result = getaddrinfo (host_name, port_str, &hints, &res); + /* as long as it doesn't start with a '/', it's assumed a host or ip */ + if(host_name[0] != '/'){ + memset (&hints, 0, sizeof (hints)); + hints.ai_family = address_family; + hints.ai_protocol = proto; + hints.ai_socktype = socktype; - if (result != 0) { - printf ("%s\n", gai_strerror (result)); - return STATE_UNKNOWN; - } - else { - while (res) { + snprintf (port_str, sizeof (port_str), "%d", port); + result = getaddrinfo (host_name, port_str, &hints, &res); + + if (result != 0) { + printf ("%s\n", gai_strerror (result)); + return STATE_UNKNOWN; + } + + r = res; + while (r) { /* attempt to create a socket */ - *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ? - SOCK_DGRAM : SOCK_STREAM, res->ai_protocol); + *sd = socket (r->ai_family, socktype, r->ai_protocol); if (*sd < 0) { - printf ("Socket creation failed\n"); - freeaddrinfo (res); + printf ("%s\n", _("Socket creation failed")); + freeaddrinfo (r); return STATE_UNKNOWN; } /* attempt to open a connection */ - result = connect (*sd, res->ai_addr, res->ai_addrlen); + result = connect (*sd, r->ai_addr, r->ai_addrlen); if (result == 0) { was_refused = FALSE; @@ -252,50 +206,60 @@ my_connect (const char *host_name, int port, int *sd, int proto) if (result < 0) { switch (errno) { case ECONNREFUSED: - switch (econn_refuse_state) { - case STATE_OK: - case STATE_WARNING: - was_refused = TRUE; - } + was_refused = TRUE; break; } } close (*sd); - res = res->ai_next; + r = r->ai_next; } freeaddrinfo (res); + } + /* else the hostname is interpreted as a path to a unix socket */ + else { + if(strlen(host_name) >= UNIX_PATH_MAX){ + die(STATE_UNKNOWN, _("Supplied path too long unix domain socket")); + } + memset(&su, 0, sizeof(su)); + su.sun_family = AF_UNIX; + strncpy(su.sun_path, host_name, UNIX_PATH_MAX); + *sd = socket(PF_UNIX, SOCK_STREAM, 0); + if(sd < 0){ + die(STATE_UNKNOWN, _("Socket creation failed")); + } + result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); + if (result < 0 && errno == ECONNREFUSED) + was_refused = TRUE; } if (result == 0) return STATE_OK; - else if (was_refused) - return econn_refuse_state; + else if (was_refused) { + switch (econn_refuse_state) { /* a user-defined expected outcome */ + case STATE_OK: + case STATE_WARNING: /* user wants WARN or OK on refusal */ + return econn_refuse_state; + break; + case STATE_CRITICAL: /* user did not set econn_refuse_state */ + printf ("%s\n", strerror(errno)); + return econn_refuse_state; + break; + default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */ + return STATE_UNKNOWN; + break; + } + } else { printf ("%s\n", strerror(errno)); return STATE_CRITICAL; } } - -int -send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size) -{ - return send_request (sd, IPPROTO_TCP, send_buffer, recv_buffer, recv_size); -} - - -int -send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size) -{ - return send_request (sd, IPPROTO_UDP, send_buffer, recv_buffer, recv_size); -} - - int send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size) { - int result; + int result = STATE_OK; int send_result; int recv_result; struct timeval tv; @@ -303,7 +267,7 @@ send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int send_result = send (sd, send_buffer, strlen (send_buffer), 0); if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) { - printf ("send() failed\n"); + printf ("%s\n", _("Send failed")); result = STATE_WARNING; } @@ -318,7 +282,7 @@ send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int /* make sure some data has arrived */ if (!FD_ISSET (sd, &readfds)) { strcpy (recv_buffer, ""); - printf ("No data was received from host!\n"); + printf ("%s\n", _("No data was received from host!")); result = STATE_WARNING; } @@ -327,7 +291,7 @@ send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int if (recv_result == -1) { strcpy (recv_buffer, ""); if (proto != IPPROTO_TCP) - printf ("recv() failed\n"); + printf ("%s\n", _("Receive failed")); result = STATE_WARNING; } else @@ -349,6 +313,13 @@ is_host (const char *address) return (FALSE); } +void +host_or_die(const char *str) +{ + if(!str || (!is_addr(str) && !is_hostname(str))) + usage_va(_("Invalid hostname/address - %s"), str); +} + int is_addr (const char *address) { @@ -385,28 +356,3 @@ resolve_host_or_addr (const char *address, int family) return TRUE; } } - -int -is_inet_addr (const char *address) -{ - return resolve_host_or_addr (address, AF_INET); -} - -#ifdef USE_IPV6 -int -is_inet6_addr (const char *address) -{ - return resolve_host_or_addr (address, AF_INET6); -} -#endif - -int -is_hostname (const char *s1) -{ -#ifdef USE_IPV6 - return resolve_host_or_addr (s1, address_family); -#else - return resolve_host_or_addr (s1, AF_INET); -#endif -} -