netutils.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. ****************************************************************************/
  31. #include "common.h"
  32. #include "netutils.h"
  33. int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
  34. int econn_refuse_state = STATE_CRITICAL;
  35. int was_refused = FALSE;
  36. /* handles socket timeouts */
  37. void
  38. socket_timeout_alarm_handler (int sig)
  39. {
  40. printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
  41. exit (STATE_CRITICAL);
  42. }
  43. /* connects to a host on a specified TCP port, sends a string,
  44. and gets a response */
  45. int
  46. process_tcp_request (char *server_address, int server_port,
  47. char *send_buffer, char *recv_buffer, int recv_size)
  48. {
  49. int result;
  50. result = process_request (server_address, server_port,
  51. IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
  52. return result;
  53. }
  54. /* connects to a host on a specified UDP port, sends a string, and gets a
  55. response */
  56. int
  57. process_udp_request (char *server_address, int server_port,
  58. char *send_buffer, char *recv_buffer, int recv_size)
  59. {
  60. int result;
  61. result = process_request (server_address, server_port,
  62. IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
  63. return result;
  64. }
  65. /* connects to a host on a specified tcp port, sends a string, and gets a
  66. response. loops on select-recv until timeout or eof to get all of a
  67. multi-packet answer */
  68. int
  69. process_tcp_request2 (char *server_address, int server_port,
  70. char *send_buffer, char *recv_buffer, int recv_size)
  71. {
  72. int result;
  73. int send_result;
  74. int recv_result;
  75. int sd;
  76. struct timeval tv;
  77. fd_set readfds;
  78. int recv_length = 0;
  79. result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
  80. if (result != STATE_OK)
  81. return STATE_CRITICAL;
  82. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  83. if (send_result != strlen (send_buffer)) {
  84. printf ("send() failed\n");
  85. result = STATE_WARNING;
  86. }
  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);
  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 ("No data was recieved from host!\n");
  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. 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) */
  134. close (sd);
  135. return result;
  136. }
  137. /* connects to a host on a specified port, sends a string, and gets a
  138. response */
  139. int
  140. process_request (char *server_address, int server_port, int proto,
  141. char *send_buffer, char *recv_buffer, int recv_size)
  142. {
  143. int result;
  144. int send_result;
  145. int recv_result;
  146. int sd;
  147. struct timeval tv;
  148. fd_set readfds;
  149. result = STATE_OK;
  150. result = my_connect (server_address, server_port, &sd, proto);
  151. if (result != STATE_OK)
  152. return STATE_CRITICAL;
  153. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  154. if (send_result != strlen (send_buffer)) {
  155. printf ("send() failed\n");
  156. result = STATE_WARNING;
  157. }
  158. /* wait up to the number of seconds for socket timeout minus one
  159. for data from the host */
  160. tv.tv_sec = socket_timeout - 1;
  161. tv.tv_usec = 0;
  162. FD_ZERO (&readfds);
  163. FD_SET (sd, &readfds);
  164. select (sd + 1, &readfds, NULL, NULL, &tv);
  165. /* make sure some data has arrived */
  166. if (!FD_ISSET (sd, &readfds)) {
  167. strcpy (recv_buffer, "");
  168. printf ("No data was recieved from host!\n");
  169. result = STATE_WARNING;
  170. }
  171. else {
  172. recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
  173. if (recv_result == -1) {
  174. strcpy (recv_buffer, "");
  175. if (proto != IPPROTO_TCP)
  176. printf ("recv() failed\n");
  177. result = STATE_WARNING;
  178. }
  179. else
  180. recv_buffer[recv_result] = 0;
  181. /* terminate returned string */
  182. recv_buffer[recv_size - 1] = 0;
  183. }
  184. close (sd);
  185. return result;
  186. }
  187. /* opens a connection to a remote host/tcp port */
  188. int
  189. my_tcp_connect (char *host_name, int port, int *sd)
  190. {
  191. int result;
  192. result = my_connect (host_name, port, sd, IPPROTO_TCP);
  193. return result;
  194. }
  195. /* opens a connection to a remote host/udp port */
  196. int
  197. my_udp_connect (char *host_name, int port, int *sd)
  198. {
  199. int result;
  200. result = my_connect (host_name, port, sd, IPPROTO_UDP);
  201. return result;
  202. }
  203. /* opens a tcp or udp connection to a remote host */
  204. int
  205. my_connect (char *host_name, int port, int *sd, int proto)
  206. {
  207. struct addrinfo hints;
  208. struct addrinfo *res;
  209. struct addrinfo *ptrp;
  210. char port_str[6];
  211. int result;
  212. memset (&hints, 0, sizeof (hints));
  213. hints.ai_family = PF_UNSPEC;
  214. hints.ai_protocol = proto;
  215. snprintf (port_str, sizeof (port_str), "%d", port);
  216. result = getaddrinfo (host_name, port_str, &hints, &res);
  217. if (result != 0) {
  218. printf ("%s\n", gai_strerror (result));
  219. return STATE_UNKNOWN;
  220. }
  221. else {
  222. while (res) {
  223. /* attempt to create a socket */
  224. *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
  225. SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
  226. if (*sd < 0) {
  227. printf ("Socket creation failed\n");
  228. freeaddrinfo (res);
  229. return STATE_UNKNOWN;
  230. }
  231. /* attempt to open a connection */
  232. result = connect (*sd, res->ai_addr, res->ai_addrlen);
  233. if (result == 0) {
  234. was_refused = FALSE;
  235. break;
  236. }
  237. if (result < 0) {
  238. switch (errno) {
  239. case ECONNREFUSED:
  240. switch (econn_refuse_state) {
  241. case STATE_OK:
  242. case STATE_WARNING:
  243. was_refused = TRUE;
  244. }
  245. break;
  246. }
  247. }
  248. close (*sd);
  249. res = res->ai_next;
  250. }
  251. freeaddrinfo (res);
  252. }
  253. if (result == 0)
  254. return STATE_OK;
  255. else if (was_refused)
  256. return econn_refuse_state;
  257. else {
  258. printf ("%s\n", strerror(errno));
  259. return STATE_CRITICAL;
  260. }
  261. }
  262. int
  263. is_host (char *address)
  264. {
  265. if (is_addr (address) || is_hostname (address))
  266. return (TRUE);
  267. return (FALSE);
  268. }
  269. int
  270. is_addr (char *address)
  271. {
  272. #ifdef USE_IPV6
  273. if (is_inet_addr (address) || is_inet6_addr (address))
  274. #else
  275. if (is_inet_addr (address))
  276. #endif
  277. return (TRUE);
  278. return (FALSE);
  279. }
  280. int
  281. resolve_host_or_addr (char *address, int family)
  282. {
  283. struct addrinfo hints;
  284. struct addrinfo *res;
  285. int retval;
  286. memset (&hints, 0, sizeof (hints));
  287. hints.ai_family = family;
  288. retval = getaddrinfo (address, NULL, &hints, &res);
  289. if (retval != 0)
  290. return FALSE;
  291. else {
  292. freeaddrinfo (res);
  293. return TRUE;
  294. }
  295. }
  296. int
  297. is_inet_addr (char *address)
  298. {
  299. return resolve_host_or_addr (address, AF_INET);
  300. }
  301. #ifdef USE_IPV6
  302. int
  303. is_inet6_addr (char *address)
  304. {
  305. return resolve_host_or_addr (address, AF_INET6);
  306. }
  307. #endif
  308. int
  309. is_hostname (char *s1)
  310. {
  311. #ifdef USE_IPV6
  312. return resolve_host_or_addr (s1, AF_UNSPEC);
  313. #else
  314. return resolve_host_or_addr (s1, AF_INET);
  315. #endif
  316. }