pinger.c 18 KB

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