netutils.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 "config.h"
  32. #include "common.h"
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. extern int socket_timeout;
  36. RETSIGTYPE socket_timeout_alarm_handler (int);
  37. int process_tcp_request2 (char *, int, char *, char *, int);
  38. int process_tcp_request (char *, int, char *, char *, int);
  39. int process_udp_request (char *, int, char *, char *, int);
  40. int process_request (char *, int, char *, char *, char *, int);
  41. int my_tcp_connect (char *, int, int *);
  42. int my_udp_connect (char *, int, int *);
  43. int my_connect (char *, int, int *, char *);
  44. int my_inet_aton (register const char *, struct in_addr *);
  45. /* handles socket timeouts */
  46. void
  47. socket_timeout_alarm_handler (int sig)
  48. {
  49. printf ("Socket timeout after %d seconds\n", socket_timeout);
  50. exit (STATE_CRITICAL);
  51. }
  52. /* connects to a host on a specified TCP port, sends a string,
  53. and gets a response */
  54. int
  55. process_tcp_request (char *server_address,
  56. int server_port,
  57. char *send_buffer, char *recv_buffer, int recv_size)
  58. {
  59. int result;
  60. char proto[4] = "tcp";
  61. result = process_request (server_address,
  62. server_port,
  63. proto, send_buffer, recv_buffer, recv_size);
  64. return result;
  65. }
  66. /* connects to a host on a specified UDP port, sends a string, and gets a
  67. response */
  68. int
  69. process_udp_request (char *server_address,
  70. int server_port,
  71. char *send_buffer, char *recv_buffer, int recv_size)
  72. {
  73. int result;
  74. char proto[4] = "udp";
  75. result = process_request (server_address,
  76. server_port,
  77. proto, send_buffer, recv_buffer, recv_size);
  78. return result;
  79. }
  80. /* connects to a host on a specified tcp port, sends a string, and gets a
  81. response. loops on select-recv until timeout or eof to get all of a
  82. multi-packet answer */
  83. int
  84. process_tcp_request2 (char *server_address,
  85. int server_port,
  86. char *send_buffer, char *recv_buffer, int recv_size)
  87. {
  88. int result;
  89. int send_result;
  90. int recv_result;
  91. int sd;
  92. struct timeval tv;
  93. fd_set readfds;
  94. int recv_length = 0;
  95. result = my_connect (server_address, server_port, &sd, "tcp");
  96. if (result != STATE_OK)
  97. return STATE_CRITICAL;
  98. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  99. if (send_result != strlen (send_buffer)) {
  100. printf ("send() failed\n");
  101. result = STATE_WARNING;
  102. }
  103. while (1) {
  104. /* wait up to the number of seconds for socket timeout
  105. minus one for data from the host */
  106. tv.tv_sec = socket_timeout - 1;
  107. tv.tv_usec = 0;
  108. FD_ZERO (&readfds);
  109. FD_SET (sd, &readfds);
  110. select (sd + 1, &readfds, NULL, NULL, &tv);
  111. /* make sure some data has arrived */
  112. if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
  113. if (!recv_length) {
  114. strcpy (recv_buffer, "");
  115. printf ("No data was recieved from host!\n");
  116. result = STATE_WARNING;
  117. }
  118. else { /* this one failed, but previous ones worked */
  119. recv_buffer[recv_length] = 0;
  120. }
  121. break;
  122. }
  123. else { /* it has */
  124. recv_result =
  125. recv (sd, recv_buffer + recv_length, recv_size - recv_length - 1, 0);
  126. if (recv_result == -1) { /* recv failed, bail out */
  127. strcpy (recv_buffer + recv_length, "");
  128. result = STATE_WARNING;
  129. break;
  130. }
  131. else if (recv_result == 0) { /* end of file ? */
  132. recv_buffer[recv_length] = 0;
  133. break;
  134. }
  135. else { /* we got data! */
  136. recv_length += recv_result;
  137. if (recv_length >= recv_size - 1) { /* buffer full, we're done */
  138. recv_buffer[recv_size - 1] = 0;
  139. break;
  140. }
  141. }
  142. } /* end if(!FD_ISSET(sd,&readfds)) */
  143. } /* end while(1) */
  144. close (sd);
  145. return result;
  146. }
  147. /* connects to a host on a specified port, sends a string, and gets a
  148. response */
  149. int
  150. process_request (char *server_address,
  151. int server_port,
  152. char *proto,
  153. char *send_buffer, char *recv_buffer, int recv_size)
  154. {
  155. int result;
  156. int send_result;
  157. int recv_result;
  158. int sd;
  159. struct timeval tv;
  160. fd_set readfds;
  161. result = STATE_OK;
  162. result = my_connect (server_address, server_port, &sd, proto);
  163. if (result != STATE_OK)
  164. return STATE_CRITICAL;
  165. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  166. if (send_result != strlen (send_buffer)) {
  167. printf ("send() failed\n");
  168. result = STATE_WARNING;
  169. }
  170. /* wait up to the number of seconds for socket timeout minus one
  171. for data from the host */
  172. tv.tv_sec = socket_timeout - 1;
  173. tv.tv_usec = 0;
  174. FD_ZERO (&readfds);
  175. FD_SET (sd, &readfds);
  176. select (sd + 1, &readfds, NULL, NULL, &tv);
  177. /* make sure some data has arrived */
  178. if (!FD_ISSET (sd, &readfds)) {
  179. strcpy (recv_buffer, "");
  180. printf ("No data was recieved from host!\n");
  181. result = STATE_WARNING;
  182. }
  183. else {
  184. recv_result = recv (sd, recv_buffer, recv_size - 1, 0);
  185. if (recv_result == -1) {
  186. strcpy (recv_buffer, "");
  187. if (!strcmp (proto, "tcp"))
  188. printf ("recv() failed\n");
  189. result = STATE_WARNING;
  190. }
  191. else
  192. recv_buffer[recv_result] = 0;
  193. /* terminate returned string */
  194. recv_buffer[recv_size - 1] = 0;
  195. }
  196. close (sd);
  197. return result;
  198. }
  199. /* opens a connection to a remote host/tcp port */
  200. int
  201. my_tcp_connect (char *host_name, int port, int *sd)
  202. {
  203. int result;
  204. char proto[4] = "tcp";
  205. result = my_connect (host_name, port, sd, proto);
  206. return result;
  207. }
  208. /* opens a connection to a remote host/udp port */
  209. int
  210. my_udp_connect (char *host_name, int port, int *sd)
  211. {
  212. int result;
  213. char proto[4] = "udp";
  214. result = my_connect (host_name, port, sd, proto);
  215. return result;
  216. }
  217. /* opens a tcp or udp connection to a remote host */
  218. int
  219. my_connect (char *host_name, int port, int *sd, char *proto)
  220. {
  221. struct sockaddr_in servaddr;
  222. struct hostent *hp;
  223. struct protoent *ptrp;
  224. int result;
  225. bzero ((char *) &servaddr, sizeof (servaddr));
  226. servaddr.sin_family = AF_INET;
  227. servaddr.sin_port = htons (port);
  228. /* try to bypass using a DNS lookup if this is just an IP address */
  229. if (!my_inet_aton (host_name, &servaddr.sin_addr)) {
  230. /* else do a DNS lookup */
  231. hp = gethostbyname ((const char *) host_name);
  232. if (hp == NULL) {
  233. printf ("Invalid host name '%s'\n", host_name);
  234. return STATE_UNKNOWN;
  235. }
  236. memcpy (&servaddr.sin_addr, hp->h_addr, hp->h_length);
  237. }
  238. /* map transport protocol name to protocol number */
  239. if ((ptrp = getprotobyname (proto)) == NULL) {
  240. printf ("Cannot map \"%s\" to protocol number\n", proto);
  241. return STATE_UNKNOWN;
  242. }
  243. /* create a socket */
  244. *sd =
  245. socket (PF_INET, (!strcmp (proto, "udp")) ? SOCK_DGRAM : SOCK_STREAM,
  246. ptrp->p_proto);
  247. if (*sd < 0) {
  248. printf ("Socket creation failed\n");
  249. return STATE_UNKNOWN;
  250. }
  251. /* open a connection */
  252. result = connect (*sd, (struct sockaddr *) &servaddr, sizeof (servaddr));
  253. if (result < 0) {
  254. switch (errno) {
  255. case ECONNREFUSED:
  256. printf ("Connection refused by host\n");
  257. break;
  258. case ETIMEDOUT:
  259. printf ("Timeout while attempting connection\n");
  260. break;
  261. case ENETUNREACH:
  262. printf ("Network is unreachable\n");
  263. break;
  264. default:
  265. printf ("Connection refused or timed out\n");
  266. }
  267. return STATE_CRITICAL;
  268. }
  269. return STATE_OK;
  270. }
  271. /* This code was taken from Fyodor's nmap utility, which was originally
  272. taken from the GLIBC 2.0.6 libraries because Solaris doesn't contain
  273. the inet_aton() funtion. */
  274. int
  275. my_inet_aton (register const char *cp, struct in_addr *addr)
  276. {
  277. register unsigned int val; /* changed from u_long --david */
  278. register int base, n;
  279. register char c;
  280. u_int parts[4];
  281. register u_int *pp = parts;
  282. c = *cp;
  283. for (;;) {
  284. /*
  285. * Collect number up to ``.''.
  286. * Values are specified as for C:
  287. * 0x=hex, 0=octal, isdigit=decimal.
  288. */
  289. if (!isdigit ((int) c))
  290. return (0);
  291. val = 0;
  292. base = 10;
  293. if (c == '0') {
  294. c = *++cp;
  295. if (c == 'x' || c == 'X')
  296. base = 16, c = *++cp;
  297. else
  298. base = 8;
  299. }
  300. for (;;) {
  301. if (isascii ((int) c) && isdigit ((int) c)) {
  302. val = (val * base) + (c - '0');
  303. c = *++cp;
  304. }
  305. else if (base == 16 && isascii ((int) c) && isxdigit ((int) c)) {
  306. val = (val << 4) | (c + 10 - (islower ((int) c) ? 'a' : 'A'));
  307. c = *++cp;
  308. }
  309. else
  310. break;
  311. }
  312. if (c == '.') {
  313. /*
  314. * Internet format:
  315. * a.b.c.d
  316. * a.b.c (with c treated as 16 bits)
  317. * a.b (with b treated as 24 bits)
  318. */
  319. if (pp >= parts + 3)
  320. return (0);
  321. *pp++ = val;
  322. c = *++cp;
  323. }
  324. else
  325. break;
  326. }
  327. /* Check for trailing characters */
  328. if (c != '\0' && (!isascii ((int) c) || !isspace ((int) c)))
  329. return (0);
  330. /* Concoct the address according to the number of parts specified */
  331. n = pp - parts + 1;
  332. switch (n) {
  333. case 0:
  334. return (0); /* initial nondigit */
  335. case 1: /* a -- 32 bits */
  336. break;
  337. case 2: /* a.b -- 8.24 bits */
  338. if (val > 0xffffff)
  339. return (0);
  340. val |= parts[0] << 24;
  341. break;
  342. case 3: /* a.b.c -- 8.8.16 bits */
  343. if (val > 0xffff)
  344. return (0);
  345. val |= (parts[0] << 24) | (parts[1] << 16);
  346. break;
  347. case 4: /* a.b.c.d -- 8.8.8.8 bits */
  348. if (val > 0xff)
  349. return (0);
  350. val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  351. break;
  352. }
  353. if (addr)
  354. addr->s_addr = htonl (val);
  355. return (1);
  356. }