netutils.c 7.9 KB

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