netutils.c 8.6 KB

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