082452768642e7a7bb7673af9130dddaec8801be
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 }
237 int
238 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
239 {
240 int result = STATE_OK;
241 int send_result;
242 int recv_result;
243 struct timeval tv;
244 fd_set readfds;
246 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
247 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
248 printf (_("Send failed\n"));
249 result = STATE_WARNING;
250 }
252 /* wait up to the number of seconds for socket timeout minus one
253 for data from the host */
254 tv.tv_sec = socket_timeout - 1;
255 tv.tv_usec = 0;
256 FD_ZERO (&readfds);
257 FD_SET (sd, &readfds);
258 select (sd + 1, &readfds, NULL, NULL, &tv);
260 /* make sure some data has arrived */
261 if (!FD_ISSET (sd, &readfds)) {
262 strcpy (recv_buffer, "");
263 printf (_("No data was received from host!\n"));
264 result = STATE_WARNING;
265 }
267 else {
268 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
269 if (recv_result == -1) {
270 strcpy (recv_buffer, "");
271 if (proto != IPPROTO_TCP)
272 printf (_("Receive failed\n"));
273 result = STATE_WARNING;
274 }
275 else
276 recv_buffer[recv_result] = 0;
278 /* die returned string */
279 recv_buffer[recv_size - 1] = 0;
280 }
281 return result;
282 }
285 int
286 is_host (const char *address)
287 {
288 if (is_addr (address) || is_hostname (address))
289 return (TRUE);
291 return (FALSE);
292 }
294 void
295 host_or_die(const char *str)
296 {
297 if(!str || (!is_addr(str) && !is_hostname(str)))
298 usage_va(_("Invalid hostname/address - %s"), str);
299 }
301 int
302 is_addr (const char *address)
303 {
304 #ifdef USE_IPV6
305 if (is_inet_addr (address) && address_family != AF_INET6)
306 #else
307 if (is_inet_addr (address))
308 #endif
309 return (TRUE);
311 #ifdef USE_IPV6
312 if (is_inet6_addr (address) && address_family != AF_INET)
313 return (TRUE);
314 #endif
316 return (FALSE);
317 }
319 int
320 resolve_host_or_addr (const char *address, int family)
321 {
322 struct addrinfo hints;
323 struct addrinfo *res;
324 int retval;
326 memset (&hints, 0, sizeof (hints));
327 hints.ai_family = family;
328 retval = getaddrinfo (address, NULL, &hints, &res);
330 if (retval != 0)
331 return FALSE;
332 else {
333 freeaddrinfo (res);
334 return TRUE;
335 }
336 }