netutils.c 8.5 KB

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