Back to index

nagios-plugins  1.4.16
netutils.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios plugins network utilities
00004 * 
00005 * License: GPL
00006 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
00007 * Copyright (c) 2003-2008 Nagios Plugins Development Team
00008 * 
00009 * Description:
00010 * 
00011 * This file contains commons functions used in many of the plugins.
00012 * 
00013 * 
00014 * This program is free software: you can redistribute it and/or modify
00015 * it under the terms of the GNU General Public License as published by
00016 * the Free Software Foundation, either version 3 of the License, or
00017 * (at your option) any later version.
00018 * 
00019 * This program is distributed in the hope that it will be useful,
00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 * GNU General Public License for more details.
00023 * 
00024 * You should have received a copy of the GNU General Public License
00025 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00026 * 
00027 * 
00028 *****************************************************************************/
00029 
00030 #define LOCAL_TIMEOUT_ALARM_HANDLER
00031 
00032 #include "common.h"
00033 #include "netutils.h"
00034 
00035 int econn_refuse_state = STATE_CRITICAL;
00036 int was_refused = FALSE;
00037 #if USE_IPV6
00038 int address_family = AF_UNSPEC;
00039 #else
00040 int address_family = AF_INET;
00041 #endif
00042 
00043 /* handles socket timeouts */
00044 void
00045 socket_timeout_alarm_handler (int sig)
00046 {
00047        if (sig == SIGALRM)
00048               printf (_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state),  socket_timeout);
00049        else
00050               printf (_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);
00051 
00052        exit (socket_timeout_state);
00053 }
00054 
00055 
00056 /* connects to a host on a specified tcp port, sends a string, and gets a
00057         response. loops on select-recv until timeout or eof to get all of a
00058         multi-packet answer */
00059 int
00060 process_tcp_request2 (const char *server_address, int server_port,
00061        const char *send_buffer, char *recv_buffer, int recv_size)
00062 {
00063 
00064        int result;
00065        int send_result;
00066        int recv_result;
00067        int sd;
00068        struct timeval tv;
00069        fd_set readfds;
00070        int recv_length = 0;
00071 
00072        result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP);
00073        if (result != STATE_OK)
00074               return STATE_CRITICAL;
00075 
00076        send_result = send (sd, send_buffer, strlen (send_buffer), 0);
00077        if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
00078               printf ("%s\n", _("Send failed"));
00079               result = STATE_WARNING;
00080        }
00081 
00082        while (1) {
00083               /* wait up to the number of seconds for socket timeout
00084                  minus one for data from the host */
00085               tv.tv_sec = socket_timeout - 1;
00086               tv.tv_usec = 0;
00087               FD_ZERO (&readfds);
00088               FD_SET (sd, &readfds);
00089               select (sd + 1, &readfds, NULL, NULL, &tv);
00090 
00091               /* make sure some data has arrived */
00092               if (!FD_ISSET (sd, &readfds)) {    /* it hasn't */
00093                      if (!recv_length) {
00094                             strcpy (recv_buffer, "");
00095                             printf ("%s\n", _("No data was received from host!"));
00096                             result = STATE_WARNING;
00097                      }
00098                      else {                                                                /* this one failed, but previous ones worked */
00099                             recv_buffer[recv_length] = 0;
00100                      }
00101                      break;
00102               }
00103               else {                                                                       /* it has */
00104                      recv_result =
00105                             recv (sd, recv_buffer + recv_length,
00106                                    (size_t)recv_size - recv_length - 1, 0);
00107                      if (recv_result == -1) {
00108                             /* recv failed, bail out */
00109                             strcpy (recv_buffer + recv_length, "");
00110                             result = STATE_WARNING;
00111                             break;
00112                      }
00113                      else if (recv_result == 0) {
00114                             /* end of file ? */
00115                             recv_buffer[recv_length] = 0;
00116                             break;
00117                      }
00118                      else {                                                                /* we got data! */
00119                             recv_length += recv_result;
00120                             if (recv_length >= recv_size - 1) {
00121                                    /* buffer full, we're done */
00122                                    recv_buffer[recv_size - 1] = 0;
00123                                    break;
00124                             }
00125                      }
00126               }
00127               /* end if(!FD_ISSET(sd,&readfds)) */
00128        }
00129        /* end while(1) */
00130 
00131        close (sd);
00132        return result;
00133 }
00134 
00135 
00136 /* connects to a host on a specified port, sends a string, and gets a
00137    response */
00138 int
00139 process_request (const char *server_address, int server_port, int proto,
00140        const char *send_buffer, char *recv_buffer, int recv_size)
00141 {
00142        int result;
00143        int sd;
00144 
00145        result = STATE_OK;
00146 
00147        result = np_net_connect (server_address, server_port, &sd, proto);
00148        if (result != STATE_OK)
00149               return STATE_CRITICAL;
00150 
00151        result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
00152 
00153        close (sd);
00154 
00155        return result;
00156 }
00157 
00158 
00159 /* opens a tcp or udp connection to a remote host or local socket */
00160 int
00161 np_net_connect (const char *host_name, int port, int *sd, int proto)
00162 {
00163        struct addrinfo hints;
00164        struct addrinfo *r, *res;
00165        struct sockaddr_un su;
00166        char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
00167        size_t len;
00168        int socktype, result;
00169 
00170        socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
00171 
00172        /* as long as it doesn't start with a '/', it's assumed a host or ip */
00173        if(host_name[0] != '/'){
00174               memset (&hints, 0, sizeof (hints));
00175               hints.ai_family = address_family;
00176               hints.ai_protocol = proto;
00177               hints.ai_socktype = socktype;
00178 
00179               len = strlen (host_name);
00180               /* check for an [IPv6] address (and strip the brackets) */
00181               if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') {
00182                      host_name++;
00183                      len -= 2;
00184               }
00185               if (len >= sizeof(host))
00186                      return STATE_UNKNOWN;
00187               memcpy (host, host_name, len);
00188               host[len] = '\0';
00189               snprintf (port_str, sizeof (port_str), "%d", port);
00190               result = getaddrinfo (host, port_str, &hints, &res);
00191 
00192               if (result != 0) {
00193                      printf ("%s\n", gai_strerror (result));
00194                      return STATE_UNKNOWN;
00195               }
00196 
00197               r = res;
00198               while (r) {
00199                      /* attempt to create a socket */
00200                      *sd = socket (r->ai_family, socktype, r->ai_protocol);
00201 
00202                      if (*sd < 0) {
00203                             printf ("%s\n", _("Socket creation failed"));
00204                             freeaddrinfo (r);
00205                             return STATE_UNKNOWN;
00206                      }
00207 
00208                      /* attempt to open a connection */
00209                      result = connect (*sd, r->ai_addr, r->ai_addrlen);
00210 
00211                      if (result == 0) {
00212                             was_refused = FALSE;
00213                             break;
00214                      }
00215 
00216                      if (result < 0) {
00217                             switch (errno) {
00218                             case ECONNREFUSED:
00219                                    was_refused = TRUE;
00220                                    break;
00221                             }
00222                      }
00223 
00224                      close (*sd);
00225                      r = r->ai_next;
00226               }
00227               freeaddrinfo (res);
00228        }
00229        /* else the hostname is interpreted as a path to a unix socket */
00230        else {
00231               if(strlen(host_name) >= UNIX_PATH_MAX){
00232                      die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
00233               }
00234               memset(&su, 0, sizeof(su));
00235               su.sun_family = AF_UNIX;
00236               strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
00237               *sd = socket(PF_UNIX, SOCK_STREAM, 0);
00238               if(*sd < 0){
00239                      die(STATE_UNKNOWN, _("Socket creation failed"));
00240               }
00241               result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
00242               if (result < 0 && errno == ECONNREFUSED)
00243                      was_refused = TRUE;
00244        }
00245 
00246        if (result == 0)
00247               return STATE_OK;
00248        else if (was_refused) {
00249               switch (econn_refuse_state) { /* a user-defined expected outcome */
00250               case STATE_OK:
00251               case STATE_WARNING:  /* user wants WARN or OK on refusal */
00252                      return econn_refuse_state;
00253                      break;
00254               case STATE_CRITICAL: /* user did not set econn_refuse_state */
00255                      printf ("%s\n", strerror(errno));
00256                      return econn_refuse_state;
00257                      break;
00258               default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
00259                      return STATE_UNKNOWN;
00260                      break;
00261               }
00262        }
00263        else {
00264               printf ("%s\n", strerror(errno));
00265               return STATE_CRITICAL;
00266        }
00267 }
00268 
00269 int
00270 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
00271 {
00272        int result = STATE_OK;
00273        int send_result;
00274        int recv_result;
00275        struct timeval tv;
00276        fd_set readfds;
00277 
00278        send_result = send (sd, send_buffer, strlen (send_buffer), 0);
00279        if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
00280               printf ("%s\n", _("Send failed"));
00281               result = STATE_WARNING;
00282        }
00283 
00284        /* wait up to the number of seconds for socket timeout minus one
00285           for data from the host */
00286        tv.tv_sec = socket_timeout - 1;
00287        tv.tv_usec = 0;
00288        FD_ZERO (&readfds);
00289        FD_SET (sd, &readfds);
00290        select (sd + 1, &readfds, NULL, NULL, &tv);
00291 
00292        /* make sure some data has arrived */
00293        if (!FD_ISSET (sd, &readfds)) {
00294               strcpy (recv_buffer, "");
00295               printf ("%s\n", _("No data was received from host!"));
00296               result = STATE_WARNING;
00297        }
00298 
00299        else {
00300               recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
00301               if (recv_result == -1) {
00302                      strcpy (recv_buffer, "");
00303                      if (proto != IPPROTO_TCP)
00304                             printf ("%s\n", _("Receive failed"));
00305                      result = STATE_WARNING;
00306               }
00307               else
00308                      recv_buffer[recv_result] = 0;
00309 
00310               /* die returned string */
00311               recv_buffer[recv_size - 1] = 0;
00312        }
00313        return result;
00314 }
00315 
00316 
00317 int
00318 is_host (const char *address)
00319 {
00320        if (is_addr (address) || is_hostname (address))
00321               return (TRUE);
00322 
00323        return (FALSE);
00324 }
00325 
00326 void
00327 host_or_die(const char *str)
00328 {
00329        if(!str || (!is_addr(str) && !is_hostname(str)))
00330               usage_va(_("Invalid hostname/address - %s"), str);
00331 }
00332 
00333 int
00334 is_addr (const char *address)
00335 {
00336 #ifdef USE_IPV6
00337        if (address_family == AF_INET && is_inet_addr (address))
00338               return TRUE;
00339        else if (address_family == AF_INET6 && is_inet6_addr (address))
00340               return TRUE;
00341 #else
00342        if (is_inet_addr (address))
00343               return (TRUE);
00344 #endif
00345 
00346        return (FALSE);
00347 }
00348 
00349 int
00350 resolve_host_or_addr (const char *address, int family)
00351 {
00352        struct addrinfo hints;
00353        struct addrinfo *res;
00354        int retval;
00355 
00356        memset (&hints, 0, sizeof (hints));
00357        hints.ai_family = family;
00358        retval = getaddrinfo (address, NULL, &hints, &res);
00359 
00360        if (retval != 0)
00361               return FALSE;
00362        else {
00363               freeaddrinfo (res);
00364               return TRUE;
00365        }
00366 }