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 * $Id$
31 *
32 ****************************************************************************/
34 #include "common.h"
35 #include "netutils.h"
37 unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
38 int econn_refuse_state = STATE_CRITICAL;
39 int was_refused = FALSE;
40 int address_family = AF_UNSPEC;
42 /* handles socket timeouts */
43 void
44 socket_timeout_alarm_handler (int sig)
45 {
46 if (sig == SIGALRM)
47 printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout);
48 else
49 printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout);
51 exit (STATE_CRITICAL);
52 }
55 /* connects to a host on a specified tcp port, sends a string, and gets a
56 response. loops on select-recv until timeout or eof to get all of a
57 multi-packet answer */
58 int
59 process_tcp_request2 (const char *server_address, int server_port,
60 const char *send_buffer, char *recv_buffer, int recv_size)
61 {
63 int result;
64 int send_result;
65 int recv_result;
66 int sd;
67 struct timeval tv;
68 fd_set readfds;
69 int recv_length = 0;
71 result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP);
72 if (result != STATE_OK)
73 return STATE_CRITICAL;
75 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
76 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
77 printf (_("Send failed\n"));
78 result = STATE_WARNING;
79 }
81 while (1) {
82 /* wait up to the number of seconds for socket timeout
83 minus one for data from the host */
84 tv.tv_sec = socket_timeout - 1;
85 tv.tv_usec = 0;
86 FD_ZERO (&readfds);
87 FD_SET (sd, &readfds);
88 select (sd + 1, &readfds, NULL, NULL, &tv);
90 /* make sure some data has arrived */
91 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
92 if (!recv_length) {
93 strcpy (recv_buffer, "");
94 printf (_("No data was received from host!\n"));
95 result = STATE_WARNING;
96 }
97 else { /* this one failed, but previous ones worked */
98 recv_buffer[recv_length] = 0;
99 }
100 break;
101 }
102 else { /* it has */
103 recv_result =
104 recv (sd, recv_buffer + recv_length,
105 (size_t)recv_size - recv_length - 1, 0);
106 if (recv_result == -1) {
107 /* recv failed, bail out */
108 strcpy (recv_buffer + recv_length, "");
109 result = STATE_WARNING;
110 break;
111 }
112 else if (recv_result == 0) {
113 /* end of file ? */
114 recv_buffer[recv_length] = 0;
115 break;
116 }
117 else { /* we got data! */
118 recv_length += recv_result;
119 if (recv_length >= recv_size - 1) {
120 /* buffer full, we're done */
121 recv_buffer[recv_size - 1] = 0;
122 break;
123 }
124 }
125 }
126 /* end if(!FD_ISSET(sd,&readfds)) */
127 }
128 /* end while(1) */
130 close (sd);
131 return result;
132 }
135 /* connects to a host on a specified port, sends a string, and gets a
136 response */
137 int
138 process_request (const char *server_address, int server_port, int proto,
139 const char *send_buffer, char *recv_buffer, int recv_size)
140 {
141 int result;
142 int sd;
144 result = STATE_OK;
146 result = np_net_connect (server_address, server_port, &sd, proto);
147 if (result != STATE_OK)
148 return STATE_CRITICAL;
150 result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
152 close (sd);
154 return result;
155 }
158 /* opens a tcp or udp connection to a remote host */
159 int
160 np_net_connect (const char *host_name, int port, int *sd, int proto)
161 {
162 struct addrinfo hints;
163 struct addrinfo *res, *res0;
164 char port_str[6];
165 int result;
167 memset (&hints, 0, sizeof (hints));
168 hints.ai_family = address_family;
169 hints.ai_protocol = proto;
170 hints.ai_socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
172 snprintf (port_str, sizeof (port_str), "%d", port);
173 result = getaddrinfo (host_name, port_str, &hints, &res0);
175 if (result != 0) {
176 printf ("%s\n", gai_strerror (result));
177 return STATE_UNKNOWN;
178 }
179 else {
180 res = res0;
181 while (res) {
182 /* attempt to create a socket */
183 *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
184 SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
186 if (*sd < 0) {
187 printf (_("Socket creation failed\n"));
188 freeaddrinfo (res);
189 return STATE_UNKNOWN;
190 }
192 /* attempt to open a connection */
193 result = connect (*sd, res->ai_addr, res->ai_addrlen);
195 if (result == 0) {
196 was_refused = FALSE;
197 break;
198 }
200 if (result < 0) {
201 switch (errno) {
202 case ECONNREFUSED:
203 was_refused = TRUE;
204 break;
205 }
206 }
208 close (*sd);
209 res = res->ai_next;
210 }
211 freeaddrinfo (res0);
212 }
214 if (result == 0)
215 return STATE_OK;
216 else if (was_refused) {
217 switch (econn_refuse_state) { /* a user-defined expected outcome */
218 case STATE_OK:
219 case STATE_WARNING: /* user wants WARN or OK on refusal */
220 return econn_refuse_state;
221 break;
222 case STATE_CRITICAL: /* user did not set econn_refuse_state */
223 printf ("%s\n", strerror(errno));
224 return econn_refuse_state;
225 break;
226 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
227 return STATE_UNKNOWN;
228 break;
229 }
230 }
231 else {
232 printf ("%s\n", strerror(errno));
233 return STATE_CRITICAL;
234 }
235 }
238 int
239 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
240 {
241 int result = STATE_OK;
242 int send_result;
243 int recv_result;
244 struct timeval tv;
245 fd_set readfds;
247 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
248 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
249 printf (_("Send failed\n"));
250 result = STATE_WARNING;
251 }
253 /* wait up to the number of seconds for socket timeout minus one
254 for data from the host */
255 tv.tv_sec = socket_timeout - 1;
256 tv.tv_usec = 0;
257 FD_ZERO (&readfds);
258 FD_SET (sd, &readfds);
259 select (sd + 1, &readfds, NULL, NULL, &tv);
261 /* make sure some data has arrived */
262 if (!FD_ISSET (sd, &readfds)) {
263 strcpy (recv_buffer, "");
264 printf (_("No data was received from host!\n"));
265 result = STATE_WARNING;
266 }
268 else {
269 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
270 if (recv_result == -1) {
271 strcpy (recv_buffer, "");
272 if (proto != IPPROTO_TCP)
273 printf (_("Receive failed\n"));
274 result = STATE_WARNING;
275 }
276 else
277 recv_buffer[recv_result] = 0;
279 /* die returned string */
280 recv_buffer[recv_size - 1] = 0;
281 }
282 return result;
283 }
286 int
287 is_host (const char *address)
288 {
289 if (is_addr (address) || is_hostname (address))
290 return (TRUE);
292 return (FALSE);
293 }
295 int
296 is_addr (const char *address)
297 {
298 #ifdef USE_IPV6
299 if (is_inet_addr (address) && address_family != AF_INET6)
300 #else
301 if (is_inet_addr (address))
302 #endif
303 return (TRUE);
305 #ifdef USE_IPV6
306 if (is_inet6_addr (address) && address_family != AF_INET)
307 return (TRUE);
308 #endif
310 return (FALSE);
311 }
313 int
314 resolve_host_or_addr (const char *address, int family)
315 {
316 struct addrinfo hints;
317 struct addrinfo *res;
318 int retval;
320 memset (&hints, 0, sizeof (hints));
321 hints.ai_family = family;
322 retval = getaddrinfo (address, NULL, &hints, &res);
324 if (retval != 0)
325 return FALSE;
326 else {
327 freeaddrinfo (res);
328 return TRUE;
329 }
330 }