pinger.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 <netinet/ip6.h>
  25. #include <netinet/icmp6.h>
  26. #include <arpa/inet.h>
  27. #include <netdb.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include <signal.h>
  33. #include <poll.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <ctype.h>
  37. #include <errno.h>
  38. #include <math.h>
  39. #include <glib.h>
  40. #define STORM_PHASE 0
  41. #define STANDBY_PHASE 1
  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, icmp6_socket;
  49. static int ident; /* process id to identify our packets */
  50. static long ntransmitted = 0; /* sequence # for outbound packets = #sent */
  51. static u_char outpack[56];
  52. static u_char packet[1024];
  53. int hostcnt = 0;
  54. int has_pinged;
  55. int terminated = 0;
  56. typedef struct _host_data {
  57. int nhost; // cislo poce
  58. GString *hostname, *percentage, *sent_str, *recv_str, *msg, *shortmsg;
  59. int dynamic;
  60. int dummy;
  61. union {
  62. struct sockaddr addr;
  63. struct sockaddr_in in4;
  64. struct sockaddr_in6 in6;
  65. } addr;
  66. int sent, recv, rep;
  67. int tmp_sent, tmp_recv, tmp_rep;
  68. int dupflag, error_flag;
  69. long tsum, tmp_tsum;
  70. union {
  71. struct icmp v4;
  72. struct icmp6_hdr v6;
  73. } icmp;
  74. u_char rcvd_tbl[MAX_DUP_CHK / 8];
  75. int phase;
  76. int counter;
  77. int updatefreq;
  78. int delay;
  79. } host_data;
  80. GList *hosts = NULL;
  81. void update_host_stats(host_data * h);
  82. void update_host_packinfo(host_data * h);
  83. static host_data *host_malloc()
  84. {
  85. host_data *h = (host_data *) g_malloc(sizeof(host_data));
  86. memset(h, 0, sizeof(host_data));
  87. h->hostname = g_string_new(NULL);
  88. h->percentage = g_string_new(NULL);
  89. h->sent_str = g_string_new(NULL);
  90. h->recv_str = g_string_new(NULL);
  91. h->msg = g_string_new(NULL);
  92. h->shortmsg = g_string_new(NULL);
  93. return h;
  94. }
  95. static void host_free(host_data * h)
  96. {
  97. g_string_free(h->hostname, TRUE);
  98. g_string_free(h->percentage, TRUE);
  99. g_string_free(h->sent_str, TRUE);
  100. g_string_free(h->recv_str, TRUE);
  101. g_string_free(h->msg, TRUE);
  102. g_string_free(h->shortmsg, TRUE);
  103. g_free(h);
  104. }
  105. static gint compare_nhost(gconstpointer a, gconstpointer b)
  106. {
  107. return ((host_data *) a)->nhost - *(int32_t *) b;
  108. }
  109. static gint compare_nhost2(gconstpointer a, gconstpointer b)
  110. {
  111. return ((host_data *) a)->nhost - ((host_data *) b)->nhost;
  112. }
  113. static gint compare_delay(gconstpointer a, gconstpointer b)
  114. {
  115. return ((host_data *) b)->delay - ((host_data *) a)->delay;
  116. }
  117. /*
  118. * in_cksum --
  119. * Checksum routine for Internet Protocol family headers (C Version)
  120. */
  121. static int in_cksum(u_char * addr, int len)
  122. {
  123. int nleft = len;
  124. u_short *w = (u_short *)addr;
  125. int sum = 0;
  126. u_short answer = 0;
  127. /*
  128. * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  129. * sequential 16 bit words to it, and at the end, fold back all the
  130. * carry bits from the top 16 bits into the lower 16 bits.
  131. */
  132. while (nleft > 1) {
  133. sum += *w++;
  134. nleft -= 2;
  135. }
  136. /* mop up an odd byte, if necessary */
  137. if (nleft == 1) {
  138. *(u_char *) (&answer) = *(u_char *) w;
  139. sum += answer;
  140. }
  141. /* add back carry outs from top 16 bits to low 16 bits */
  142. sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  143. sum += (sum >> 16); /* add carry */
  144. answer = ~sum; /* truncate to 16 bits */
  145. return (answer);
  146. }
  147. static void write_result(host_data * h, gchar * msg, gchar * shortmsg)
  148. {
  149. g_string_assign(h->msg, msg);
  150. g_string_assign(h->shortmsg, shortmsg);
  151. }
  152. static void host_check_dup(host_data * h, int seq)
  153. {
  154. if (TST(seq % MAX_DUP_CHK)) {
  155. ++h->rep;
  156. ++h->tmp_rep;
  157. --h->recv;
  158. --h->tmp_recv;
  159. h->dupflag = 1;
  160. } else {
  161. SET(seq % MAX_DUP_CHK);
  162. h->dupflag = 0;
  163. }
  164. }
  165. static void set_packet_data(u_char *buf, host_data *h)
  166. {
  167. (void) gettimeofday((struct timeval *) buf,
  168. (struct timezone *) NULL);
  169. *(int32_t *) (buf + sizeof(struct timeval)) = h->nhost;
  170. }
  171. static host_data * get_packet_data(u_char *buf, host_data **h, struct timeval *tv)
  172. {
  173. GList *l;
  174. int32_t *hidp = (int32_t *) (buf + sizeof(struct timeval));
  175. *tv = *(struct timeval *) buf;
  176. l = g_list_find_custom(hosts, hidp, compare_nhost);
  177. if (l) {
  178. *h = (host_data *) l->data;
  179. } else {
  180. *h = 0;
  181. fprintf(stderr, "Unknown host ID (%" PRIi32 ")\n", *hidp);
  182. }
  183. return *h;
  184. }
  185. /*
  186. * pinger --
  187. * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
  188. * will be added on by the kernel. The ID field is our UNIX process ID,
  189. * and the sequence number is an ascending integer. The first 8 bytes
  190. * of the data portion are used to hold a UNIX "timeval" struct in VAX
  191. * byte-order, to compute the round-trip time.
  192. *
  193. * Another 4 bytes are the index of the host being pinged -JM
  194. */
  195. static void pinger4(host_data * h)
  196. {
  197. struct icmp *icp;
  198. int i;
  199. has_pinged = 1;
  200. icp = (struct icmp *) outpack;
  201. icp->icmp_type = ICMP_ECHO;
  202. icp->icmp_code = 0;
  203. icp->icmp_cksum = 0;
  204. icp->icmp_seq = htons(ntransmitted++);
  205. icp->icmp_id = ident; /* ID */
  206. h->sent++;
  207. h->tmp_sent++;
  208. CLR(icp->icmp_seq % MAX_DUP_CHK);
  209. set_packet_data(icp->icmp_data, h);
  210. /* compute ICMP checksum here */
  211. icp->icmp_cksum = in_cksum(outpack, sizeof(outpack));
  212. i = sendto(icmp_socket, outpack, sizeof(outpack), 0,
  213. (struct sockaddr *)&h->addr, sizeof(h->addr));
  214. if (i < 0 || i != sizeof(outpack)) {
  215. perror("pinger: sendto");
  216. write_result(h, "Error sending packet", "Err");
  217. }
  218. }
  219. static void pinger6(host_data * h)
  220. {
  221. struct icmp6_hdr *icmp;
  222. int i;
  223. has_pinged = 1;
  224. icmp = (struct icmp6_hdr *) outpack;
  225. icmp->icmp6_type = ICMP6_ECHO_REQUEST;
  226. icmp->icmp6_code = 0;
  227. icmp->icmp6_cksum = 0;
  228. icmp->icmp6_seq = htons(ntransmitted++);
  229. icmp->icmp6_id = ident; /* ID */
  230. h->sent++;
  231. h->tmp_sent++;
  232. CLR(icmp->icmp6_seq % MAX_DUP_CHK);
  233. set_packet_data(outpack + sizeof(*icmp), h);
  234. i = sendto(icmp6_socket, outpack, sizeof(outpack), 0,
  235. &h->addr.addr, sizeof(struct sockaddr_in6));
  236. if (i < 0 || i != sizeof(outpack)) {
  237. perror("pinger: sendto");
  238. write_result(h, "Error sending packet", "Err");
  239. }
  240. }
  241. static void pinger(host_data * h)
  242. {
  243. if (h->addr.addr.sa_family == AF_INET)
  244. pinger4(h);
  245. else if (h->addr.addr.sa_family == AF_INET6)
  246. pinger6(h);
  247. }
  248. /*
  249. * pr_icmph --
  250. * Print a descriptive string about an ICMP header.
  251. */
  252. static gchar *pr_icmph(struct icmp *icp)
  253. {
  254. GString *s = g_string_new(NULL);
  255. gchar *c;
  256. switch (icp->icmp_type) {
  257. case ICMP_ECHOREPLY:
  258. g_string_assign(s, "Echo Reply");
  259. /* XXX ID + Seq + Data */
  260. break;
  261. case ICMP_DEST_UNREACH:
  262. switch (icp->icmp_code) {
  263. case ICMP_NET_UNREACH:
  264. g_string_assign(s, "Destination Net Unreachable");
  265. break;
  266. case ICMP_HOST_UNREACH:
  267. g_string_assign(s, "Destination Host Unreachable");
  268. break;
  269. case ICMP_PROT_UNREACH:
  270. g_string_assign(s, "Destination Protocol Unreachable");
  271. break;
  272. case ICMP_PORT_UNREACH:
  273. g_string_assign(s, "Destination Port Unreachable");
  274. break;
  275. case ICMP_FRAG_NEEDED:
  276. g_string_assign(s, "Frag needed and DF set");
  277. break;
  278. case ICMP_SR_FAILED:
  279. g_string_assign(s, "Source Route Failed");
  280. break;
  281. case ICMP_NET_UNKNOWN:
  282. g_string_assign(s, "Network Unknown");
  283. break;
  284. case ICMP_HOST_UNKNOWN:
  285. g_string_assign(s, "Host Unknown");
  286. break;
  287. case ICMP_HOST_ISOLATED:
  288. g_string_assign(s, "Host Isolated");
  289. break;
  290. case ICMP_NET_UNR_TOS:
  291. g_string_assign(s,
  292. "Destination Network Unreachable At This TOS");
  293. break;
  294. case ICMP_HOST_UNR_TOS:
  295. g_string_assign(s, "Destination Host Unreachable At This TOS");
  296. break;
  297. #ifdef ICMP_PKT_FILTERED
  298. case ICMP_PKT_FILTERED:
  299. g_string_assign(s, "Packet Filtered");
  300. break;
  301. #endif
  302. #ifdef ICMP_PREC_VIOLATION
  303. case ICMP_PREC_VIOLATION:
  304. g_string_assign(s, "Precedence Violation");
  305. break;
  306. #endif
  307. #ifdef ICMP_PREC_CUTOFF
  308. case ICMP_PREC_CUTOFF:
  309. g_string_assign(s, "Precedence Cutoff");
  310. break;
  311. #endif
  312. default:
  313. g_string_sprintf(s, "Dest Unreachable, Unknown Code: %d",
  314. icp->icmp_code);
  315. break;
  316. }
  317. break;
  318. case ICMP_SOURCE_QUENCH:
  319. g_string_assign(s, "Source Quench");
  320. break;
  321. case ICMP_REDIRECT:
  322. switch (icp->icmp_code) {
  323. case ICMP_REDIR_NET:
  324. g_string_assign(s, "Redirect Network");
  325. break;
  326. case ICMP_REDIR_HOST:
  327. g_string_assign(s, "Redirect Host");
  328. break;
  329. case ICMP_REDIR_NETTOS:
  330. g_string_assign(s, "Redirect Type of Service and Network");
  331. break;
  332. case ICMP_REDIR_HOSTTOS:
  333. g_string_assign(s, "Redirect Type of Service and Host");
  334. break;
  335. default:
  336. g_string_sprintf(s, "Redirect, Bad Code: %d", icp->icmp_code);
  337. break;
  338. }
  339. g_string_sprintfa(s, " (New addr: %s)",
  340. inet_ntoa(icp->icmp_gwaddr));
  341. break;
  342. case ICMP_ECHO:
  343. g_string_assign(s, "Echo Request");
  344. /* XXX ID + Seq + Data */
  345. break;
  346. case ICMP_TIME_EXCEEDED:
  347. switch (icp->icmp_code) {
  348. case ICMP_EXC_TTL:
  349. g_string_assign(s, "Time to live exceeded");
  350. break;
  351. case ICMP_EXC_FRAGTIME:
  352. g_string_assign(s, "Frag reassembly time exceeded");
  353. break;
  354. default:
  355. g_string_sprintf(s, "Time exceeded, Bad Code: %d",
  356. icp->icmp_code);
  357. break;
  358. }
  359. break;
  360. case ICMP_PARAMETERPROB:
  361. g_string_sprintf(s, "Parameter problem: IP address = %s",
  362. inet_ntoa(icp->icmp_gwaddr));
  363. break;
  364. case ICMP_TIMESTAMP:
  365. g_string_assign(s, "Timestamp");
  366. /* XXX ID + Seq + 3 timestamps */
  367. break;
  368. case ICMP_TIMESTAMPREPLY:
  369. g_string_assign(s, "Timestamp Reply");
  370. /* XXX ID + Seq + 3 timestamps */
  371. break;
  372. case ICMP_INFO_REQUEST:
  373. g_string_assign(s, "Information Request");
  374. /* XXX ID + Seq */
  375. break;
  376. case ICMP_INFO_REPLY:
  377. g_string_assign(s, "Information Reply");
  378. /* XXX ID + Seq */
  379. break;
  380. #ifdef ICMP_MASKREQ
  381. case ICMP_MASKREQ:
  382. g_string_assign(s, "Address Mask Request");
  383. break;
  384. #endif
  385. #ifdef ICMP_MASKREPLY
  386. case ICMP_MASKREPLY:
  387. g_string_assign(s, "Address Mask Reply");
  388. break;
  389. #endif
  390. default:
  391. g_string_sprintf(s, "Bad ICMP type: %d", icp->icmp_type);
  392. }
  393. c = s->str;
  394. g_string_free(s, FALSE);
  395. return c;
  396. }
  397. /*
  398. * pr_icmph --
  399. * Print a descriptive string about an ICMPv6 header.
  400. */
  401. static gchar *pr_icmph6(struct icmp6_hdr *icmp)
  402. {
  403. GString *s = g_string_new(NULL);
  404. gchar *c;
  405. switch (icmp->icmp6_type) {
  406. case ICMP6_ECHO_REPLY:
  407. g_string_assign(s, "Echo Reply");
  408. /* XXX ID + Seq + Data */
  409. break;
  410. case ICMP6_DST_UNREACH:
  411. switch (icmp->icmp6_code) {
  412. case ICMP6_DST_UNREACH_NOROUTE:
  413. g_string_assign(s, "No route to destination");
  414. break;
  415. case ICMP6_DST_UNREACH_ADMIN:
  416. g_string_assign(s, "Communication with destination administratively prohibited");
  417. break;
  418. case ICMP6_DST_UNREACH_BEYONDSCOPE:
  419. g_string_assign(s, "Beyond scope of source address");
  420. break;
  421. case ICMP6_DST_UNREACH_ADDR:
  422. g_string_assign(s, "Address unreachable");
  423. break;
  424. case ICMP6_DST_UNREACH_NOPORT:
  425. g_string_assign(s, "Bad port");
  426. break;
  427. default:
  428. g_string_sprintf(s, "Dest Unreachable, Unknown Code: %d",
  429. icmp->icmp6_code);
  430. break;
  431. }
  432. break;
  433. case ICMP6_ECHO_REQUEST:
  434. g_string_assign(s, "Echo Request");
  435. /* XXX ID + Seq + Data */
  436. break;
  437. case ICMP6_TIME_EXCEEDED:
  438. switch (icmp->icmp6_code) {
  439. case ICMP6_TIME_EXCEED_TRANSIT:
  440. g_string_assign(s, "Hop Limit == 0 in transit");
  441. break;
  442. case ICMP6_TIME_EXCEED_REASSEMBLY:
  443. g_string_assign(s, "Frag reassembly time exceeded");
  444. break;
  445. default:
  446. g_string_sprintf(s, "Time exceeded, Bad Code: %d",
  447. icmp->icmp6_code);
  448. break;
  449. }
  450. break;
  451. case ICMP_PARAMETERPROB:
  452. switch (icmp->icmp6_code) {
  453. case ICMP6_PARAMPROB_HEADER:
  454. g_string_assign(s, "Erroneous header field");
  455. break;
  456. case ICMP6_PARAMPROB_NEXTHEADER:
  457. g_string_assign(s, "Unrecognized Next Header");
  458. break;
  459. case ICMP6_PARAMPROB_OPTION:
  460. g_string_assign(s, "Unrecognized IPv6 option");
  461. break;
  462. default:
  463. g_string_sprintf(s, "Parameter problem, Unknown Code: %d",
  464. icmp->icmp6_code);
  465. break;
  466. }
  467. break;
  468. case ICMP6_PACKET_TOO_BIG:
  469. g_string_sprintf(s, "Packet too big, Bad Code: %d",
  470. icmp->icmp6_code);
  471. break;
  472. default:
  473. g_string_sprintf(s, "Bad ICMP type: %d", icmp->icmp6_type);
  474. }
  475. c = s->str;
  476. g_string_free(s, FALSE);
  477. return c;
  478. }
  479. /*
  480. * tvsub --
  481. * Subtract 2 timeval structs: out = out - in. Out is assumed to
  482. * be >= in.
  483. */
  484. static void tvsub(struct timeval *out, struct timeval *in)
  485. {
  486. if ((out->tv_usec -= in->tv_usec) < 0) {
  487. --out->tv_sec;
  488. out->tv_usec += 1000000;
  489. }
  490. out->tv_sec -= in->tv_sec;
  491. }
  492. // process a received packet
  493. void pr_pack(u_char *buf, int cc, struct sockaddr_in *from)
  494. {
  495. struct icmp *icp;
  496. struct ip *ip;
  497. struct timeval tv, tvs;
  498. long triptime = 0;
  499. int hlen;
  500. host_data *h;
  501. (void) gettimeofday(&tv, (struct timezone *) NULL);
  502. if (cc < sizeof(outpack))
  503. return;
  504. /* Check the IP header */
  505. ip = (struct ip *) buf;
  506. hlen = ip->ip_hl << 2;
  507. /* Now the ICMP part */
  508. cc -= hlen;
  509. icp = (struct icmp *) (buf + hlen);
  510. if (icp->icmp_type == ICMP_ECHOREPLY) {
  511. if (icp->icmp_id != ident)
  512. return; /* 'Twas not our ECHO */
  513. if (!get_packet_data(icp->icmp_data, &h, &tvs))
  514. return;
  515. ++h->recv;
  516. ++h->tmp_recv;
  517. tvsub(&tv, &tvs);
  518. triptime = tv.tv_sec * 1000000 + tv.tv_usec;
  519. h->tsum += triptime;
  520. h->tmp_tsum += triptime;
  521. host_check_dup(h, ntohs(icp->icmp_seq));
  522. } else {
  523. switch (icp->icmp_type) {
  524. case ICMP_ECHO:
  525. return;
  526. case ICMP_DEST_UNREACH:
  527. case ICMP_TIME_EXCEEDED:
  528. case ICMP_PARAMETERPROB:
  529. {
  530. struct ip *iph = (struct ip *) (&icp->icmp_data);
  531. struct icmp *icp1 =
  532. (struct icmp *) ((u_char *) iph +
  533. iph->ip_hl * 4);
  534. if (icp1->icmp_type != ICMP_ECHO ||
  535. iph->ip_src.s_addr != ip->ip_dst.s_addr ||
  536. icp1->icmp_id != ident)
  537. {
  538. return;
  539. }
  540. if (!get_packet_data(icp1->icmp_data, &h, &tvs))
  541. return;
  542. h->icmp.v4 = *icp;
  543. h->error_flag = 1;
  544. }
  545. }
  546. }
  547. }
  548. // process a received packet
  549. void pr_pack6(u_char *buf, int cc, struct sockaddr_in6 *from)
  550. {
  551. struct icmp6_hdr *icmp;
  552. u_char *data;
  553. struct timeval tv, tvs;
  554. long triptime = 0;
  555. host_data *h;
  556. (void) gettimeofday(&tv, (struct timezone *) NULL);
  557. if (cc < sizeof(outpack))
  558. return;
  559. /* Now the ICMP part */
  560. icmp = (struct icmp6_hdr *) buf;
  561. data = (u_char *)(icmp + 1);
  562. if (icmp->icmp6_type == ICMP6_ECHO_REPLY) {
  563. if (icmp->icmp6_id != ident)
  564. return; /* 'Twas not our ECHO */
  565. if (!get_packet_data(data, &h, &tvs))
  566. return;
  567. ++h->recv;
  568. ++h->tmp_recv;
  569. tvsub(&tv, &tvs);
  570. triptime = tv.tv_sec * 1000000 + tv.tv_usec;
  571. h->tsum += triptime;
  572. h->tmp_tsum += triptime;
  573. host_check_dup(h, ntohs(icmp->icmp6_seq));
  574. } else {
  575. switch (icmp->icmp6_type) {
  576. case ICMP6_ECHO_REPLY:
  577. return;
  578. case ICMP6_PACKET_TOO_BIG:
  579. case ICMP6_DST_UNREACH:
  580. case ICMP6_TIME_EXCEEDED:
  581. case ICMP6_PARAM_PROB:
  582. {
  583. struct ip6_hdr *orig_ip = (struct ip6_hdr *) (icmp + 1);
  584. struct icmp6_hdr *orig_icmp = (struct icmp6_hdr *) (orig_ip + 1);
  585. u_char *orig_data = (u_char *)(orig_icmp + 1);
  586. if (orig_icmp->icmp6_id != ident
  587. || orig_ip->ip6_nxt != IPPROTO_ICMPV6
  588. || orig_icmp->icmp6_type != ICMP6_ECHO_REQUEST)
  589. {
  590. return;
  591. }
  592. if (!get_packet_data(orig_data, &h, &tvs))
  593. return;
  594. if (!IN6_ARE_ADDR_EQUAL (&orig_ip->ip6_dst, &h->addr.in6.sin6_addr))
  595. return;
  596. h->icmp.v6 = *icmp;
  597. h->error_flag = 1;
  598. }
  599. }
  600. }
  601. }
  602. void clear_tmp_flags(host_data * h)
  603. {
  604. h->tmp_tsum = 0;
  605. h->tmp_sent = 0;
  606. h->tmp_recv = 0;
  607. h->tmp_rep = 0;
  608. h->dupflag = 0;
  609. h->error_flag = 0;
  610. }
  611. void dump_host(host_data * h)
  612. {
  613. printf("%s\n", h->percentage->str);
  614. printf("%s\n", h->sent_str->str);
  615. printf("%s\n", h->recv_str->str);
  616. printf("%s\n", h->msg->str);
  617. printf("%s\n", h->shortmsg->str);
  618. }
  619. int hostname_to_addr(const char *hostname, struct sockaddr *addr)
  620. {
  621. struct addrinfo hints;
  622. struct addrinfo *result = 0, *rp = 0;
  623. int s, ret = -1;
  624. memset(&hints, 0, sizeof(struct addrinfo));
  625. hints.ai_family = AF_UNSPEC;
  626. hints.ai_socktype = 0;
  627. hints.ai_flags = 0;
  628. hints.ai_protocol = 0; /* Any protocol */
  629. hints.ai_canonname = NULL;
  630. hints.ai_addr = NULL;
  631. hints.ai_next = NULL;
  632. s = getaddrinfo(hostname, NULL, &hints, &result);
  633. if (s != 0) {
  634. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  635. return -1;
  636. }
  637. for (rp = result; rp != NULL; rp = rp->ai_next) {
  638. if (rp->ai_addr->sa_family == AF_INET
  639. || rp->ai_addr->sa_family == AF_INET6)
  640. {
  641. if (rp->ai_addr->sa_family == AF_INET) {
  642. assert(rp->ai_addrlen == sizeof(struct sockaddr_in));
  643. }
  644. if (rp->ai_addr->sa_family == AF_INET6) {
  645. assert(rp->ai_addrlen == sizeof(struct sockaddr_in6));
  646. }
  647. memcpy(addr, rp->ai_addr, rp->ai_addrlen);
  648. ret = 0;
  649. break;
  650. }
  651. }
  652. freeaddrinfo(result);
  653. return ret;
  654. }
  655. // recheck the dns (needed for dialup users or dynamic DNS)
  656. int update_dns(host_data *h)
  657. {
  658. return hostname_to_addr(h->hostname->str, &h->addr.addr);
  659. }
  660. void ping_host(host_data * h)
  661. {
  662. gchar *msg;
  663. if (h->dummy) {
  664. if (h->counter == 120) {
  665. if (update_dns(h) == 0) {
  666. h->dummy = 0;
  667. update_host_stats(h);
  668. clear_tmp_flags(h);
  669. update_host_packinfo(h);
  670. h->phase = STORM_PHASE;
  671. } else {
  672. h->phase = STANDBY_PHASE;
  673. }
  674. h->delay = 0;
  675. h->counter = -1;
  676. }
  677. h->counter++;
  678. return;
  679. }
  680. if (!h->dummy && h->dynamic && h->counter == 0) {
  681. update_dns(h);
  682. }
  683. if (h->error_flag) {
  684. if (h->addr.addr.sa_family == AF_INET)
  685. msg = pr_icmph(&h->icmp.v4);
  686. else
  687. msg = pr_icmph6(&h->icmp.v6);
  688. write_result(h, msg, "Err");
  689. g_free(msg);
  690. }
  691. // fprintf(stderr, "pinger: ping_host, No. %d, delay = %d\n", h->nhost, h->delay);
  692. switch (h->phase) {
  693. case STORM_PHASE:
  694. if (h->counter == 7 || h->counter == 15 || h->counter == 23) {
  695. update_host_stats(h);
  696. clear_tmp_flags(h);
  697. }
  698. if ((h->counter >= 0 && h->counter < 4)
  699. || (h->counter >= 8 && h->counter < 12)
  700. || (h->counter >= 16 && h->counter < 20)) {
  701. if (has_pinged) {
  702. h->delay++;
  703. goto dontpingyet;
  704. }
  705. pinger(h);
  706. h->delay = 0;
  707. }
  708. if (h->counter == 59) {
  709. h->counter = -1;
  710. h->phase = STANDBY_PHASE;
  711. }
  712. break;
  713. case STANDBY_PHASE:
  714. if (h->counter == 7) {
  715. update_host_stats(h);
  716. clear_tmp_flags(h);
  717. }
  718. if (h->counter >= 0 && h->counter < 4) {
  719. if (has_pinged) {
  720. h->delay++;
  721. goto dontpingyet;
  722. }
  723. pinger(h);
  724. h->delay = 0;
  725. }
  726. if (h->counter == h->updatefreq) {
  727. h->counter = -1;
  728. h->phase = STANDBY_PHASE;
  729. }
  730. break;
  731. }
  732. h->counter++;
  733. dontpingyet:
  734. update_host_packinfo(h);
  735. }
  736. gint timeout_callback()
  737. {
  738. has_pinged = 0;
  739. hosts = g_list_sort(hosts, compare_delay);
  740. g_list_foreach(hosts, (GFunc) ping_host, NULL);
  741. hosts = g_list_sort(hosts, compare_nhost2);
  742. g_list_foreach(hosts, (GFunc) dump_host, NULL);
  743. fflush(stdout);
  744. return TRUE;
  745. }
  746. void receiver()
  747. {
  748. int cc;
  749. socklen_t fromlen;
  750. struct timeval tv_old,tv_new;
  751. int avail;
  752. struct pollfd fds[2];
  753. fds[0].fd = icmp_socket;
  754. fds[1].fd = icmp6_socket;
  755. gettimeofday(&tv_old, NULL);
  756. for (;!terminated;) {
  757. fds[0].events = fds[1].events = POLLIN;
  758. fds[0].revents = fds[1].revents = 0;
  759. avail = poll(fds, 2, 100);
  760. if (avail > 0) {
  761. if (fds[0].revents & POLLIN) {
  762. struct sockaddr_in from;
  763. fromlen = sizeof(from);
  764. if ((cc = recvfrom(icmp_socket, packet, sizeof(packet), 0,
  765. (struct sockaddr *) &from, &fromlen)) < 0) {
  766. if (errno != EAGAIN)
  767. perror("pinger: recvfrom");
  768. } else {
  769. pr_pack(packet, cc, &from);
  770. }
  771. }
  772. if (fds[1].revents & POLLIN) {
  773. struct sockaddr_in6 from;
  774. fromlen = sizeof(from);
  775. if ((cc = recvfrom(icmp6_socket, packet, sizeof(packet), 0,
  776. (struct sockaddr *) &from, &fromlen)) < 0) {
  777. if (errno != EAGAIN)
  778. perror("pinger: recvfrom");
  779. } else {
  780. pr_pack6(packet, cc, &from);
  781. }
  782. }
  783. } else if (avail < 0) {
  784. perror("poll");
  785. }
  786. gettimeofday(&tv_new, NULL);
  787. tvsub(&tv_new, &tv_old);
  788. if (tv_new.tv_sec >= 1) {
  789. gettimeofday(&tv_old, NULL);
  790. timeout_callback();
  791. }
  792. }
  793. }
  794. void update_host_packinfo(host_data * h)
  795. {
  796. g_string_sprintf(h->sent_str, "%d", h->sent);
  797. g_string_sprintf(h->recv_str, "%d", h->recv);
  798. }
  799. void update_host_stats(host_data * h)
  800. {
  801. long trip;
  802. GString *s = g_string_new(NULL);
  803. GString *s2 = g_string_new(NULL);
  804. if (h->tmp_sent > 0) {
  805. g_string_sprintf(s, "%d", h->tmp_recv * 100 / h->tmp_sent);
  806. g_string_assign(h->percentage, s->str);
  807. } else {
  808. g_string_assign(h->percentage, "");
  809. }
  810. if (h->tmp_recv > 0) {
  811. trip = h->tmp_tsum / (h->tmp_recv + h->tmp_rep);
  812. if (trip >= 1000000) {
  813. g_string_sprintf(s, "%ld.%03ld s", trip / 1000000,
  814. (trip % 1000000) / 1000);
  815. g_string_sprintf(s2, ">s");
  816. } else if (trip >= 10000) {
  817. g_string_sprintf(s, "%ld.%03ld ms", trip / 1000, trip % 1000);
  818. g_string_sprintf(s2, "%ld", trip / 1000);
  819. } else if (trip >= 1000) {
  820. g_string_sprintf(s, "%ld.%03ld ms", trip / 1000, trip % 1000);
  821. g_string_sprintf(s2, "%ld.%01ld", trip / 1000,
  822. (trip % 1000) / 100);
  823. } else {
  824. g_string_sprintf(s, "0.%01ld ms", trip / 100);
  825. g_string_sprintf(s2, "0.%01ld", trip / 100);
  826. }
  827. write_result(h, s->str, s2->str);
  828. }
  829. g_string_free(s, TRUE);
  830. g_string_free(s2, TRUE);
  831. if (h->dummy) {
  832. write_result(h, "Dummy host", "##");
  833. return;
  834. }
  835. if (!h->error_flag) {
  836. if (h->tmp_sent > 0 && h->tmp_recv == 0)
  837. write_result(h, "Request timed out", "TO");
  838. }
  839. }
  840. void append_host(struct sockaddr *addr, char * hostname, char * updatefreq, char * dynamic, int dummy)
  841. {
  842. host_data *h = host_malloc();
  843. if (addr->sa_family == AF_INET)
  844. h->addr.in4 = *(struct sockaddr_in *)addr;
  845. else if (addr->sa_family == AF_INET6)
  846. h->addr.in6 = *(struct sockaddr_in6 *)addr;
  847. g_string_assign(h->hostname, hostname);
  848. h->dummy = dummy;
  849. h->nhost = hostcnt++;
  850. h->sent = 0;
  851. h->recv = 0;
  852. h->rep = 0;
  853. h->dupflag = 0;
  854. h->error_flag = 0;
  855. h->tsum = 0;
  856. h->tmp_sent = 0;
  857. h->tmp_recv = 0;
  858. h->tmp_rep = 0;
  859. h->tmp_tsum = 0;
  860. h->phase = STORM_PHASE;
  861. h->counter = 0;
  862. h->delay = 0;
  863. if (updatefreq) {
  864. h->updatefreq = atoi(updatefreq);
  865. if (h->updatefreq < 30) {
  866. h->updatefreq = 30;
  867. }
  868. if (h->updatefreq > 3600) {
  869. h->updatefreq = 3600;
  870. }
  871. } else {
  872. h->updatefreq = 59;
  873. }
  874. h->dynamic = atoi(dynamic) ? 1 : 0;
  875. hosts = g_list_append(hosts, h);
  876. update_host_stats(h);
  877. clear_tmp_flags(h);
  878. update_host_packinfo(h);
  879. }
  880. void free_hosts()
  881. {
  882. g_list_foreach(hosts, (GFunc) host_free, NULL);
  883. g_list_free(hosts);
  884. }
  885. void term_signal(int signum, siginfo_t *info, void *data)
  886. {
  887. terminated = 1;
  888. }
  889. int main(int argc, char **argv)
  890. {
  891. int i;
  892. struct sigaction sig;
  893. struct protoent *proto;
  894. if (!(proto = getprotobyname("icmp"))) {
  895. (void) fprintf(stderr, "pinger: unknown protocol icmp.\n");
  896. exit(2);
  897. }
  898. if ((icmp_socket = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
  899. if (errno == EPERM) {
  900. fprintf(stderr, "pinger: must run as root\n");
  901. } else
  902. perror("pinger: socket");
  903. exit(2);
  904. }
  905. struct icmp6_filter filter;
  906. const int on = 1;
  907. int err;
  908. if ((icmp6_socket = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
  909. if (errno == EPERM) {
  910. fprintf(stderr, "pinger: must run as root\n");
  911. } else
  912. perror("pinger: socket");
  913. exit(2);
  914. }
  915. /* Tell which ICMPs we are interested in. */
  916. ICMP6_FILTER_SETBLOCKALL (&filter);
  917. ICMP6_FILTER_SETPASS (ICMP6_ECHO_REPLY, &filter);
  918. ICMP6_FILTER_SETPASS (ICMP6_DST_UNREACH, &filter);
  919. ICMP6_FILTER_SETPASS (ICMP6_PACKET_TOO_BIG, &filter);
  920. ICMP6_FILTER_SETPASS (ICMP6_TIME_EXCEEDED, &filter);
  921. ICMP6_FILTER_SETPASS (ICMP6_PARAM_PROB, &filter);
  922. err = setsockopt (icmp6_socket, IPPROTO_ICMPV6, ICMP6_FILTER,
  923. &filter, sizeof (filter));
  924. if (err)
  925. {
  926. close (icmp6_socket);
  927. exit(2);
  928. }
  929. err = setsockopt (icmp6_socket, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
  930. &on, sizeof (on));
  931. if (err)
  932. {
  933. close (icmp6_socket);
  934. exit(2);
  935. }
  936. setuid(getuid());
  937. fcntl(icmp_socket, F_SETFL, O_NONBLOCK);
  938. fcntl(icmp6_socket, F_SETFL, O_NONBLOCK);
  939. ident = getpid() & 0xFFFF;
  940. for (i = 1; i < argc - 2; i += 3) {
  941. union {
  942. struct sockaddr addr;
  943. struct sockaddr_in in4;
  944. struct sockaddr_in6 in6;
  945. } addr;
  946. int res;
  947. res = hostname_to_addr(argv[i], &addr.addr);
  948. if (res == 0) {
  949. append_host(&addr.addr, argv[i], argv[i+1], argv[i+2], 0);
  950. } else if (i <= argc-3) {
  951. memset(&addr, 0, sizeof(addr));
  952. addr.addr.sa_family = AF_INET;
  953. append_host(&addr.addr, argv[i], argv[i+1], argv[i+2], 1); // dummy host
  954. }
  955. }
  956. sigfillset(&sig.sa_mask);
  957. sig.sa_flags = SA_SIGINFO | SA_RESTART;
  958. sig.sa_sigaction = term_signal;
  959. sigaction(SIGINT, &sig, 0);
  960. sigaction(SIGTERM, &sig, 0);
  961. sigaction(SIGPIPE, &sig, 0);
  962. sigaction(SIGHUP, &sig, 0);
  963. receiver();
  964. free_hosts();
  965. close(icmp_socket);
  966. close(icmp6_socket);
  967. return 0;
  968. }