Code

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