pinger.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*____________________________________________________________________________
  2. pinger - gkrellm multiping helper app
  3. Copyright (C) 2002 Jindrich Makovicka
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, Write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ____________________________________________________________________________*/
  16. #include <sys/param.h>
  17. #include <sys/socket.h>
  18. #include <sys/file.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21. #include <netinet/in.h>
  22. #include <netinet/ip.h>
  23. #include <netinet/ip_icmp.h>
  24. #include <arpa/inet.h>
  25. #include <netdb.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <signal.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <ctype.h>
  33. #include <errno.h>
  34. #include <math.h>
  35. #include <glib.h>
  36. #define STORM_PHASE 0
  37. #define STANDBY_PHASE 1
  38. #define DEFDATALEN (64 - 8) /* default data length */
  39. #define MAXIPLEN 60
  40. #define MAXICMPLEN 76
  41. #define MAXPACKET (65536 - 60 - 8) /* max packet size */
  42. #define MAX_DUP_CHK (8 * 128)
  43. #define A(bit) h->rcvd_tbl[(bit)>>3] /* identify byte in array */
  44. #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
  45. #define SET(bit) (A(bit) |= B(bit))
  46. #define CLR(bit) (A(bit) &= (~B(bit)))
  47. #define TST(bit) (A(bit) & B(bit))
  48. int icmp_socket;
  49. static int ident; /* process id to identify our packets */
  50. static int datalen = DEFDATALEN;
  51. static long ntransmitted = 0; /* sequence # for outbound packets = #sent */
  52. u_char outpack[MAXPACKET];
  53. u_char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
  54. int packlen = DEFDATALEN + MAXIPLEN + MAXICMPLEN;
  55. int hostcnt = 0;
  56. int has_pinged;
  57. int terminated = 0;
  58. typedef struct _host_data {
  59. int nhost; // cislo poce
  60. GString *hostname, *percentage, *sent_str, *recv_str, *msg, *shortmsg;
  61. int dynamic;
  62. int dummy;
  63. struct sockaddr addr;
  64. int sent, recv, rep;
  65. int tmp_sent, tmp_recv, tmp_rep;
  66. int dupflag, error_flag;
  67. long tsum, tmp_tsum;
  68. struct icmp icp;
  69. char rcvd_tbl[MAX_DUP_CHK / 8];
  70. int phase;
  71. int counter;
  72. int updatefreq;
  73. int delay;
  74. } host_data;
  75. GList *hosts = NULL;
  76. void update_host_stats(host_data * h);
  77. void update_host_packinfo(host_data * h);
  78. static host_data *host_malloc()
  79. {
  80. host_data *h = (host_data *) g_malloc(sizeof(host_data));
  81. memset(h, 0, sizeof(host_data));
  82. h->hostname = g_string_new(NULL);
  83. h->percentage = g_string_new(NULL);
  84. h->sent_str = g_string_new(NULL);
  85. h->recv_str = g_string_new(NULL);
  86. h->msg = g_string_new(NULL);
  87. h->shortmsg = g_string_new(NULL);
  88. return h;
  89. }
  90. static void host_free(host_data * h)
  91. {
  92. g_string_free(h->hostname, TRUE);
  93. g_string_free(h->percentage, TRUE);
  94. g_string_free(h->sent_str, TRUE);
  95. g_string_free(h->recv_str, TRUE);
  96. g_string_free(h->msg, TRUE);
  97. g_string_free(h->shortmsg, TRUE);
  98. g_free(h);
  99. }
  100. static gint compare_nhost(gconstpointer a, gconstpointer b)
  101. {
  102. return ((host_data *) a)->nhost - *(int *) b;
  103. }
  104. static gint compare_nhost2(gconstpointer a, gconstpointer b)
  105. {
  106. return ((host_data *) a)->nhost - ((host_data *) b)->nhost;
  107. }
  108. static gint compare_delay(gconstpointer a, gconstpointer b)
  109. {
  110. return ((host_data *) b)->delay - ((host_data *) a)->delay;
  111. }
  112. /*
  113. * in_cksum --
  114. * Checksum routine for Internet Protocol family headers (C Version)
  115. */
  116. static int in_cksum(u_short * addr, int len)
  117. {
  118. int nleft = len;
  119. u_short *w = addr;
  120. int sum = 0;
  121. u_short answer = 0;
  122. /*
  123. * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  124. * sequential 16 bit words to it, and at the end, fold back all the
  125. * carry bits from the top 16 bits into the lower 16 bits.
  126. */
  127. while (nleft > 1) {
  128. sum += *w++;
  129. nleft -= 2;
  130. }
  131. /* mop up an odd byte, if necessary */
  132. if (nleft == 1) {
  133. *(u_char *) (&answer) = *(u_char *) w;
  134. sum += answer;
  135. }
  136. /* add back carry outs from top 16 bits to low 16 bits */
  137. sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  138. sum += (sum >> 16); /* add carry */
  139. answer = ~sum; /* truncate to 16 bits */
  140. return (answer);
  141. }
  142. static void write_result(host_data * h, gchar * msg, gchar * shortmsg)
  143. {
  144. g_string_assign(h->msg, msg);
  145. g_string_assign(h->shortmsg, shortmsg);
  146. }
  147. /*
  148. * pinger --
  149. * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
  150. * will be added on by the kernel. The ID field is our UNIX process ID,
  151. * and the sequence number is an ascending integer. The first 8 bytes
  152. * of the data portion are used to hold a UNIX "timeval" struct in VAX
  153. * byte-order, to compute the round-trip time.
  154. *
  155. * Another 4 bytes are the index of the host being pinged -JM
  156. */
  157. static void pinger(host_data * h)
  158. {
  159. struct icmp *icp;
  160. int cc;
  161. int i;
  162. has_pinged = 1;
  163. // fprintf(stderr, "pinger: pinging host No. %d\n", h->nhost);
  164. icp = (struct icmp *) outpack;
  165. icp->icmp_type = ICMP_ECHO;
  166. icp->icmp_code = 0;
  167. icp->icmp_cksum = 0;
  168. icp->icmp_seq = ntransmitted++;
  169. icp->icmp_id = ident; /* ID */
  170. h->sent++;
  171. h->tmp_sent++;
  172. CLR(icp->icmp_seq % MAX_DUP_CHK);
  173. (void) gettimeofday((struct timeval *) &outpack[8],
  174. (struct timezone *) NULL);
  175. *(int *) &outpack[8 + sizeof(struct timeval)] = h->nhost;
  176. cc = datalen + 8; /* skips ICMP portion */
  177. /* compute ICMP checksum here */
  178. icp->icmp_cksum = in_cksum((u_short *) icp, cc);
  179. i = sendto(icmp_socket, (char *) outpack, cc, 0, &h->addr,
  180. sizeof(struct sockaddr));
  181. if (i < 0 || i != cc) {
  182. write_result(h, "Error sending packet", "Err");
  183. }
  184. }
  185. /*
  186. * pr_icmph --
  187. * Print a descriptive string about an ICMP header.
  188. */
  189. static gchar *pr_icmph(struct icmp *icp)
  190. {
  191. GString *s = g_string_new(NULL);
  192. gchar *c;
  193. switch (icp->icmp_type) {
  194. case ICMP_ECHOREPLY:
  195. g_string_assign(s, "Echo Reply");
  196. /* XXX ID + Seq + Data */
  197. break;
  198. case ICMP_DEST_UNREACH:
  199. switch (icp->icmp_code) {
  200. case ICMP_NET_UNREACH:
  201. g_string_assign(s, "Destination Net Unreachable");
  202. break;
  203. case ICMP_HOST_UNREACH:
  204. g_string_assign(s, "Destination Host Unreachable");
  205. break;
  206. case ICMP_PROT_UNREACH:
  207. g_string_assign(s, "Destination Protocol Unreachable");
  208. break;
  209. case ICMP_PORT_UNREACH:
  210. g_string_assign(s, "Destination Port Unreachable");
  211. break;
  212. case ICMP_FRAG_NEEDED:
  213. g_string_assign(s, "Frag needed and DF set");
  214. break;
  215. case ICMP_SR_FAILED:
  216. g_string_assign(s, "Source Route Failed");
  217. break;
  218. case ICMP_NET_UNKNOWN:
  219. g_string_assign(s, "Network Unknown");
  220. break;
  221. case ICMP_HOST_UNKNOWN:
  222. g_string_assign(s, "Host Unknown");
  223. break;
  224. case ICMP_HOST_ISOLATED:
  225. g_string_assign(s, "Host Isolated");
  226. break;
  227. case ICMP_NET_UNR_TOS:
  228. g_string_assign(s,
  229. "Destination Network Unreachable At This TOS");
  230. break;
  231. case ICMP_HOST_UNR_TOS:
  232. g_string_assign(s, "Destination Host Unreachable At This TOS");
  233. break;
  234. #ifdef ICMP_PKT_FILTERED
  235. case ICMP_PKT_FILTERED:
  236. g_string_assign(s, "Packet Filtered");
  237. break;
  238. #endif
  239. #ifdef ICMP_PREC_VIOLATION
  240. case ICMP_PREC_VIOLATION:
  241. g_string_assign(s, "Precedence Violation");
  242. break;
  243. #endif
  244. #ifdef ICMP_PREC_CUTOFF
  245. case ICMP_PREC_CUTOFF:
  246. g_string_assign(s, "Precedence Cutoff");
  247. break;
  248. #endif
  249. default:
  250. g_string_sprintf(s, "Dest Unreachable, Unknown Code: %d",
  251. icp->icmp_code);
  252. break;
  253. }
  254. break;
  255. case ICMP_SOURCE_QUENCH:
  256. g_string_assign(s, "Source Quench");
  257. break;
  258. case ICMP_REDIRECT:
  259. switch (icp->icmp_code) {
  260. case ICMP_REDIR_NET:
  261. g_string_assign(s, "Redirect Network");
  262. break;
  263. case ICMP_REDIR_HOST:
  264. g_string_assign(s, "Redirect Host");
  265. break;
  266. case ICMP_REDIR_NETTOS:
  267. g_string_assign(s, "Redirect Type of Service and Network");
  268. break;
  269. case ICMP_REDIR_HOSTTOS:
  270. g_string_assign(s, "Redirect Type of Service and Host");
  271. break;
  272. default:
  273. g_string_sprintf(s, "Redirect, Bad Code: %d", icp->icmp_code);
  274. break;
  275. }
  276. g_string_sprintfa(s, " (New addr: %s)",
  277. inet_ntoa(icp->icmp_gwaddr));
  278. break;
  279. case ICMP_ECHO:
  280. g_string_assign(s, "Echo Request");
  281. /* XXX ID + Seq + Data */
  282. break;
  283. case ICMP_TIME_EXCEEDED:
  284. switch (icp->icmp_code) {
  285. case ICMP_EXC_TTL:
  286. g_string_assign(s, "Time to live exceeded");
  287. break;
  288. case ICMP_EXC_FRAGTIME:
  289. g_string_assign(s, "Frag reassembly time exceeded");
  290. break;
  291. default:
  292. g_string_sprintf(s, "Time exceeded, Bad Code: %d",
  293. icp->icmp_code);
  294. break;
  295. }
  296. break;
  297. case ICMP_PARAMETERPROB:
  298. g_string_sprintf(s, "Parameter problem: IP address = %s",
  299. inet_ntoa(icp->icmp_gwaddr));
  300. break;
  301. case ICMP_TIMESTAMP:
  302. g_string_assign(s, "Timestamp");
  303. /* XXX ID + Seq + 3 timestamps */
  304. break;
  305. case ICMP_TIMESTAMPREPLY:
  306. g_string_assign(s, "Timestamp Reply");
  307. /* XXX ID + Seq + 3 timestamps */
  308. break;
  309. case ICMP_INFO_REQUEST:
  310. g_string_assign(s, "Information Request");
  311. /* XXX ID + Seq */
  312. break;
  313. case ICMP_INFO_REPLY:
  314. g_string_assign(s, "Information Reply");
  315. /* XXX ID + Seq */
  316. break;
  317. #ifdef ICMP_MASKREQ
  318. case ICMP_MASKREQ:
  319. g_string_assign(s, "Address Mask Request");
  320. break;
  321. #endif
  322. #ifdef ICMP_MASKREPLY
  323. case ICMP_MASKREPLY:
  324. g_string_assign(s, "Address Mask Reply");
  325. break;
  326. #endif
  327. default:
  328. g_string_sprintf(s, "Bad ICMP type: %d", icp->icmp_type);
  329. }
  330. c = s->str;
  331. g_string_free(s, FALSE);
  332. return c;
  333. }
  334. /*
  335. * tvsub --
  336. * Subtract 2 timeval structs: out = out - in. Out is assumed to
  337. * be >= in.
  338. */
  339. static void tvsub(struct timeval *out, struct timeval *in)
  340. {
  341. if ((out->tv_usec -= in->tv_usec) < 0) {
  342. --out->tv_sec;
  343. out->tv_usec += 1000000;
  344. }
  345. out->tv_sec -= in->tv_sec;
  346. }
  347. // process a received packet
  348. void pr_pack(char *buf, int cc, struct sockaddr_in *from)
  349. {
  350. struct icmp *icp;
  351. struct ip *ip;
  352. struct timeval tv, *tp;
  353. long triptime = 0;
  354. int hlen;
  355. host_data *h;
  356. (void) gettimeofday(&tv, (struct timezone *) NULL);
  357. if (cc < datalen + ICMP_MINLEN)
  358. return;
  359. /* Check the IP header */
  360. ip = (struct ip *) buf;
  361. hlen = ip->ip_hl << 2;
  362. /* Now the ICMP part */
  363. cc -= hlen;
  364. icp = (struct icmp *) (buf + hlen);
  365. if (icp->icmp_type == ICMP_ECHOREPLY) {
  366. if (icp->icmp_id != ident)
  367. return; /* 'Twas not our ECHO */
  368. h = (host_data *) g_list_find_custom(hosts,
  369. (int *) &icp->
  370. icmp_data[sizeof
  371. (struct timeval)],
  372. compare_nhost)->data;
  373. if (h == NULL) return; /* host not found */
  374. ++h->recv;
  375. ++h->tmp_recv;
  376. tp = (struct timeval *) icp->icmp_data;
  377. tvsub(&tv, tp);
  378. triptime = tv.tv_sec * 1000000 + tv.tv_usec;
  379. h->tsum += triptime;
  380. h->tmp_tsum += triptime;
  381. if (TST(icp->icmp_seq % MAX_DUP_CHK)) {
  382. ++h->rep;
  383. ++h->tmp_rep;
  384. --h->recv;
  385. --h->tmp_recv;
  386. h->dupflag = 1;
  387. } else {
  388. SET(icp->icmp_seq % MAX_DUP_CHK);
  389. h->dupflag = 0;
  390. }
  391. } else {
  392. switch (icp->icmp_type) {
  393. case ICMP_ECHO:
  394. return;
  395. case ICMP_SOURCE_QUENCH:
  396. case ICMP_REDIRECT:
  397. case ICMP_DEST_UNREACH:
  398. case ICMP_TIME_EXCEEDED:
  399. case ICMP_PARAMETERPROB:
  400. {
  401. struct ip *iph = (struct ip *) (&icp->icmp_data);
  402. struct icmp *icp1 =
  403. (struct icmp *) ((unsigned char *) iph +
  404. iph->ip_hl * 4);
  405. int error_pkt;
  406. if (icp1->icmp_type != ICMP_ECHO ||
  407. iph->ip_src.s_addr != ip->ip_dst.s_addr ||
  408. icp1->icmp_id != ident)
  409. return;
  410. error_pkt = (icp->icmp_type != ICMP_REDIRECT &&
  411. icp->icmp_type != ICMP_SOURCE_QUENCH);
  412. h = (host_data *) g_list_find_custom(hosts,
  413. (int *) &icp1->
  414. icmp_data[sizeof
  415. (struct
  416. timeval)],
  417. compare_nhost)->data;
  418. if (h) {
  419. h->icp = *icp;
  420. h->error_flag = 1;
  421. }
  422. }
  423. }
  424. }
  425. }
  426. void clear_tmp_flags(host_data * h)
  427. {
  428. h->tmp_tsum = 0;
  429. h->tmp_sent = 0;
  430. h->tmp_recv = 0;
  431. h->tmp_rep = 0;
  432. h->dupflag = 0;
  433. h->error_flag = 0;
  434. }
  435. void dump_host(host_data * h)
  436. {
  437. // fprintf(stderr, "dump_host: %d\n", h->nhost);
  438. printf("%s\n", h->percentage->str);
  439. printf("%s\n", h->sent_str->str);
  440. printf("%s\n", h->recv_str->str);
  441. printf("%s\n", h->msg->str);
  442. printf("%s\n", h->shortmsg->str);
  443. }
  444. int hostname_to_addr(const char *hostname, struct sockaddr *addr)
  445. {
  446. struct addrinfo hints;
  447. struct addrinfo *result = 0, *rp = 0;
  448. int s, ret = -1;
  449. memset(&hints, 0, sizeof(struct addrinfo));
  450. hints.ai_family = AF_INET; /* Allow IPv4 */
  451. hints.ai_socktype = 0;
  452. hints.ai_flags = 0;
  453. hints.ai_protocol = 0; /* Any protocol */
  454. hints.ai_canonname = NULL;
  455. hints.ai_addr = NULL;
  456. hints.ai_next = NULL;
  457. s = getaddrinfo(hostname, NULL, &hints, &result);
  458. if (s != 0) {
  459. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  460. return -1;
  461. }
  462. for (rp = result; rp != NULL; rp = rp->ai_next) {
  463. fprintf(stderr, "%d\n", rp->ai_addrlen);
  464. if (rp->ai_addr->sa_family == AF_INET) {
  465. *addr = *rp->ai_addr;
  466. ret = 0;
  467. break;
  468. }
  469. }
  470. freeaddrinfo(result);
  471. return ret;
  472. }
  473. // recheck the dns (needed for dialup users or dynamic DNS)
  474. int update_dns(host_data *h)
  475. {
  476. int res;
  477. struct sockaddr addr;
  478. res = hostname_to_addr(h->hostname->str, &addr);
  479. if (res == 0) {
  480. h->addr = addr;
  481. return 0;
  482. }
  483. return 1;
  484. }
  485. void ping_host(host_data * h)
  486. {
  487. gchar *msg;
  488. if (h->dummy) {
  489. if (h->counter == 120) {
  490. if (update_dns(h) == 0) {
  491. h->dummy = 0;
  492. update_host_stats(h);
  493. clear_tmp_flags(h);
  494. update_host_packinfo(h);
  495. h->phase = STORM_PHASE;
  496. } else {
  497. h->phase = STANDBY_PHASE;
  498. }
  499. h->delay = 0;
  500. h->counter = -1;
  501. }
  502. h->counter++;
  503. return;
  504. }
  505. if (!h->dummy && h->dynamic && h->counter == 0) {
  506. update_dns(h);
  507. }
  508. if (h->error_flag) {
  509. msg = pr_icmph(&h->icp);
  510. write_result(h, msg, "Err");
  511. g_free(msg);
  512. }
  513. // fprintf(stderr, "pinger: ping_host, No. %d, delay = %d\n", h->nhost, h->delay);
  514. switch (h->phase) {
  515. case STORM_PHASE:
  516. if (h->counter == 7 || h->counter == 15 || h->counter == 23) {
  517. update_host_stats(h);
  518. clear_tmp_flags(h);
  519. }
  520. if ((h->counter >= 0 && h->counter < 4)
  521. || (h->counter >= 8 && h->counter < 12)
  522. || (h->counter >= 16 && h->counter < 20)) {
  523. if (has_pinged) {
  524. h->delay++;
  525. goto dontpingyet;
  526. }
  527. pinger(h);
  528. h->delay = 0;
  529. }
  530. if (h->counter == 59) {
  531. h->counter = -1;
  532. h->phase = STANDBY_PHASE;
  533. }
  534. break;
  535. case STANDBY_PHASE:
  536. if (h->counter == 7) {
  537. update_host_stats(h);
  538. clear_tmp_flags(h);
  539. }
  540. if (h->counter >= 0 && h->counter < 4) {
  541. if (has_pinged) {
  542. h->delay++;
  543. goto dontpingyet;
  544. }
  545. pinger(h);
  546. h->delay = 0;
  547. }
  548. if (h->counter == h->updatefreq) {
  549. h->counter = -1;
  550. h->phase = STANDBY_PHASE;
  551. }
  552. break;
  553. }
  554. h->counter++;
  555. dontpingyet:
  556. update_host_packinfo(h);
  557. }
  558. gint timeout_callback()
  559. {
  560. has_pinged = 0;
  561. hosts = g_list_sort(hosts, compare_delay);
  562. g_list_foreach(hosts, (GFunc) ping_host, NULL);
  563. hosts = g_list_sort(hosts, compare_nhost2);
  564. g_list_foreach(hosts, (GFunc) dump_host, NULL);
  565. fflush(stdout);
  566. return TRUE;
  567. }
  568. void receiver()
  569. {
  570. int cc;
  571. struct sockaddr_in from;
  572. size_t fromlen;
  573. struct timeval tv,tv_old,tv_new;
  574. fd_set rfds;
  575. int avail;
  576. FD_ZERO(&rfds);
  577. FD_SET(icmp_socket, &rfds);
  578. tv.tv_usec = 500000;
  579. tv.tv_sec = 0;
  580. gettimeofday(&tv_old, NULL);
  581. fromlen = sizeof(from);
  582. for (;!terminated;) {
  583. FD_ZERO(&rfds);
  584. FD_SET(icmp_socket, &rfds);
  585. tv.tv_usec = 100000;
  586. tv.tv_sec = 0;
  587. avail = select(icmp_socket + 1, &rfds, NULL, NULL, &tv);
  588. if (avail) {
  589. if ((cc = recvfrom(icmp_socket, (char *) packet, packlen, 0,
  590. (struct sockaddr *) &from, &fromlen)) < 0) {
  591. perror("ping: recvfrom");
  592. } else {
  593. pr_pack((char *) packet, cc, &from);
  594. }
  595. }
  596. gettimeofday(&tv_new, NULL);
  597. tvsub(&tv_new, &tv_old);
  598. if (tv_new.tv_sec >= 1) {
  599. gettimeofday(&tv_old, NULL);
  600. timeout_callback();
  601. }
  602. }
  603. }
  604. void update_host_packinfo(host_data * h)
  605. {
  606. g_string_sprintf(h->sent_str, "%d", h->sent);
  607. g_string_sprintf(h->recv_str, "%d", h->recv);
  608. }
  609. void update_host_stats(host_data * h)
  610. {
  611. long trip;
  612. GString *s = g_string_new(NULL);
  613. GString *s2 = g_string_new(NULL);
  614. if (h->tmp_sent > 0) {
  615. g_string_sprintf(s, "%d", h->tmp_recv * 100 / h->tmp_sent);
  616. g_string_assign(h->percentage, s->str);
  617. } else {
  618. g_string_assign(h->percentage, "");
  619. }
  620. if (h->tmp_recv > 0) {
  621. trip = h->tmp_tsum / (h->tmp_recv + h->tmp_rep);
  622. if (trip >= 1000000) {
  623. g_string_sprintf(s, "%ld.%03ld s", trip / 1000000,
  624. (trip % 1000000) / 1000);
  625. g_string_sprintf(s2, ">s");
  626. } else if (trip >= 10000) {
  627. g_string_sprintf(s, "%ld.%03ld ms", trip / 1000, trip % 1000);
  628. g_string_sprintf(s2, "%ld", trip / 1000);
  629. } else if (trip >= 1000) {
  630. g_string_sprintf(s, "%ld.%03ld ms", trip / 1000, trip % 1000);
  631. g_string_sprintf(s2, "%ld.%01ld", trip / 1000,
  632. (trip % 1000) / 100);
  633. } else {
  634. g_string_sprintf(s, "0.%01ld ms", trip / 100);
  635. g_string_sprintf(s2, "0.%01ld", trip / 100);
  636. }
  637. write_result(h, s->str, s2->str);
  638. }
  639. g_string_free(s, TRUE);
  640. g_string_free(s2, TRUE);
  641. if (h->dummy) {
  642. write_result(h, "Dummy host", "##");
  643. return;
  644. }
  645. if (!h->error_flag) {
  646. if (h->tmp_sent > 0 && h->tmp_recv == 0)
  647. write_result(h, "Request timed out", "TO");
  648. }
  649. }
  650. void append_host(struct sockaddr addr, char * hostname, char * updatefreq, char * dynamic, int dummy)
  651. {
  652. host_data *h = host_malloc();
  653. h->addr = addr;
  654. g_string_assign(h->hostname, hostname);
  655. h->dummy = dummy;
  656. h->nhost = hostcnt++;
  657. h->sent = 0;
  658. h->recv = 0;
  659. h->rep = 0;
  660. h->dupflag = 0;
  661. h->error_flag = 0;
  662. h->tsum = 0;
  663. h->tmp_sent = 0;
  664. h->tmp_recv = 0;
  665. h->tmp_rep = 0;
  666. h->tmp_tsum = 0;
  667. h->phase = STORM_PHASE;
  668. h->counter = 0;
  669. h->delay = 0;
  670. if (updatefreq) {
  671. h->updatefreq = atoi(updatefreq);
  672. if (h->updatefreq < 30) {
  673. h->updatefreq = 30;
  674. }
  675. if (h->updatefreq > 3600) {
  676. h->updatefreq = 3600;
  677. }
  678. } else {
  679. h->updatefreq = 59;
  680. }
  681. h->dynamic = atoi(dynamic) ? 1 : 0;
  682. hosts = g_list_append(hosts, h);
  683. update_host_stats(h);
  684. clear_tmp_flags(h);
  685. update_host_packinfo(h);
  686. }
  687. void free_hosts()
  688. {
  689. g_list_foreach(hosts, (GFunc) host_free, NULL);
  690. g_list_free(hosts);
  691. }
  692. void term_signal(int signum, siginfo_t *info, void *data)
  693. {
  694. terminated = 1;
  695. }
  696. int main(int argc, char **argv)
  697. {
  698. int i;
  699. struct sigaction sig;
  700. struct protoent *proto;
  701. if (!(proto = getprotobyname("icmp"))) {
  702. (void) fprintf(stderr, "pinger: unknown protocol icmp.\n");
  703. exit(2);
  704. }
  705. if ((icmp_socket = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
  706. if (errno == EPERM) {
  707. fprintf(stderr, "pinger: must run as root\n");
  708. } else
  709. perror("pinger: socket");
  710. exit(2);
  711. }
  712. setuid(getuid());
  713. fcntl(icmp_socket, F_SETFL, O_NONBLOCK);
  714. ident = getpid() & 0xFFFF;
  715. for (i = 1; i < argc - 2; i += 3) {
  716. struct sockaddr addr;
  717. int res;
  718. res = hostname_to_addr(argv[i], &addr);
  719. if (res == 0) {
  720. append_host(addr, argv[i], argv[i+1], argv[i+2], 0);
  721. } else if (i <= argc-3) {
  722. memset(&addr, 0, sizeof(addr));
  723. addr.sa_family = AF_INET;
  724. append_host(addr, argv[i], argv[i+1], argv[i+2], 1); // dummy host
  725. }
  726. }
  727. sigfillset(&sig.sa_mask);
  728. sig.sa_flags = SA_SIGINFO | SA_RESTART;
  729. sig.sa_sigaction = term_signal;
  730. sigaction(SIGINT, &sig, 0);
  731. sigaction(SIGTERM, &sig, 0);
  732. sigaction(SIGPIPE, &sig, 0);
  733. sigaction(SIGHUP, &sig, 0);
  734. receiver();
  735. free_hosts();
  736. return 0;
  737. }