Code

If unspecified set LDAP_OPT_SUCCESS to LDAP_SUCCESS (Sergei Haramundanis - #1498923)
[nagiosplug.git] / plugins / netutils.c
index db64ef09b230dcf1a106e6ec3eff6a34b1c5b5da..7bf225437aaaf3f86a015b5692be00c710a86118 100644 (file)
 *
 ****************************************************************************/
 
+#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
@@ -74,7 +80,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 +97,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 */
@@ -155,42 +161,46 @@ process_request (const char *server_address, int server_port, int proto,
 }
 
 
-/* opens a tcp or udp connection to a remote host */
+/* opens a tcp or udp connection to a remote host or local socket */
 int
 np_net_connect (const char *host_name, int port, int *sd, int proto)
 {
        struct addrinfo hints;
-       struct addrinfo *res, *res0;
+       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, &res0);
+       /* 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 {
-               res = res0;
-               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;
@@ -206,9 +216,25 @@ np_net_connect (const char *host_name, int port, int *sd, int proto)
                        }
 
                        close (*sd);
-                       res = res->ai_next;
+                       r = r->ai_next;
                }
-               freeaddrinfo (res0);
+               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)
@@ -245,7 +271,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;
        }
 
@@ -260,7 +286,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;
        }
 
@@ -269,7 +295,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
@@ -291,18 +317,23 @@ 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)
 {
 #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