pinger.c 18 KB

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