pinger.c 19 KB

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