pinger.c 31 KB

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