diff --git a/plugins/netutils.c b/plugins/netutils.c
index ce8b428b2ec8009680c8d36f980bb8e3e2a9d425..c99983f03bd9e68a2cbeb348efce2d0b0a1ed1e3 100644 (file)
--- a/plugins/netutils.c
+++ b/plugins/netutils.c
#include "common.h"
#include "netutils.h"
-int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
+unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
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)
{
-
- printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
+ if (sig == SIGALRM)
+ printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
+ else
+ 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 (char *server_address, int server_port,
- char *send_buffer, char *recv_buffer, int recv_size)
+process_tcp_request (const char *server_address, int server_port,
+ const char *send_buffer, char *recv_buffer, int recv_size)
{
int result;
/* connects to a host on a specified UDP port, sends a string, and gets a
response */
int
-process_udp_request (char *server_address, int server_port,
- char *send_buffer, char *recv_buffer, int recv_size)
+process_udp_request (const char *server_address, int server_port,
+ const char *send_buffer, char *recv_buffer, int recv_size)
{
int result;
response. loops on select-recv until timeout or eof to get all of a
multi-packet answer */
int
-process_tcp_request2 (char *server_address, int server_port,
- char *send_buffer, char *recv_buffer, int recv_size)
+process_tcp_request2 (const char *server_address, int server_port,
+ const char *send_buffer, char *recv_buffer, int recv_size)
{
int result;
return STATE_CRITICAL;
send_result = send (sd, send_buffer, strlen (send_buffer), 0);
- if (send_result != strlen (send_buffer)) {
+ if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
printf ("send() failed\n");
result = STATE_WARNING;
}
if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
if (!recv_length) {
strcpy (recv_buffer, "");
- printf ("No data was recieved from host!\n");
+ printf ("No data was received from host!\n");
result = STATE_WARNING;
}
else { /* this one failed, but previous ones worked */
else { /* it has */
recv_result =
recv (sd, recv_buffer + recv_length,
- recv_size - recv_length - 1, 0);
+ (size_t)recv_size - recv_length - 1, 0);
if (recv_result == -1) {
/* recv failed, bail out */
strcpy (recv_buffer + recv_length, "");
/* connects to a host on a specified port, sends a string, and gets a
response */
int
-process_request (char *server_address, int server_port, int proto,
- char *send_buffer, char *recv_buffer, int recv_size)
+process_request (const char *server_address, int server_port, int proto,
+ const char *send_buffer, char *recv_buffer, int recv_size)
{
int result;
- int send_result;
- int recv_result;
int sd;
- struct timeval tv;
- fd_set readfds;
result = STATE_OK;
if (result != STATE_OK)
return STATE_CRITICAL;
- send_result = send (sd, send_buffer, strlen (send_buffer), 0);
- if (send_result != strlen (send_buffer)) {
- printf ("send() failed\n");
- 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 ("No data was recieved from host!\n");
- result = STATE_WARNING;
- }
-
- else {
- recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
- if (recv_result == -1) {
- strcpy (recv_buffer, "");
- if (proto != IPPROTO_TCP)
- printf ("recv() failed\n");
- result = STATE_WARNING;
- }
- else
- recv_buffer[recv_result] = 0;
-
- /* terminate returned string */
- recv_buffer[recv_size - 1] = 0;
- }
+ result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
close (sd);
/* opens a connection to a remote host/tcp port */
int
-my_tcp_connect (char *host_name, int port, int *sd)
+my_tcp_connect (const char *host_name, int port, int *sd)
{
int result;
/* opens a connection to a remote host/udp port */
int
-my_udp_connect (char *host_name, int port, int *sd)
+my_udp_connect (const char *host_name, int port, int *sd)
{
int result;
/* opens a tcp or udp connection to a remote host */
-int
-my_connect (char *host_name, int port, int *sd, int proto)
+static int
+my_connect (const char *host_name, int port, int *sd, int proto)
{
struct addrinfo hints;
struct addrinfo *res;
- struct addrinfo *ptrp;
char port_str[6];
int result;
memset (&hints, 0, sizeof (hints));
- hints.ai_family = PF_UNSPEC;
+ hints.ai_family = address_family;
hints.ai_protocol = proto;
+ hints.ai_socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
snprintf (port_str, sizeof (port_str), "%d", port);
result = getaddrinfo (host_name, port_str, &hints, &res);
while (res) {
/* attempt to create a socket */
*sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
- SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
+ SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
if (*sd < 0) {
printf ("Socket creation failed\n");
if (result < 0) {
switch (errno) {
- case ECONNREFUSED:
- switch (econn_refuse_state) {
- case STATE_OK:
- case STATE_WARNING:
- was_refused = TRUE;
- }
- break;
+ case ECONNREFUSED:
+ was_refused = TRUE;
+ break;
}
}
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
-is_host (char *address)
+send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
{
- if (is_addr (address) || is_hostname (address))
- return (TRUE);
+ return send_request (sd, IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
+}
- return (FALSE);
+
+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
-is_addr (char *address)
+send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
+{
+ 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 ("send() failed\n");
+ 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 ("No data was received from host!\n");
+ 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 ("recv() failed\n");
+ result = STATE_WARNING;
+ }
+ else
+ recv_buffer[recv_result] = 0;
+
+ /* die returned string */
+ recv_buffer[recv_size - 1] = 0;
+ }
+ return result;
+}
+
+
+int
+is_host (const char *address)
+{
+ if (is_addr (address) || is_hostname (address))
+ return (TRUE);
+
+ return (FALSE);
+}
+
+int
+is_addr (const char *address)
{
#ifdef USE_IPV6
- if (is_inet_addr (address) || is_inet6_addr (address))
+ if (is_inet_addr (address) && address_family != AF_INET6)
#else
- if (is_inet_addr (address))
+ if (is_inet_addr (address))
#endif
- return (TRUE);
+ return (TRUE);
- return (FALSE);
+#ifdef USE_IPV6
+ if (is_inet6_addr (address) && address_family != AF_INET)
+ return (TRUE);
+#endif
+
+ return (FALSE);
}
int
-resolve_host_or_addr (char *address, int family)
+resolve_host_or_addr (const char *address, int family)
{
- 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;
- }
+ 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;
+ }
}
int
-is_inet_addr (char *address)
+is_inet_addr (const char *address)
{
- return resolve_host_or_addr (address, AF_INET);
+ return resolve_host_or_addr (address, AF_INET);
}
#ifdef USE_IPV6
int
-is_inet6_addr (char *address)
+is_inet6_addr (const char *address)
{
- return resolve_host_or_addr (address, AF_INET6);
+ return resolve_host_or_addr (address, AF_INET6);
}
#endif
int
-is_hostname (char *s1)
+is_hostname (const char *s1)
{
#ifdef USE_IPV6
- return resolve_host_or_addr (s1, AF_UNSPEC);
+ return resolve_host_or_addr (s1, address_family);
#else
- return resolve_host_or_addr (s1, AF_INET);
+ return resolve_host_or_addr (s1, AF_INET);
#endif
}