4
0

netutils.c 9.6 KB

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