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 "netutils.h"
34 int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
35 int econn_refuse_state = STATE_CRITICAL;
36 int was_refused = FALSE;
38 /* handles socket timeouts */
39 void
40 socket_timeout_alarm_handler (int sig)
41 {
43 printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
45 exit (STATE_CRITICAL);
46 }
49 /* connects to a host on a specified TCP port, sends a string,
50 and gets a response */
51 int
52 process_tcp_request (char *server_address, int server_port,
53 char *send_buffer, char *recv_buffer, int recv_size)
54 {
55 int result;
57 result = process_request (server_address, server_port,
58 IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
60 return result;
61 }
64 /* connects to a host on a specified UDP port, sends a string, and gets a
65 response */
66 int
67 process_udp_request (char *server_address, int server_port,
68 char *send_buffer, char *recv_buffer, int recv_size)
69 {
70 int result;
72 result = process_request (server_address, server_port,
73 IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
75 return result;
76 }
80 /* connects to a host on a specified tcp port, sends a string, and gets a
81 response. loops on select-recv until timeout or eof to get all of a
82 multi-packet answer */
83 int
84 process_tcp_request2 (char *server_address, int server_port,
85 char *send_buffer, char *recv_buffer, int recv_size)
86 {
88 int result;
89 int send_result;
90 int recv_result;
91 int sd;
92 struct timeval tv;
93 fd_set readfds;
94 int recv_length = 0;
96 result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
97 if (result != STATE_OK)
98 return STATE_CRITICAL;
100 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
101 if (send_result != strlen (send_buffer)) {
102 printf ("send() failed\n");
103 result = STATE_WARNING;
104 }
106 while (1) {
107 /* wait up to the number of seconds for socket timeout
108 minus one for data from the host */
109 tv.tv_sec = socket_timeout - 1;
110 tv.tv_usec = 0;
111 FD_ZERO (&readfds);
112 FD_SET (sd, &readfds);
113 select (sd + 1, &readfds, NULL, NULL, &tv);
115 /* make sure some data has arrived */
116 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
117 if (!recv_length) {
118 strcpy (recv_buffer, "");
119 printf ("No data was recieved from host!\n");
120 result = STATE_WARNING;
121 }
122 else { /* this one failed, but previous ones worked */
123 recv_buffer[recv_length] = 0;
124 }
125 break;
126 }
127 else { /* it has */
128 recv_result =
129 recv (sd, recv_buffer + recv_length,
130 recv_size - recv_length - 1, 0);
131 if (recv_result == -1) {
132 /* recv failed, bail out */
133 strcpy (recv_buffer + recv_length, "");
134 result = STATE_WARNING;
135 break;
136 }
137 else if (recv_result == 0) {
138 /* end of file ? */
139 recv_buffer[recv_length] = 0;
140 break;
141 }
142 else { /* we got data! */
143 recv_length += recv_result;
144 if (recv_length >= recv_size - 1) {
145 /* buffer full, we're done */
146 recv_buffer[recv_size - 1] = 0;
147 break;
148 }
149 }
150 }
151 /* end if(!FD_ISSET(sd,&readfds)) */
152 }
153 /* end while(1) */
155 close (sd);
156 return result;
157 }
159 /* connects to a host on a specified port, sends a string, and gets a
160 response */
161 int
162 process_request (char *server_address, int server_port, int proto,
163 char *send_buffer, char *recv_buffer, int recv_size)
164 {
165 int result;
166 int send_result;
167 int recv_result;
168 int sd;
169 struct timeval tv;
170 fd_set readfds;
172 result = STATE_OK;
174 result = my_connect (server_address, server_port, &sd, proto);
175 if (result != STATE_OK)
176 return STATE_CRITICAL;
178 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
179 if (send_result != strlen (send_buffer)) {
180 printf ("send() failed\n");
181 result = STATE_WARNING;
182 }
184 /* wait up to the number of seconds for socket timeout minus one
185 for data from the host */
186 tv.tv_sec = socket_timeout - 1;
187 tv.tv_usec = 0;
188 FD_ZERO (&readfds);
189 FD_SET (sd, &readfds);
190 select (sd + 1, &readfds, NULL, NULL, &tv);
192 /* make sure some data has arrived */
193 if (!FD_ISSET (sd, &readfds)) {
194 strcpy (recv_buffer, "");
195 printf ("No data was recieved from host!\n");
196 result = STATE_WARNING;
197 }
199 else {
200 recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
201 if (recv_result == -1) {
202 strcpy (recv_buffer, "");
203 if (proto != IPPROTO_TCP)
204 printf ("recv() failed\n");
205 result = STATE_WARNING;
206 }
207 else
208 recv_buffer[recv_result] = 0;
210 /* terminate returned string */
211 recv_buffer[recv_size - 1] = 0;
212 }
214 close (sd);
216 return result;
217 }
220 /* opens a connection to a remote host/tcp port */
221 int
222 my_tcp_connect (char *host_name, int port, int *sd)
223 {
224 int result;
226 result = my_connect (host_name, port, sd, IPPROTO_TCP);
228 return result;
229 }
232 /* opens a connection to a remote host/udp port */
233 int
234 my_udp_connect (char *host_name, int port, int *sd)
235 {
236 int result;
238 result = my_connect (host_name, port, sd, IPPROTO_UDP);
240 return result;
241 }
244 /* opens a tcp or udp connection to a remote host */
245 int
246 my_connect (char *host_name, int port, int *sd, int proto)
247 {
248 struct addrinfo hints;
249 struct addrinfo *res;
250 struct addrinfo *ptrp;
251 char port_str[6];
252 int result;
254 memset (&hints, 0, sizeof (hints));
255 hints.ai_family = PF_UNSPEC;
256 hints.ai_protocol = proto;
258 snprintf (port_str, sizeof (port_str), "%d", port);
259 result = getaddrinfo (host_name, port_str, &hints, &res);
261 if (result != 0) {
262 printf ("%s\n", gai_strerror (result));
263 return STATE_UNKNOWN;
264 }
265 else {
266 while (res) {
267 /* attempt to create a socket */
268 *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
269 SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
271 if (*sd < 0) {
272 printf ("Socket creation failed\n");
273 freeaddrinfo (res);
274 return STATE_UNKNOWN;
275 }
277 /* attempt to open a connection */
278 result = connect (*sd, res->ai_addr, res->ai_addrlen);
280 if (result == 0) {
281 was_refused = FALSE;
282 break;
283 }
285 if (result < 0) {
286 switch (errno) {
287 case ECONNREFUSED:
288 switch (econn_refuse_state) {
289 case STATE_OK:
290 case STATE_WARNING:
291 was_refused = TRUE;
292 }
293 break;
294 }
295 }
297 close (*sd);
298 res = res->ai_next;
299 }
300 freeaddrinfo (res);
301 }
303 if (result == 0)
304 return STATE_OK;
305 else if (was_refused)
306 return econn_refuse_state;
307 else {
308 printf ("%s\n", strerror(errno));
309 return STATE_CRITICAL;
310 }
311 }
313 int
314 is_host (char *address)
315 {
316 if (is_addr (address) || is_hostname (address))
317 return (TRUE);
319 return (FALSE);
320 }
322 int
323 is_addr (char *address)
324 {
325 #ifdef USE_IPV6
326 if (is_inet_addr (address) || is_inet6_addr (address))
327 #else
328 if (is_inet_addr (address))
329 #endif
330 return (TRUE);
332 return (FALSE);
333 }
335 int
336 resolve_host_or_addr (char *address, int family)
337 {
338 struct addrinfo hints;
339 struct addrinfo *res;
340 int retval;
342 memset (&hints, 0, sizeof (hints));
343 hints.ai_family = family;
344 retval = getaddrinfo (address, NULL, &hints, &res);
346 if (retval != 0)
347 return FALSE;
348 else {
349 freeaddrinfo (res);
350 return TRUE;
351 }
352 }
354 int
355 is_inet_addr (char *address)
356 {
357 return resolve_host_or_addr (address, AF_INET);
358 }
360 #ifdef USE_IPV6
361 int
362 is_inet6_addr (char *address)
363 {
364 return resolve_host_or_addr (address, AF_INET6);
365 }
366 #endif
368 int
369 is_hostname (char *s1)
370 {
371 #ifdef USE_IPV6
372 return resolve_host_or_addr (s1, AF_UNSPEC);
373 #else
374 return resolve_host_or_addr (s1, AF_INET);
375 #endif
376 }