Back to index

nagios-plugins  1.4.16
Defines | Functions | Variables
netutils.c File Reference
#include "common.h"
#include "netutils.h"

Go to the source code of this file.

Defines

#define LOCAL_TIMEOUT_ALARM_HANDLER

Functions

void socket_timeout_alarm_handler (int sig)
int process_tcp_request2 (const char *server_address, int server_port, const char *send_buffer, char *recv_buffer, int recv_size)
int process_request (const char *server_address, int server_port, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
int np_net_connect (const char *host_name, int port, int *sd, int proto)
int send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
int is_host (const char *address)
void host_or_die (const char *str)
int is_addr (const char *address)
int resolve_host_or_addr (const char *address, int family)

Variables

int econn_refuse_state = STATE_CRITICAL
int was_refused = FALSE
int address_family = AF_INET

Define Documentation

Definition at line 30 of file netutils.c.


Function Documentation

void host_or_die ( const char *  str)

Definition at line 327 of file netutils.c.

{
       if(!str || (!is_addr(str) && !is_hostname(str)))
              usage_va(_("Invalid hostname/address - %s"), str);
}

Here is the call graph for this function:

int is_addr ( const char *  address)

Definition at line 334 of file netutils.c.

{
#ifdef USE_IPV6
       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))
              return (TRUE);
#endif

       return (FALSE);
}

Here is the caller graph for this function:

int is_host ( const char *  address)

Definition at line 318 of file netutils.c.

{
       if (is_addr (address) || is_hostname (address))
              return (TRUE);

       return (FALSE);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int np_net_connect ( const char *  host_name,
int  port,
int *  sd,
int  proto 
)

Definition at line 161 of file netutils.c.

{
       struct addrinfo hints;
       struct addrinfo *r, *res;
       struct sockaddr_un su;
       char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
       size_t len;
       int socktype, result;

       socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;

       /* 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;

              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, 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 (r->ai_family, socktype, r->ai_protocol);

                     if (*sd < 0) {
                            printf ("%s\n", _("Socket creation failed"));
                            freeaddrinfo (r);
                            return STATE_UNKNOWN;
                     }

                     /* attempt to open a connection */
                     result = connect (*sd, r->ai_addr, r->ai_addrlen);

                     if (result == 0) {
                            was_refused = FALSE;
                            break;
                     }

                     if (result < 0) {
                            switch (errno) {
                            case ECONNREFUSED:
                                   was_refused = TRUE;
                                   break;
                            }
                     }

                     close (*sd);
                     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) {
              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;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int process_request ( const char *  server_address,
int  server_port,
int  proto,
const char *  send_buffer,
char *  recv_buffer,
int  recv_size 
)

Definition at line 139 of file netutils.c.

{
       int result;
       int sd;

       result = STATE_OK;

       result = np_net_connect (server_address, server_port, &sd, proto);
       if (result != STATE_OK)
              return STATE_CRITICAL;

       result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);

       close (sd);

       return result;
}

Here is the call graph for this function:

int process_tcp_request2 ( const char *  server_address,
int  server_port,
const char *  send_buffer,
char *  recv_buffer,
int  recv_size 
)

Definition at line 60 of file netutils.c.

{

       int result;
       int send_result;
       int recv_result;
       int sd;
       struct timeval tv;
       fd_set readfds;
       int recv_length = 0;

       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 ("%s\n", _("Send failed"));
              result = STATE_WARNING;
       }

       while (1) {
              /* 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;
              FD_ZERO (&readfds);
              FD_SET (sd, &readfds);
              select (sd + 1, &readfds, NULL, NULL, &tv);

              /* make sure some data has arrived */
              if (!FD_ISSET (sd, &readfds)) {    /* it hasn't */
                     if (!recv_length) {
                            strcpy (recv_buffer, "");
                            printf ("%s\n", _("No data was received from host!"));
                            result = STATE_WARNING;
                     }
                     else {                                                                /* this one failed, but previous ones worked */
                            recv_buffer[recv_length] = 0;
                     }
                     break;
              }
              else {                                                                       /* it has */
                     recv_result =
                            recv (sd, recv_buffer + recv_length,
                                   (size_t)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 ? */
                            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 */
                                   recv_buffer[recv_size - 1] = 0;
                                   break;
                            }
                     }
              }
              /* end if(!FD_ISSET(sd,&readfds)) */
       }
       /* end while(1) */

       close (sd);
       return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int resolve_host_or_addr ( const char *  address,
int  family 
)

Definition at line 350 of file netutils.c.

{
       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 {
              freeaddrinfo (res);
              return TRUE;
       }
}

Here is the call graph for this function:

int send_request ( int  sd,
int  proto,
const char *  send_buffer,
char *  recv_buffer,
int  recv_size 
)

Definition at line 270 of file netutils.c.

{
       int result = STATE_OK;
       int send_result;
       int recv_result;
       struct timeval tv;
       fd_set readfds;

       send_result = send (sd, send_buffer, strlen (send_buffer), 0);
       if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
              printf ("%s\n", _("Send failed"));
              result = STATE_WARNING;
       }

       /* 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;
       FD_ZERO (&readfds);
       FD_SET (sd, &readfds);
       select (sd + 1, &readfds, NULL, NULL, &tv);

       /* make sure some data has arrived */
       if (!FD_ISSET (sd, &readfds)) {
              strcpy (recv_buffer, "");
              printf ("%s\n", _("No data was received from host!"));
              result = STATE_WARNING;
       }

       else {
              recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
              if (recv_result == -1) {
                     strcpy (recv_buffer, "");
                     if (proto != IPPROTO_TCP)
                            printf ("%s\n", _("Receive failed"));
                     result = STATE_WARNING;
              }
              else
                     recv_buffer[recv_result] = 0;

              /* die returned string */
              recv_buffer[recv_size - 1] = 0;
       }
       return result;
}

Here is the caller graph for this function:

void socket_timeout_alarm_handler ( int  sig)

Definition at line 45 of file netutils.c.

{
       if (sig == SIGALRM)
              printf (_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state),  socket_timeout);
       else
              printf (_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);

       exit (socket_timeout_state);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int address_family = AF_INET

Definition at line 40 of file netutils.c.

Definition at line 35 of file netutils.c.

Definition at line 36 of file netutils.c.