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 ****************************************************************************/
32 #include "netutils.h"
34 int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
36 /* handles socket timeouts */
37 void
38 socket_timeout_alarm_handler (int sig)
39 {
41 printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
43 exit (STATE_CRITICAL);
44 }
47 /* connects to a host on a specified TCP port, sends a string,
48 and gets a response */
49 int
50 process_tcp_request (char *server_address, int server_port,
51 char *send_buffer, char *recv_buffer, int recv_size)
52 {
53 int result;
55 result = process_request (server_address, server_port,
56 IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
58 return result;
59 }
62 /* connects to a host on a specified UDP port, sends a string, and gets a
63 response */
64 int
65 process_udp_request (char *server_address, int server_port,
66 char *send_buffer, char *recv_buffer, int recv_size)
67 {
68 int result;
70 result = process_request (server_address, server_port,
71 IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
73 return result;
74 }
78 /* connects to a host on a specified tcp port, sends a string, and gets a
79 response. loops on select-recv until timeout or eof to get all of a
80 multi-packet answer */
81 int
82 process_tcp_request2 (char *server_address, int server_port,
83 char *send_buffer, char *recv_buffer, int recv_size)
84 {
86 int result;
87 int send_result;
88 int recv_result;
89 int sd;
90 struct timeval tv;
91 fd_set readfds;
92 int recv_length = 0;
94 result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
95 if (result != STATE_OK)
96 return STATE_CRITICAL;
98 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
99 if (send_result != strlen (send_buffer)) {
100 printf ("send() failed\n");
101 result = STATE_WARNING;
102 }
104 while (1) {
105 /* wait up to the number of seconds for socket timeout
106 minus one for data from the host */
107 tv.tv_sec = socket_timeout - 1;
108 tv.tv_usec = 0;
109 FD_ZERO (&readfds);
110 FD_SET (sd, &readfds);
111 select (sd + 1, &readfds, NULL, NULL, &tv);
113 /* make sure some data has arrived */
114 if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
115 if (!recv_length) {
116 strcpy (recv_buffer, "");
117 printf ("No data was recieved from host!\n");
118 result = STATE_WARNING;
119 }
120 else { /* this one failed, but previous ones worked */
121 recv_buffer[recv_length] = 0;
122 }
123 break;
124 }
125 else { /* it has */
126 recv_result =
127 recv (sd, recv_buffer + recv_length,
128 recv_size - recv_length - 1, 0);
129 if (recv_result == -1) {
130 /* recv failed, bail out */
131 strcpy (recv_buffer + recv_length, "");
132 result = STATE_WARNING;
133 break;
134 }
135 else if (recv_result == 0) {
136 /* end of file ? */
137 recv_buffer[recv_length] = 0;
138 break;
139 }
140 else { /* we got data! */
141 recv_length += recv_result;
142 if (recv_length >= recv_size - 1) {
143 /* buffer full, we're done */
144 recv_buffer[recv_size - 1] = 0;
145 break;
146 }
147 }
148 }
149 /* end if(!FD_ISSET(sd,&readfds)) */
150 }
151 /* end while(1) */
153 close (sd);
154 return result;
155 }
157 /* connects to a host on a specified port, sends a string, and gets a
158 response */
159 int
160 process_request (char *server_address, int server_port, int proto,
161 char *send_buffer, char *recv_buffer, int recv_size)
162 {
163 int result;
164 int send_result;
165 int recv_result;
166 int sd;
167 struct timeval tv;
168 fd_set readfds;
170 result = STATE_OK;
172 result = my_connect (server_address, server_port, &sd, proto);
173 if (result != STATE_OK)
174 return STATE_CRITICAL;
176 send_result = send (sd, send_buffer, strlen (send_buffer), 0);
177 if (send_result != strlen (send_buffer)) {
178 printf ("send() failed\n");
179 result = STATE_WARNING;
180 }
182 /* wait up to the number of seconds for socket timeout minus one
183 for data from the host */
184 tv.tv_sec = socket_timeout - 1;
185 tv.tv_usec = 0;
186 FD_ZERO (&readfds);
187 FD_SET (sd, &readfds);
188 select (sd + 1, &readfds, NULL, NULL, &tv);
190 /* make sure some data has arrived */
191 if (!FD_ISSET (sd, &readfds)) {
192 strcpy (recv_buffer, "");
193 printf ("No data was recieved from host!\n");
194 result = STATE_WARNING;
195 }
197 else {
198 recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
199 if (recv_result == -1) {
200 strcpy (recv_buffer, "");
201 if (proto != IPPROTO_TCP)
202 printf ("recv() failed\n");
203 result = STATE_WARNING;
204 }
205 else
206 recv_buffer[recv_result] = 0;
208 /* terminate returned string */
209 recv_buffer[recv_size - 1] = 0;
210 }
212 close (sd);
214 return result;
215 }
218 /* opens a connection to a remote host/tcp port */
219 int
220 my_tcp_connect (char *host_name, int port, int *sd)
221 {
222 int result;
224 result = my_connect (host_name, port, sd, IPPROTO_TCP);
226 return result;
227 }
230 /* opens a connection to a remote host/udp port */
231 int
232 my_udp_connect (char *host_name, int port, int *sd)
233 {
234 int result;
236 result = my_connect (host_name, port, sd, IPPROTO_UDP);
238 return result;
239 }
242 /* opens a tcp or udp connection to a remote host */
243 int
244 my_connect (char *host_name, int port, int *sd, int proto)
245 {
246 struct addrinfo hints;
247 struct addrinfo *res;
248 struct addrinfo *ptrp;
249 char port_str[6];
250 int result;
252 memset (&hints, 0, sizeof (hints));
253 hints.ai_family = PF_UNSPEC;
254 hints.ai_protocol = proto;
256 snprintf (port_str, sizeof (port_str), "%d", port);
257 result = getaddrinfo (host_name, port_str, &hints, &res);
259 if (result != 0) {
260 printf ("%s\n", gai_strerror (result));
261 return STATE_UNKNOWN;
262 }
263 else {
264 while (res) {
265 /* attempt to create a socket */
266 *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
267 SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
269 if (*sd < 0) {
270 printf ("Socket creation failed\n");
271 freeaddrinfo (res);
272 return STATE_UNKNOWN;
273 }
275 /* attempt to open a connection */
276 result = connect (*sd, res->ai_addr, res->ai_addrlen);
278 if (result == 0)
279 break;
281 close (*sd);
282 res = res->ai_next;
283 }
284 freeaddrinfo (res);
285 }
287 if (result == 0)
288 return STATE_OK;
289 else {
290 printf ("%s\n", strerror(errno));
291 return STATE_CRITICAL;
292 }
293 }
295 int
296 is_host (char *address)
297 {
298 if (is_addr (address) || is_hostname (address))
299 return (TRUE);
301 return (FALSE);
302 }
304 int
305 is_addr (char *address)
306 {
307 #ifdef USE_IPV6
308 if (is_inet_addr (address) || is_inet6_addr (address))
309 #else
310 if (is_inet_addr (address))
311 #endif
312 return (TRUE);
314 return (FALSE);
315 }
317 int
318 resolve_host_or_addr (char *address, int family)
319 {
320 struct addrinfo hints;
321 struct addrinfo *res;
322 int retval;
324 memset (&hints, 0, sizeof (hints));
325 hints.ai_family = family;
326 retval = getaddrinfo (address, NULL, &hints, &res);
328 if (retval != 0)
329 return FALSE;
330 else {
331 freeaddrinfo (res);
332 return TRUE;
333 }
334 }
336 int
337 is_inet_addr (char *address)
338 {
339 return resolve_host_or_addr (address, AF_INET);
340 }
342 #ifdef USE_IPV6
343 int
344 is_inet6_addr (char *address)
345 {
346 return resolve_host_or_addr (address, AF_INET6);
347 }
348 #endif
350 int
351 is_hostname (char *s1)
352 {
353 #ifdef USE_IPV6
354 return resolve_host_or_addr (s1, AF_UNSPEC);
355 #else
356 return resolve_host_or_addr (s1, AF_INET);
357 #endif
358 }