6f3a151019ee7b47d7635de3e2269855337eec9d
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 or local socket */
159 int
160 np_net_connect (const char *host_name, int port, int *sd, int proto)
161 {
162 struct addrinfo hints;
163 struct addrinfo *r, *res;
164 struct sockaddr_un su;
165 char port_str[6];
166 int socktype, result;
168 socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
170 /* as long as it doesn't start with a '/', it's assumed a host or ip */
171 if(host_name[0] != '/'){
172 memset (&hints, 0, sizeof (hints));
173 hints.ai_family = address_family;
174 hints.ai_protocol = proto;
175 hints.ai_socktype = socktype;
177 snprintf (port_str, sizeof (port_str), "%d", port);
178 result = getaddrinfo (host_name, port_str, &hints, &res);
180 if (result != 0) {
181 printf ("%s\n", gai_strerror (result));
182 return STATE_UNKNOWN;
183 }
185 r = res;
186 while (r) {
187 /* attempt to create a socket */
188 *sd = socket (r->ai_family, socktype, r->ai_protocol);
190 if (*sd < 0) {
191 printf (_("Socket creation failed\n"));
192 freeaddrinfo (r);
193 return STATE_UNKNOWN;
194 }
196 /* attempt to open a connection */
197 result = connect (*sd, r->ai_addr, r->ai_addrlen);
199 if (result == 0) {
200 was_refused = FALSE;
201 break;
202 }
204 if (result < 0) {
205 switch (errno) {
206 case ECONNREFUSED:
207 was_refused = TRUE;
208 break;
209 }
210 }
212 close (*sd);
213 r = r->ai_next;
214 }
215 freeaddrinfo (res);
216 }
217 /* else the hostname is interpreted as a path to a unix socket */
218 else {
219 if(strlen(host_name) >= UNIX_PATH_MAX){
220 die(_("Supplied path too long unix domain socket"));
221 }
222 memset(&su, 0, sizeof(su));
223 su.sun_family = AF_UNIX;
224 strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
225 *sd = socket(PF_UNIX, SOCK_STREAM, 0);
226 if(sd < 0){
227 die(_("Socket creation failed"));
228 }
229 result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
230 if (result < 0 && errno == ECONNREFUSED)
231 was_refused = TRUE;
232 }
234 if (result == 0)
235 return STATE_OK;
236 else if (was_refused) {
237 switch (econn_refuse_state) { /* a user-defined expected outcome */
238 case STATE_OK:
239 case STATE_WARNING: /* user wants WARN or OK on refusal */
240 return econn_refuse_state;
241 break;
242 case STATE_CRITICAL: /* user did not set econn_refuse_state */
243 printf ("%s\n", strerror(errno));
244 return econn_refuse_state;
245 break;
246 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
247 return STATE_UNKNOWN;
248 break;
249 }
250 }
251 else {
252 printf ("%s\n", strerror(errno));
253 return STATE_CRITICAL;
254 }
255 }
257 int
258 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
259 {
260 int result = STATE_OK;
261 int send_result;
262 int recv_result;
263 struct timeval tv;
264 fd_set readfds;
266 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
267 if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
268 printf (_("Send failed\n"));
269 result = STATE_WARNING;
270 }
272 /* wait up to the number of seconds for socket timeout minus one
273 for data from the host */
274 tv.tv_sec = socket_timeout - 1;
275 tv.tv_usec = 0;
276 FD_ZERO (&readfds);
277 FD_SET (sd, &readfds);
278 select (sd + 1, &readfds, NULL, NULL, &tv);
280 /* make sure some data has arrived */
281 if (!FD_ISSET (sd, &readfds)) {
282 strcpy (recv_buffer, "");
283 printf (_("No data was received from host!\n"));
284 result = STATE_WARNING;
285 }
287 else {
288 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
289 if (recv_result == -1) {
290 strcpy (recv_buffer, "");
291 if (proto != IPPROTO_TCP)
292 printf (_("Receive failed\n"));
293 result = STATE_WARNING;
294 }
295 else
296 recv_buffer[recv_result] = 0;
298 /* die returned string */
299 recv_buffer[recv_size - 1] = 0;
300 }
301 return result;
302 }
305 int
306 is_host (const char *address)
307 {
308 if (is_addr (address) || is_hostname (address))
309 return (TRUE);
311 return (FALSE);
312 }
314 void
315 host_or_die(const char *str)
316 {
317 if(!str || (!is_addr(str) && !is_hostname(str)))
318 usage_va(_("Invalid hostname/address - %s"), str);
319 }
321 int
322 is_addr (const char *address)
323 {
324 #ifdef USE_IPV6
325 if (is_inet_addr (address) && address_family != AF_INET6)
326 #else
327 if (is_inet_addr (address))
328 #endif
329 return (TRUE);
331 #ifdef USE_IPV6
332 if (is_inet6_addr (address) && address_family != AF_INET)
333 return (TRUE);
334 #endif
336 return (FALSE);
337 }
339 int
340 resolve_host_or_addr (const char *address, int family)
341 {
342 struct addrinfo hints;
343 struct addrinfo *res;
344 int retval;
346 memset (&hints, 0, sizeof (hints));
347 hints.ai_family = family;
348 retval = getaddrinfo (address, NULL, &hints, &res);
350 if (retval != 0)
351 return FALSE;
352 else {
353 freeaddrinfo (res);
354 return TRUE;
355 }
356 }