ping6.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * $Id: ping6.c,v 1.6 2004/03/15 08:28:48 andersen Exp $
  4. * Mini ping implementation for busybox
  5. *
  6. * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * This version of ping is adapted from the ping in netkit-base 0.10,
  23. * which is:
  24. *
  25. * Copyright (c) 1989 The Regents of the University of California.
  26. * All rights reserved.
  27. *
  28. * This code is derived from software contributed to Berkeley by
  29. * Mike Muuss.
  30. *
  31. * Original copyright notice is retained at the end of this file.
  32. *
  33. * This version is an adaptation of ping.c from busybox.
  34. * The code was modified by Bart Visscher <magick@linux-fan.com>
  35. */
  36. #include <sys/param.h>
  37. #include <sys/socket.h>
  38. #include <sys/file.h>
  39. #include <sys/time.h>
  40. #include <sys/times.h>
  41. #include <sys/signal.h>
  42. #include <netinet/in.h>
  43. #include <netinet/ip6.h>
  44. #include <netinet/icmp6.h>
  45. #include <arpa/inet.h>
  46. #include <net/if.h>
  47. #include <netdb.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <errno.h>
  51. #include <unistd.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54. #include <stddef.h> /* offsetof */
  55. #include "busybox.h"
  56. static const int DEFDATALEN = 56;
  57. static const int MAXIPLEN = 60;
  58. static const int MAXICMPLEN = 76;
  59. static const int MAXPACKET = 65468;
  60. #define MAX_DUP_CHK (8 * 128)
  61. static const int MAXWAIT = 10;
  62. static const int PINGINTERVAL = 1; /* second */
  63. #define O_QUIET (1 << 0)
  64. #define O_VERBOSE (1 << 1)
  65. #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
  66. #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
  67. #define SET(bit) (A(bit) |= B(bit))
  68. #define CLR(bit) (A(bit) &= (~B(bit)))
  69. #define TST(bit) (A(bit) & B(bit))
  70. static void ping(const char *host);
  71. /* simple version */
  72. #ifndef CONFIG_FEATURE_FANCY_PING6
  73. static struct hostent *h;
  74. void noresp(int ign)
  75. {
  76. printf("No response from %s\n", h->h_name);
  77. exit(EXIT_FAILURE);
  78. }
  79. static void ping(const char *host)
  80. {
  81. struct sockaddr_in6 pingaddr;
  82. struct icmp6_hdr *pkt;
  83. int pingsock, c;
  84. int sockopt;
  85. char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
  86. pingsock = create_icmp6_socket();
  87. memset(&pingaddr, 0, sizeof(struct sockaddr_in));
  88. pingaddr.sin6_family = AF_INET6;
  89. h = xgethostbyname2(host, AF_INET6);
  90. memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
  91. pkt = (struct icmp6_hdr *) packet;
  92. memset(pkt, 0, sizeof(packet));
  93. pkt->icmp6_type = ICMP6_ECHO_REQUEST;
  94. sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
  95. setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
  96. sizeof(sockopt));
  97. c = sendto(pingsock, packet, sizeof(packet), 0,
  98. (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
  99. if (c < 0 || c != sizeof(packet))
  100. bb_perror_msg_and_die("sendto");
  101. signal(SIGALRM, noresp);
  102. alarm(5); /* give the host 5000ms to respond */
  103. /* listen for replies */
  104. while (1) {
  105. struct sockaddr_in6 from;
  106. size_t fromlen = sizeof(from);
  107. if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
  108. (struct sockaddr *) &from, &fromlen)) < 0) {
  109. if (errno == EINTR)
  110. continue;
  111. bb_perror_msg("recvfrom");
  112. continue;
  113. }
  114. if (c >= 8) { /* icmp6_hdr */
  115. pkt = (struct icmp6_hdr *) packet;
  116. if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
  117. break;
  118. }
  119. }
  120. printf("%s is alive!\n", h->h_name);
  121. return;
  122. }
  123. extern int ping6_main(int argc, char **argv)
  124. {
  125. argc--;
  126. argv++;
  127. if (argc < 1)
  128. bb_show_usage();
  129. ping(*argv);
  130. return EXIT_SUCCESS;
  131. }
  132. #else /* ! CONFIG_FEATURE_FANCY_PING6 */
  133. /* full(er) version */
  134. static struct sockaddr_in6 pingaddr;
  135. static int pingsock = -1;
  136. static int datalen; /* intentionally uninitialized to work around gcc bug */
  137. static char* ifname;
  138. static long ntransmitted, nreceived, nrepeats, pingcount;
  139. static int myid, options;
  140. static unsigned long tmin = ULONG_MAX, tmax, tsum;
  141. static char rcvd_tbl[MAX_DUP_CHK / 8];
  142. # ifdef CONFIG_FEATURE_FANCY_PING
  143. extern
  144. # endif
  145. struct hostent *hostent;
  146. static void sendping(int);
  147. static void pingstats(int);
  148. static void unpack(char *, int, struct sockaddr_in6 *, int);
  149. /**************************************************************************/
  150. static void pingstats(int junk)
  151. {
  152. int status;
  153. signal(SIGINT, SIG_IGN);
  154. printf("\n--- %s ping statistics ---\n", hostent->h_name);
  155. printf("%ld packets transmitted, ", ntransmitted);
  156. printf("%ld packets received, ", nreceived);
  157. if (nrepeats)
  158. printf("%ld duplicates, ", nrepeats);
  159. if (ntransmitted)
  160. printf("%ld%% packet loss\n",
  161. (ntransmitted - nreceived) * 100 / ntransmitted);
  162. if (nreceived)
  163. printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
  164. tmin / 10, tmin % 10,
  165. (tsum / (nreceived + nrepeats)) / 10,
  166. (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
  167. if (nreceived != 0)
  168. status = EXIT_SUCCESS;
  169. else
  170. status = EXIT_FAILURE;
  171. exit(status);
  172. }
  173. static void sendping(int junk)
  174. {
  175. struct icmp6_hdr *pkt;
  176. int i;
  177. char packet[datalen + 8];
  178. pkt = (struct icmp6_hdr *) packet;
  179. pkt->icmp6_type = ICMP6_ECHO_REQUEST;
  180. pkt->icmp6_code = 0;
  181. pkt->icmp6_cksum = 0;
  182. pkt->icmp6_seq = ntransmitted++;
  183. pkt->icmp6_id = myid;
  184. CLR(pkt->icmp6_seq % MAX_DUP_CHK);
  185. gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
  186. i = sendto(pingsock, packet, sizeof(packet), 0,
  187. (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
  188. if (i < 0)
  189. bb_perror_msg_and_die("sendto");
  190. else if ((size_t)i != sizeof(packet))
  191. bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
  192. (int)sizeof(packet));
  193. signal(SIGALRM, sendping);
  194. if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
  195. alarm(PINGINTERVAL);
  196. } else { /* done, wait for the last ping to come back */
  197. /* todo, don't necessarily need to wait so long... */
  198. signal(SIGALRM, pingstats);
  199. alarm(MAXWAIT);
  200. }
  201. }
  202. static char *icmp6_type_name (int id)
  203. {
  204. switch (id) {
  205. case ICMP6_DST_UNREACH: return "Destination Unreachable";
  206. case ICMP6_PACKET_TOO_BIG: return "Packet too big";
  207. case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
  208. case ICMP6_PARAM_PROB: return "Parameter Problem";
  209. case ICMP6_ECHO_REPLY: return "Echo Reply";
  210. case ICMP6_ECHO_REQUEST: return "Echo Request";
  211. case ICMP6_MEMBERSHIP_QUERY: return "Membership Query";
  212. case ICMP6_MEMBERSHIP_REPORT: return "Membership Report";
  213. case ICMP6_MEMBERSHIP_REDUCTION: return "Membership Reduction";
  214. default: return "unknown ICMP type";
  215. }
  216. }
  217. static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
  218. {
  219. struct icmp6_hdr *icmppkt;
  220. struct timeval tv, *tp;
  221. int dupflag;
  222. unsigned long triptime;
  223. char buf[INET6_ADDRSTRLEN];
  224. gettimeofday(&tv, NULL);
  225. /* discard if too short */
  226. if (sz < (datalen + sizeof(struct icmp6_hdr)))
  227. return;
  228. icmppkt = (struct icmp6_hdr *) packet;
  229. if (icmppkt->icmp6_id != myid)
  230. return; /* not our ping */
  231. if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
  232. ++nreceived;
  233. tp = (struct timeval *) &icmppkt->icmp6_data8[4];
  234. if ((tv.tv_usec -= tp->tv_usec) < 0) {
  235. --tv.tv_sec;
  236. tv.tv_usec += 1000000;
  237. }
  238. tv.tv_sec -= tp->tv_sec;
  239. triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
  240. tsum += triptime;
  241. if (triptime < tmin)
  242. tmin = triptime;
  243. if (triptime > tmax)
  244. tmax = triptime;
  245. if (TST(icmppkt->icmp6_seq % MAX_DUP_CHK)) {
  246. ++nrepeats;
  247. --nreceived;
  248. dupflag = 1;
  249. } else {
  250. SET(icmppkt->icmp6_seq % MAX_DUP_CHK);
  251. dupflag = 0;
  252. }
  253. if (options & O_QUIET)
  254. return;
  255. printf("%d bytes from %s: icmp6_seq=%u", sz,
  256. inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr,
  257. buf, sizeof(buf)),
  258. icmppkt->icmp6_seq);
  259. printf(" ttl=%d time=%lu.%lu ms", hoplimit,
  260. triptime / 10, triptime % 10);
  261. if (dupflag)
  262. printf(" (DUP!)");
  263. printf("\n");
  264. } else
  265. if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
  266. bb_error_msg("Warning: Got ICMP %d (%s)",
  267. icmppkt->icmp6_type, icmp6_type_name (icmppkt->icmp6_type));
  268. }
  269. static void ping(const char *host)
  270. {
  271. char packet[datalen + MAXIPLEN + MAXICMPLEN];
  272. char buf[INET6_ADDRSTRLEN];
  273. int sockopt;
  274. struct msghdr msg;
  275. struct sockaddr_in6 from;
  276. struct iovec iov;
  277. char control_buf[CMSG_SPACE(36)];
  278. pingsock = create_icmp6_socket();
  279. memset(&pingaddr, 0, sizeof(struct sockaddr_in));
  280. pingaddr.sin6_family = AF_INET6;
  281. hostent = xgethostbyname2(host, AF_INET6);
  282. if (hostent->h_addrtype != AF_INET6)
  283. bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported.");
  284. memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
  285. #ifdef ICMP6_FILTER
  286. {
  287. struct icmp6_filter filt;
  288. if (!(options & O_VERBOSE)) {
  289. ICMP6_FILTER_SETBLOCKALL(&filt);
  290. #if 0
  291. if ((options & F_FQDN) || (options & F_FQDNOLD) ||
  292. (options & F_NODEADDR) || (options & F_SUPTYPES))
  293. ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
  294. else
  295. #endif
  296. ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
  297. } else {
  298. ICMP6_FILTER_SETPASSALL(&filt);
  299. }
  300. if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
  301. sizeof(filt)) < 0)
  302. bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
  303. }
  304. #endif /*ICMP6_FILTER*/
  305. /* enable broadcast pings */
  306. sockopt = 1;
  307. setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
  308. sizeof(sockopt));
  309. /* set recv buf for broadcast pings */
  310. sockopt = 48 * 1024;
  311. setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
  312. sizeof(sockopt));
  313. sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
  314. setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
  315. sizeof(sockopt));
  316. sockopt = 1;
  317. setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, (char *) &sockopt,
  318. sizeof(sockopt));
  319. if (ifname) {
  320. if ((pingaddr.sin6_scope_id = if_nametoindex(ifname)) == 0)
  321. bb_error_msg_and_die("%s: invalid interface name", ifname);
  322. }
  323. printf("PING %s (%s): %d data bytes\n",
  324. hostent->h_name,
  325. inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr,
  326. buf, sizeof(buf)),
  327. datalen);
  328. signal(SIGINT, pingstats);
  329. /* start the ping's going ... */
  330. sendping(0);
  331. /* listen for replies */
  332. msg.msg_name=&from;
  333. msg.msg_namelen=sizeof(from);
  334. msg.msg_iov=&iov;
  335. msg.msg_iovlen=1;
  336. msg.msg_control=control_buf;
  337. iov.iov_base=packet;
  338. iov.iov_len=sizeof(packet);
  339. while (1) {
  340. int c;
  341. struct cmsghdr *cmsgptr = NULL;
  342. int hoplimit=-1;
  343. msg.msg_controllen=sizeof(control_buf);
  344. if ((c = recvmsg(pingsock, &msg, 0)) < 0) {
  345. if (errno == EINTR)
  346. continue;
  347. bb_perror_msg("recvfrom");
  348. continue;
  349. }
  350. for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
  351. cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
  352. if (cmsgptr->cmsg_level == SOL_IPV6 &&
  353. cmsgptr->cmsg_type == IPV6_HOPLIMIT ) {
  354. hoplimit=*(int*)CMSG_DATA(cmsgptr);
  355. }
  356. }
  357. unpack(packet, c, &from, hoplimit);
  358. if (pingcount > 0 && nreceived >= pingcount)
  359. break;
  360. }
  361. pingstats(0);
  362. }
  363. extern int ping6_main(int argc, char **argv)
  364. {
  365. char *thisarg;
  366. datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
  367. argc--;
  368. argv++;
  369. options = 0;
  370. /* Parse any options */
  371. while (argc >= 1 && **argv == '-') {
  372. thisarg = *argv;
  373. thisarg++;
  374. switch (*thisarg) {
  375. case 'v':
  376. options &= ~O_QUIET;
  377. options |= O_VERBOSE;
  378. break;
  379. case 'q':
  380. options &= ~O_VERBOSE;
  381. options |= O_QUIET;
  382. break;
  383. case 'c':
  384. if (--argc <= 0)
  385. bb_show_usage();
  386. argv++;
  387. pingcount = atoi(*argv);
  388. break;
  389. case 's':
  390. if (--argc <= 0)
  391. bb_show_usage();
  392. argv++;
  393. datalen = atoi(*argv);
  394. break;
  395. case 'I':
  396. if (--argc <= 0)
  397. bb_show_usage();
  398. argv++;
  399. ifname = *argv;
  400. break;
  401. default:
  402. bb_show_usage();
  403. }
  404. argc--;
  405. argv++;
  406. }
  407. if (argc < 1)
  408. bb_show_usage();
  409. myid = getpid() & 0xFFFF;
  410. ping(*argv);
  411. return EXIT_SUCCESS;
  412. }
  413. #endif /* ! CONFIG_FEATURE_FANCY_PING6 */
  414. /*
  415. * Copyright (c) 1989 The Regents of the University of California.
  416. * All rights reserved.
  417. *
  418. * This code is derived from software contributed to Berkeley by
  419. * Mike Muuss.
  420. *
  421. * Redistribution and use in source and binary forms, with or without
  422. * modification, are permitted provided that the following conditions
  423. * are met:
  424. * 1. Redistributions of source code must retain the above copyright
  425. * notice, this list of conditions and the following disclaimer.
  426. * 2. Redistributions in binary form must reproduce the above copyright
  427. * notice, this list of conditions and the following disclaimer in the
  428. * documentation and/or other materials provided with the distribution.
  429. *
  430. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  431. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  432. *
  433. * 4. Neither the name of the University nor the names of its contributors
  434. * may be used to endorse or promote products derived from this software
  435. * without specific prior written permission.
  436. *
  437. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  438. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  439. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  440. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  441. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  442. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  443. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  444. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  445. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  446. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  447. * SUCH DAMAGE.
  448. */