pinger.c 30 KB

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