netutils.c 9.5 KB

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