pinger.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. char *data = (char *)(orig_icmp + 1);
  591. h = (host_data *) g_list_find_custom(
  592. hosts,
  593. (int *)&data[sizeof(struct timeval)],
  594. compare_nhost)->data;
  595. if (h == NULL) return;
  596. if (!IN6_ARE_ADDR_EQUAL (&orig_ip->ip6_dst, &h->addr.in6.sin6_addr)
  597. || orig_ip->ip6_nxt != IPPROTO_ICMPV6
  598. || orig_icmp->icmp6_type != ICMP6_ECHO_REQUEST
  599. || orig_icmp->icmp6_id != ident)
  600. {
  601. return;
  602. }
  603. h->icp.v6 = *icp;
  604. h->error_flag = 1;
  605. }
  606. }
  607. }
  608. }
  609. void clear_tmp_flags(host_data * h)
  610. {
  611. h->tmp_tsum = 0;
  612. h->tmp_sent = 0;
  613. h->tmp_recv = 0;
  614. h->tmp_rep = 0;
  615. h->dupflag = 0;
  616. h->error_flag = 0;
  617. }
  618. void dump_host(host_data * h)
  619. {
  620. printf("%s\n", h->percentage->str);
  621. printf("%s\n", h->sent_str->str);
  622. printf("%s\n", h->recv_str->str);
  623. printf("%s\n", h->msg->str);
  624. printf("%s\n", h->shortmsg->str);
  625. }
  626. int hostname_to_addr(const char *hostname, struct sockaddr *addr)
  627. {
  628. struct addrinfo hints;
  629. struct addrinfo *result = 0, *rp = 0;
  630. int s, ret = -1;
  631. memset(&hints, 0, sizeof(struct addrinfo));
  632. hints.ai_family = AF_UNSPEC;
  633. hints.ai_socktype = 0;
  634. hints.ai_flags = 0;
  635. hints.ai_protocol = 0; /* Any protocol */
  636. hints.ai_canonname = NULL;
  637. hints.ai_addr = NULL;
  638. hints.ai_next = NULL;
  639. s = getaddrinfo(hostname, NULL, &hints, &result);
  640. if (s != 0) {
  641. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  642. return -1;
  643. }
  644. for (rp = result; rp != NULL; rp = rp->ai_next) {
  645. if (rp->ai_addr->sa_family == AF_INET
  646. || rp->ai_addr->sa_family == AF_INET6)
  647. {
  648. if (rp->ai_addr->sa_family == AF_INET) {
  649. assert(rp->ai_addrlen == sizeof(struct sockaddr_in));
  650. }
  651. if (rp->ai_addr->sa_family == AF_INET6) {
  652. assert(rp->ai_addrlen == sizeof(struct sockaddr_in6));
  653. }
  654. memcpy(addr, rp->ai_addr, rp->ai_addrlen);
  655. ret = 0;
  656. break;
  657. }
  658. }
  659. freeaddrinfo(result);
  660. return ret;
  661. }
  662. // recheck the dns (needed for dialup users or dynamic DNS)
  663. int update_dns(host_data *h)
  664. {
  665. return hostname_to_addr(h->hostname->str, &h->addr.addr);
  666. }
  667. void ping_host(host_data * h)
  668. {
  669. gchar *msg;
  670. if (h->dummy) {
  671. if (h->counter == 120) {
  672. if (update_dns(h) == 0) {
  673. h->dummy = 0;
  674. update_host_stats(h);
  675. clear_tmp_flags(h);
  676. update_host_packinfo(h);
  677. h->phase = STORM_PHASE;
  678. } else {
  679. h->phase = STANDBY_PHASE;
  680. }
  681. h->delay = 0;
  682. h->counter = -1;
  683. }
  684. h->counter++;
  685. return;
  686. }
  687. if (!h->dummy && h->dynamic && h->counter == 0) {
  688. update_dns(h);
  689. }
  690. if (h->error_flag) {
  691. if (h->addr.addr.sa_family == AF_INET)
  692. msg = pr_icmph(&h->icp.v4);
  693. else
  694. msg = pr_icmph6(&h->icp.v6);
  695. write_result(h, msg, "Err");
  696. g_free(msg);
  697. }
  698. // fprintf(stderr, "pinger: ping_host, No. %d, delay = %d\n", h->nhost, h->delay);
  699. switch (h->phase) {
  700. case STORM_PHASE:
  701. if (h->counter == 7 || h->counter == 15 || h->counter == 23) {
  702. update_host_stats(h);
  703. clear_tmp_flags(h);
  704. }
  705. if ((h->counter >= 0 && h->counter < 4)
  706. || (h->counter >= 8 && h->counter < 12)
  707. || (h->counter >= 16 && h->counter < 20)) {
  708. if (has_pinged) {
  709. h->delay++;
  710. goto dontpingyet;
  711. }
  712. pinger(h);
  713. h->delay = 0;
  714. }
  715. if (h->counter == 59) {
  716. h->counter = -1;
  717. h->phase = STANDBY_PHASE;
  718. }
  719. break;
  720. case STANDBY_PHASE:
  721. if (h->counter == 7) {
  722. update_host_stats(h);
  723. clear_tmp_flags(h);
  724. }
  725. if (h->counter >= 0 && h->counter < 4) {
  726. if (has_pinged) {
  727. h->delay++;
  728. goto dontpingyet;
  729. }
  730. pinger(h);
  731. h->delay = 0;
  732. }
  733. if (h->counter == h->updatefreq) {
  734. h->counter = -1;
  735. h->phase = STANDBY_PHASE;
  736. }
  737. break;
  738. }
  739. h->counter++;
  740. dontpingyet:
  741. update_host_packinfo(h);
  742. }
  743. gint timeout_callback()
  744. {
  745. has_pinged = 0;
  746. hosts = g_list_sort(hosts, compare_delay);
  747. g_list_foreach(hosts, (GFunc) ping_host, NULL);
  748. hosts = g_list_sort(hosts, compare_nhost2);
  749. g_list_foreach(hosts, (GFunc) dump_host, NULL);
  750. fflush(stdout);
  751. return TRUE;
  752. }
  753. void receiver()
  754. {
  755. int cc;
  756. socklen_t fromlen;
  757. struct timeval tv,tv_old,tv_new;
  758. fd_set rfds;
  759. int avail;
  760. FD_ZERO(&rfds);
  761. FD_SET(icmp_socket, &rfds);
  762. FD_SET(icmp6_socket, &rfds);
  763. tv.tv_usec = 500000;
  764. tv.tv_sec = 0;
  765. gettimeofday(&tv_old, NULL);
  766. for (;!terminated;) {
  767. FD_ZERO(&rfds);
  768. FD_SET(icmp_socket, &rfds);
  769. FD_SET(icmp6_socket, &rfds);
  770. tv.tv_usec = 100000;
  771. tv.tv_sec = 0;
  772. avail = select(icmp6_socket + 1, &rfds, NULL, NULL, &tv);
  773. if (avail) {
  774. if (FD_ISSET(icmp_socket, &rfds)) {
  775. struct sockaddr_in from;
  776. fromlen = sizeof(from);
  777. if ((cc = recvfrom(icmp_socket, (char *) packet, sizeof(packet), 0,
  778. (struct sockaddr *) &from, &fromlen)) < 0) {
  779. perror("pinger: recvfrom");
  780. } else {
  781. pr_pack((char *) packet, cc, &from);
  782. }
  783. } else if (FD_ISSET(icmp6_socket, &rfds)) {
  784. struct sockaddr_in6 from;
  785. fromlen = sizeof(from);
  786. if ((cc = recvfrom(icmp6_socket, (char *) packet, sizeof(packet), 0,
  787. (struct sockaddr *) &from, &fromlen)) < 0) {
  788. perror("pinger: recvfrom");
  789. } else {
  790. pr_pack6((char *) packet, cc, &from);
  791. }
  792. }
  793. }
  794. gettimeofday(&tv_new, NULL);
  795. tvsub(&tv_new, &tv_old);
  796. if (tv_new.tv_sec >= 1) {
  797. gettimeofday(&tv_old, NULL);
  798. timeout_callback();
  799. }
  800. }
  801. }
  802. void update_host_packinfo(host_data * h)
  803. {
  804. g_string_sprintf(h->sent_str, "%d", h->sent);
  805. g_string_sprintf(h->recv_str, "%d", h->recv);
  806. }
  807. void update_host_stats(host_data * h)
  808. {
  809. long trip;
  810. GString *s = g_string_new(NULL);
  811. GString *s2 = g_string_new(NULL);
  812. if (h->tmp_sent > 0) {
  813. g_string_sprintf(s, "%d", h->tmp_recv * 100 / h->tmp_sent);
  814. g_string_assign(h->percentage, s->str);
  815. } else {
  816. g_string_assign(h->percentage, "");
  817. }
  818. if (h->tmp_recv > 0) {
  819. trip = h->tmp_tsum / (h->tmp_recv + h->tmp_rep);
  820. if (trip >= 1000000) {
  821. g_string_sprintf(s, "%ld.%03ld s", trip / 1000000,
  822. (trip % 1000000) / 1000);
  823. g_string_sprintf(s2, ">s");
  824. } else if (trip >= 10000) {
  825. g_string_sprintf(s, "%ld.%03ld ms", trip / 1000, trip % 1000);
  826. g_string_sprintf(s2, "%ld", trip / 1000);
  827. } else if (trip >= 1000) {
  828. g_string_sprintf(s, "%ld.%03ld ms", trip / 1000, trip % 1000);
  829. g_string_sprintf(s2, "%ld.%01ld", trip / 1000,
  830. (trip % 1000) / 100);
  831. } else {
  832. g_string_sprintf(s, "0.%01ld ms", trip / 100);
  833. g_string_sprintf(s2, "0.%01ld", trip / 100);
  834. }
  835. write_result(h, s->str, s2->str);
  836. }
  837. g_string_free(s, TRUE);
  838. g_string_free(s2, TRUE);
  839. if (h->dummy) {
  840. write_result(h, "Dummy host", "##");
  841. return;
  842. }
  843. if (!h->error_flag) {
  844. if (h->tmp_sent > 0 && h->tmp_recv == 0)
  845. write_result(h, "Request timed out", "TO");
  846. }
  847. }
  848. void append_host(struct sockaddr *addr, char * hostname, char * updatefreq, char * dynamic, int dummy)
  849. {
  850. host_data *h = host_malloc();
  851. if (addr->sa_family == AF_INET)
  852. h->addr.in4 = *(struct sockaddr_in *)addr;
  853. else if (addr->sa_family == AF_INET6)
  854. h->addr.in6 = *(struct sockaddr_in6 *)addr;
  855. g_string_assign(h->hostname, hostname);
  856. h->dummy = dummy;
  857. h->nhost = hostcnt++;
  858. h->sent = 0;
  859. h->recv = 0;
  860. h->rep = 0;
  861. h->dupflag = 0;
  862. h->error_flag = 0;
  863. h->tsum = 0;
  864. h->tmp_sent = 0;
  865. h->tmp_recv = 0;
  866. h->tmp_rep = 0;
  867. h->tmp_tsum = 0;
  868. h->phase = STORM_PHASE;
  869. h->counter = 0;
  870. h->delay = 0;
  871. if (updatefreq) {
  872. h->updatefreq = atoi(updatefreq);
  873. if (h->updatefreq < 30) {
  874. h->updatefreq = 30;
  875. }
  876. if (h->updatefreq > 3600) {
  877. h->updatefreq = 3600;
  878. }
  879. } else {
  880. h->updatefreq = 59;
  881. }
  882. h->dynamic = atoi(dynamic) ? 1 : 0;
  883. hosts = g_list_append(hosts, h);
  884. update_host_stats(h);
  885. clear_tmp_flags(h);
  886. update_host_packinfo(h);
  887. }
  888. void free_hosts()
  889. {
  890. g_list_foreach(hosts, (GFunc) host_free, NULL);
  891. g_list_free(hosts);
  892. }
  893. void term_signal(int signum, siginfo_t *info, void *data)
  894. {
  895. terminated = 1;
  896. }
  897. int main(int argc, char **argv)
  898. {
  899. int i;
  900. struct sigaction sig;
  901. struct protoent *proto;
  902. if (!(proto = getprotobyname("icmp"))) {
  903. (void) fprintf(stderr, "pinger: unknown protocol icmp.\n");
  904. exit(2);
  905. }
  906. if ((icmp_socket = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
  907. if (errno == EPERM) {
  908. fprintf(stderr, "pinger: must run as root\n");
  909. } else
  910. perror("pinger: socket");
  911. exit(2);
  912. }
  913. struct icmp6_filter filter;
  914. const int on = 1;
  915. int err;
  916. if ((icmp6_socket = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
  917. if (errno == EPERM) {
  918. fprintf(stderr, "pinger: must run as root\n");
  919. } else
  920. perror("pinger: socket");
  921. exit(2);
  922. }
  923. /* Tell which ICMPs we are interested in. */
  924. ICMP6_FILTER_SETBLOCKALL (&filter);
  925. ICMP6_FILTER_SETPASS (ICMP6_ECHO_REPLY, &filter);
  926. ICMP6_FILTER_SETPASS (ICMP6_DST_UNREACH, &filter);
  927. ICMP6_FILTER_SETPASS (ICMP6_PACKET_TOO_BIG, &filter);
  928. ICMP6_FILTER_SETPASS (ICMP6_TIME_EXCEEDED, &filter);
  929. ICMP6_FILTER_SETPASS (ICMP6_PARAM_PROB, &filter);
  930. err = setsockopt (icmp6_socket, IPPROTO_ICMPV6, ICMP6_FILTER,
  931. &filter, sizeof (filter));
  932. if (err)
  933. {
  934. close (icmp6_socket);
  935. exit(2);
  936. }
  937. err = setsockopt (icmp6_socket, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
  938. &on, sizeof (on));
  939. if (err)
  940. {
  941. close (icmp6_socket);
  942. exit(2);
  943. }
  944. setuid(getuid());
  945. fcntl(icmp_socket, F_SETFL, O_NONBLOCK);
  946. fcntl(icmp6_socket, F_SETFL, O_NONBLOCK);
  947. ident = getpid() & 0xFFFF;
  948. for (i = 1; i < argc - 2; i += 3) {
  949. union {
  950. struct sockaddr addr;
  951. struct sockaddr_in in4;
  952. struct sockaddr_in6 in6;
  953. } addr;
  954. int res;
  955. res = hostname_to_addr(argv[i], &addr.addr);
  956. if (res == 0) {
  957. append_host(&addr.addr, argv[i], argv[i+1], argv[i+2], 0);
  958. } else if (i <= argc-3) {
  959. memset(&addr, 0, sizeof(addr));
  960. addr.addr.sa_family = AF_INET;
  961. append_host(&addr.addr, argv[i], argv[i+1], argv[i+2], 1); // dummy host
  962. }
  963. }
  964. sigfillset(&sig.sa_mask);
  965. sig.sa_flags = SA_SIGINFO | SA_RESTART;
  966. sig.sa_sigaction = term_signal;
  967. sigaction(SIGINT, &sig, 0);
  968. sigaction(SIGTERM, &sig, 0);
  969. sigaction(SIGPIPE, &sig, 0);
  970. sigaction(SIGHUP, &sig, 0);
  971. receiver();
  972. free_hosts();
  973. close(icmp_socket);
  974. close(icmp6_socket);
  975. return 0;
  976. }