netutils.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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;
  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, &res);
  188. if (result != 0) {
  189. printf ("%s\n", gai_strerror (result));
  190. return STATE_UNKNOWN;
  191. }
  192. else {
  193. while (res) {
  194. /* attempt to create a socket */
  195. *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
  196. SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
  197. if (*sd < 0) {
  198. printf ("Socket creation failed\n");
  199. freeaddrinfo (res);
  200. return STATE_UNKNOWN;
  201. }
  202. /* attempt to open a connection */
  203. result = connect (*sd, res->ai_addr, res->ai_addrlen);
  204. if (result == 0) {
  205. was_refused = FALSE;
  206. break;
  207. }
  208. if (result < 0) {
  209. switch (errno) {
  210. case ECONNREFUSED:
  211. was_refused = TRUE;
  212. break;
  213. }
  214. }
  215. close (*sd);
  216. res = res->ai_next;
  217. }
  218. freeaddrinfo (res);
  219. }
  220. if (result == 0)
  221. return STATE_OK;
  222. else if (was_refused) {
  223. switch (econn_refuse_state) { /* a user-defined expected outcome */
  224. case STATE_OK:
  225. case STATE_WARNING: /* user wants WARN or OK on refusal */
  226. return econn_refuse_state;
  227. break;
  228. case STATE_CRITICAL: /* user did not set econn_refuse_state */
  229. printf ("%s\n", strerror(errno));
  230. return econn_refuse_state;
  231. break;
  232. default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
  233. return STATE_UNKNOWN;
  234. break;
  235. }
  236. }
  237. else {
  238. printf ("%s\n", strerror(errno));
  239. return STATE_CRITICAL;
  240. }
  241. }
  242. int
  243. send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
  244. {
  245. return send_request (sd, IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
  246. }
  247. int
  248. send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
  249. {
  250. return send_request (sd, IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
  251. }
  252. int
  253. send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
  254. {
  255. int result = STATE_OK;
  256. int send_result;
  257. int recv_result;
  258. struct timeval tv;
  259. fd_set readfds;
  260. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  261. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  262. printf ("send() failed\n");
  263. result = STATE_WARNING;
  264. }
  265. /* wait up to the number of seconds for socket timeout minus one
  266. for data from the host */
  267. tv.tv_sec = socket_timeout - 1;
  268. tv.tv_usec = 0;
  269. FD_ZERO (&readfds);
  270. FD_SET (sd, &readfds);
  271. select (sd + 1, &readfds, NULL, NULL, &tv);
  272. /* make sure some data has arrived */
  273. if (!FD_ISSET (sd, &readfds)) {
  274. strcpy (recv_buffer, "");
  275. printf ("No data was received from host!\n");
  276. result = STATE_WARNING;
  277. }
  278. else {
  279. recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
  280. if (recv_result == -1) {
  281. strcpy (recv_buffer, "");
  282. if (proto != IPPROTO_TCP)
  283. printf ("recv() failed\n");
  284. result = STATE_WARNING;
  285. }
  286. else
  287. recv_buffer[recv_result] = 0;
  288. /* die returned string */
  289. recv_buffer[recv_size - 1] = 0;
  290. }
  291. return result;
  292. }
  293. int
  294. is_host (const char *address)
  295. {
  296. if (is_addr (address) || is_hostname (address))
  297. return (TRUE);
  298. return (FALSE);
  299. }
  300. int
  301. is_addr (const char *address)
  302. {
  303. #ifdef USE_IPV6
  304. if (is_inet_addr (address) && address_family != AF_INET6)
  305. #else
  306. if (is_inet_addr (address))
  307. #endif
  308. return (TRUE);
  309. #ifdef USE_IPV6
  310. if (is_inet6_addr (address) && address_family != AF_INET)
  311. return (TRUE);
  312. #endif
  313. return (FALSE);
  314. }
  315. int
  316. resolve_host_or_addr (const char *address, int family)
  317. {
  318. struct addrinfo hints;
  319. struct addrinfo *res;
  320. int retval;
  321. memset (&hints, 0, sizeof (hints));
  322. hints.ai_family = family;
  323. retval = getaddrinfo (address, NULL, &hints, &res);
  324. if (retval != 0)
  325. return FALSE;
  326. else {
  327. freeaddrinfo (res);
  328. return TRUE;
  329. }
  330. }
  331. int
  332. is_inet_addr (const char *address)
  333. {
  334. return resolve_host_or_addr (address, AF_INET);
  335. }
  336. #ifdef USE_IPV6
  337. int
  338. is_inet6_addr (const char *address)
  339. {
  340. return resolve_host_or_addr (address, AF_INET6);
  341. }
  342. #endif
  343. int
  344. is_hostname (const char *s1)
  345. {
  346. #ifdef USE_IPV6
  347. return resolve_host_or_addr (s1, address_family);
  348. #else
  349. return resolve_host_or_addr (s1, AF_INET);
  350. #endif
  351. }