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 #define LOCAL_TIMEOUT_ALARM_HANDLER
36 #include "common.h"
37 #include "netutils.h"
39 unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
40 int econn_refuse_state = STATE_CRITICAL;
41 int was_refused = FALSE;
42 #if USE_IPV6
43 int address_family = AF_UNSPEC;
44 #else
45 int address_family = AF_INET;
46 #endif
48 /* handles socket timeouts */
49 void
50 socket_timeout_alarm_handler (int sig)
51 {
52 if (sig == SIGALRM)
53 printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout);
54 else
55 printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout);
57 exit (STATE_CRITICAL);
58 }
61 /* connects to a host on a specified tcp port, sends a string, and gets a
62 response. loops on select-recv until timeout or eof to get all of a
63 multi-packet answer */
64 int
65 process_tcp_request2 (const char *server_address, int server_port,
66 const char *send_buffer, char *recv_buffer, int recv_size)
67 {
69 int result;
70 int send_result;
71 int recv_result;
72 int sd;
73 struct timeval tv;
74 fd_set readfds;
75 int recv_length = 0;
77 result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP);
78 if (result != STATE_OK)
79 return STATE_CRITICAL;
81 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
82 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
83 printf ("%s\n", _("Send failed"));
84 result = STATE_WARNING;
85 }
87 while (1) {
88 /* wait up to the number of seconds for socket timeout
89 minus one for data from the host */
90 tv.tv_sec = socket_timeout - 1;
91 tv.tv_usec = 0;
92 FD_ZERO (&readfds);
93 FD_SET (sd, &readfds);
94 select (sd + 1, &readfds, NULL, NULL, &tv);
96 /* make sure some data has arrived */
97 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
98 if (!recv_length) {
99 strcpy (recv_buffer, "");
100 printf ("%s\n", _("No data was received from host!"));
101 result = STATE_WARNING;
102 }
103 else { /* this one failed, but previous ones worked */
104 recv_buffer[recv_length] = 0;
105 }
106 break;
107 }
108 else { /* it has */
109 recv_result =
110 recv (sd, recv_buffer + recv_length,
111 (size_t)recv_size - recv_length - 1, 0);
112 if (recv_result == -1) {
113 /* recv failed, bail out */
114 strcpy (recv_buffer + recv_length, "");
115 result = STATE_WARNING;
116 break;
117 }
118 else if (recv_result == 0) {
119 /* end of file ? */
120 recv_buffer[recv_length] = 0;
121 break;
122 }
123 else { /* we got data! */
124 recv_length += recv_result;
125 if (recv_length >= recv_size - 1) {
126 /* buffer full, we're done */
127 recv_buffer[recv_size - 1] = 0;
128 break;
129 }
130 }
131 }
132 /* end if(!FD_ISSET(sd,&readfds)) */
133 }
134 /* end while(1) */
136 close (sd);
137 return result;
138 }
141 /* connects to a host on a specified port, sends a string, and gets a
142 response */
143 int
144 process_request (const char *server_address, int server_port, int proto,
145 const char *send_buffer, char *recv_buffer, int recv_size)
146 {
147 int result;
148 int sd;
150 result = STATE_OK;
152 result = np_net_connect (server_address, server_port, &sd, proto);
153 if (result != STATE_OK)
154 return STATE_CRITICAL;
156 result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
158 close (sd);
160 return result;
161 }
164 /* opens a tcp or udp connection to a remote host or local socket */
165 int
166 np_net_connect (const char *host_name, int port, int *sd, int proto)
167 {
168 struct addrinfo hints;
169 struct addrinfo *r, *res;
170 struct sockaddr_un su;
171 char port_str[6];
172 int socktype, result;
174 socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
176 /* as long as it doesn't start with a '/', it's assumed a host or ip */
177 if(host_name[0] != '/'){
178 memset (&hints, 0, sizeof (hints));
179 hints.ai_family = address_family;
180 hints.ai_protocol = proto;
181 hints.ai_socktype = socktype;
183 snprintf (port_str, sizeof (port_str), "%d", port);
184 result = getaddrinfo (host_name, port_str, &hints, &res);
186 if (result != 0) {
187 printf ("%s\n", gai_strerror (result));
188 return STATE_UNKNOWN;
189 }
191 r = res;
192 while (r) {
193 /* attempt to create a socket */
194 *sd = socket (r->ai_family, socktype, r->ai_protocol);
196 if (*sd < 0) {
197 printf ("%s\n", _("Socket creation failed"));
198 freeaddrinfo (r);
199 return STATE_UNKNOWN;
200 }
202 /* attempt to open a connection */
203 result = connect (*sd, r->ai_addr, r->ai_addrlen);
205 if (result == 0) {
206 was_refused = FALSE;
207 break;
208 }
210 if (result < 0) {
211 switch (errno) {
212 case ECONNREFUSED:
213 was_refused = TRUE;
214 break;
215 }
216 }
218 close (*sd);
219 r = r->ai_next;
220 }
221 freeaddrinfo (res);
222 }
223 /* else the hostname is interpreted as a path to a unix socket */
224 else {
225 if(strlen(host_name) >= UNIX_PATH_MAX){
226 die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
227 }
228 memset(&su, 0, sizeof(su));
229 su.sun_family = AF_UNIX;
230 strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
231 *sd = socket(PF_UNIX, SOCK_STREAM, 0);
232 if(*sd < 0){
233 die(STATE_UNKNOWN, _("Socket creation failed"));
234 }
235 result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
236 if (result < 0 && errno == ECONNREFUSED)
237 was_refused = TRUE;
238 }
240 if (result == 0)
241 return STATE_OK;
242 else if (was_refused) {
243 switch (econn_refuse_state) { /* a user-defined expected outcome */
244 case STATE_OK:
245 case STATE_WARNING: /* user wants WARN or OK on refusal */
246 return econn_refuse_state;
247 break;
248 case STATE_CRITICAL: /* user did not set econn_refuse_state */
249 printf ("%s\n", strerror(errno));
250 return econn_refuse_state;
251 break;
252 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
253 return STATE_UNKNOWN;
254 break;
255 }
256 }
257 else {
258 printf ("%s\n", strerror(errno));
259 return STATE_CRITICAL;
260 }
261 }
263 int
264 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
265 {
266 int result = STATE_OK;
267 int send_result;
268 int recv_result;
269 struct timeval tv;
270 fd_set readfds;
272 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
273 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
274 printf ("%s\n", _("Send failed"));
275 result = STATE_WARNING;
276 }
278 /* wait up to the number of seconds for socket timeout minus one
279 for data from the host */
280 tv.tv_sec = socket_timeout - 1;
281 tv.tv_usec = 0;
282 FD_ZERO (&readfds);
283 FD_SET (sd, &readfds);
284 select (sd + 1, &readfds, NULL, NULL, &tv);
286 /* make sure some data has arrived */
287 if (!FD_ISSET (sd, &readfds)) {
288 strcpy (recv_buffer, "");
289 printf ("%s\n", _("No data was received from host!"));
290 result = STATE_WARNING;
291 }
293 else {
294 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
295 if (recv_result == -1) {
296 strcpy (recv_buffer, "");
297 if (proto != IPPROTO_TCP)
298 printf ("%s\n", _("Receive failed"));
299 result = STATE_WARNING;
300 }
301 else
302 recv_buffer[recv_result] = 0;
304 /* die returned string */
305 recv_buffer[recv_size - 1] = 0;
306 }
307 return result;
308 }
311 int
312 is_host (const char *address)
313 {
314 if (is_addr (address) || is_hostname (address))
315 return (TRUE);
317 return (FALSE);
318 }
320 void
321 host_or_die(const char *str)
322 {
323 if(!str || (!is_addr(str) && !is_hostname(str)))
324 usage_va(_("Invalid hostname/address - %s"), str);
325 }
327 int
328 is_addr (const char *address)
329 {
330 #ifdef USE_IPV6
331 if (address_family == AF_INET && is_inet_addr (address))
332 return TRUE;
333 else if (address_family == AF_INET6 && is_inet6_addr (address))
334 return TRUE;
335 #else
336 if (is_inet_addr (address))
337 return (TRUE);
338 #endif
340 return (FALSE);
341 }
343 int
344 resolve_host_or_addr (const char *address, int family)
345 {
346 struct addrinfo hints;
347 struct addrinfo *res;
348 int retval;
350 memset (&hints, 0, sizeof (hints));
351 hints.ai_family = family;
352 retval = getaddrinfo (address, NULL, &hints, &res);
354 if (retval != 0)
355 return FALSE;
356 else {
357 freeaddrinfo (res);
358 return TRUE;
359 }
360 }