1
/****************************************************************************
3
* Nagios plugins network utilities
6
* Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8
* Last Modified: $Date: 2002/02/28 06:42:59 $
12
* This file contains commons functions used in many of the plugins.
14
* License Information:
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.
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.
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.
30
****************************************************************************/
34
#include <sys/socket.h>
35
#include <netinet/in.h>
36
#include <arpa/inet.h>
39
extern int socket_timeout;
40
RETSIGTYPE socket_timeout_alarm_handler (int);
42
int process_tcp_request2 (char *, int, char *, char *, int);
43
int process_tcp_request (char *, int, char *, char *, int);
44
int process_udp_request (char *, int, char *, char *, int);
45
int process_request (char *, int, char *, char *, char *, int);
47
int my_tcp_connect (char *, int, int *);
48
int my_udp_connect (char *, int, int *);
49
int my_connect (char *, int, int *, char *);
51
int my_inet_aton (register const char *, struct in_addr *);
53
/* handles socket timeouts */
55
socket_timeout_alarm_handler (int sig)
58
printf ("Socket timeout after %d seconds\n", socket_timeout);
60
exit (STATE_CRITICAL);
64
/* connects to a host on a specified TCP port, sends a string,
65
and gets a response */
67
process_tcp_request (char *server_address,
69
char *send_buffer, char *recv_buffer, int recv_size)
72
char proto[4] = "tcp";
74
result = process_request (server_address,
76
proto, send_buffer, recv_buffer, recv_size);
82
/* connects to a host on a specified UDP port, sends a string, and gets a
85
process_udp_request (char *server_address,
87
char *send_buffer, char *recv_buffer, int recv_size)
90
char proto[4] = "udp";
92
result = process_request (server_address,
94
proto, send_buffer, recv_buffer, recv_size);
101
/* connects to a host on a specified tcp port, sends a string, and gets a
102
response. loops on select-recv until timeout or eof to get all of a
103
multi-packet answer */
105
process_tcp_request2 (char *server_address,
107
char *send_buffer, char *recv_buffer, int recv_size)
118
result = my_connect (server_address, server_port, &sd, "tcp");
119
if (result != STATE_OK)
120
return STATE_CRITICAL;
122
send_result = send (sd, send_buffer, strlen (send_buffer), 0);
123
if (send_result != strlen (send_buffer)) {
124
printf ("send() failed\n");
125
result = STATE_WARNING;
129
/* wait up to the number of seconds for socket timeout
130
minus one for data from the host */
131
tv.tv_sec = socket_timeout - 1;
134
FD_SET (sd, &readfds);
135
select (sd + 1, &readfds, NULL, NULL, &tv);
137
/* make sure some data has arrived */
138
if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
140
strcpy (recv_buffer, "");
141
printf ("No data was recieved from host!\n");
142
result = STATE_WARNING;
144
else { /* this one failed, but previous ones worked */
145
recv_buffer[recv_length] = 0;
151
recv (sd, recv_buffer + recv_length, recv_size - recv_length - 1, 0);
152
if (recv_result == -1) { /* recv failed, bail out */
153
strcpy (recv_buffer + recv_length, "");
154
result = STATE_WARNING;
157
else if (recv_result == 0) { /* end of file ? */
158
recv_buffer[recv_length] = 0;
161
else { /* we got data! */
162
recv_length += recv_result;
163
if (recv_length >= recv_size - 1) { /* buffer full, we're done */
164
recv_buffer[recv_size - 1] = 0;
168
} /* end if(!FD_ISSET(sd,&readfds)) */
175
/* connects to a host on a specified port, sends a string, and gets a
178
process_request (char *server_address,
181
char *send_buffer, char *recv_buffer, int recv_size)
192
result = my_connect (server_address, server_port, &sd, proto);
193
if (result != STATE_OK)
194
return STATE_CRITICAL;
196
send_result = send (sd, send_buffer, strlen (send_buffer), 0);
197
if (send_result != strlen (send_buffer)) {
198
printf ("send() failed\n");
199
result = STATE_WARNING;
202
/* wait up to the number of seconds for socket timeout minus one
203
for data from the host */
204
tv.tv_sec = socket_timeout - 1;
207
FD_SET (sd, &readfds);
208
select (sd + 1, &readfds, NULL, NULL, &tv);
210
/* make sure some data has arrived */
211
if (!FD_ISSET (sd, &readfds)) {
212
strcpy (recv_buffer, "");
213
printf ("No data was recieved from host!\n");
214
result = STATE_WARNING;
218
recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
219
if (recv_result == -1) {
220
strcpy (recv_buffer, "");
221
if (!strcmp (proto, "tcp"))
222
printf ("recv() failed\n");
223
result = STATE_WARNING;
226
recv_buffer[recv_result] = 0;
228
/* terminate returned string */
229
recv_buffer[recv_size - 1] = 0;
238
/* opens a connection to a remote host/tcp port */
240
my_tcp_connect (char *host_name, int port, int *sd)
243
char proto[4] = "tcp";
245
result = my_connect (host_name, port, sd, proto);
251
/* opens a connection to a remote host/udp port */
253
my_udp_connect (char *host_name, int port, int *sd)
256
char proto[4] = "udp";
258
result = my_connect (host_name, port, sd, proto);
264
/* opens a tcp or udp connection to a remote host */
266
my_connect (char *host_name, int port, int *sd, char *proto)
268
struct sockaddr_in servaddr;
270
struct protoent *ptrp;
273
bzero ((char *) &servaddr, sizeof (servaddr));
274
servaddr.sin_family = AF_INET;
275
servaddr.sin_port = htons (port);
277
/* try to bypass using a DNS lookup if this is just an IP address */
278
if (!my_inet_aton (host_name, &servaddr.sin_addr)) {
280
/* else do a DNS lookup */
281
hp = gethostbyname ((const char *) host_name);
283
printf ("Invalid host name '%s'\n", host_name);
284
return STATE_UNKNOWN;
287
memcpy (&servaddr.sin_addr, hp->h_addr, hp->h_length);
290
/* map transport protocol name to protocol number */
291
if ((ptrp = getprotobyname (proto)) == NULL) {
292
printf ("Cannot map \"%s\" to protocol number\n", proto);
293
return STATE_UNKNOWN;
296
/* create a socket */
298
socket (PF_INET, (!strcmp (proto, "udp")) ? SOCK_DGRAM : SOCK_STREAM,
301
printf ("Socket creation failed\n");
302
return STATE_UNKNOWN;
305
/* open a connection */
306
result = connect (*sd, (struct sockaddr *) &servaddr, sizeof (servaddr));
310
printf ("Connection refused by host\n");
313
printf ("Timeout while attempting connection\n");
316
printf ("Network is unreachable\n");
319
printf ("Connection refused or timed out\n");
322
return STATE_CRITICAL;
330
/* This code was taken from Fyodor's nmap utility, which was originally
331
taken from the GLIBC 2.0.6 libraries because Solaris doesn't contain
332
the inet_aton() funtion. */
334
my_inet_aton (register const char *cp, struct in_addr *addr)
336
register unsigned int val; /* changed from u_long --david */
337
register int base, n;
340
register u_int *pp = parts;
347
* Collect number up to ``.''.
348
* Values are specified as for C:
349
* 0x=hex, 0=octal, isdigit=decimal.
351
if (!isdigit ((int) c))
358
if (c == 'x' || c == 'X')
359
base = 16, c = *++cp;
365
if (isascii ((int) c) && isdigit ((int) c)) {
366
val = (val * base) + (c - '0');
369
else if (base == 16 && isascii ((int) c) && isxdigit ((int) c)) {
370
val = (val << 4) | (c + 10 - (islower ((int) c) ? 'a' : 'A'));
382
* a.b.c (with c treated as 16 bits)
383
* a.b (with b treated as 24 bits)
394
/* Check for trailing characters */
395
if (c != '\0' && (!isascii ((int) c) || !isspace ((int) c)))
398
/* Concoct the address according to the number of parts specified */
403
return (0); /* initial nondigit */
405
case 1: /* a -- 32 bits */
408
case 2: /* a.b -- 8.24 bits */
411
val |= parts[0] << 24;
414
case 3: /* a.b.c -- 8.8.16 bits */
417
val |= (parts[0] << 24) | (parts[1] << 16);
420
case 4: /* a.b.c.d -- 8.8.8.8 bits */
423
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
428
addr->s_addr = htonl (val);