netutils.c 8.3 KB

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