netutils.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. ****************************************************************************/
  33. #include "common.h"
  34. #include "netutils.h"
  35. unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
  36. int econn_refuse_state = STATE_CRITICAL;
  37. int was_refused = FALSE;
  38. int address_family = AF_UNSPEC;
  39. /* handles socket timeouts */
  40. void
  41. socket_timeout_alarm_handler (int sig)
  42. {
  43. if (sig == SIGALRM)
  44. printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout);
  45. else
  46. printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout);
  47. exit (STATE_CRITICAL);
  48. }
  49. /* connects to a host on a specified tcp port, sends a string, and gets a
  50. response. loops on select-recv until timeout or eof to get all of a
  51. multi-packet answer */
  52. int
  53. process_tcp_request2 (const char *server_address, int server_port,
  54. const char *send_buffer, char *recv_buffer, int recv_size)
  55. {
  56. int result;
  57. int send_result;
  58. int recv_result;
  59. int sd;
  60. struct timeval tv;
  61. fd_set readfds;
  62. int recv_length = 0;
  63. result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
  64. if (result != STATE_OK)
  65. return STATE_CRITICAL;
  66. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  67. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  68. printf (_("Send failed\n"));
  69. result = STATE_WARNING;
  70. }
  71. while (1) {
  72. /* wait up to the number of seconds for socket timeout
  73. minus one for data from the host */
  74. tv.tv_sec = socket_timeout - 1;
  75. tv.tv_usec = 0;
  76. FD_ZERO (&readfds);
  77. FD_SET (sd, &readfds);
  78. select (sd + 1, &readfds, NULL, NULL, &tv);
  79. /* make sure some data has arrived */
  80. if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
  81. if (!recv_length) {
  82. strcpy (recv_buffer, "");
  83. printf (_("No data was received from host!\n"));
  84. result = STATE_WARNING;
  85. }
  86. else { /* this one failed, but previous ones worked */
  87. recv_buffer[recv_length] = 0;
  88. }
  89. break;
  90. }
  91. else { /* it has */
  92. recv_result =
  93. recv (sd, recv_buffer + recv_length,
  94. (size_t)recv_size - recv_length - 1, 0);
  95. if (recv_result == -1) {
  96. /* recv failed, bail out */
  97. strcpy (recv_buffer + recv_length, "");
  98. result = STATE_WARNING;
  99. break;
  100. }
  101. else if (recv_result == 0) {
  102. /* end of file ? */
  103. recv_buffer[recv_length] = 0;
  104. break;
  105. }
  106. else { /* we got data! */
  107. recv_length += recv_result;
  108. if (recv_length >= recv_size - 1) {
  109. /* buffer full, we're done */
  110. recv_buffer[recv_size - 1] = 0;
  111. break;
  112. }
  113. }
  114. }
  115. /* end if(!FD_ISSET(sd,&readfds)) */
  116. }
  117. /* end while(1) */
  118. close (sd);
  119. return result;
  120. }
  121. /* connects to a host on a specified port, sends a string, and gets a
  122. response */
  123. int
  124. process_request (const char *server_address, int server_port, int proto,
  125. const char *send_buffer, char *recv_buffer, int recv_size)
  126. {
  127. int result;
  128. int sd;
  129. result = STATE_OK;
  130. result = my_connect (server_address, server_port, &sd, proto);
  131. if (result != STATE_OK)
  132. return STATE_CRITICAL;
  133. result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
  134. close (sd);
  135. return result;
  136. }
  137. /* opens a tcp or udp connection to a remote host */
  138. int
  139. my_connect (const char *host_name, int port, int *sd, int proto)
  140. {
  141. struct addrinfo hints;
  142. struct addrinfo *res, *res0;
  143. char port_str[6];
  144. int result;
  145. memset (&hints, 0, sizeof (hints));
  146. hints.ai_family = address_family;
  147. hints.ai_protocol = proto;
  148. hints.ai_socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
  149. snprintf (port_str, sizeof (port_str), "%d", port);
  150. result = getaddrinfo (host_name, port_str, &hints, &res0);
  151. if (result != 0) {
  152. printf ("%s\n", gai_strerror (result));
  153. return STATE_UNKNOWN;
  154. }
  155. else {
  156. res = res0;
  157. while (res) {
  158. /* attempt to create a socket */
  159. *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
  160. SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
  161. if (*sd < 0) {
  162. printf (_("Socket creation failed\n"));
  163. freeaddrinfo (res);
  164. return STATE_UNKNOWN;
  165. }
  166. /* attempt to open a connection */
  167. result = connect (*sd, res->ai_addr, res->ai_addrlen);
  168. if (result == 0) {
  169. was_refused = FALSE;
  170. break;
  171. }
  172. if (result < 0) {
  173. switch (errno) {
  174. case ECONNREFUSED:
  175. was_refused = TRUE;
  176. break;
  177. }
  178. }
  179. close (*sd);
  180. res = res->ai_next;
  181. }
  182. freeaddrinfo (res0);
  183. }
  184. if (result == 0)
  185. return STATE_OK;
  186. else if (was_refused) {
  187. switch (econn_refuse_state) { /* a user-defined expected outcome */
  188. case STATE_OK:
  189. case STATE_WARNING: /* user wants WARN or OK on refusal */
  190. return econn_refuse_state;
  191. break;
  192. case STATE_CRITICAL: /* user did not set econn_refuse_state */
  193. printf ("%s\n", strerror(errno));
  194. return econn_refuse_state;
  195. break;
  196. default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
  197. return STATE_UNKNOWN;
  198. break;
  199. }
  200. }
  201. else {
  202. printf ("%s\n", strerror(errno));
  203. return STATE_CRITICAL;
  204. }
  205. }
  206. int
  207. send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
  208. {
  209. int result = STATE_OK;
  210. int send_result;
  211. int recv_result;
  212. struct timeval tv;
  213. fd_set readfds;
  214. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  215. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  216. printf (_("Send failed\n"));
  217. result = STATE_WARNING;
  218. }
  219. /* wait up to the number of seconds for socket timeout minus one
  220. for data from the host */
  221. tv.tv_sec = socket_timeout - 1;
  222. tv.tv_usec = 0;
  223. FD_ZERO (&readfds);
  224. FD_SET (sd, &readfds);
  225. select (sd + 1, &readfds, NULL, NULL, &tv);
  226. /* make sure some data has arrived */
  227. if (!FD_ISSET (sd, &readfds)) {
  228. strcpy (recv_buffer, "");
  229. printf (_("No data was received from host!\n"));
  230. result = STATE_WARNING;
  231. }
  232. else {
  233. recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
  234. if (recv_result == -1) {
  235. strcpy (recv_buffer, "");
  236. if (proto != IPPROTO_TCP)
  237. printf (_("Receive failed\n"));
  238. result = STATE_WARNING;
  239. }
  240. else
  241. recv_buffer[recv_result] = 0;
  242. /* die returned string */
  243. recv_buffer[recv_size - 1] = 0;
  244. }
  245. return result;
  246. }
  247. int
  248. is_host (const char *address)
  249. {
  250. if (is_addr (address) || is_hostname (address))
  251. return (TRUE);
  252. return (FALSE);
  253. }
  254. int
  255. is_addr (const char *address)
  256. {
  257. #ifdef USE_IPV6
  258. if (is_inet_addr (address) && address_family != AF_INET6)
  259. #else
  260. if (is_inet_addr (address))
  261. #endif
  262. return (TRUE);
  263. #ifdef USE_IPV6
  264. if (is_inet6_addr (address) && address_family != AF_INET)
  265. return (TRUE);
  266. #endif
  267. return (FALSE);
  268. }
  269. int
  270. resolve_host_or_addr (const char *address, int family)
  271. {
  272. struct addrinfo hints;
  273. struct addrinfo *res;
  274. int retval;
  275. memset (&hints, 0, sizeof (hints));
  276. hints.ai_family = family;
  277. retval = getaddrinfo (address, NULL, &hints, &res);
  278. if (retval != 0)
  279. return FALSE;
  280. else {
  281. freeaddrinfo (res);
  282. return TRUE;
  283. }
  284. }