Code

check_ping is now coded with -4 & -6 options to call PING or PING6 command
[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 ****************************************************************************/
32 #include "common.h"
33 #include "netutils.h"
35 int socket_timeout = DEFAULT_SOCKET_TIMEOUT; 
36 int econn_refuse_state = STATE_CRITICAL;
37 int was_refused = FALSE;
38 int address_family = AF_UNSPEC;
40 /* handles socket timeouts */
41 void
42 socket_timeout_alarm_handler (int sig)
43 {
44         if (sig == SIGALRM)
45                 printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
46         else
47                 printf ("CRITICAL - Abnormal timeout after %d seconds\n", socket_timeout);
49         exit (STATE_CRITICAL);
50 }
53 /* connects to a host on a specified TCP port, sends a string,
54    and gets a response */
55 int
56 process_tcp_request (char *server_address, int server_port,
57         char *send_buffer, char *recv_buffer, int recv_size)
58 {
59         int result;
61         result = process_request (server_address, server_port,
62                         IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
64         return result;
65 }
68 /* connects to a host on a specified UDP port, sends a string, and gets a
69     response */
70 int
71 process_udp_request (char *server_address, int server_port,
72         char *send_buffer, char *recv_buffer, int recv_size)
73 {
74         int result;
76         result = process_request (server_address, server_port,
77                         IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
79         return result;
80 }
84 /* connects to a host on a specified tcp port, sends a string, and gets a 
85          response. loops on select-recv until timeout or eof to get all of a 
86          multi-packet answer */
87 int
88 process_tcp_request2 (char *server_address, int server_port,
89         char *send_buffer, char *recv_buffer, int recv_size)
90 {
92         int result;
93         int send_result;
94         int recv_result;
95         int sd;
96         struct timeval tv;
97         fd_set readfds;
98         int recv_length = 0;
100         result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
101         if (result != STATE_OK)
102                 return STATE_CRITICAL;
104         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
105         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
106                 printf ("send() failed\n");
107                 result = STATE_WARNING;
108         }
110         while (1) {
111                 /* wait up to the number of seconds for socket timeout
112                    minus one for data from the host */
113                 tv.tv_sec = socket_timeout - 1;
114                 tv.tv_usec = 0;
115                 FD_ZERO (&readfds);
116                 FD_SET (sd, &readfds);
117                 select (sd + 1, &readfds, NULL, NULL, &tv);
119                 /* make sure some data has arrived */
120                 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
121                         if (!recv_length) {
122                                 strcpy (recv_buffer, "");
123                                 printf ("No data was recieved from host!\n");
124                                 result = STATE_WARNING;
125                         }
126                         else {                                                                          /* this one failed, but previous ones worked */
127                                 recv_buffer[recv_length] = 0;
128                         }
129                         break;
130                 }
131                 else {                                                                                  /* it has */
132                         recv_result =
133                                 recv (sd, recv_buffer + recv_length, 
134                                         recv_size - recv_length - 1, 0);
135                         if (recv_result == -1) {
136                                 /* recv failed, bail out */
137                                 strcpy (recv_buffer + recv_length, "");
138                                 result = STATE_WARNING;
139                                 break;
140                         }
141                         else if (recv_result == 0) {
142                                 /* end of file ? */
143                                 recv_buffer[recv_length] = 0;
144                                 break;
145                         }
146                         else {                                                                          /* we got data! */
147                                 recv_length += recv_result;
148                                 if (recv_length >= recv_size - 1) {
149                                         /* buffer full, we're done */
150                                         recv_buffer[recv_size - 1] = 0;
151                                         break;
152                                 }
153                         }
154                 }
155                 /* end if(!FD_ISSET(sd,&readfds)) */
156         }
157         /* end while(1) */
159         close (sd);
160         return result;
163 /* connects to a host on a specified port, sends a string, and gets a 
164    response */
165 int
166 process_request (char *server_address, int server_port, int proto,
167         char *send_buffer, char *recv_buffer, int recv_size)
169         int result;
170         int send_result;
171         int recv_result;
172         int sd;
173         struct timeval tv;
174         fd_set readfds;
176         result = STATE_OK;
178         result = my_connect (server_address, server_port, &sd, proto);
179         if (result != STATE_OK)
180                 return STATE_CRITICAL;
182         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
183         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
184                 printf ("send() failed\n");
185                 result = STATE_WARNING;
186         }
188         /* wait up to the number of seconds for socket timeout minus one 
189            for data from the host */
190         tv.tv_sec = socket_timeout - 1;
191         tv.tv_usec = 0;
192         FD_ZERO (&readfds);
193         FD_SET (sd, &readfds);
194         select (sd + 1, &readfds, NULL, NULL, &tv);
196         /* make sure some data has arrived */
197         if (!FD_ISSET (sd, &readfds)) {
198                 strcpy (recv_buffer, "");
199                 printf ("No data was recieved from host!\n");
200                 result = STATE_WARNING;
201         }
203         else {
204                 recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
205                 if (recv_result == -1) {
206                         strcpy (recv_buffer, "");
207                         if (proto != IPPROTO_TCP)
208                                 printf ("recv() failed\n");
209                         result = STATE_WARNING;
210                 }
211                 else
212                         recv_buffer[recv_result] = 0;
214                 /* terminate returned string */
215                 recv_buffer[recv_size - 1] = 0;
216         }
218         close (sd);
220         return result;
224 /* opens a connection to a remote host/tcp port */
225 int
226 my_tcp_connect (char *host_name, int port, int *sd)
228         int result;
230         result = my_connect (host_name, port, sd, IPPROTO_TCP);
232         return result;
236 /* opens a connection to a remote host/udp port */
237 int
238 my_udp_connect (char *host_name, int port, int *sd)
240         int result;
242         result = my_connect (host_name, port, sd, IPPROTO_UDP);
244         return result;
248 /* opens a tcp or udp connection to a remote host */
249 int
250 my_connect (char *host_name, int port, int *sd, int proto)
252         struct addrinfo hints;
253         struct addrinfo *res;
254         char port_str[6];
255         int result;
257         memset (&hints, 0, sizeof (hints));
258         hints.ai_family = address_family;
259         hints.ai_protocol = proto;
260         hints.ai_socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
262         snprintf (port_str, sizeof (port_str), "%d", port);
263         result = getaddrinfo (host_name, port_str, &hints, &res);
265         if (result != 0) {
266                 printf ("%s\n", gai_strerror (result));
267                 return STATE_UNKNOWN;
268         }
269         else {
270                 while (res) {
271                         /* attempt to create a socket */
272                         *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
273                                       SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
275                         if (*sd < 0) {
276                                 printf ("Socket creation failed\n");
277                                 freeaddrinfo (res);
278                                 return STATE_UNKNOWN;
279                         }
281                         /* attempt to open a connection */
282                         result = connect (*sd, res->ai_addr, res->ai_addrlen);
284                         if (result == 0) {
285                                 was_refused = FALSE;
286                                 break;
287                         }
289                         if (result < 0) {
290                                 switch (errno) {
291                                 case ECONNREFUSED:
292                                         switch (econn_refuse_state) {
293                                         case STATE_OK:
294                                         case STATE_WARNING:
295                                                 was_refused = TRUE;
296                                         }
297                                         break;
298                                 }
299                         }
301                         close (*sd);
302                         res = res->ai_next;
303                 }
304                 freeaddrinfo (res);
305         }
307         if (result == 0)
308                 return STATE_OK;
309         else if (was_refused)
310                 return econn_refuse_state;
311         else {
312                 printf ("%s\n", strerror(errno));
313                 return STATE_CRITICAL;
314         }
317 int
318 is_host (char *address)
320         if (is_addr (address) || is_hostname (address))
321                 return (TRUE);
323         return (FALSE);
326 int
327 is_addr (char *address)
329         if (is_inet_addr (address) && address_family != AF_INET6)
330                 return (TRUE);
332 #ifdef USE_IPV6
333         if (is_inet6_addr (address) && address_family != AF_INET)
334                 return (TRUE);
335 #endif
337         return (FALSE);
340 int
341 resolve_host_or_addr (char *address, int family)
343         struct addrinfo hints;
344         struct addrinfo *res;
345         int retval;
347         memset (&hints, 0, sizeof (hints));
348         hints.ai_family = family;
349         retval = getaddrinfo (address, NULL, &hints, &res);
351         if (retval != 0)
352                 return FALSE;
353         else {
354                 freeaddrinfo (res);
355                 return TRUE;
356         }
359 int
360 is_inet_addr (char *address)
362         return resolve_host_or_addr (address, AF_INET);
365 #ifdef USE_IPV6
366 int
367 is_inet6_addr (char *address)
369         return resolve_host_or_addr (address, AF_INET6);
371 #endif
373 int
374 is_hostname (char *s1)
376 #ifdef USE_IPV6
377         return resolve_host_or_addr (s1, address_family);
378 #else
379         return resolve_host_or_addr (s1, AF_INET);
380 #endif