netutils.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. switch (econn_refuse_state) {
  212. case STATE_OK:
  213. case STATE_WARNING:
  214. was_refused = TRUE;
  215. }
  216. break;
  217. }
  218. }
  219. close (*sd);
  220. res = res->ai_next;
  221. }
  222. freeaddrinfo (res);
  223. }
  224. if (result == 0)
  225. return STATE_OK;
  226. else if (was_refused)
  227. return econn_refuse_state;
  228. else {
  229. printf ("%s\n", strerror(errno));
  230. return STATE_CRITICAL;
  231. }
  232. }
  233. int
  234. send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
  235. {
  236. return send_request (sd, IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
  237. }
  238. int
  239. send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
  240. {
  241. return send_request (sd, IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
  242. }
  243. int
  244. send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
  245. {
  246. int result;
  247. int send_result;
  248. int recv_result;
  249. struct timeval tv;
  250. fd_set readfds;
  251. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  252. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  253. printf ("send() failed\n");
  254. result = STATE_WARNING;
  255. }
  256. /* wait up to the number of seconds for socket timeout minus one
  257. for data from the host */
  258. tv.tv_sec = socket_timeout - 1;
  259. tv.tv_usec = 0;
  260. FD_ZERO (&readfds);
  261. FD_SET (sd, &readfds);
  262. select (sd + 1, &readfds, NULL, NULL, &tv);
  263. /* make sure some data has arrived */
  264. if (!FD_ISSET (sd, &readfds)) {
  265. strcpy (recv_buffer, "");
  266. printf ("No data was received from host!\n");
  267. result = STATE_WARNING;
  268. }
  269. else {
  270. recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
  271. if (recv_result == -1) {
  272. strcpy (recv_buffer, "");
  273. if (proto != IPPROTO_TCP)
  274. printf ("recv() failed\n");
  275. result = STATE_WARNING;
  276. }
  277. else
  278. recv_buffer[recv_result] = 0;
  279. /* die returned string */
  280. recv_buffer[recv_size - 1] = 0;
  281. }
  282. return result;
  283. }
  284. int
  285. is_host (const char *address)
  286. {
  287. if (is_addr (address) || is_hostname (address))
  288. return (TRUE);
  289. return (FALSE);
  290. }
  291. int
  292. is_addr (const char *address)
  293. {
  294. #ifdef USE_IPV6
  295. if (is_inet_addr (address) && address_family != AF_INET6)
  296. #else
  297. if (is_inet_addr (address))
  298. #endif
  299. return (TRUE);
  300. #ifdef USE_IPV6
  301. if (is_inet6_addr (address) && address_family != AF_INET)
  302. return (TRUE);
  303. #endif
  304. return (FALSE);
  305. }
  306. int
  307. resolve_host_or_addr (const char *address, int family)
  308. {
  309. struct addrinfo hints;
  310. struct addrinfo *res;
  311. int retval;
  312. memset (&hints, 0, sizeof (hints));
  313. hints.ai_family = family;
  314. retval = getaddrinfo (address, NULL, &hints, &res);
  315. if (retval != 0)
  316. return FALSE;
  317. else {
  318. freeaddrinfo (res);
  319. return TRUE;
  320. }
  321. }
  322. int
  323. is_inet_addr (const char *address)
  324. {
  325. return resolve_host_or_addr (address, AF_INET);
  326. }
  327. #ifdef USE_IPV6
  328. int
  329. is_inet6_addr (const char *address)
  330. {
  331. return resolve_host_or_addr (address, AF_INET6);
  332. }
  333. #endif
  334. int
  335. is_hostname (const char *s1)
  336. {
  337. #ifdef USE_IPV6
  338. return resolve_host_or_addr (s1, address_family);
  339. #else
  340. return resolve_host_or_addr (s1, AF_INET);
  341. #endif
  342. }