Code

AF indepedent routines introduced.
[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 "config.h"
33 #include "common.h"
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
37 extern int socket_timeout;
38 RETSIGTYPE socket_timeout_alarm_handler (int);
40 int process_tcp_request2 (char *, int, char *, char *, int);
41 int process_tcp_request (char *, int, char *, char *, int);
42 int process_udp_request (char *, int, char *, char *, int);
43 int process_request (char *, int, int, char *, char *, int);
45 int my_tcp_connect (char *, int, int *);
46 int my_udp_connect (char *, int, int *);
47 int my_connect (char *, int, int *, int);
49 /* handles socket timeouts */
50 void
51 socket_timeout_alarm_handler (int sig)
52 {
54         printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
56         exit (STATE_CRITICAL);
57 }
60 /* connects to a host on a specified TCP port, sends a string,
61    and gets a response */
62 int
63 process_tcp_request (char *server_address, int server_port,
64         char *send_buffer, char *recv_buffer, int recv_size)
65 {
66         int result;
68         result = process_request (server_address, server_port,
69                         IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
71         return result;
72 }
75 /* connects to a host on a specified UDP port, sends a string, and gets a
76     response */
77 int
78 process_udp_request (char *server_address, int server_port,
79         char *send_buffer, char *recv_buffer, int recv_size)
80 {
81         int result;
83         result = process_request (server_address, server_port,
84                         IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
86         return result;
87 }
91 /* connects to a host on a specified tcp port, sends a string, and gets a 
92          response. loops on select-recv until timeout or eof to get all of a 
93          multi-packet answer */
94 int
95 process_tcp_request2 (char *server_address, int server_port,
96         char *send_buffer, char *recv_buffer, int recv_size)
97 {
99         int result;
100         int send_result;
101         int recv_result;
102         int sd;
103         struct timeval tv;
104         fd_set readfds;
105         int recv_length = 0;
107         result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
108         if (result != STATE_OK)
109                 return STATE_CRITICAL;
111         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
112         if (send_result != strlen (send_buffer)) {
113                 printf ("send() failed\n");
114                 result = STATE_WARNING;
115         }
117         while (1) {
118                 /* wait up to the number of seconds for socket timeout
119                    minus one for data from the host */
120                 tv.tv_sec = socket_timeout - 1;
121                 tv.tv_usec = 0;
122                 FD_ZERO (&readfds);
123                 FD_SET (sd, &readfds);
124                 select (sd + 1, &readfds, NULL, NULL, &tv);
126                 /* make sure some data has arrived */
127                 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
128                         if (!recv_length) {
129                                 strcpy (recv_buffer, "");
130                                 printf ("No data was recieved from host!\n");
131                                 result = STATE_WARNING;
132                         }
133                         else {                                                                          /* this one failed, but previous ones worked */
134                                 recv_buffer[recv_length] = 0;
135                         }
136                         break;
137                 }
138                 else {                                                                                  /* it has */
139                         recv_result =
140                                 recv (sd, recv_buffer + recv_length, 
141                                         recv_size - recv_length - 1, 0);
142                         if (recv_result == -1) {
143                                 /* recv failed, bail out */
144                                 strcpy (recv_buffer + recv_length, "");
145                                 result = STATE_WARNING;
146                                 break;
147                         }
148                         else if (recv_result == 0) {
149                                 /* end of file ? */
150                                 recv_buffer[recv_length] = 0;
151                                 break;
152                         }
153                         else {                                                                          /* we got data! */
154                                 recv_length += recv_result;
155                                 if (recv_length >= recv_size - 1) {
156                                         /* buffer full, we're done */
157                                         recv_buffer[recv_size - 1] = 0;
158                                         break;
159                                 }
160                         }
161                 }
162                 /* end if(!FD_ISSET(sd,&readfds)) */
163         }
164         /* end while(1) */
166         close (sd);
167         return result;
170 /* connects to a host on a specified port, sends a string, and gets a 
171    response */
172 int
173 process_request (char *server_address, int server_port, int proto,
174         char *send_buffer, char *recv_buffer, int recv_size)
176         int result;
177         int send_result;
178         int recv_result;
179         int sd;
180         struct timeval tv;
181         fd_set readfds;
183         result = STATE_OK;
185         result = my_connect (server_address, server_port, &sd, proto);
186         if (result != STATE_OK)
187                 return STATE_CRITICAL;
189         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
190         if (send_result != strlen (send_buffer)) {
191                 printf ("send() failed\n");
192                 result = STATE_WARNING;
193         }
195         /* wait up to the number of seconds for socket timeout minus one 
196            for data from the host */
197         tv.tv_sec = socket_timeout - 1;
198         tv.tv_usec = 0;
199         FD_ZERO (&readfds);
200         FD_SET (sd, &readfds);
201         select (sd + 1, &readfds, NULL, NULL, &tv);
203         /* make sure some data has arrived */
204         if (!FD_ISSET (sd, &readfds)) {
205                 strcpy (recv_buffer, "");
206                 printf ("No data was recieved from host!\n");
207                 result = STATE_WARNING;
208         }
210         else {
211                 recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
212                 if (recv_result == -1) {
213                         strcpy (recv_buffer, "");
214                         if (proto != IPPROTO_TCP)
215                                 printf ("recv() failed\n");
216                         result = STATE_WARNING;
217                 }
218                 else
219                         recv_buffer[recv_result] = 0;
221                 /* terminate returned string */
222                 recv_buffer[recv_size - 1] = 0;
223         }
225         close (sd);
227         return result;
231 /* opens a connection to a remote host/tcp port */
232 int
233 my_tcp_connect (char *host_name, int port, int *sd)
235         int result;
237         result = my_connect (host_name, port, sd, IPPROTO_TCP);
239         return result;
243 /* opens a connection to a remote host/udp port */
244 int
245 my_udp_connect (char *host_name, int port, int *sd)
247         int result;
249         result = my_connect (host_name, port, sd, IPPROTO_UDP);
251         return result;
255 /* opens a tcp or udp connection to a remote host */
256 int
257 my_connect (char *host_name, int port, int *sd, int proto)
259         struct addrinfo hints;
260         struct addrinfo *res;
261         struct addrinfo *ptrp;
262         char port_str[6];
263         int result;
265         memset (&hints, 0, sizeof (hints));
266         hints.ai_family = PF_UNSPEC;
267         hints.ai_protocol = proto;
269         snprintf (port_str, sizeof (port_str), "%d", port);
270         result = getaddrinfo (host_name, port_str, &hints, &res);
272         if (result != 0) {
273                 printf ("%s\n", gai_strerror (result));
274                 return STATE_UNKNOWN;
275         }
276         else {
277                 while (res) {
278                         /* attempt to create a socket */
279                         *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
280                                 SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
282                         if (*sd < 0) {
283                                 printf ("Socket creation failed\n");
284                                 freeaddrinfo (res);
285                                 return STATE_UNKNOWN;
286                         }
288                         /* attempt to open a connection */
289                         result = connect (*sd, res->ai_addr, res->ai_addrlen);
291                         if (result == 0)
292                                 break;
294                         close (*sd);
295                         res = res->ai_next;
296                 }
297                 freeaddrinfo (res);
298         }
300         if (result == 0)
301                 return STATE_OK;
302         else {
303                 printf ("%s\n", strerror(errno));
304                 return STATE_CRITICAL;
305         }