ping6.c 14 KB

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