Code

code cleanup to clear strict compiler warnings
[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;
39 /* handles socket timeouts */
40 void
41 socket_timeout_alarm_handler (int sig)
42 {
43         if (sig == SIGALRM)
44                 printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
45         else
46                 printf ("CRITICAL - Abnormal timeout after %d seconds\n", socket_timeout);
48         exit (STATE_CRITICAL);
49 }
52 /* connects to a host on a specified TCP port, sends a string,
53    and gets a response */
54 int
55 process_tcp_request (char *server_address, int server_port,
56         char *send_buffer, char *recv_buffer, int recv_size)
57 {
58         int result;
60         result = process_request (server_address, server_port,
61                         IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
63         return result;
64 }
67 /* connects to a host on a specified UDP port, sends a string, and gets a
68     response */
69 int
70 process_udp_request (char *server_address, int server_port,
71         char *send_buffer, char *recv_buffer, int recv_size)
72 {
73         int result;
75         result = process_request (server_address, server_port,
76                         IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
78         return result;
79 }
83 /* connects to a host on a specified tcp port, sends a string, and gets a 
84          response. loops on select-recv until timeout or eof to get all of a 
85          multi-packet answer */
86 int
87 process_tcp_request2 (char *server_address, int server_port,
88         char *send_buffer, char *recv_buffer, int recv_size)
89 {
91         int result;
92         int send_result;
93         int recv_result;
94         int sd;
95         struct timeval tv;
96         fd_set readfds;
97         int recv_length = 0;
99         result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
100         if (result != STATE_OK)
101                 return STATE_CRITICAL;
103         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
104         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
105                 printf ("send() failed\n");
106                 result = STATE_WARNING;
107         }
109         while (1) {
110                 /* wait up to the number of seconds for socket timeout
111                    minus one for data from the host */
112                 tv.tv_sec = socket_timeout - 1;
113                 tv.tv_usec = 0;
114                 FD_ZERO (&readfds);
115                 FD_SET (sd, &readfds);
116                 select (sd + 1, &readfds, NULL, NULL, &tv);
118                 /* make sure some data has arrived */
119                 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
120                         if (!recv_length) {
121                                 strcpy (recv_buffer, "");
122                                 printf ("No data was recieved from host!\n");
123                                 result = STATE_WARNING;
124                         }
125                         else {                                                                          /* this one failed, but previous ones worked */
126                                 recv_buffer[recv_length] = 0;
127                         }
128                         break;
129                 }
130                 else {                                                                                  /* it has */
131                         recv_result =
132                                 recv (sd, recv_buffer + recv_length, 
133                                         recv_size - recv_length - 1, 0);
134                         if (recv_result == -1) {
135                                 /* recv failed, bail out */
136                                 strcpy (recv_buffer + recv_length, "");
137                                 result = STATE_WARNING;
138                                 break;
139                         }
140                         else if (recv_result == 0) {
141                                 /* end of file ? */
142                                 recv_buffer[recv_length] = 0;
143                                 break;
144                         }
145                         else {                                                                          /* we got data! */
146                                 recv_length += recv_result;
147                                 if (recv_length >= recv_size - 1) {
148                                         /* buffer full, we're done */
149                                         recv_buffer[recv_size - 1] = 0;
150                                         break;
151                                 }
152                         }
153                 }
154                 /* end if(!FD_ISSET(sd,&readfds)) */
155         }
156         /* end while(1) */
158         close (sd);
159         return result;
162 /* connects to a host on a specified port, sends a string, and gets a 
163    response */
164 int
165 process_request (char *server_address, int server_port, int proto,
166         char *send_buffer, char *recv_buffer, int recv_size)
168         int result;
169         int send_result;
170         int recv_result;
171         int sd;
172         struct timeval tv;
173         fd_set readfds;
175         result = STATE_OK;
177         result = my_connect (server_address, server_port, &sd, proto);
178         if (result != STATE_OK)
179                 return STATE_CRITICAL;
181         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
182         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
183                 printf ("send() failed\n");
184                 result = STATE_WARNING;
185         }
187         /* wait up to the number of seconds for socket timeout minus one 
188            for data from the host */
189         tv.tv_sec = socket_timeout - 1;
190         tv.tv_usec = 0;
191         FD_ZERO (&readfds);
192         FD_SET (sd, &readfds);
193         select (sd + 1, &readfds, NULL, NULL, &tv);
195         /* make sure some data has arrived */
196         if (!FD_ISSET (sd, &readfds)) {
197                 strcpy (recv_buffer, "");
198                 printf ("No data was recieved from host!\n");
199                 result = STATE_WARNING;
200         }
202         else {
203                 recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
204                 if (recv_result == -1) {
205                         strcpy (recv_buffer, "");
206                         if (proto != IPPROTO_TCP)
207                                 printf ("recv() failed\n");
208                         result = STATE_WARNING;
209                 }
210                 else
211                         recv_buffer[recv_result] = 0;
213                 /* terminate returned string */
214                 recv_buffer[recv_size - 1] = 0;
215         }
217         close (sd);
219         return result;
223 /* opens a connection to a remote host/tcp port */
224 int
225 my_tcp_connect (char *host_name, int port, int *sd)
227         int result;
229         result = my_connect (host_name, port, sd, IPPROTO_TCP);
231         return result;
235 /* opens a connection to a remote host/udp port */
236 int
237 my_udp_connect (char *host_name, int port, int *sd)
239         int result;
241         result = my_connect (host_name, port, sd, IPPROTO_UDP);
243         return result;
247 /* opens a tcp or udp connection to a remote host */
248 int
249 my_connect (char *host_name, int port, int *sd, int proto)
251         struct addrinfo hints;
252         struct addrinfo *res;
253         char port_str[6];
254         int result;
256         memset (&hints, 0, sizeof (hints));
257         hints.ai_family = PF_UNSPEC;
258         hints.ai_protocol = proto;
260         snprintf (port_str, sizeof (port_str), "%d", port);
261         result = getaddrinfo (host_name, port_str, &hints, &res);
263         if (result != 0) {
264                 printf ("%s\n", gai_strerror (result));
265                 return STATE_UNKNOWN;
266         }
267         else {
268                 while (res) {
269                         /* attempt to create a socket */
270                         *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
271                                       SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
273                         if (*sd < 0) {
274                                 printf ("Socket creation failed\n");
275                                 freeaddrinfo (res);
276                                 return STATE_UNKNOWN;
277                         }
279                         /* attempt to open a connection */
280                         result = connect (*sd, res->ai_addr, res->ai_addrlen);
282                         if (result == 0) {
283                                 was_refused = FALSE;
284                                 break;
285                         }
287                         if (result < 0) {
288                                 switch (errno) {
289                                 case ECONNREFUSED:
290                                         switch (econn_refuse_state) {
291                                         case STATE_OK:
292                                         case STATE_WARNING:
293                                                 was_refused = TRUE;
294                                         }
295                                         break;
296                                 }
297                         }
299                         close (*sd);
300                         res = res->ai_next;
301                 }
302                 freeaddrinfo (res);
303         }
305         if (result == 0)
306                 return STATE_OK;
307         else if (was_refused)
308                 return econn_refuse_state;
309         else {
310                 printf ("%s\n", strerror(errno));
311                 return STATE_CRITICAL;
312         }
315 int
316 is_host (char *address)
318         if (is_addr (address) || is_hostname (address))
319                 return (TRUE);
321         return (FALSE);
324 int
325 is_addr (char *address)
327         if (is_inet_addr (address))
328                 return (TRUE);
330 #ifdef USE_IPV6
331         if (is_inet6_addr (address))
332                 return (TRUE);
333 #endif
335         return (FALSE);
338 int
339 resolve_host_or_addr (char *address, int family)
341         struct addrinfo hints;
342         struct addrinfo *res;
343         int retval;
345         memset (&hints, 0, sizeof (hints));
346         hints.ai_family = family;
347         retval = getaddrinfo (address, NULL, &hints, &res);
349         if (retval != 0)
350                 return FALSE;
351         else {
352                 freeaddrinfo (res);
353                 return TRUE;
354         }
357 int
358 is_inet_addr (char *address)
360         return resolve_host_or_addr (address, AF_INET);
363 #ifdef USE_IPV6
364 int
365 is_inet6_addr (char *address)
367         return resolve_host_or_addr (address, AF_INET6);
369 #endif
371 int
372 is_hostname (char *s1)
374 #ifdef USE_IPV6
375         return resolve_host_or_addr (s1, AF_UNSPEC);
376 #else
377         return resolve_host_or_addr (s1, AF_INET);
378 #endif