X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fnetutils.c;h=1d6100abf843efd3f25a8209adbdcfa4042d8fd4;hb=386b791af2a5c9d95531d2ca047dd8ffc12bcb2e;hp=6f3a151019ee7b47d7635de3e2269855337eec9d;hpb=16cd0c8151eee652d88b0b6f3aaa5b917cb87e13;p=nagiosplug.git diff --git a/plugins/netutils.c b/plugins/netutils.c index 6f3a151..1d6100a 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -1,59 +1,60 @@ -/**************************************************************************** -* +/***************************************************************************** +* * Nagios plugins network utilities -* +* * License: GPL * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) -* -* Last Modified: $Date$ -* +* Copyright (c) 2003-2008 Nagios Plugins Development Team +* * Description: -* +* * This file contains commons functions used in many of the plugins. -* -* License Information: -* -* This program is free software; you can redistribute it and/or modify +* +* +* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or +* the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. -* +* * You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -* -* $Id$ -* -****************************************************************************/ +* along with this program. If not, see . +* +* +*****************************************************************************/ + +#define LOCAL_TIMEOUT_ALARM_HANDLER #include "common.h" #include "netutils.h" -unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; int econn_refuse_state = STATE_CRITICAL; int was_refused = FALSE; +#if USE_IPV6 int address_family = AF_UNSPEC; +#else +int address_family = AF_INET; +#endif /* handles socket timeouts */ void socket_timeout_alarm_handler (int sig) { if (sig == SIGALRM) - printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout); + printf (_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); else - printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout); + printf (_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); - exit (STATE_CRITICAL); + exit (socket_timeout_state); } -/* 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 +/* 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 */ int process_tcp_request2 (const char *server_address, int server_port, @@ -74,7 +75,7 @@ process_tcp_request2 (const char *server_address, int server_port, 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; } @@ -91,7 +92,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 */ @@ -101,7 +102,7 @@ process_tcp_request2 (const char *server_address, int server_port, } else { /* it has */ recv_result = - recv (sd, recv_buffer + recv_length, + recv (sd, recv_buffer + recv_length, (size_t)recv_size - recv_length - 1, 0); if (recv_result == -1) { /* recv failed, bail out */ @@ -132,7 +133,7 @@ process_tcp_request2 (const char *server_address, int server_port, } -/* connects to a host on a specified port, sends a string, and gets a +/* connects to a host on a specified port, sends a string, and gets a response */ int process_request (const char *server_address, int server_port, int proto, @@ -162,7 +163,8 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) struct addrinfo hints; struct addrinfo *r, *res; struct sockaddr_un su; - char port_str[6]; + char port_str[6], host[MAX_HOST_ADDRESS_LENGTH]; + size_t len; int socktype, result; socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; @@ -174,8 +176,18 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) hints.ai_protocol = proto; hints.ai_socktype = socktype; + len = strlen (host_name); + /* check for an [IPv6] address (and strip the brackets) */ + if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') { + host_name++; + len -= 2; + } + if (len >= sizeof(host)) + return STATE_UNKNOWN; + memcpy (host, host_name, len); + host[len] = '\0'; snprintf (port_str, sizeof (port_str), "%d", port); - result = getaddrinfo (host_name, port_str, &hints, &res); + result = getaddrinfo (host, port_str, &hints, &res); if (result != 0) { printf ("%s\n", gai_strerror (result)); @@ -188,7 +200,7 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) *sd = socket (r->ai_family, socktype, r->ai_protocol); if (*sd < 0) { - printf (_("Socket creation failed\n")); + printf ("%s\n", _("Socket creation failed")); freeaddrinfo (r); return STATE_UNKNOWN; } @@ -213,18 +225,18 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) 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(_("Supplied path too long unix domain socket")); + 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(_("Socket creation failed")); + if(*sd < 0){ + die(STATE_UNKNOWN, _("Socket creation failed")); } result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); if (result < 0 && errno == ECONNREFUSED) @@ -235,7 +247,7 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) return STATE_OK; else if (was_refused) { switch (econn_refuse_state) { /* a user-defined expected outcome */ - case STATE_OK: + case STATE_OK: case STATE_WARNING: /* user wants WARN or OK on refusal */ return econn_refuse_state; break; @@ -265,11 +277,11 @@ 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; } - /* wait up to the number of seconds for socket timeout minus one + /* wait up to the number of seconds for socket timeout minus one for data from the host */ tv.tv_sec = socket_timeout - 1; tv.tv_usec = 0; @@ -280,7 +292,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; } @@ -289,7 +301,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 (_("Receive failed\n")); + printf ("%s\n", _("Receive failed")); result = STATE_WARNING; } else @@ -322,14 +334,12 @@ int is_addr (const char *address) { #ifdef USE_IPV6 - if (is_inet_addr (address) && address_family != AF_INET6) + if (address_family == AF_INET && is_inet_addr (address)) + return TRUE; + else if (address_family == AF_INET6 && is_inet6_addr (address)) + return TRUE; #else if (is_inet_addr (address)) -#endif - return (TRUE); - -#ifdef USE_IPV6 - if (is_inet6_addr (address) && address_family != AF_INET) return (TRUE); #endif