4
0

netutils.c 8.2 KB

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