Code

Bulk EOL cleanup
[nagiosplug.git] / plugins / netutils.c
1 /*****************************************************************************
2
3 * Nagios plugins network utilities
4
5 * License: GPL
6 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
7 * Copyright (c) 2003-2008 Nagios Plugins Development Team
8
9 * Last Modified: $Date$
10
11 * Description:
12
13 * This file contains commons functions used in many of the plugins.
14
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 3 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, see <http://www.gnu.org/licenses/>.
28
29 * $Id$
30
31 *****************************************************************************/
33 #define LOCAL_TIMEOUT_ALARM_HANDLER
35 #include "common.h"
36 #include "netutils.h"
38 unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
39 int econn_refuse_state = STATE_CRITICAL;
40 int was_refused = FALSE;
41 #if USE_IPV6
42 int address_family = AF_UNSPEC;
43 #else
44 int address_family = AF_INET;
45 #endif
47 /* handles socket timeouts */
48 void
49 socket_timeout_alarm_handler (int sig)
50 {
51         if (sig == SIGALRM)
52                 printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout);
53         else
54                 printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout);
56         exit (STATE_CRITICAL);
57 }
60 /* connects to a host on a specified tcp port, sends a string, and gets a
61          response. loops on select-recv until timeout or eof to get all of a
62          multi-packet answer */
63 int
64 process_tcp_request2 (const char *server_address, int server_port,
65         const char *send_buffer, char *recv_buffer, int recv_size)
66 {
68         int result;
69         int send_result;
70         int recv_result;
71         int sd;
72         struct timeval tv;
73         fd_set readfds;
74         int recv_length = 0;
76         result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP);
77         if (result != STATE_OK)
78                 return STATE_CRITICAL;
80         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
81         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
82                 printf ("%s\n", _("Send failed"));
83                 result = STATE_WARNING;
84         }
86         while (1) {
87                 /* wait up to the number of seconds for socket timeout
88                    minus one for data from the host */
89                 tv.tv_sec = socket_timeout - 1;
90                 tv.tv_usec = 0;
91                 FD_ZERO (&readfds);
92                 FD_SET (sd, &readfds);
93                 select (sd + 1, &readfds, NULL, NULL, &tv);
95                 /* make sure some data has arrived */
96                 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
97                         if (!recv_length) {
98                                 strcpy (recv_buffer, "");
99                                 printf ("%s\n", _("No data was received from host!"));
100                                 result = STATE_WARNING;
101                         }
102                         else {                                                                          /* this one failed, but previous ones worked */
103                                 recv_buffer[recv_length] = 0;
104                         }
105                         break;
106                 }
107                 else {                                                                                  /* it has */
108                         recv_result =
109                                 recv (sd, recv_buffer + recv_length,
110                                         (size_t)recv_size - recv_length - 1, 0);
111                         if (recv_result == -1) {
112                                 /* recv failed, bail out */
113                                 strcpy (recv_buffer + recv_length, "");
114                                 result = STATE_WARNING;
115                                 break;
116                         }
117                         else if (recv_result == 0) {
118                                 /* end of file ? */
119                                 recv_buffer[recv_length] = 0;
120                                 break;
121                         }
122                         else {                                                                          /* we got data! */
123                                 recv_length += recv_result;
124                                 if (recv_length >= recv_size - 1) {
125                                         /* buffer full, we're done */
126                                         recv_buffer[recv_size - 1] = 0;
127                                         break;
128                                 }
129                         }
130                 }
131                 /* end if(!FD_ISSET(sd,&readfds)) */
132         }
133         /* end while(1) */
135         close (sd);
136         return result;
140 /* connects to a host on a specified port, sends a string, and gets a
141    response */
142 int
143 process_request (const char *server_address, int server_port, int proto,
144         const char *send_buffer, char *recv_buffer, int recv_size)
146         int result;
147         int sd;
149         result = STATE_OK;
151         result = np_net_connect (server_address, server_port, &sd, proto);
152         if (result != STATE_OK)
153                 return STATE_CRITICAL;
155         result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
157         close (sd);
159         return result;
163 /* opens a tcp or udp connection to a remote host or local socket */
164 int
165 np_net_connect (const char *host_name, int port, int *sd, int proto)
167         struct addrinfo hints;
168         struct addrinfo *r, *res;
169         struct sockaddr_un su;
170         char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
171         size_t len;
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                 len = strlen (host_name);
184                 /* check for an [IPv6] address (and strip the brackets) */
185                 if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') {
186                         host_name++;
187                         len -= 2;
188                 }
189                 if (len >= sizeof(host))
190                         return STATE_UNKNOWN;
191                 memcpy (host, host_name, len);
192                 host[len] = '\0';
193                 snprintf (port_str, sizeof (port_str), "%d", port);
194                 result = getaddrinfo (host, port_str, &hints, &res);
196                 if (result != 0) {
197                         printf ("%s\n", gai_strerror (result));
198                         return STATE_UNKNOWN;
199                 }
201                 r = res;
202                 while (r) {
203                         /* attempt to create a socket */
204                         *sd = socket (r->ai_family, socktype, r->ai_protocol);
206                         if (*sd < 0) {
207                                 printf ("%s\n", _("Socket creation failed"));
208                                 freeaddrinfo (r);
209                                 return STATE_UNKNOWN;
210                         }
212                         /* attempt to open a connection */
213                         result = connect (*sd, r->ai_addr, r->ai_addrlen);
215                         if (result == 0) {
216                                 was_refused = FALSE;
217                                 break;
218                         }
220                         if (result < 0) {
221                                 switch (errno) {
222                                 case ECONNREFUSED:
223                                         was_refused = TRUE;
224                                         break;
225                                 }
226                         }
228                         close (*sd);
229                         r = r->ai_next;
230                 }
231                 freeaddrinfo (res);
232         }
233         /* else the hostname is interpreted as a path to a unix socket */
234         else {
235                 if(strlen(host_name) >= UNIX_PATH_MAX){
236                         die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
237                 }
238                 memset(&su, 0, sizeof(su));
239                 su.sun_family = AF_UNIX;
240                 strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
241                 *sd = socket(PF_UNIX, SOCK_STREAM, 0);
242                 if(*sd < 0){
243                         die(STATE_UNKNOWN, _("Socket creation failed"));
244                 }
245                 result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
246                 if (result < 0 && errno == ECONNREFUSED)
247                         was_refused = TRUE;
248         }
250         if (result == 0)
251                 return STATE_OK;
252         else if (was_refused) {
253                 switch (econn_refuse_state) { /* a user-defined expected outcome */
254                 case STATE_OK:
255                 case STATE_WARNING:  /* user wants WARN or OK on refusal */
256                         return econn_refuse_state;
257                         break;
258                 case STATE_CRITICAL: /* user did not set econn_refuse_state */
259                         printf ("%s\n", strerror(errno));
260                         return econn_refuse_state;
261                         break;
262                 default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
263                         return STATE_UNKNOWN;
264                         break;
265                 }
266         }
267         else {
268                 printf ("%s\n", strerror(errno));
269                 return STATE_CRITICAL;
270         }
273 int
274 send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
276         int result = STATE_OK;
277         int send_result;
278         int recv_result;
279         struct timeval tv;
280         fd_set readfds;
282         send_result = send (sd, send_buffer, strlen (send_buffer), 0);
283         if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
284                 printf ("%s\n", _("Send failed"));
285                 result = STATE_WARNING;
286         }
288         /* wait up to the number of seconds for socket timeout minus one
289            for data from the host */
290         tv.tv_sec = socket_timeout - 1;
291         tv.tv_usec = 0;
292         FD_ZERO (&readfds);
293         FD_SET (sd, &readfds);
294         select (sd + 1, &readfds, NULL, NULL, &tv);
296         /* make sure some data has arrived */
297         if (!FD_ISSET (sd, &readfds)) {
298                 strcpy (recv_buffer, "");
299                 printf ("%s\n", _("No data was received from host!"));
300                 result = STATE_WARNING;
301         }
303         else {
304                 recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
305                 if (recv_result == -1) {
306                         strcpy (recv_buffer, "");
307                         if (proto != IPPROTO_TCP)
308                                 printf ("%s\n", _("Receive failed"));
309                         result = STATE_WARNING;
310                 }
311                 else
312                         recv_buffer[recv_result] = 0;
314                 /* die returned string */
315                 recv_buffer[recv_size - 1] = 0;
316         }
317         return result;
321 int
322 is_host (const char *address)
324         if (is_addr (address) || is_hostname (address))
325                 return (TRUE);
327         return (FALSE);
330 void
331 host_or_die(const char *str)
333         if(!str || (!is_addr(str) && !is_hostname(str)))
334                 usage_va(_("Invalid hostname/address - %s"), str);
337 int
338 is_addr (const char *address)
340 #ifdef USE_IPV6
341         if (address_family == AF_INET && is_inet_addr (address))
342                 return TRUE;
343         else if (address_family == AF_INET6 && is_inet6_addr (address))
344                 return TRUE;
345 #else
346         if (is_inet_addr (address))
347                 return (TRUE);
348 #endif
350         return (FALSE);
353 int
354 resolve_host_or_addr (const char *address, int family)
356         struct addrinfo hints;
357         struct addrinfo *res;
358         int retval;
360         memset (&hints, 0, sizeof (hints));
361         hints.ai_family = family;
362         retval = getaddrinfo (address, NULL, &hints, &res);
364         if (retval != 0)
365                 return FALSE;
366         else {
367                 freeaddrinfo (res);
368                 return TRUE;
369         }