summaryrefslogtreecommitdiffstats
path: root/plugins/netutils.c
blob: db64ef09b230dcf1a106e6ec3eff6a34b1c5b5da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/****************************************************************************
*
* Nagios plugins network utilities
*
* License: GPL
* Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
*
* Last Modified: $Date$
*
* Description:
*
* This file contains commons functions used in many of the plugins.
*
* License Information:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
*
****************************************************************************/

#include "common.h"
#include "netutils.h"

unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; 
int econn_refuse_state = STATE_CRITICAL;
int was_refused = FALSE;
int address_family = AF_UNSPEC;

/* handles socket timeouts */
void
socket_timeout_alarm_handler (int sig)
{
	if (sig == SIGALRM)
		printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout);
	else
		printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout);

	exit (STATE_CRITICAL);
}


/* connects to a host on a specified tcp port, sends a string, and gets a 
	 response. loops on select-recv until timeout or eof to get all of a 
	 multi-packet answer */
int
process_tcp_request2 (const char *server_address, int server_port,
	const char *send_buffer, char *recv_buffer, int recv_size)
{

	int result;
	int send_result;
	int recv_result;
	int sd;
	struct timeval tv;
	fd_set readfds;
	int recv_length = 0;

	result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP);
	if (result != STATE_OK)
		return STATE_CRITICAL;

	send_result = send (sd, send_buffer, strlen (send_buffer), 0);
	if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
		printf (_("Send failed\n"));
		result = STATE_WARNING;
	}

	while (1) {
		/* wait up to the number of seconds for socket timeout
		   minus one for data from the host */
		tv.tv_sec = socket_timeout - 1;
		tv.tv_usec = 0;
		FD_ZERO (&readfds);
		FD_SET (sd, &readfds);
		select (sd + 1, &readfds, NULL, NULL, &tv);

		/* make sure some data has arrived */
		if (!FD_ISSET (sd, &readfds)) {	/* it hasn't */
			if (!recv_length) {
				strcpy (recv_buffer, "");
				printf (_("No data was received from host!\n"));
				result = STATE_WARNING;
			}
			else {										/* this one failed, but previous ones worked */
				recv_buffer[recv_length] = 0;
			}
			break;
		}
		else {											/* it has */
			recv_result =
				recv (sd, recv_buffer + recv_length, 
					(size_t)recv_size - recv_length - 1, 0);
			if (recv_result == -1) {
				/* recv failed, bail out */
				strcpy (recv_buffer + recv_length, "");
				result = STATE_WARNING;
				break;
			}
			else if (recv_result == 0) {
				/* end of file ? */
				recv_buffer[recv_length] = 0;
				break;
			}
			else {										/* we got data! */
				recv_length += recv_result;
				if (recv_length >= recv_size - 1) {
					/* buffer full, we're done */
					recv_buffer[recv_size - 1] = 0;
					break;
				}
			}
		}
		/* end if(!FD_ISSET(sd,&readfds)) */
	}
	/* end while(1) */

	close (sd);
	return result;
}


/* connects to a host on a specified port, sends a string, and gets a 
   response */
int
process_request (const char *server_address, int server_port, int proto,
	const char *send_buffer, char *recv_buffer, int recv_size)
{
	int result;
	int sd;

	result = STATE_OK;

	result = np_net_connect (server_address, server_port, &sd, proto);
	if (result != STATE_OK)
		return STATE_CRITICAL;

	result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);

	close (sd);

	return result;
}


/* opens a tcp or udp connection to a remote host */
int
np_net_connect (const char *host_name, int port, int *sd, int proto)
{
	struct addrinfo hints;
	struct addrinfo *res, *res0;
	char port_str[6];
	int result;

	memset (&hints, 0, sizeof (hints));
	hints.ai_family = address_family;
	hints.ai_protocol = proto;
	hints.ai_socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;

	snprintf (port_str, sizeof (port_str), "%d", port);
	result = getaddrinfo (host_name, port_str, &hints, &res0);

	if (result != 0) {
		printf ("%s\n", gai_strerror (result));
		return STATE_UNKNOWN;
	}
	else {
		res = res0;
		while (res) {
			/* attempt to create a socket */
			*sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
			              SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);

			if (*sd < 0) {
				printf (_("Socket creation failed\n"));
				freeaddrinfo (res);
				return STATE_UNKNOWN;
			}

			/* attempt to open a connection */
			result = connect (*sd, res->ai_addr, res->ai_addrlen);

			if (result == 0) {
				was_refused = FALSE;
				break;
			}

			if (result < 0) {
				switch (errno) {
				case ECONNREFUSED:
					was_refused = TRUE;
					break;
				}
			}

			close (*sd);
			res = res->ai_next;
		}
		freeaddrinfo (res0);
	}

	if (result == 0)
		return STATE_OK;
	else if (was_refused) {
		switch (econn_refuse_state) { /* a user-defined expected outcome */
		case STATE_OK:       
		case STATE_WARNING:  /* user wants WARN or OK on refusal */
			return econn_refuse_state;
			break;
		case STATE_CRITICAL: /* user did not set econn_refuse_state */
			printf ("%s\n", strerror(errno));
			return econn_refuse_state;
			break;
		default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
			return STATE_UNKNOWN;
			break;
		}
	}
	else {
		printf ("%s\n", strerror(errno));
		return STATE_CRITICAL;
	}
}

int
send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
{
	int result = STATE_OK;
	int send_result;
	int recv_result;
	struct timeval tv;
	fd_set readfds;

	send_result = send (sd, send_buffer, strlen (send_buffer), 0);
	if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
		printf (_("Send failed\n"));
		result = STATE_WARNING;
	}

	/* wait up to the number of seconds for socket timeout minus one 
	   for data from the host */
	tv.tv_sec = socket_timeout - 1;
	tv.tv_usec = 0;
	FD_ZERO (&readfds);
	FD_SET (sd, &readfds);
	select (sd + 1, &readfds, NULL, NULL, &tv);

	/* make sure some data has arrived */
	if (!FD_ISSET (sd, &readfds)) {
		strcpy (recv_buffer, "");
		printf (_("No data was received from host!\n"));
		result = STATE_WARNING;
	}

	else {
		recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
		if (recv_result == -1) {
			strcpy (recv_buffer, "");
			if (proto != IPPROTO_TCP)
				printf (_("Receive failed\n"));
			result = STATE_WARNING;
		}
		else
			recv_buffer[recv_result] = 0;

		/* die returned string */
		recv_buffer[recv_size - 1] = 0;
	}
	return result;
}


int
is_host (const char *address)
{
	if (is_addr (address) || is_hostname (address))
		return (TRUE);

	return (FALSE);
}

int
is_addr (const char *address)
{
#ifdef USE_IPV6
	if (is_inet_addr (address) && address_family != AF_INET6)
#else
	if (is_inet_addr (address))
#endif
		return (TRUE);

#ifdef USE_IPV6
	if (is_inet6_addr (address) && address_family != AF_INET)
		return (TRUE);
#endif

	return (FALSE);
}

int
resolve_host_or_addr (const char *address, int family)
{
	struct addrinfo hints;
	struct addrinfo *res;
	int retval;

	memset (&hints, 0, sizeof (hints));
	hints.ai_family = family;
	retval = getaddrinfo (address, NULL, &hints, &res);

	if (retval != 0)
		return FALSE;
	else {
		freeaddrinfo (res);
		return TRUE;
	}
}