Code

Reduced DNS lookups in check_ping and netutils.c in IPv6 configurations
[nagiosplug.git] / plugins / netutils.c
1 /****************************************************************************
2 *
3 * Nagios plugins network utilities
4 *
5 * License: GPL
6 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains commons functions used in many of the plugins.
13 *
14 * License Information:
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 * $Id$
31 *
32 ****************************************************************************/
34 #define LOCAL_TIMEOUT_ALARM_HANDLER
36 #include "common.h"
37 #include "netutils.h"
39 unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; 
40 int econn_refuse_state = STATE_CRITICAL;
41 int was_refused = FALSE;
42 int address_family = AF_UNSPEC;
44 /* handles socket timeouts */
45 void
46 socket_timeout_alarm_handler (int sig)
47 {
48         if (sig == SIGALRM)
49                 printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout);
50         else
51                 printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout);
53         exit (STATE_CRITICAL);
54 }
57 /* connects to a host on a specified tcp port, sends a string, and gets a 
58          response. loops on select-recv until timeout or eof to get all of a 
59          multi-packet answer */
60 int
61 process_tcp_request2 (const char *server_address, int server_port,
62         const char *send_buffer, char *recv_buffer, int recv_size)
63 {
65         int result;
66         int send_result;
67         int recv_result;
68         int sd;
69         struct timeval tv;
70         fd_set readfds;
71         int recv_length = 0;
73         result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP);
74         if (result != STATE_OK)
75                 return STATE_CRITICAL;
77         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
78         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
79                 printf ("%s\n", _("Send failed"));
80                 result = STATE_WARNING;
81         }
83         while (1) {
84                 /* wait up to the number of seconds for socket timeout
85                    minus one for data from the host */
86                 tv.tv_sec = socket_timeout - 1;
87                 tv.tv_usec = 0;
88                 FD_ZERO (&readfds);
89                 FD_SET (sd, &readfds);
90                 select (sd + 1, &readfds, NULL, NULL, &tv);
92                 /* make sure some data has arrived */
93                 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
94                         if (!recv_length) {
95                                 strcpy (recv_buffer, "");
96                                 printf ("%s\n", _("No data was received from host!"));
97                                 result = STATE_WARNING;
98                         }
99                         else {                                                                          /* this one failed, but previous ones worked */
100                                 recv_buffer[recv_length] = 0;
101                         }
102                         break;
103                 }
104                 else {                                                                                  /* it has */
105                         recv_result =
106                                 recv (sd, recv_buffer + recv_length, 
107                                         (size_t)recv_size - recv_length - 1, 0);
108                         if (recv_result == -1) {
109                                 /* recv failed, bail out */
110                                 strcpy (recv_buffer + recv_length, "");
111                                 result = STATE_WARNING;
112                                 break;
113                         }
114                         else if (recv_result == 0) {
115                                 /* end of file ? */
116                                 recv_buffer[recv_length] = 0;
117                                 break;
118                         }
119                         else {                                                                          /* we got data! */
120                                 recv_length += recv_result;
121                                 if (recv_length >= recv_size - 1) {
122                                         /* buffer full, we're done */
123                                         recv_buffer[recv_size - 1] = 0;
124                                         break;
125                                 }
126                         }
127                 }
128                 /* end if(!FD_ISSET(sd,&readfds)) */
129         }
130         /* end while(1) */
132         close (sd);
133         return result;
137 /* connects to a host on a specified port, sends a string, and gets a 
138    response */
139 int
140 process_request (const char *server_address, int server_port, int proto,
141         const char *send_buffer, char *recv_buffer, int recv_size)
143         int result;
144         int sd;
146         result = STATE_OK;
148         result = np_net_connect (server_address, server_port, &sd, proto);
149         if (result != STATE_OK)
150                 return STATE_CRITICAL;
152         result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
154         close (sd);
156         return result;
160 /* opens a tcp or udp connection to a remote host or local socket */
161 int
162 np_net_connect (const char *host_name, int port, int *sd, int proto)
164         struct addrinfo hints;
165         struct addrinfo *r, *res;
166         struct sockaddr_un su;
167         char port_str[6];
168         int socktype, result;
170         socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
172         /* as long as it doesn't start with a '/', it's assumed a host or ip */
173         if(host_name[0] != '/'){
174                 memset (&hints, 0, sizeof (hints));
175                 hints.ai_family = address_family;
176                 hints.ai_protocol = proto;
177                 hints.ai_socktype = socktype;
179                 snprintf (port_str, sizeof (port_str), "%d", port);
180                 result = getaddrinfo (host_name, port_str, &hints, &res);
182                 if (result != 0) {
183                         printf ("%s\n", gai_strerror (result));
184                         return STATE_UNKNOWN;
185                 }
187                 r = res;
188                 while (r) {
189                         /* attempt to create a socket */
190                         *sd = socket (r->ai_family, socktype, r->ai_protocol);
192                         if (*sd < 0) {
193                                 printf ("%s\n", _("Socket creation failed"));
194                                 freeaddrinfo (r);
195                                 return STATE_UNKNOWN;
196                         }
198                         /* attempt to open a connection */
199                         result = connect (*sd, r->ai_addr, r->ai_addrlen);
201                         if (result == 0) {
202                                 was_refused = FALSE;
203                                 break;
204                         }
206                         if (result < 0) {
207                                 switch (errno) {
208                                 case ECONNREFUSED:
209                                         was_refused = TRUE;
210                                         break;
211                                 }
212                         }
214                         close (*sd);
215                         r = r->ai_next;
216                 }
217                 freeaddrinfo (res);
218         } 
219         /* else the hostname is interpreted as a path to a unix socket */
220         else {
221                 if(strlen(host_name) >= UNIX_PATH_MAX){
222                         die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
223                 }
224                 memset(&su, 0, sizeof(su));
225                 su.sun_family = AF_UNIX;
226                 strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
227                 *sd = socket(PF_UNIX, SOCK_STREAM, 0);
228                 if(sd < 0){
229                         die(STATE_UNKNOWN, _("Socket creation failed"));
230                 }
231                 result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
232                 if (result < 0 && errno == ECONNREFUSED)
233                         was_refused = TRUE;
234         }
236         if (result == 0)
237                 return STATE_OK;
238         else if (was_refused) {
239                 switch (econn_refuse_state) { /* a user-defined expected outcome */
240                 case STATE_OK:       
241                 case STATE_WARNING:  /* user wants WARN or OK on refusal */
242                         return econn_refuse_state;
243                         break;
244                 case STATE_CRITICAL: /* user did not set econn_refuse_state */
245                         printf ("%s\n", strerror(errno));
246                         return econn_refuse_state;
247                         break;
248                 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
249                         return STATE_UNKNOWN;
250                         break;
251                 }
252         }
253         else {
254                 printf ("%s\n", strerror(errno));
255                 return STATE_CRITICAL;
256         }
259 int
260 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
262         int result = STATE_OK;
263         int send_result;
264         int recv_result;
265         struct timeval tv;
266         fd_set readfds;
268         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
269         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
270                 printf ("%s\n", _("Send failed"));
271                 result = STATE_WARNING;
272         }
274         /* wait up to the number of seconds for socket timeout minus one 
275            for data from the host */
276         tv.tv_sec = socket_timeout - 1;
277         tv.tv_usec = 0;
278         FD_ZERO (&readfds);
279         FD_SET (sd, &readfds);
280         select (sd + 1, &readfds, NULL, NULL, &tv);
282         /* make sure some data has arrived */
283         if (!FD_ISSET (sd, &readfds)) {
284                 strcpy (recv_buffer, "");
285                 printf ("%s\n", _("No data was received from host!"));
286                 result = STATE_WARNING;
287         }
289         else {
290                 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
291                 if (recv_result == -1) {
292                         strcpy (recv_buffer, "");
293                         if (proto != IPPROTO_TCP)
294                                 printf ("%s\n", _("Receive failed"));
295                         result = STATE_WARNING;
296                 }
297                 else
298                         recv_buffer[recv_result] = 0;
300                 /* die returned string */
301                 recv_buffer[recv_size - 1] = 0;
302         }
303         return result;
307 int
308 is_host (const char *address)
310         if (is_addr (address) || is_hostname (address))
311                 return (TRUE);
313         return (FALSE);
316 void
317 host_or_die(const char *str)
319         if(!str || (!is_addr(str) && !is_hostname(str)))
320                 usage_va(_("Invalid hostname/address - %s"), str);
323 int
324 is_addr (const char *address)
326 #ifdef USE_IPV6
327         if (address_family == AF_INET && is_inet_addr (address))
328                 return TRUE;
329         else if (address_family == AF_INET6 && is_inet6_addr (address)) 
330                 return TRUE;
331 #else
332         if (is_inet_addr (address))
333                 return (TRUE);
334 #endif
336         return (FALSE);
339 int
340 resolve_host_or_addr (const char *address, int family)
342         struct addrinfo hints;
343         struct addrinfo *res;
344         int retval;
346         memset (&hints, 0, sizeof (hints));
347         hints.ai_family = family;
348         retval = getaddrinfo (address, NULL, &hints, &res);
350         if (retval != 0)
351                 return FALSE;
352         else {
353                 freeaddrinfo (res);
354                 return TRUE;
355         }