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;
162 }
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)
169 {
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;
184 }
187 /* opens a connection to a remote host/tcp port */
188 int
189 my_tcp_connect (const char *host_name, int port, int *sd)
190 {
191 int result;
193 result = my_connect (host_name, port, sd, IPPROTO_TCP);
195 return result;
196 }
199 /* opens a connection to a remote host/udp port */
200 int
201 my_udp_connect (const char *host_name, int port, int *sd)
202 {
203 int result;
205 result = my_connect (host_name, port, sd, IPPROTO_UDP);
207 return result;
208 }
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)
214 {
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 was_refused = TRUE;
256 break;
257 }
258 }
260 close (*sd);
261 res = res->ai_next;
262 }
263 freeaddrinfo (res);
264 }
266 if (result == 0)
267 return STATE_OK;
268 else if (was_refused) {
269 switch (econn_refuse_state) { /* a user-defined expected outcome */
270 case STATE_OK:
271 case STATE_WARNING: /* user wants WARN or OK on refusal */
272 return econn_refuse_state;
273 break;
274 case STATE_CRITICAL: /* user did not set econn_refuse_state */
275 printf ("%s\n", strerror(errno));
276 return econn_refuse_state;
277 break;
278 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
279 return STATE_UNKNOWN;
280 break;
281 }
282 }
283 else {
284 printf ("%s\n", strerror(errno));
285 return STATE_CRITICAL;
286 }
287 }
290 int
291 send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
292 {
293 return send_request (sd, IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
294 }
297 int
298 send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
299 {
300 return send_request (sd, IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
301 }
304 int
305 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
306 {
307 int result = STATE_OK;
308 int send_result;
309 int recv_result;
310 struct timeval tv;
311 fd_set readfds;
313 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
314 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
315 printf ("send() failed\n");
316 result = STATE_WARNING;
317 }
319 /* wait up to the number of seconds for socket timeout minus one
320 for data from the host */
321 tv.tv_sec = socket_timeout - 1;
322 tv.tv_usec = 0;
323 FD_ZERO (&readfds);
324 FD_SET (sd, &readfds);
325 select (sd + 1, &readfds, NULL, NULL, &tv);
327 /* make sure some data has arrived */
328 if (!FD_ISSET (sd, &readfds)) {
329 strcpy (recv_buffer, "");
330 printf ("No data was received from host!\n");
331 result = STATE_WARNING;
332 }
334 else {
335 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
336 if (recv_result == -1) {
337 strcpy (recv_buffer, "");
338 if (proto != IPPROTO_TCP)
339 printf ("recv() failed\n");
340 result = STATE_WARNING;
341 }
342 else
343 recv_buffer[recv_result] = 0;
345 /* die returned string */
346 recv_buffer[recv_size - 1] = 0;
347 }
348 return result;
349 }
352 int
353 is_host (const char *address)
354 {
355 if (is_addr (address) || is_hostname (address))
356 return (TRUE);
358 return (FALSE);
359 }
361 int
362 is_addr (const char *address)
363 {
364 #ifdef USE_IPV6
365 if (is_inet_addr (address) && address_family != AF_INET6)
366 #else
367 if (is_inet_addr (address))
368 #endif
369 return (TRUE);
371 #ifdef USE_IPV6
372 if (is_inet6_addr (address) && address_family != AF_INET)
373 return (TRUE);
374 #endif
376 return (FALSE);
377 }
379 int
380 resolve_host_or_addr (const char *address, int family)
381 {
382 struct addrinfo hints;
383 struct addrinfo *res;
384 int retval;
386 memset (&hints, 0, sizeof (hints));
387 hints.ai_family = family;
388 retval = getaddrinfo (address, NULL, &hints, &res);
390 if (retval != 0)
391 return FALSE;
392 else {
393 freeaddrinfo (res);
394 return TRUE;
395 }
396 }
398 int
399 is_inet_addr (const char *address)
400 {
401 return resolve_host_or_addr (address, AF_INET);
402 }
404 #ifdef USE_IPV6
405 int
406 is_inet6_addr (const char *address)
407 {
408 return resolve_host_or_addr (address, AF_INET6);
409 }
410 #endif
412 int
413 is_hostname (const char *s1)
414 {
415 #ifdef USE_IPV6
416 return resolve_host_or_addr (s1, address_family);
417 #else
418 return resolve_host_or_addr (s1, AF_INET);
419 #endif
420 }