pinger.c 31 KB

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