Code

was making up to 34 separate tcp connections - now we open one and reuse
[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 sd;
173         result = STATE_OK;
175         result = my_connect (server_address, server_port, &sd, proto);
176         if (result != STATE_OK)
177                 return STATE_CRITICAL;
179         result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
181         close (sd);
183         return result;
187 /* opens a connection to a remote host/tcp port */
188 int
189 my_tcp_connect (const char *host_name, int port, int *sd)
191         int result;
193         result = my_connect (host_name, port, sd, IPPROTO_TCP);
195         return result;
199 /* opens a connection to a remote host/udp port */
200 int
201 my_udp_connect (const char *host_name, int port, int *sd)
203         int result;
205         result = my_connect (host_name, port, sd, IPPROTO_UDP);
207         return result;
211 /* opens a tcp or udp connection to a remote host */
212 static int
213 my_connect (const char *host_name, int port, int *sd, int proto)
215         struct addrinfo hints;
216         struct addrinfo *res;
217         char port_str[6];
218         int result;
220         memset (&hints, 0, sizeof (hints));
221         hints.ai_family = address_family;
222         hints.ai_protocol = proto;
223         hints.ai_socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
225         snprintf (port_str, sizeof (port_str), "%d", port);
226         result = getaddrinfo (host_name, port_str, &hints, &res);
228         if (result != 0) {
229                 printf ("%s\n", gai_strerror (result));
230                 return STATE_UNKNOWN;
231         }
232         else {
233                 while (res) {
234                         /* attempt to create a socket */
235                         *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
236                                       SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
238                         if (*sd < 0) {
239                                 printf ("Socket creation failed\n");
240                                 freeaddrinfo (res);
241                                 return STATE_UNKNOWN;
242                         }
244                         /* attempt to open a connection */
245                         result = connect (*sd, res->ai_addr, res->ai_addrlen);
247                         if (result == 0) {
248                                 was_refused = FALSE;
249                                 break;
250                         }
252                         if (result < 0) {
253                                 switch (errno) {
254                                 case ECONNREFUSED:
255                                         switch (econn_refuse_state) {
256                                         case STATE_OK:
257                                         case STATE_WARNING:
258                                                 was_refused = TRUE;
259                                         }
260                                         break;
261                                 }
262                         }
264                         close (*sd);
265                         res = res->ai_next;
266                 }
267                 freeaddrinfo (res);
268         }
270         if (result == 0)
271                 return STATE_OK;
272         else if (was_refused)
273                 return econn_refuse_state;
274         else {
275                 printf ("%s\n", strerror(errno));
276                 return STATE_CRITICAL;
277         }
281 int
282 send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
284         return send_request (sd, IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
288 int
289 send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
291         return send_request (sd, IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
295 int
296 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
298         int result;
299         int send_result;
300         int recv_result;
301         struct timeval tv;
302         fd_set readfds;
304         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
305         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
306                 printf ("send() failed\n");
307                 result = STATE_WARNING;
308         }
310         /* wait up to the number of seconds for socket timeout minus one 
311            for data from the host */
312         tv.tv_sec = socket_timeout - 1;
313         tv.tv_usec = 0;
314         FD_ZERO (&readfds);
315         FD_SET (sd, &readfds);
316         select (sd + 1, &readfds, NULL, NULL, &tv);
318         /* make sure some data has arrived */
319         if (!FD_ISSET (sd, &readfds)) {
320                 strcpy (recv_buffer, "");
321                 printf ("No data was received from host!\n");
322                 result = STATE_WARNING;
323         }
325         else {
326                 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
327                 if (recv_result == -1) {
328                         strcpy (recv_buffer, "");
329                         if (proto != IPPROTO_TCP)
330                                 printf ("recv() failed\n");
331                         result = STATE_WARNING;
332                 }
333                 else
334                         recv_buffer[recv_result] = 0;
336                 /* die returned string */
337                 recv_buffer[recv_size - 1] = 0;
338         }
339         return result;
343 int
344 is_host (const char *address)
346         if (is_addr (address) || is_hostname (address))
347                 return (TRUE);
349         return (FALSE);
352 int
353 is_addr (const char *address)
355 #ifdef USE_IPV6
356         if (is_inet_addr (address) && address_family != AF_INET6)
357 #else
358         if (is_inet_addr (address))
359 #endif
360                 return (TRUE);
362 #ifdef USE_IPV6
363         if (is_inet6_addr (address) && address_family != AF_INET)
364                 return (TRUE);
365 #endif
367         return (FALSE);
370 int
371 resolve_host_or_addr (const char *address, int family)
373         struct addrinfo hints;
374         struct addrinfo *res;
375         int retval;
377         memset (&hints, 0, sizeof (hints));
378         hints.ai_family = family;
379         retval = getaddrinfo (address, NULL, &hints, &res);
381         if (retval != 0)
382                 return FALSE;
383         else {
384                 freeaddrinfo (res);
385                 return TRUE;
386         }
389 int
390 is_inet_addr (const char *address)
392         return resolve_host_or_addr (address, AF_INET);
395 #ifdef USE_IPV6
396 int
397 is_inet6_addr (const char *address)
399         return resolve_host_or_addr (address, AF_INET6);
401 #endif
403 int
404 is_hostname (const char *s1)
406 #ifdef USE_IPV6
407         return resolve_host_or_addr (s1, address_family);
408 #else
409         return resolve_host_or_addr (s1, AF_INET);
410 #endif