netutils.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*****************************************************************************
  2. *
  3. * Monitoring Plugins network utilities
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  7. * Copyright (c) 2003-2008 Monitoring Plugins Development Team
  8. *
  9. * Description:
  10. *
  11. * This file contains commons functions used in many of the plugins.
  12. *
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. *
  28. *****************************************************************************/
  29. #include "common.h"
  30. #include "netutils.h"
  31. unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
  32. unsigned int socket_timeout_state = STATE_CRITICAL;
  33. int econn_refuse_state = STATE_CRITICAL;
  34. int was_refused = FALSE;
  35. #if USE_IPV6
  36. int address_family = AF_UNSPEC;
  37. #else
  38. int address_family = AF_INET;
  39. #endif
  40. /* handles socket timeouts */
  41. void
  42. socket_timeout_alarm_handler (int sig)
  43. {
  44. if (sig == SIGALRM)
  45. printf (_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);
  46. else
  47. printf (_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);
  48. exit (socket_timeout_state);
  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. /* send back STATE_UNKOWN if there's an error
  143. send back STATE_OK if we connect
  144. send back STATE_CRITICAL if we can't connect.
  145. Let upstream figure out what to send to the user. */
  146. struct addrinfo hints;
  147. struct addrinfo *r, *res;
  148. struct sockaddr_un su;
  149. char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
  150. size_t len;
  151. int socktype, result;
  152. short is_socket = (host_name[0] == '/');
  153. socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
  154. /* as long as it doesn't start with a '/', it's assumed a host or ip */
  155. if (!is_socket){
  156. memset (&hints, 0, sizeof (hints));
  157. hints.ai_family = address_family;
  158. hints.ai_protocol = proto;
  159. hints.ai_socktype = socktype;
  160. len = strlen (host_name);
  161. /* check for an [IPv6] address (and strip the brackets) */
  162. if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') {
  163. host_name++;
  164. len -= 2;
  165. }
  166. if (len >= sizeof(host))
  167. return STATE_UNKNOWN;
  168. memcpy (host, host_name, len);
  169. host[len] = '\0';
  170. snprintf (port_str, sizeof (port_str), "%d", port);
  171. result = getaddrinfo (host, port_str, &hints, &res);
  172. if (result != 0) {
  173. printf ("%s\n", gai_strerror (result));
  174. return STATE_UNKNOWN;
  175. }
  176. r = res;
  177. while (r) {
  178. /* attempt to create a socket */
  179. *sd = socket (r->ai_family, socktype, r->ai_protocol);
  180. if (*sd < 0) {
  181. printf ("%s\n", _("Socket creation failed"));
  182. freeaddrinfo (r);
  183. return STATE_UNKNOWN;
  184. }
  185. /* attempt to open a connection */
  186. result = connect (*sd, r->ai_addr, r->ai_addrlen);
  187. if (result == 0) {
  188. was_refused = FALSE;
  189. break;
  190. }
  191. if (result < 0) {
  192. switch (errno) {
  193. case ECONNREFUSED:
  194. was_refused = TRUE;
  195. break;
  196. }
  197. }
  198. close (*sd);
  199. r = r->ai_next;
  200. }
  201. freeaddrinfo (res);
  202. }
  203. /* else the hostname is interpreted as a path to a unix socket */
  204. else {
  205. if(strlen(host_name) >= UNIX_PATH_MAX){
  206. die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
  207. }
  208. memset(&su, 0, sizeof(su));
  209. su.sun_family = AF_UNIX;
  210. strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
  211. *sd = socket(PF_UNIX, SOCK_STREAM, 0);
  212. if(*sd < 0){
  213. die(STATE_UNKNOWN, _("Socket creation failed"));
  214. }
  215. result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
  216. if (result < 0 && errno == ECONNREFUSED)
  217. was_refused = TRUE;
  218. }
  219. if (result == 0)
  220. return STATE_OK;
  221. else if (was_refused) {
  222. switch (econn_refuse_state) { /* a user-defined expected outcome */
  223. case STATE_OK:
  224. case STATE_WARNING: /* user wants WARN or OK on refusal, or... */
  225. case STATE_CRITICAL: /* user did not set econn_refuse_state, or wanted critical */
  226. if (is_socket)
  227. printf("connect to file socket %s: %s\n", host_name, strerror(errno));
  228. else
  229. printf("connect to address %s and port %d: %s\n",
  230. host_name, port, strerror(errno));
  231. return STATE_CRITICAL;
  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. if (is_socket)
  240. printf("connect to file socket %s: %s\n", host_name, strerror(errno));
  241. else
  242. printf("connect to address %s and port %d: %s\n",
  243. host_name, port, strerror(errno));
  244. return STATE_CRITICAL;
  245. }
  246. }
  247. int
  248. send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
  249. {
  250. int result = STATE_OK;
  251. int send_result;
  252. int recv_result;
  253. struct timeval tv;
  254. fd_set readfds;
  255. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  256. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  257. printf ("%s\n", _("Send failed"));
  258. result = STATE_WARNING;
  259. }
  260. /* wait up to the number of seconds for socket timeout minus one
  261. for data from the host */
  262. tv.tv_sec = socket_timeout - 1;
  263. tv.tv_usec = 0;
  264. FD_ZERO (&readfds);
  265. FD_SET (sd, &readfds);
  266. select (sd + 1, &readfds, NULL, NULL, &tv);
  267. /* make sure some data has arrived */
  268. if (!FD_ISSET (sd, &readfds)) {
  269. strcpy (recv_buffer, "");
  270. printf ("%s\n", _("No data was received from host!"));
  271. result = STATE_WARNING;
  272. }
  273. else {
  274. recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
  275. if (recv_result == -1) {
  276. strcpy (recv_buffer, "");
  277. if (proto != IPPROTO_TCP)
  278. printf ("%s\n", _("Receive failed"));
  279. result = STATE_WARNING;
  280. }
  281. else
  282. recv_buffer[recv_result] = 0;
  283. /* die returned string */
  284. recv_buffer[recv_size - 1] = 0;
  285. }
  286. return result;
  287. }
  288. int
  289. is_host (const char *address)
  290. {
  291. if (is_addr (address) || is_hostname (address))
  292. return (TRUE);
  293. return (FALSE);
  294. }
  295. void
  296. host_or_die(const char *str)
  297. {
  298. if(!str || (!is_addr(str) && !is_hostname(str)))
  299. usage_va(_("Invalid hostname/address - %s"), str);
  300. }
  301. int
  302. is_addr (const char *address)
  303. {
  304. #ifdef USE_IPV6
  305. if (address_family == AF_INET && is_inet_addr (address))
  306. return TRUE;
  307. else if (address_family == AF_INET6 && is_inet6_addr (address))
  308. return TRUE;
  309. #else
  310. if (is_inet_addr (address))
  311. return (TRUE);
  312. #endif
  313. return (FALSE);
  314. }
  315. int
  316. dns_lookup (const char *in, struct sockaddr_storage *ss, int family)
  317. {
  318. struct addrinfo hints;
  319. struct addrinfo *res;
  320. int retval;
  321. memset (&hints, 0, sizeof(struct addrinfo));
  322. hints.ai_family = family;
  323. retval = getaddrinfo (in, NULL, &hints, &res);
  324. if (retval != 0)
  325. return FALSE;
  326. if (ss != NULL)
  327. memcpy (ss, res->ai_addr, res->ai_addrlen);
  328. freeaddrinfo (res);
  329. return TRUE;
  330. }