netutils.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 "config.h"
  32. #include "common.h"
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. extern int socket_timeout;
  36. RETSIGTYPE socket_timeout_alarm_handler (int);
  37. int process_tcp_request2 (char *, int, char *, char *, int);
  38. int process_tcp_request (char *, int, char *, char *, int);
  39. int process_udp_request (char *, int, char *, char *, int);
  40. int process_request (char *, int, int, char *, char *, int);
  41. int my_tcp_connect (char *, int, int *);
  42. int my_udp_connect (char *, int, int *);
  43. int my_connect (char *, int, int *, int);
  44. /* handles socket timeouts */
  45. void
  46. socket_timeout_alarm_handler (int sig)
  47. {
  48. printf ("CRITICAL - Socket timeout after %d seconds\n", socket_timeout);
  49. exit (STATE_CRITICAL);
  50. }
  51. /* connects to a host on a specified TCP port, sends a string,
  52. and gets a response */
  53. int
  54. process_tcp_request (char *server_address, int server_port,
  55. char *send_buffer, char *recv_buffer, int recv_size)
  56. {
  57. int result;
  58. result = process_request (server_address, server_port,
  59. IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
  60. return result;
  61. }
  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;
  69. result = process_request (server_address, server_port,
  70. IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
  71. return result;
  72. }
  73. /* connects to a host on a specified tcp port, sends a string, and gets a
  74. response. loops on select-recv until timeout or eof to get all of a
  75. multi-packet answer */
  76. int
  77. process_tcp_request2 (char *server_address, int server_port,
  78. char *send_buffer, char *recv_buffer, int recv_size)
  79. {
  80. int result;
  81. int send_result;
  82. int recv_result;
  83. int sd;
  84. struct timeval tv;
  85. fd_set readfds;
  86. int recv_length = 0;
  87. result = my_connect (server_address, server_port, &sd, IPPROTO_TCP);
  88. if (result != STATE_OK)
  89. return STATE_CRITICAL;
  90. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  91. if (send_result != strlen (send_buffer)) {
  92. printf ("send() failed\n");
  93. result = STATE_WARNING;
  94. }
  95. while (1) {
  96. /* wait up to the number of seconds for socket timeout
  97. minus one for data from the host */
  98. tv.tv_sec = socket_timeout - 1;
  99. tv.tv_usec = 0;
  100. FD_ZERO (&readfds);
  101. FD_SET (sd, &readfds);
  102. select (sd + 1, &readfds, NULL, NULL, &tv);
  103. /* make sure some data has arrived */
  104. if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
  105. if (!recv_length) {
  106. strcpy (recv_buffer, "");
  107. printf ("No data was recieved from host!\n");
  108. result = STATE_WARNING;
  109. }
  110. else { /* this one failed, but previous ones worked */
  111. recv_buffer[recv_length] = 0;
  112. }
  113. break;
  114. }
  115. else { /* it has */
  116. recv_result =
  117. recv (sd, recv_buffer + recv_length,
  118. recv_size - recv_length - 1, 0);
  119. if (recv_result == -1) {
  120. /* recv failed, bail out */
  121. strcpy (recv_buffer + recv_length, "");
  122. result = STATE_WARNING;
  123. break;
  124. }
  125. else if (recv_result == 0) {
  126. /* end of file ? */
  127. recv_buffer[recv_length] = 0;
  128. break;
  129. }
  130. else { /* we got data! */
  131. recv_length += recv_result;
  132. if (recv_length >= recv_size - 1) {
  133. /* buffer full, we're done */
  134. recv_buffer[recv_size - 1] = 0;
  135. break;
  136. }
  137. }
  138. }
  139. /* end if(!FD_ISSET(sd,&readfds)) */
  140. }
  141. /* end while(1) */
  142. close (sd);
  143. return result;
  144. }
  145. /* connects to a host on a specified port, sends a string, and gets a
  146. response */
  147. int
  148. process_request (char *server_address, int server_port, int proto,
  149. char *send_buffer, char *recv_buffer, int recv_size)
  150. {
  151. int result;
  152. int send_result;
  153. int recv_result;
  154. int sd;
  155. struct timeval tv;
  156. fd_set readfds;
  157. result = STATE_OK;
  158. result = my_connect (server_address, server_port, &sd, proto);
  159. if (result != STATE_OK)
  160. return STATE_CRITICAL;
  161. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  162. if (send_result != strlen (send_buffer)) {
  163. printf ("send() failed\n");
  164. result = STATE_WARNING;
  165. }
  166. /* wait up to the number of seconds for socket timeout minus one
  167. for data from the host */
  168. tv.tv_sec = socket_timeout - 1;
  169. tv.tv_usec = 0;
  170. FD_ZERO (&readfds);
  171. FD_SET (sd, &readfds);
  172. select (sd + 1, &readfds, NULL, NULL, &tv);
  173. /* make sure some data has arrived */
  174. if (!FD_ISSET (sd, &readfds)) {
  175. strcpy (recv_buffer, "");
  176. printf ("No data was recieved from host!\n");
  177. result = STATE_WARNING;
  178. }
  179. else {
  180. recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
  181. if (recv_result == -1) {
  182. strcpy (recv_buffer, "");
  183. if (proto != IPPROTO_TCP)
  184. printf ("recv() failed\n");
  185. result = STATE_WARNING;
  186. }
  187. else
  188. recv_buffer[recv_result] = 0;
  189. /* terminate returned string */
  190. recv_buffer[recv_size - 1] = 0;
  191. }
  192. close (sd);
  193. return result;
  194. }
  195. /* opens a connection to a remote host/tcp port */
  196. int
  197. my_tcp_connect (char *host_name, int port, int *sd)
  198. {
  199. int result;
  200. result = my_connect (host_name, port, sd, IPPROTO_TCP);
  201. return result;
  202. }
  203. /* opens a connection to a remote host/udp port */
  204. int
  205. my_udp_connect (char *host_name, int port, int *sd)
  206. {
  207. int result;
  208. result = my_connect (host_name, port, sd, IPPROTO_UDP);
  209. return result;
  210. }
  211. /* opens a tcp or udp connection to a remote host */
  212. int
  213. my_connect (char *host_name, int port, int *sd, int proto)
  214. {
  215. struct addrinfo hints;
  216. struct addrinfo *res;
  217. struct addrinfo *ptrp;
  218. char port_str[6];
  219. int result;
  220. memset (&hints, 0, sizeof (hints));
  221. hints.ai_family = PF_UNSPEC;
  222. hints.ai_protocol = proto;
  223. snprintf (port_str, sizeof (port_str), "%d", port);
  224. result = getaddrinfo (host_name, port_str, &hints, &res);
  225. if (result != 0) {
  226. printf ("%s\n", gai_strerror (result));
  227. return STATE_UNKNOWN;
  228. }
  229. else {
  230. while (res) {
  231. /* attempt to create a socket */
  232. *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
  233. SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
  234. if (*sd < 0) {
  235. printf ("Socket creation failed\n");
  236. freeaddrinfo (res);
  237. return STATE_UNKNOWN;
  238. }
  239. /* attempt to open a connection */
  240. result = connect (*sd, res->ai_addr, res->ai_addrlen);
  241. if (result == 0)
  242. break;
  243. close (*sd);
  244. res = res->ai_next;
  245. }
  246. freeaddrinfo (res);
  247. }
  248. if (result == 0)
  249. return STATE_OK;
  250. else {
  251. printf ("%s\n", strerror(errno));
  252. return STATE_CRITICAL;
  253. }
  254. }