ping.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini ping implementation for busybox
  4. *
  5. * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  6. *
  7. * Adapted from the ping in netkit-base 0.10:
  8. * Copyright (c) 1989 The Regents of the University of California.
  9. * All rights reserved.
  10. *
  11. * This code is derived from software contributed to Berkeley by
  12. * Mike Muuss.
  13. *
  14. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  15. */
  16. /* from ping6.c:
  17. * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  18. *
  19. * This version of ping is adapted from the ping in netkit-base 0.10,
  20. * which is:
  21. *
  22. * Original copyright notice is retained at the end of this file.
  23. *
  24. * This version is an adaptation of ping.c from busybox.
  25. * The code was modified by Bart Visscher <magick@linux-fan.com>
  26. */
  27. #include <net/if.h>
  28. #include <netinet/ip_icmp.h>
  29. #include "libbb.h"
  30. #if ENABLE_PING6
  31. #include <netinet/icmp6.h>
  32. /* I see RENUMBERED constants in bits/in.h - !!?
  33. * What a fuck is going on with libc? Is it a glibc joke? */
  34. #ifdef IPV6_2292HOPLIMIT
  35. #undef IPV6_HOPLIMIT
  36. #define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
  37. #endif
  38. #endif
  39. enum {
  40. DEFDATALEN = 56,
  41. MAXIPLEN = 60,
  42. MAXICMPLEN = 76,
  43. MAXPACKET = 65468,
  44. MAX_DUP_CHK = (8 * 128),
  45. MAXWAIT = 10,
  46. PINGINTERVAL = 1, /* 1 second */
  47. };
  48. /* common routines */
  49. static int in_cksum(unsigned short *buf, int sz)
  50. {
  51. int nleft = sz;
  52. int sum = 0;
  53. unsigned short *w = buf;
  54. unsigned short ans = 0;
  55. while (nleft > 1) {
  56. sum += *w++;
  57. nleft -= 2;
  58. }
  59. if (nleft == 1) {
  60. *(unsigned char *) (&ans) = *(unsigned char *) w;
  61. sum += ans;
  62. }
  63. sum = (sum >> 16) + (sum & 0xFFFF);
  64. sum += (sum >> 16);
  65. ans = ~sum;
  66. return ans;
  67. }
  68. #if !ENABLE_FEATURE_FANCY_PING
  69. /* simple version */
  70. static char *hostname;
  71. static void noresp(int ign ATTRIBUTE_UNUSED)
  72. {
  73. printf("No response from %s\n", hostname);
  74. exit(EXIT_FAILURE);
  75. }
  76. static void ping4(len_and_sockaddr *lsa)
  77. {
  78. struct sockaddr_in pingaddr;
  79. struct icmp *pkt;
  80. int pingsock, c;
  81. char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
  82. pingsock = create_icmp_socket();
  83. pingaddr = lsa->sin;
  84. pkt = (struct icmp *) packet;
  85. memset(pkt, 0, sizeof(packet));
  86. pkt->icmp_type = ICMP_ECHO;
  87. pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
  88. c = xsendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN,
  89. (struct sockaddr *) &pingaddr, sizeof(pingaddr));
  90. /* listen for replies */
  91. while (1) {
  92. struct sockaddr_in from;
  93. socklen_t fromlen = sizeof(from);
  94. c = recvfrom(pingsock, packet, sizeof(packet), 0,
  95. (struct sockaddr *) &from, &fromlen);
  96. if (c < 0) {
  97. if (errno != EINTR)
  98. bb_perror_msg("recvfrom");
  99. continue;
  100. }
  101. if (c >= 76) { /* ip + icmp */
  102. struct iphdr *iphdr = (struct iphdr *) packet;
  103. pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
  104. if (pkt->icmp_type == ICMP_ECHOREPLY)
  105. break;
  106. }
  107. }
  108. if (ENABLE_FEATURE_CLEAN_UP)
  109. close(pingsock);
  110. }
  111. #if ENABLE_PING6
  112. static void ping6(len_and_sockaddr *lsa)
  113. {
  114. struct sockaddr_in6 pingaddr;
  115. struct icmp6_hdr *pkt;
  116. int pingsock, c;
  117. int sockopt;
  118. char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
  119. pingsock = create_icmp6_socket();
  120. pingaddr = lsa->sin6;
  121. pkt = (struct icmp6_hdr *) packet;
  122. memset(pkt, 0, sizeof(packet));
  123. pkt->icmp6_type = ICMP6_ECHO_REQUEST;
  124. sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
  125. setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
  126. c = xsendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr),
  127. (struct sockaddr *) &pingaddr, sizeof(pingaddr));
  128. /* listen for replies */
  129. while (1) {
  130. struct sockaddr_in6 from;
  131. socklen_t fromlen = sizeof(from);
  132. c = recvfrom(pingsock, packet, sizeof(packet), 0,
  133. (struct sockaddr *) &from, &fromlen);
  134. if (c < 0) {
  135. if (errno != EINTR)
  136. bb_perror_msg("recvfrom");
  137. continue;
  138. }
  139. if (c >= 8) { /* icmp6_hdr */
  140. pkt = (struct icmp6_hdr *) packet;
  141. if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
  142. break;
  143. }
  144. }
  145. if (ENABLE_FEATURE_CLEAN_UP)
  146. close(pingsock);
  147. }
  148. #endif
  149. int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  150. int ping_main(int argc, char **argv)
  151. {
  152. len_and_sockaddr *lsa;
  153. #if ENABLE_PING6
  154. sa_family_t af = AF_UNSPEC;
  155. while ((++argv)[0] && argv[0][0] == '-') {
  156. if (argv[0][1] == '4') {
  157. af = AF_INET;
  158. continue;
  159. }
  160. if (argv[0][1] == '6') {
  161. af = AF_INET6;
  162. continue;
  163. }
  164. bb_show_usage();
  165. }
  166. #else
  167. argv++;
  168. #endif
  169. hostname = *argv;
  170. if (!hostname)
  171. bb_show_usage();
  172. #if ENABLE_PING6
  173. lsa = xhost_and_af2sockaddr(hostname, 0, af);
  174. #else
  175. lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
  176. #endif
  177. /* Set timer _after_ DNS resolution */
  178. signal(SIGALRM, noresp);
  179. alarm(5); /* give the host 5000ms to respond */
  180. #if ENABLE_PING6
  181. if (lsa->sa.sa_family == AF_INET6)
  182. ping6(lsa);
  183. else
  184. #endif
  185. ping4(lsa);
  186. printf("%s is alive!\n", hostname);
  187. return EXIT_SUCCESS;
  188. }
  189. #else /* FEATURE_FANCY_PING */
  190. /* full(er) version */
  191. #define OPT_STRING ("qvc:s:I:4" USE_PING6("6"))
  192. enum {
  193. OPT_QUIET = 1 << 0,
  194. OPT_VERBOSE = 1 << 1,
  195. OPT_c = 1 << 2,
  196. OPT_s = 1 << 3,
  197. OPT_I = 1 << 4,
  198. OPT_IPV4 = 1 << 5,
  199. OPT_IPV6 = (1 << 6) * ENABLE_PING6,
  200. };
  201. struct globals {
  202. int pingsock;
  203. int if_index;
  204. char *opt_I;
  205. len_and_sockaddr *source_lsa;
  206. unsigned datalen;
  207. unsigned long ntransmitted, nreceived, nrepeats, pingcount;
  208. uint16_t myid;
  209. unsigned tmin, tmax; /* in us */
  210. unsigned long long tsum; /* in us, sum of all times */
  211. const char *hostname;
  212. const char *dotted;
  213. union {
  214. struct sockaddr sa;
  215. struct sockaddr_in sin;
  216. #if ENABLE_PING6
  217. struct sockaddr_in6 sin6;
  218. #endif
  219. } pingaddr;
  220. char rcvd_tbl[MAX_DUP_CHK / 8];
  221. };
  222. #define G (*(struct globals*)&bb_common_bufsiz1)
  223. #define pingsock (G.pingsock )
  224. #define if_index (G.if_index )
  225. #define source_lsa (G.source_lsa )
  226. #define opt_I (G.opt_I )
  227. #define datalen (G.datalen )
  228. #define ntransmitted (G.ntransmitted)
  229. #define nreceived (G.nreceived )
  230. #define nrepeats (G.nrepeats )
  231. #define pingcount (G.pingcount )
  232. #define myid (G.myid )
  233. #define tmin (G.tmin )
  234. #define tmax (G.tmax )
  235. #define tsum (G.tsum )
  236. #define hostname (G.hostname )
  237. #define dotted (G.dotted )
  238. #define pingaddr (G.pingaddr )
  239. #define rcvd_tbl (G.rcvd_tbl )
  240. void BUG_ping_globals_too_big(void);
  241. #define INIT_G() do { \
  242. if (sizeof(G) > COMMON_BUFSIZE) \
  243. BUG_ping_globals_too_big(); \
  244. pingsock = -1; \
  245. tmin = UINT_MAX; \
  246. } while (0)
  247. #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
  248. #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
  249. #define SET(bit) (A(bit) |= B(bit))
  250. #define CLR(bit) (A(bit) &= (~B(bit)))
  251. #define TST(bit) (A(bit) & B(bit))
  252. /**************************************************************************/
  253. static void pingstats(int junk ATTRIBUTE_UNUSED)
  254. {
  255. signal(SIGINT, SIG_IGN);
  256. printf("\n--- %s ping statistics ---\n", hostname);
  257. printf("%lu packets transmitted, ", ntransmitted);
  258. printf("%lu packets received, ", nreceived);
  259. if (nrepeats)
  260. printf("%lu duplicates, ", nrepeats);
  261. if (ntransmitted)
  262. ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted;
  263. printf("%lu%% packet loss\n", ntransmitted);
  264. if (tmin != UINT_MAX) {
  265. unsigned tavg = tsum / (nreceived + nrepeats);
  266. printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n",
  267. tmin / 1000, tmin % 1000,
  268. tavg / 1000, tavg % 1000,
  269. tmax / 1000, tmax % 1000);
  270. }
  271. exit(nreceived == 0); /* (nreceived == 0) is true (1) -- 'failure' */
  272. }
  273. static void sendping_tail(void (*sp)(int), const void *pkt, int size_pkt)
  274. {
  275. int sz;
  276. CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
  277. ntransmitted++;
  278. /* sizeof(pingaddr) can be larger than real sa size, but I think
  279. * it doesn't matter */
  280. sz = xsendto(pingsock, pkt, size_pkt, &pingaddr.sa, sizeof(pingaddr));
  281. if (sz != size_pkt)
  282. bb_error_msg_and_die(bb_msg_write_error);
  283. signal(SIGALRM, sp);
  284. if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
  285. alarm(PINGINTERVAL);
  286. } else { /* done, wait for the last ping to come back */
  287. /* todo, don't necessarily need to wait so long... */
  288. signal(SIGALRM, pingstats);
  289. alarm(MAXWAIT);
  290. }
  291. }
  292. static void sendping4(int junk ATTRIBUTE_UNUSED)
  293. {
  294. /* +4 reserves a place for timestamp, which may end up sitting
  295. * *after* packet. Saves one if() */
  296. struct icmp *pkt = alloca(datalen + ICMP_MINLEN + 4);
  297. pkt->icmp_type = ICMP_ECHO;
  298. pkt->icmp_code = 0;
  299. pkt->icmp_cksum = 0;
  300. pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
  301. pkt->icmp_id = myid;
  302. /* We don't do hton, because we will read it back on the same machine */
  303. /*if (datalen >= 4)*/
  304. *(uint32_t*)&pkt->icmp_dun = monotonic_us();
  305. pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);
  306. sendping_tail(sendping4, pkt, datalen + ICMP_MINLEN);
  307. }
  308. #if ENABLE_PING6
  309. static void sendping6(int junk ATTRIBUTE_UNUSED)
  310. {
  311. struct icmp6_hdr *pkt = alloca(datalen + sizeof(struct icmp6_hdr) + 4);
  312. pkt->icmp6_type = ICMP6_ECHO_REQUEST;
  313. pkt->icmp6_code = 0;
  314. pkt->icmp6_cksum = 0;
  315. pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
  316. pkt->icmp6_id = myid;
  317. /*if (datalen >= 4)*/
  318. *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
  319. sendping_tail(sendping6, pkt, datalen + sizeof(struct icmp6_hdr));
  320. }
  321. #endif
  322. static const char *icmp_type_name(int id)
  323. {
  324. switch (id) {
  325. case ICMP_ECHOREPLY: return "Echo Reply";
  326. case ICMP_DEST_UNREACH: return "Destination Unreachable";
  327. case ICMP_SOURCE_QUENCH: return "Source Quench";
  328. case ICMP_REDIRECT: return "Redirect (change route)";
  329. case ICMP_ECHO: return "Echo Request";
  330. case ICMP_TIME_EXCEEDED: return "Time Exceeded";
  331. case ICMP_PARAMETERPROB: return "Parameter Problem";
  332. case ICMP_TIMESTAMP: return "Timestamp Request";
  333. case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
  334. case ICMP_INFO_REQUEST: return "Information Request";
  335. case ICMP_INFO_REPLY: return "Information Reply";
  336. case ICMP_ADDRESS: return "Address Mask Request";
  337. case ICMP_ADDRESSREPLY: return "Address Mask Reply";
  338. default: return "unknown ICMP type";
  339. }
  340. }
  341. #if ENABLE_PING6
  342. /* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
  343. * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
  344. #ifndef MLD_LISTENER_QUERY
  345. # define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
  346. #endif
  347. #ifndef MLD_LISTENER_REPORT
  348. # define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
  349. #endif
  350. #ifndef MLD_LISTENER_REDUCTION
  351. # define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
  352. #endif
  353. static const char *icmp6_type_name(int id)
  354. {
  355. switch (id) {
  356. case ICMP6_DST_UNREACH: return "Destination Unreachable";
  357. case ICMP6_PACKET_TOO_BIG: return "Packet too big";
  358. case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
  359. case ICMP6_PARAM_PROB: return "Parameter Problem";
  360. case ICMP6_ECHO_REPLY: return "Echo Reply";
  361. case ICMP6_ECHO_REQUEST: return "Echo Request";
  362. case MLD_LISTENER_QUERY: return "Listener Query";
  363. case MLD_LISTENER_REPORT: return "Listener Report";
  364. case MLD_LISTENER_REDUCTION: return "Listener Reduction";
  365. default: return "unknown ICMP type";
  366. }
  367. }
  368. #endif
  369. static void unpack_tail(int sz, uint32_t *tp,
  370. const char *from_str,
  371. uint16_t recv_seq, int ttl)
  372. {
  373. const char *dupmsg = " (DUP!)";
  374. unsigned triptime = triptime; /* for gcc */
  375. ++nreceived;
  376. if (tp) {
  377. /* (int32_t) cast is for hypothetical 64-bit unsigned */
  378. /* (doesn't hurt 32-bit real-world anyway) */
  379. triptime = (int32_t) ((uint32_t)monotonic_us() - *tp);
  380. tsum += triptime;
  381. if (triptime < tmin)
  382. tmin = triptime;
  383. if (triptime > tmax)
  384. tmax = triptime;
  385. }
  386. if (TST(recv_seq % MAX_DUP_CHK)) {
  387. ++nrepeats;
  388. --nreceived;
  389. } else {
  390. SET(recv_seq % MAX_DUP_CHK);
  391. dupmsg += 7;
  392. }
  393. if (option_mask32 & OPT_QUIET)
  394. return;
  395. printf("%d bytes from %s: seq=%u ttl=%d", sz,
  396. from_str, recv_seq, ttl);
  397. if (tp)
  398. printf(" time=%u.%03u ms", triptime / 1000, triptime % 1000);
  399. puts(dupmsg);
  400. fflush(stdout);
  401. }
  402. static void unpack4(char *buf, int sz, struct sockaddr_in *from)
  403. {
  404. struct icmp *icmppkt;
  405. struct iphdr *iphdr;
  406. int hlen;
  407. /* discard if too short */
  408. if (sz < (datalen + ICMP_MINLEN))
  409. return;
  410. /* check IP header */
  411. iphdr = (struct iphdr *) buf;
  412. hlen = iphdr->ihl << 2;
  413. sz -= hlen;
  414. icmppkt = (struct icmp *) (buf + hlen);
  415. if (icmppkt->icmp_id != myid)
  416. return; /* not our ping */
  417. if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
  418. uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
  419. uint32_t *tp = NULL;
  420. if (sz >= ICMP_MINLEN + sizeof(uint32_t))
  421. tp = (uint32_t *) icmppkt->icmp_data;
  422. unpack_tail(sz, tp,
  423. inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
  424. recv_seq, iphdr->ttl);
  425. } else if (icmppkt->icmp_type != ICMP_ECHO) {
  426. bb_error_msg("warning: got ICMP %d (%s)",
  427. icmppkt->icmp_type,
  428. icmp_type_name(icmppkt->icmp_type));
  429. }
  430. }
  431. #if ENABLE_PING6
  432. static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
  433. {
  434. struct icmp6_hdr *icmppkt;
  435. char buf[INET6_ADDRSTRLEN];
  436. /* discard if too short */
  437. if (sz < (datalen + sizeof(struct icmp6_hdr)))
  438. return;
  439. icmppkt = (struct icmp6_hdr *) packet;
  440. if (icmppkt->icmp6_id != myid)
  441. return; /* not our ping */
  442. if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
  443. uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
  444. uint32_t *tp = NULL;
  445. if (sz >= sizeof(struct icmp6_hdr) + sizeof(uint32_t))
  446. tp = (uint32_t *) &icmppkt->icmp6_data8[4];
  447. unpack_tail(sz, tp,
  448. inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr,
  449. buf, sizeof(buf)),
  450. recv_seq, hoplimit);
  451. } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) {
  452. bb_error_msg("warning: got ICMP %d (%s)",
  453. icmppkt->icmp6_type,
  454. icmp6_type_name(icmppkt->icmp6_type));
  455. }
  456. }
  457. #endif
  458. static void ping4(len_and_sockaddr *lsa)
  459. {
  460. char packet[datalen + MAXIPLEN + MAXICMPLEN];
  461. int sockopt;
  462. pingsock = create_icmp_socket();
  463. pingaddr.sin = lsa->sin;
  464. if (source_lsa) {
  465. if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF,
  466. &source_lsa->sa, source_lsa->len))
  467. bb_error_msg_and_die("can't set multicast source interface");
  468. xbind(pingsock, &source_lsa->sa, source_lsa->len);
  469. }
  470. if (opt_I)
  471. setsockopt(pingsock, SOL_SOCKET, SO_BINDTODEVICE, opt_I, strlen(opt_I) + 1);
  472. /* enable broadcast pings */
  473. setsockopt_broadcast(pingsock);
  474. /* set recv buf for broadcast pings */
  475. sockopt = 48 * 1024; /* explain why 48k? */
  476. setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
  477. signal(SIGINT, pingstats);
  478. /* start the ping's going ... */
  479. sendping4(0);
  480. /* listen for replies */
  481. while (1) {
  482. struct sockaddr_in from;
  483. socklen_t fromlen = (socklen_t) sizeof(from);
  484. int c;
  485. c = recvfrom(pingsock, packet, sizeof(packet), 0,
  486. (struct sockaddr *) &from, &fromlen);
  487. if (c < 0) {
  488. if (errno != EINTR)
  489. bb_perror_msg("recvfrom");
  490. continue;
  491. }
  492. unpack4(packet, c, &from);
  493. if (pingcount > 0 && nreceived >= pingcount)
  494. break;
  495. }
  496. }
  497. #if ENABLE_PING6
  498. extern int BUG_bad_offsetof_icmp6_cksum(void);
  499. static void ping6(len_and_sockaddr *lsa)
  500. {
  501. char packet[datalen + MAXIPLEN + MAXICMPLEN];
  502. int sockopt;
  503. struct msghdr msg;
  504. struct sockaddr_in6 from;
  505. struct iovec iov;
  506. char control_buf[CMSG_SPACE(36)];
  507. pingsock = create_icmp6_socket();
  508. pingaddr.sin6 = lsa->sin6;
  509. /* untested whether "-I addr" really works for IPv6: */
  510. if (source_lsa)
  511. xbind(pingsock, &source_lsa->sa, source_lsa->len);
  512. if (opt_I)
  513. setsockopt(pingsock, SOL_SOCKET, SO_BINDTODEVICE, opt_I, strlen(opt_I) + 1);
  514. #ifdef ICMP6_FILTER
  515. {
  516. struct icmp6_filter filt;
  517. if (!(option_mask32 & OPT_VERBOSE)) {
  518. ICMP6_FILTER_SETBLOCKALL(&filt);
  519. ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
  520. } else {
  521. ICMP6_FILTER_SETPASSALL(&filt);
  522. }
  523. if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
  524. sizeof(filt)) < 0)
  525. bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
  526. }
  527. #endif /*ICMP6_FILTER*/
  528. /* enable broadcast pings */
  529. setsockopt_broadcast(pingsock);
  530. /* set recv buf for broadcast pings */
  531. sockopt = 48 * 1024; /* explain why 48k? */
  532. setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
  533. sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
  534. if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
  535. BUG_bad_offsetof_icmp6_cksum();
  536. setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
  537. /* request ttl info to be returned in ancillary data */
  538. setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
  539. if (if_index)
  540. pingaddr.sin6.sin6_scope_id = if_index;
  541. signal(SIGINT, pingstats);
  542. /* start the ping's going ... */
  543. sendping6(0);
  544. /* listen for replies */
  545. msg.msg_name = &from;
  546. msg.msg_namelen = sizeof(from);
  547. msg.msg_iov = &iov;
  548. msg.msg_iovlen = 1;
  549. msg.msg_control = control_buf;
  550. iov.iov_base = packet;
  551. iov.iov_len = sizeof(packet);
  552. while (1) {
  553. int c;
  554. struct cmsghdr *mp;
  555. int hoplimit = -1;
  556. msg.msg_controllen = sizeof(control_buf);
  557. c = recvmsg(pingsock, &msg, 0);
  558. if (c < 0) {
  559. if (errno != EINTR)
  560. bb_perror_msg("recvfrom");
  561. continue;
  562. }
  563. for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
  564. if (mp->cmsg_level == SOL_IPV6
  565. && mp->cmsg_type == IPV6_HOPLIMIT
  566. /* don't check len - we trust the kernel: */
  567. /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
  568. ) {
  569. hoplimit = *(int*)CMSG_DATA(mp);
  570. }
  571. }
  572. unpack6(packet, c, &from, hoplimit);
  573. if (pingcount > 0 && nreceived >= pingcount)
  574. break;
  575. }
  576. }
  577. #endif
  578. static void ping(len_and_sockaddr *lsa)
  579. {
  580. printf("PING %s (%s)", hostname, dotted);
  581. if (source_lsa) {
  582. printf(" from %s",
  583. xmalloc_sockaddr2dotted_noport(&source_lsa->sa));
  584. }
  585. printf(": %d data bytes\n", datalen);
  586. #if ENABLE_PING6
  587. if (lsa->sa.sa_family == AF_INET6)
  588. ping6(lsa);
  589. else
  590. #endif
  591. ping4(lsa);
  592. }
  593. int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  594. int ping_main(int argc, char **argv)
  595. {
  596. len_and_sockaddr *lsa;
  597. char *opt_c, *opt_s;
  598. USE_PING6(sa_family_t af = AF_UNSPEC;)
  599. INIT_G();
  600. datalen = DEFDATALEN;
  601. /* exactly one argument needed, -v and -q don't mix */
  602. opt_complementary = "=1:q--v:v--q";
  603. getopt32(argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
  604. if (option_mask32 & OPT_c)
  605. pingcount = xatoul(opt_c); // -c
  606. if (option_mask32 & OPT_s)
  607. datalen = xatou16(opt_s); // -s
  608. if (option_mask32 & OPT_I) { // -I
  609. if_index = if_nametoindex(opt_I);
  610. if (!if_index) {
  611. /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */
  612. source_lsa = xdotted2sockaddr(opt_I, 0);
  613. opt_I = NULL; /* don't try to bind to device later */
  614. }
  615. }
  616. myid = (uint16_t) getpid();
  617. hostname = argv[optind];
  618. #if ENABLE_PING6
  619. if (option_mask32 & OPT_IPV4)
  620. af = AF_INET;
  621. if (option_mask32 & OPT_IPV6)
  622. af = AF_INET6;
  623. lsa = xhost_and_af2sockaddr(hostname, 0, af);
  624. #else
  625. lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
  626. #endif
  627. if (source_lsa && source_lsa->sa.sa_family != lsa->sa.sa_family)
  628. /* leaking it here... */
  629. source_lsa = NULL;
  630. dotted = xmalloc_sockaddr2dotted_noport(&lsa->sa);
  631. ping(lsa);
  632. pingstats(0);
  633. return EXIT_SUCCESS;
  634. }
  635. #endif /* FEATURE_FANCY_PING */
  636. #if ENABLE_PING6
  637. int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  638. int ping6_main(int argc, char **argv)
  639. {
  640. argv[0] = (char*)"-6";
  641. return ping_main(argc + 1, argv - 1);
  642. }
  643. #endif
  644. /* from ping6.c:
  645. * Copyright (c) 1989 The Regents of the University of California.
  646. * All rights reserved.
  647. *
  648. * This code is derived from software contributed to Berkeley by
  649. * Mike Muuss.
  650. *
  651. * Redistribution and use in source and binary forms, with or without
  652. * modification, are permitted provided that the following conditions
  653. * are met:
  654. * 1. Redistributions of source code must retain the above copyright
  655. * notice, this list of conditions and the following disclaimer.
  656. * 2. Redistributions in binary form must reproduce the above copyright
  657. * notice, this list of conditions and the following disclaimer in the
  658. * documentation and/or other materials provided with the distribution.
  659. *
  660. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  661. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  662. *
  663. * 4. Neither the name of the University nor the names of its contributors
  664. * may be used to endorse or promote products derived from this software
  665. * without specific prior written permission.
  666. *
  667. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  668. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  669. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  670. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  671. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  672. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  673. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  674. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  675. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  676. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  677. * SUCH DAMAGE.
  678. */