Code

spell fix "received"
[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 unsigned 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 static int my_connect(const char *address, int port, int *sd, int proto);
41 /* handles socket timeouts */
42 void
43 socket_timeout_alarm_handler (int sig)
44 {
45         if (sig == SIGALRM)
46                 printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
47         else
48                 printf ("CRITICAL - Abnormal timeout after %d seconds\n", socket_timeout);
50         exit (STATE_CRITICAL);
51 }
54 /* connects to a host on a specified TCP port, sends a string,
55    and gets a response */
56 int
57 process_tcp_request (const char *server_address, int server_port,
58         const char *send_buffer, char *recv_buffer, int recv_size)
59 {
60         int result;
62         result = process_request (server_address, server_port,
63                         IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
65         return result;
66 }
69 /* connects to a host on a specified UDP port, sends a string, and gets a
70     response */
71 int
72 process_udp_request (const char *server_address, int server_port,
73         const char *send_buffer, char *recv_buffer, int recv_size)
74 {
75         int result;
77         result = process_request (server_address, server_port,
78                         IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
80         return result;
81 }
85 /* connects to a host on a specified tcp port, sends a string, and gets a 
86          response. loops on select-recv until timeout or eof to get all of a 
87          multi-packet answer */
88 int
89 process_tcp_request2 (const char *server_address, int server_port,
90         const char *send_buffer, char *recv_buffer, int recv_size)
91 {
93         int result;
94         int send_result;
95         int recv_result;
96         int sd;
97         struct timeval tv;
98         fd_set readfds;
99         int recv_length = 0;
101         result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
102         if (result != STATE_OK)
103                 return STATE_CRITICAL;
105         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
106         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
107                 printf ("send() failed\n");
108                 result = STATE_WARNING;
109         }
111         while (1) {
112                 /* wait up to the number of seconds for socket timeout
113                    minus one for data from the host */
114                 tv.tv_sec = socket_timeout - 1;
115                 tv.tv_usec = 0;
116                 FD_ZERO (&readfds);
117                 FD_SET (sd, &readfds);
118                 select (sd + 1, &readfds, NULL, NULL, &tv);
120                 /* make sure some data has arrived */
121                 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
122                         if (!recv_length) {
123                                 strcpy (recv_buffer, "");
124                                 printf ("No data was received from host!\n");
125                                 result = STATE_WARNING;
126                         }
127                         else {                                                                          /* this one failed, but previous ones worked */
128                                 recv_buffer[recv_length] = 0;
129                         }
130                         break;
131                 }
132                 else {                                                                                  /* it has */
133                         recv_result =
134                                 recv (sd, recv_buffer + recv_length, 
135                                         (size_t)recv_size - recv_length - 1, 0);
136                         if (recv_result == -1) {
137                                 /* recv failed, bail out */
138                                 strcpy (recv_buffer + recv_length, "");
139                                 result = STATE_WARNING;
140                                 break;
141                         }
142                         else if (recv_result == 0) {
143                                 /* end of file ? */
144                                 recv_buffer[recv_length] = 0;
145                                 break;
146                         }
147                         else {                                                                          /* we got data! */
148                                 recv_length += recv_result;
149                                 if (recv_length >= recv_size - 1) {
150                                         /* buffer full, we're done */
151                                         recv_buffer[recv_size - 1] = 0;
152                                         break;
153                                 }
154                         }
155                 }
156                 /* end if(!FD_ISSET(sd,&readfds)) */
157         }
158         /* end while(1) */
160         close (sd);
161         return result;
164 /* connects to a host on a specified port, sends a string, and gets a 
165    response */
166 int
167 process_request (const char *server_address, int server_port, int proto,
168         const char *send_buffer, char *recv_buffer, int recv_size)
170         int result;
171         int send_result;
172         int recv_result;
173         int sd;
174         struct timeval tv;
175         fd_set readfds;
177         result = STATE_OK;
179         result = my_connect (server_address, server_port, &sd, proto);
180         if (result != STATE_OK)
181                 return STATE_CRITICAL;
183         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
184         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
185                 printf ("send() failed\n");
186                 result = STATE_WARNING;
187         }
189         /* wait up to the number of seconds for socket timeout minus one 
190            for data from the host */
191         tv.tv_sec = socket_timeout - 1;
192         tv.tv_usec = 0;
193         FD_ZERO (&readfds);
194         FD_SET (sd, &readfds);
195         select (sd + 1, &readfds, NULL, NULL, &tv);
197         /* make sure some data has arrived */
198         if (!FD_ISSET (sd, &readfds)) {
199                 strcpy (recv_buffer, "");
200                 printf ("No data was received from host!\n");
201                 result = STATE_WARNING;
202         }
204         else {
205                 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
206                 if (recv_result == -1) {
207                         strcpy (recv_buffer, "");
208                         if (proto != IPPROTO_TCP)
209                                 printf ("recv() failed\n");
210                         result = STATE_WARNING;
211                 }
212                 else
213                         recv_buffer[recv_result] = 0;
215                 /* die returned string */
216                 recv_buffer[recv_size - 1] = 0;
217         }
219         close (sd);
221         return result;
225 /* opens a connection to a remote host/tcp port */
226 int
227 my_tcp_connect (const char *host_name, int port, int *sd)
229         int result;
231         result = my_connect (host_name, port, sd, IPPROTO_TCP);
233         return result;
237 /* opens a connection to a remote host/udp port */
238 int
239 my_udp_connect (const char *host_name, int port, int *sd)
241         int result;
243         result = my_connect (host_name, port, sd, IPPROTO_UDP);
245         return result;
249 /* opens a tcp or udp connection to a remote host */
250 static int
251 my_connect (const char *host_name, int port, int *sd, int proto)
253         struct addrinfo hints;
254         struct addrinfo *res;
255         char port_str[6];
256         int result;
258         memset (&hints, 0, sizeof (hints));
259         hints.ai_family = address_family;
260         hints.ai_protocol = proto;
261         hints.ai_socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
263         snprintf (port_str, sizeof (port_str), "%d", port);
264         result = getaddrinfo (host_name, port_str, &hints, &res);
266         if (result != 0) {
267                 printf ("%s\n", gai_strerror (result));
268                 return STATE_UNKNOWN;
269         }
270         else {
271                 while (res) {
272                         /* attempt to create a socket */
273                         *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
274                                       SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
276                         if (*sd < 0) {
277                                 printf ("Socket creation failed\n");
278                                 freeaddrinfo (res);
279                                 return STATE_UNKNOWN;
280                         }
282                         /* attempt to open a connection */
283                         result = connect (*sd, res->ai_addr, res->ai_addrlen);
285                         if (result == 0) {
286                                 was_refused = FALSE;
287                                 break;
288                         }
290                         if (result < 0) {
291                                 switch (errno) {
292                                 case ECONNREFUSED:
293                                         switch (econn_refuse_state) {
294                                         case STATE_OK:
295                                         case STATE_WARNING:
296                                                 was_refused = TRUE;
297                                         }
298                                         break;
299                                 }
300                         }
302                         close (*sd);
303                         res = res->ai_next;
304                 }
305                 freeaddrinfo (res);
306         }
308         if (result == 0)
309                 return STATE_OK;
310         else if (was_refused)
311                 return econn_refuse_state;
312         else {
313                 printf ("%s\n", strerror(errno));
314                 return STATE_CRITICAL;
315         }
318 int
319 is_host (const char *address)
321         if (is_addr (address) || is_hostname (address))
322                 return (TRUE);
324         return (FALSE);
327 int
328 is_addr (const char *address)
330 #ifdef USE_IPV6
331         if (is_inet_addr (address) && address_family != AF_INET6)
332 #else
333         if (is_inet_addr (address))
334 #endif
335                 return (TRUE);
337 #ifdef USE_IPV6
338         if (is_inet6_addr (address) && address_family != AF_INET)
339                 return (TRUE);
340 #endif
342         return (FALSE);
345 int
346 resolve_host_or_addr (const char *address, int family)
348         struct addrinfo hints;
349         struct addrinfo *res;
350         int retval;
352         memset (&hints, 0, sizeof (hints));
353         hints.ai_family = family;
354         retval = getaddrinfo (address, NULL, &hints, &res);
356         if (retval != 0)
357                 return FALSE;
358         else {
359                 freeaddrinfo (res);
360                 return TRUE;
361         }
364 int
365 is_inet_addr (const char *address)
367         return resolve_host_or_addr (address, AF_INET);
370 #ifdef USE_IPV6
371 int
372 is_inet6_addr (const char *address)
374         return resolve_host_or_addr (address, AF_INET6);
376 #endif
378 int
379 is_hostname (const char *s1)
381 #ifdef USE_IPV6
382         return resolve_host_or_addr (s1, address_family);
383 #else
384         return resolve_host_or_addr (s1, AF_INET);
385 #endif