004-ping.patch 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. diff -Nur busybox-1.22.1.orig/networking/ping.c busybox-1.22.1/networking/ping.c
  2. --- busybox-1.22.1.orig/networking/ping.c 2014-01-20 03:38:10.000000000 +0100
  3. +++ busybox-1.22.1/networking/ping.c 2014-05-24 14:31:09.000000000 +0200
  4. @@ -152,6 +152,7 @@
  5. pingsock = 0,
  6. };
  7. +static int using_dgram;
  8. static void
  9. #if ENABLE_PING6
  10. create_icmp_socket(len_and_sockaddr *lsa)
  11. @@ -184,6 +185,7 @@
  12. if (sock < 0)
  13. #endif
  14. bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  15. + using_dgram = 1;
  16. }
  17. xmove_fd(sock, pingsock);
  18. @@ -234,10 +236,12 @@
  19. bb_perror_msg("recvfrom");
  20. continue;
  21. }
  22. - if (c >= 76) { /* ip + icmp */
  23. - struct iphdr *iphdr = (struct iphdr *) G.packet;
  24. + if (c >= 76 || using_dgram && (c == 64)) { /* ip + icmp */
  25. + if(!using_dgram) {
  26. + struct iphdr *iphdr = (struct iphdr *) G.packet;
  27. - pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */
  28. + pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */
  29. + } else pkt = (struct icmp *) G.packet;
  30. if (pkt->icmp_type == ICMP_ECHOREPLY)
  31. break;
  32. }
  33. @@ -628,19 +632,21 @@
  34. }
  35. static void unpack4(char *buf, int sz, struct sockaddr_in *from)
  36. {
  37. - struct icmp *icmppkt;
  38. struct iphdr *iphdr;
  39. + struct icmp *icmppkt;
  40. int hlen;
  41. /* discard if too short */
  42. if (sz < (datalen + ICMP_MINLEN))
  43. return;
  44. + if(!using_dgram) {
  45. + /* check IP header */
  46. + iphdr = (struct iphdr *) buf;
  47. + hlen = iphdr->ihl << 2;
  48. + sz -= hlen;
  49. + icmppkt = (struct icmp *) (buf + hlen);
  50. + } else icmppkt = (struct icmp *) buf;
  51. - /* check IP header */
  52. - iphdr = (struct iphdr *) buf;
  53. - hlen = iphdr->ihl << 2;
  54. - sz -= hlen;
  55. - icmppkt = (struct icmp *) (buf + hlen);
  56. if (icmppkt->icmp_id != myid)
  57. return; /* not our ping */
  58. @@ -652,7 +658,7 @@
  59. tp = (uint32_t *) icmppkt->icmp_data;
  60. unpack_tail(sz, tp,
  61. inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
  62. - recv_seq, iphdr->ttl);
  63. + recv_seq, using_dgram ? 42 : iphdr->ttl);
  64. } else if (icmppkt->icmp_type != ICMP_ECHO) {
  65. bb_error_msg("warning: got ICMP %d (%s)",
  66. icmppkt->icmp_type,
  67. @@ -696,11 +702,31 @@
  68. int sockopt;
  69. pingaddr.sin = lsa->u.sin;
  70. - if (source_lsa) {
  71. + if (source_lsa && !using_dgram) {
  72. if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF,
  73. &source_lsa->u.sa, source_lsa->len))
  74. bb_error_msg_and_die("can't set multicast source interface");
  75. xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
  76. + } else if(using_dgram) {
  77. + struct sockaddr_in sa;
  78. + socklen_t sl;
  79. +
  80. + sa.sin_family = AF_INET;
  81. + sa.sin_port = 0;
  82. + sa.sin_addr.s_addr = source_lsa ?
  83. + source_lsa->u.sin.sin_addr.s_addr : 0;
  84. + sl = sizeof(sa);
  85. +
  86. + if (bind(pingsock, (struct sockaddr *) &sa, sl) == -1) {
  87. + perror("bind");
  88. + exit(2);
  89. + }
  90. +
  91. + if (getsockname(pingsock, (struct sockaddr *) &sa, &sl) == -1) {
  92. + perror("getsockname");
  93. + exit(2);
  94. + }
  95. + myid = sa.sin_port;
  96. }
  97. /* enable broadcast pings */
  98. @@ -717,6 +743,15 @@
  99. setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl));
  100. }
  101. + if(using_dgram) {
  102. + int hold = 65536;
  103. + if (setsockopt(pingsock, SOL_IP, IP_RECVTTL, (char *)&hold, sizeof(hold)))
  104. + perror("WARNING: setsockopt(IP_RECVTTL)");
  105. + if (setsockopt(pingsock, SOL_IP, IP_RETOPTS, (char *)&hold, sizeof(hold)))
  106. + perror("WARNING: setsockopt(IP_RETOPTS)");
  107. +
  108. + }
  109. +
  110. signal(SIGINT, print_stats_and_exit);
  111. /* start the ping's going ... */
  112. @@ -751,10 +786,33 @@
  113. char control_buf[CMSG_SPACE(36)];
  114. pingaddr.sin6 = lsa->u.sin6;
  115. - if (source_lsa)
  116. + if (source_lsa && !using_dgram)
  117. xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
  118. + else if(using_dgram) {
  119. + struct sockaddr_in6 sa = {0};
  120. + socklen_t sl;
  121. +
  122. + sa.sin6_family = AF_INET6;
  123. + sa.sin6_port = 0;
  124. + if(source_lsa) {
  125. + memcpy(&sa.sin6_addr, &source_lsa->u.sin6.sin6_addr, sizeof(struct in6_addr));
  126. + }
  127. + sl = sizeof(sa);
  128. +
  129. + if (bind(pingsock, (struct sockaddr *) &sa, sl) == -1) {
  130. + perror("bind");
  131. + exit(2);
  132. + }
  133. +
  134. + if (getsockname(pingsock, (struct sockaddr *) &sa, &sl) == -1) {
  135. + perror("getsockname");
  136. + exit(2);
  137. + }
  138. + myid = sa.sin6_port;
  139. + }
  140. #ifdef ICMP6_FILTER
  141. + if(!using_dgram)
  142. {
  143. struct icmp6_filter filt;
  144. if (!(option_mask32 & OPT_VERBOSE)) {
  145. @@ -880,7 +938,7 @@
  146. str_I = NULL; /* don't try to bind to device later */
  147. }
  148. }
  149. - myid = (uint16_t) getpid();
  150. + if(!using_dgram) myid = (uint16_t) getpid();
  151. hostname = argv[optind];
  152. #if ENABLE_PING6
  153. {
  154. diff -Nur busybox-1.22.1.orig/networking/ping.c.orig busybox-1.22.1/networking/ping.c.orig
  155. --- busybox-1.22.1.orig/networking/ping.c.orig 1970-01-01 01:00:00.000000000 +0100
  156. +++ busybox-1.22.1/networking/ping.c.orig 2014-01-20 03:38:10.000000000 +0100
  157. @@ -0,0 +1,966 @@
  158. +/* vi: set sw=4 ts=4: */
  159. +/*
  160. + * Mini ping implementation for busybox
  161. + *
  162. + * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  163. + *
  164. + * Adapted from the ping in netkit-base 0.10:
  165. + * Copyright (c) 1989 The Regents of the University of California.
  166. + * All rights reserved.
  167. + *
  168. + * This code is derived from software contributed to Berkeley by
  169. + * Mike Muuss.
  170. + *
  171. + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  172. + */
  173. +/* from ping6.c:
  174. + * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  175. + *
  176. + * This version of ping is adapted from the ping in netkit-base 0.10,
  177. + * which is:
  178. + *
  179. + * Original copyright notice is retained at the end of this file.
  180. + *
  181. + * This version is an adaptation of ping.c from busybox.
  182. + * The code was modified by Bart Visscher <magick@linux-fan.com>
  183. + */
  184. +
  185. +#include <net/if.h>
  186. +#include <netinet/ip_icmp.h>
  187. +#include "libbb.h"
  188. +
  189. +#ifdef __BIONIC__
  190. +/* should be in netinet/ip_icmp.h */
  191. +# define ICMP_DEST_UNREACH 3 /* Destination Unreachable */
  192. +# define ICMP_SOURCE_QUENCH 4 /* Source Quench */
  193. +# define ICMP_REDIRECT 5 /* Redirect (change route) */
  194. +# define ICMP_ECHO 8 /* Echo Request */
  195. +# define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
  196. +# define ICMP_PARAMETERPROB 12 /* Parameter Problem */
  197. +# define ICMP_TIMESTAMP 13 /* Timestamp Request */
  198. +# define ICMP_TIMESTAMPREPLY 14 /* Timestamp Reply */
  199. +# define ICMP_INFO_REQUEST 15 /* Information Request */
  200. +# define ICMP_INFO_REPLY 16 /* Information Reply */
  201. +# define ICMP_ADDRESS 17 /* Address Mask Request */
  202. +# define ICMP_ADDRESSREPLY 18 /* Address Mask Reply */
  203. +#endif
  204. +
  205. +//config:config PING
  206. +//config: bool "ping"
  207. +//config: default y
  208. +//config: select PLATFORM_LINUX
  209. +//config: help
  210. +//config: ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to
  211. +//config: elicit an ICMP ECHO_RESPONSE from a host or gateway.
  212. +//config:
  213. +//config:config PING6
  214. +//config: bool "ping6"
  215. +//config: default y
  216. +//config: depends on FEATURE_IPV6 && PING
  217. +//config: help
  218. +//config: This will give you a ping that can talk IPv6.
  219. +//config:
  220. +//config:config FEATURE_FANCY_PING
  221. +//config: bool "Enable fancy ping output"
  222. +//config: default y
  223. +//config: depends on PING
  224. +//config: help
  225. +//config: Make the output from the ping applet include statistics, and at the
  226. +//config: same time provide full support for ICMP packets.
  227. +
  228. +/* Needs socket(AF_INET, SOCK_RAW, IPPROTO_ICMP), therefore BB_SUID_MAYBE: */
  229. +//applet:IF_PING(APPLET(ping, BB_DIR_BIN, BB_SUID_MAYBE))
  230. +//applet:IF_PING6(APPLET(ping6, BB_DIR_BIN, BB_SUID_MAYBE))
  231. +
  232. +//kbuild:lib-$(CONFIG_PING) += ping.o
  233. +//kbuild:lib-$(CONFIG_PING6) += ping.o
  234. +
  235. +//usage:#if !ENABLE_FEATURE_FANCY_PING
  236. +//usage:# define ping_trivial_usage
  237. +//usage: "HOST"
  238. +//usage:# define ping_full_usage "\n\n"
  239. +//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
  240. +//usage:# define ping6_trivial_usage
  241. +//usage: "HOST"
  242. +//usage:# define ping6_full_usage "\n\n"
  243. +//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
  244. +//usage:#else
  245. +//usage:# define ping_trivial_usage
  246. +//usage: "[OPTIONS] HOST"
  247. +//usage:# define ping_full_usage "\n\n"
  248. +//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
  249. +//usage: IF_PING6(
  250. +//usage: "\n -4,-6 Force IP or IPv6 name resolution"
  251. +//usage: )
  252. +//usage: "\n -c CNT Send only CNT pings"
  253. +//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
  254. +//usage: "\n -t TTL Set TTL"
  255. +//usage: "\n -I IFACE/IP Use interface or IP address as source"
  256. +//usage: "\n -W SEC Seconds to wait for the first response (default:10)"
  257. +//usage: "\n (after all -c CNT packets are sent)"
  258. +//usage: "\n -w SEC Seconds until ping exits (default:infinite)"
  259. +//usage: "\n (can exit earlier with -c CNT)"
  260. +//usage: "\n -q Quiet, only displays output at start"
  261. +//usage: "\n and when finished"
  262. +//usage:
  263. +//usage:# define ping6_trivial_usage
  264. +//usage: "[OPTIONS] HOST"
  265. +//usage:# define ping6_full_usage "\n\n"
  266. +//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
  267. +//usage: "\n -c CNT Send only CNT pings"
  268. +//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
  269. +//usage: "\n -I IFACE/IP Use interface or IP address as source"
  270. +//usage: "\n -q Quiet, only displays output at start"
  271. +//usage: "\n and when finished"
  272. +//usage:
  273. +//usage:#endif
  274. +//usage:
  275. +//usage:#define ping_example_usage
  276. +//usage: "$ ping localhost\n"
  277. +//usage: "PING slag (127.0.0.1): 56 data bytes\n"
  278. +//usage: "64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=20.1 ms\n"
  279. +//usage: "\n"
  280. +//usage: "--- debian ping statistics ---\n"
  281. +//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
  282. +//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
  283. +//usage:#define ping6_example_usage
  284. +//usage: "$ ping6 ip6-localhost\n"
  285. +//usage: "PING ip6-localhost (::1): 56 data bytes\n"
  286. +//usage: "64 bytes from ::1: icmp6_seq=0 ttl=64 time=20.1 ms\n"
  287. +//usage: "\n"
  288. +//usage: "--- ip6-localhost ping statistics ---\n"
  289. +//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
  290. +//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
  291. +
  292. +#if ENABLE_PING6
  293. +# include <netinet/icmp6.h>
  294. +/* I see RENUMBERED constants in bits/in.h - !!?
  295. + * What a fuck is going on with libc? Is it a glibc joke? */
  296. +# ifdef IPV6_2292HOPLIMIT
  297. +# undef IPV6_HOPLIMIT
  298. +# define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
  299. +# endif
  300. +#endif
  301. +
  302. +enum {
  303. + DEFDATALEN = 56,
  304. + MAXIPLEN = 60,
  305. + MAXICMPLEN = 76,
  306. + MAX_DUP_CHK = (8 * 128),
  307. + MAXWAIT = 10,
  308. + PINGINTERVAL = 1, /* 1 second */
  309. + pingsock = 0,
  310. +};
  311. +
  312. +static void
  313. +#if ENABLE_PING6
  314. +create_icmp_socket(len_and_sockaddr *lsa)
  315. +#else
  316. +create_icmp_socket(void)
  317. +#define create_icmp_socket(lsa) create_icmp_socket()
  318. +#endif
  319. +{
  320. + int sock;
  321. +#if ENABLE_PING6
  322. + if (lsa->u.sa.sa_family == AF_INET6)
  323. + sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
  324. + else
  325. +#endif
  326. + sock = socket(AF_INET, SOCK_RAW, 1); /* 1 == ICMP */
  327. + if (sock < 0) {
  328. + if (errno != EPERM)
  329. + bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
  330. +#if defined(__linux__) || defined(__APPLE__)
  331. + /* We don't have root privileges. Try SOCK_DGRAM instead.
  332. + * Linux needs net.ipv4.ping_group_range for this to work.
  333. + * MacOSX allows ICMP_ECHO, ICMP_TSTAMP or ICMP_MASKREQ
  334. + */
  335. +#if ENABLE_PING6
  336. + if (lsa->u.sa.sa_family == AF_INET6)
  337. + sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6);
  338. + else
  339. +#endif
  340. + sock = socket(AF_INET, SOCK_DGRAM, 1); /* 1 == ICMP */
  341. + if (sock < 0)
  342. +#endif
  343. + bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  344. + }
  345. +
  346. + xmove_fd(sock, pingsock);
  347. +}
  348. +
  349. +#if !ENABLE_FEATURE_FANCY_PING
  350. +
  351. +/* Simple version */
  352. +
  353. +struct globals {
  354. + char *hostname;
  355. + char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
  356. +} FIX_ALIASING;
  357. +#define G (*(struct globals*)&bb_common_bufsiz1)
  358. +#define INIT_G() do { } while (0)
  359. +
  360. +static void noresp(int ign UNUSED_PARAM)
  361. +{
  362. + printf("No response from %s\n", G.hostname);
  363. + exit(EXIT_FAILURE);
  364. +}
  365. +
  366. +static void ping4(len_and_sockaddr *lsa)
  367. +{
  368. + struct icmp *pkt;
  369. + int c;
  370. +
  371. + pkt = (struct icmp *) G.packet;
  372. + /*memset(pkt, 0, sizeof(G.packet)); already is */
  373. + pkt->icmp_type = ICMP_ECHO;
  374. + pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet));
  375. +
  376. + xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len);
  377. +
  378. + /* listen for replies */
  379. + while (1) {
  380. +#if 0
  381. + struct sockaddr_in from;
  382. + socklen_t fromlen = sizeof(from);
  383. +
  384. + c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
  385. + (struct sockaddr *) &from, &fromlen);
  386. +#else
  387. + c = recv(pingsock, G.packet, sizeof(G.packet), 0);
  388. +#endif
  389. + if (c < 0) {
  390. + if (errno != EINTR)
  391. + bb_perror_msg("recvfrom");
  392. + continue;
  393. + }
  394. + if (c >= 76) { /* ip + icmp */
  395. + struct iphdr *iphdr = (struct iphdr *) G.packet;
  396. +
  397. + pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */
  398. + if (pkt->icmp_type == ICMP_ECHOREPLY)
  399. + break;
  400. + }
  401. + }
  402. + if (ENABLE_FEATURE_CLEAN_UP)
  403. + close(pingsock);
  404. +}
  405. +
  406. +#if ENABLE_PING6
  407. +static void ping6(len_and_sockaddr *lsa)
  408. +{
  409. + struct icmp6_hdr *pkt;
  410. + int c;
  411. + int sockopt;
  412. +
  413. + pkt = (struct icmp6_hdr *) G.packet;
  414. + /*memset(pkt, 0, sizeof(G.packet)); already is */
  415. + pkt->icmp6_type = ICMP6_ECHO_REQUEST;
  416. +
  417. + sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
  418. + setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
  419. +
  420. + xsendto(pingsock, G.packet, DEFDATALEN + sizeof(struct icmp6_hdr), &lsa->u.sa, lsa->len);
  421. +
  422. + /* listen for replies */
  423. + while (1) {
  424. +#if 0
  425. + struct sockaddr_in6 from;
  426. + socklen_t fromlen = sizeof(from);
  427. +
  428. + c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
  429. + (struct sockaddr *) &from, &fromlen);
  430. +#else
  431. + c = recv(pingsock, G.packet, sizeof(G.packet), 0);
  432. +#endif
  433. + if (c < 0) {
  434. + if (errno != EINTR)
  435. + bb_perror_msg("recvfrom");
  436. + continue;
  437. + }
  438. + if (c >= ICMP_MINLEN) { /* icmp6_hdr */
  439. + if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
  440. + break;
  441. + }
  442. + }
  443. + if (ENABLE_FEATURE_CLEAN_UP)
  444. + close(pingsock);
  445. +}
  446. +#endif
  447. +
  448. +#if !ENABLE_PING6
  449. +# define common_ping_main(af, argv) common_ping_main(argv)
  450. +#endif
  451. +static int common_ping_main(sa_family_t af, char **argv)
  452. +{
  453. + len_and_sockaddr *lsa;
  454. +
  455. + INIT_G();
  456. +
  457. +#if ENABLE_PING6
  458. + while ((++argv)[0] && argv[0][0] == '-') {
  459. + if (argv[0][1] == '4') {
  460. + af = AF_INET;
  461. + continue;
  462. + }
  463. + if (argv[0][1] == '6') {
  464. + af = AF_INET6;
  465. + continue;
  466. + }
  467. + bb_show_usage();
  468. + }
  469. +#else
  470. + argv++;
  471. +#endif
  472. +
  473. + G.hostname = *argv;
  474. + if (!G.hostname)
  475. + bb_show_usage();
  476. +
  477. +#if ENABLE_PING6
  478. + lsa = xhost_and_af2sockaddr(G.hostname, 0, af);
  479. +#else
  480. + lsa = xhost_and_af2sockaddr(G.hostname, 0, AF_INET);
  481. +#endif
  482. + /* Set timer _after_ DNS resolution */
  483. + signal(SIGALRM, noresp);
  484. + alarm(5); /* give the host 5000ms to respond */
  485. +
  486. + create_icmp_socket(lsa);
  487. +#if ENABLE_PING6
  488. + if (lsa->u.sa.sa_family == AF_INET6)
  489. + ping6(lsa);
  490. + else
  491. +#endif
  492. + ping4(lsa);
  493. + printf("%s is alive!\n", G.hostname);
  494. + return EXIT_SUCCESS;
  495. +}
  496. +
  497. +
  498. +#else /* FEATURE_FANCY_PING */
  499. +
  500. +
  501. +/* Full(er) version */
  502. +
  503. +#define OPT_STRING ("qvc:s:t:w:W:I:n4" IF_PING6("6"))
  504. +enum {
  505. + OPT_QUIET = 1 << 0,
  506. + OPT_VERBOSE = 1 << 1,
  507. + OPT_c = 1 << 2,
  508. + OPT_s = 1 << 3,
  509. + OPT_t = 1 << 4,
  510. + OPT_w = 1 << 5,
  511. + OPT_W = 1 << 6,
  512. + OPT_I = 1 << 7,
  513. + /*OPT_n = 1 << 8, - ignored */
  514. + OPT_IPV4 = 1 << 9,
  515. + OPT_IPV6 = (1 << 10) * ENABLE_PING6,
  516. +};
  517. +
  518. +
  519. +struct globals {
  520. + int if_index;
  521. + char *str_I;
  522. + len_and_sockaddr *source_lsa;
  523. + unsigned datalen;
  524. + unsigned pingcount; /* must be int-sized */
  525. + unsigned opt_ttl;
  526. + unsigned long ntransmitted, nreceived, nrepeats;
  527. + uint16_t myid;
  528. + unsigned tmin, tmax; /* in us */
  529. + unsigned long long tsum; /* in us, sum of all times */
  530. + unsigned deadline;
  531. + unsigned timeout;
  532. + unsigned total_secs;
  533. + unsigned sizeof_rcv_packet;
  534. + char *rcv_packet; /* [datalen + MAXIPLEN + MAXICMPLEN] */
  535. + void *snd_packet; /* [datalen + ipv4/ipv6_const] */
  536. + const char *hostname;
  537. + const char *dotted;
  538. + union {
  539. + struct sockaddr sa;
  540. + struct sockaddr_in sin;
  541. +#if ENABLE_PING6
  542. + struct sockaddr_in6 sin6;
  543. +#endif
  544. + } pingaddr;
  545. + unsigned char rcvd_tbl[MAX_DUP_CHK / 8];
  546. +} FIX_ALIASING;
  547. +#define G (*(struct globals*)&bb_common_bufsiz1)
  548. +#define if_index (G.if_index )
  549. +#define source_lsa (G.source_lsa )
  550. +#define str_I (G.str_I )
  551. +#define datalen (G.datalen )
  552. +#define pingcount (G.pingcount )
  553. +#define opt_ttl (G.opt_ttl )
  554. +#define myid (G.myid )
  555. +#define tmin (G.tmin )
  556. +#define tmax (G.tmax )
  557. +#define tsum (G.tsum )
  558. +#define deadline (G.deadline )
  559. +#define timeout (G.timeout )
  560. +#define total_secs (G.total_secs )
  561. +#define hostname (G.hostname )
  562. +#define dotted (G.dotted )
  563. +#define pingaddr (G.pingaddr )
  564. +#define rcvd_tbl (G.rcvd_tbl )
  565. +void BUG_ping_globals_too_big(void);
  566. +#define INIT_G() do { \
  567. + if (sizeof(G) > COMMON_BUFSIZE) \
  568. + BUG_ping_globals_too_big(); \
  569. + datalen = DEFDATALEN; \
  570. + timeout = MAXWAIT; \
  571. + tmin = UINT_MAX; \
  572. +} while (0)
  573. +
  574. +
  575. +#define BYTE(bit) rcvd_tbl[(bit)>>3]
  576. +#define MASK(bit) (1 << ((bit) & 7))
  577. +#define SET(bit) (BYTE(bit) |= MASK(bit))
  578. +#define CLR(bit) (BYTE(bit) &= (~MASK(bit)))
  579. +#define TST(bit) (BYTE(bit) & MASK(bit))
  580. +
  581. +static void print_stats_and_exit(int junk) NORETURN;
  582. +static void print_stats_and_exit(int junk UNUSED_PARAM)
  583. +{
  584. + unsigned long ul;
  585. + unsigned long nrecv;
  586. +
  587. + signal(SIGINT, SIG_IGN);
  588. +
  589. + nrecv = G.nreceived;
  590. + printf("\n--- %s ping statistics ---\n"
  591. + "%lu packets transmitted, "
  592. + "%lu packets received, ",
  593. + hostname, G.ntransmitted, nrecv
  594. + );
  595. + if (G.nrepeats)
  596. + printf("%lu duplicates, ", G.nrepeats);
  597. + ul = G.ntransmitted;
  598. + if (ul != 0)
  599. + ul = (ul - nrecv) * 100 / ul;
  600. + printf("%lu%% packet loss\n", ul);
  601. + if (tmin != UINT_MAX) {
  602. + unsigned tavg = tsum / (nrecv + G.nrepeats);
  603. + printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n",
  604. + tmin / 1000, tmin % 1000,
  605. + tavg / 1000, tavg % 1000,
  606. + tmax / 1000, tmax % 1000);
  607. + }
  608. + /* if condition is true, exit with 1 -- 'failure' */
  609. + exit(nrecv == 0 || (deadline && nrecv < pingcount));
  610. +}
  611. +
  612. +static void sendping_tail(void (*sp)(int), int size_pkt)
  613. +{
  614. + int sz;
  615. +
  616. + CLR((uint16_t)G.ntransmitted % MAX_DUP_CHK);
  617. + G.ntransmitted++;
  618. +
  619. + size_pkt += datalen;
  620. +
  621. + /* sizeof(pingaddr) can be larger than real sa size, but I think
  622. + * it doesn't matter */
  623. + sz = xsendto(pingsock, G.snd_packet, size_pkt, &pingaddr.sa, sizeof(pingaddr));
  624. + if (sz != size_pkt)
  625. + bb_error_msg_and_die(bb_msg_write_error);
  626. +
  627. + if (pingcount == 0 || deadline || G.ntransmitted < pingcount) {
  628. + /* Didn't send all pings yet - schedule next in 1s */
  629. + signal(SIGALRM, sp);
  630. + if (deadline) {
  631. + total_secs += PINGINTERVAL;
  632. + if (total_secs >= deadline)
  633. + signal(SIGALRM, print_stats_and_exit);
  634. + }
  635. + alarm(PINGINTERVAL);
  636. + } else { /* -c NN, and all NN are sent (and no deadline) */
  637. + /* Wait for the last ping to come back.
  638. + * -W timeout: wait for a response in seconds.
  639. + * Affects only timeout in absense of any responses,
  640. + * otherwise ping waits for two RTTs. */
  641. + unsigned expire = timeout;
  642. +
  643. + if (G.nreceived) {
  644. + /* approx. 2*tmax, in seconds (2 RTT) */
  645. + expire = tmax / (512*1024);
  646. + if (expire == 0)
  647. + expire = 1;
  648. + }
  649. + signal(SIGALRM, print_stats_and_exit);
  650. + alarm(expire);
  651. + }
  652. +}
  653. +
  654. +static void sendping4(int junk UNUSED_PARAM)
  655. +{
  656. + struct icmp *pkt = G.snd_packet;
  657. +
  658. + //memset(pkt, 0, datalen + ICMP_MINLEN + 4); - G.snd_packet was xzalloced
  659. + pkt->icmp_type = ICMP_ECHO;
  660. + /*pkt->icmp_code = 0;*/
  661. + pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */
  662. + pkt->icmp_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */
  663. + pkt->icmp_id = myid;
  664. +
  665. + /* If datalen < 4, we store timestamp _past_ the packet,
  666. + * but it's ok - we allocated 4 extra bytes in xzalloc() just in case.
  667. + */
  668. + /*if (datalen >= 4)*/
  669. + /* No hton: we'll read it back on the same machine */
  670. + *(uint32_t*)&pkt->icmp_dun = monotonic_us();
  671. +
  672. + pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, datalen + ICMP_MINLEN);
  673. +
  674. + sendping_tail(sendping4, ICMP_MINLEN);
  675. +}
  676. +#if ENABLE_PING6
  677. +static void sendping6(int junk UNUSED_PARAM)
  678. +{
  679. + struct icmp6_hdr *pkt = G.snd_packet;
  680. +
  681. + //memset(pkt, 0, datalen + sizeof(struct icmp6_hdr) + 4);
  682. + pkt->icmp6_type = ICMP6_ECHO_REQUEST;
  683. + /*pkt->icmp6_code = 0;*/
  684. + /*pkt->icmp6_cksum = 0;*/
  685. + pkt->icmp6_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */
  686. + pkt->icmp6_id = myid;
  687. +
  688. + /*if (datalen >= 4)*/
  689. + *(bb__aliased_uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
  690. +
  691. + //TODO? pkt->icmp_cksum = inet_cksum(...);
  692. +
  693. + sendping_tail(sendping6, sizeof(struct icmp6_hdr));
  694. +}
  695. +#endif
  696. +
  697. +static const char *icmp_type_name(int id)
  698. +{
  699. + switch (id) {
  700. + case ICMP_ECHOREPLY: return "Echo Reply";
  701. + case ICMP_DEST_UNREACH: return "Destination Unreachable";
  702. + case ICMP_SOURCE_QUENCH: return "Source Quench";
  703. + case ICMP_REDIRECT: return "Redirect (change route)";
  704. + case ICMP_ECHO: return "Echo Request";
  705. + case ICMP_TIME_EXCEEDED: return "Time Exceeded";
  706. + case ICMP_PARAMETERPROB: return "Parameter Problem";
  707. + case ICMP_TIMESTAMP: return "Timestamp Request";
  708. + case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
  709. + case ICMP_INFO_REQUEST: return "Information Request";
  710. + case ICMP_INFO_REPLY: return "Information Reply";
  711. + case ICMP_ADDRESS: return "Address Mask Request";
  712. + case ICMP_ADDRESSREPLY: return "Address Mask Reply";
  713. + default: return "unknown ICMP type";
  714. + }
  715. +}
  716. +#if ENABLE_PING6
  717. +/* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
  718. + * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
  719. +#ifndef MLD_LISTENER_QUERY
  720. +# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
  721. +#endif
  722. +#ifndef MLD_LISTENER_REPORT
  723. +# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
  724. +#endif
  725. +#ifndef MLD_LISTENER_REDUCTION
  726. +# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
  727. +#endif
  728. +static const char *icmp6_type_name(int id)
  729. +{
  730. + switch (id) {
  731. + case ICMP6_DST_UNREACH: return "Destination Unreachable";
  732. + case ICMP6_PACKET_TOO_BIG: return "Packet too big";
  733. + case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
  734. + case ICMP6_PARAM_PROB: return "Parameter Problem";
  735. + case ICMP6_ECHO_REPLY: return "Echo Reply";
  736. + case ICMP6_ECHO_REQUEST: return "Echo Request";
  737. + case MLD_LISTENER_QUERY: return "Listener Query";
  738. + case MLD_LISTENER_REPORT: return "Listener Report";
  739. + case MLD_LISTENER_REDUCTION: return "Listener Reduction";
  740. + default: return "unknown ICMP type";
  741. + }
  742. +}
  743. +#endif
  744. +
  745. +static void unpack_tail(int sz, uint32_t *tp,
  746. + const char *from_str,
  747. + uint16_t recv_seq, int ttl)
  748. +{
  749. + unsigned char *b, m;
  750. + const char *dupmsg = " (DUP!)";
  751. + unsigned triptime = triptime; /* for gcc */
  752. +
  753. + if (tp) {
  754. + /* (int32_t) cast is for hypothetical 64-bit unsigned */
  755. + /* (doesn't hurt 32-bit real-world anyway) */
  756. + triptime = (int32_t) ((uint32_t)monotonic_us() - *tp);
  757. + tsum += triptime;
  758. + if (triptime < tmin)
  759. + tmin = triptime;
  760. + if (triptime > tmax)
  761. + tmax = triptime;
  762. + }
  763. +
  764. + b = &BYTE(recv_seq % MAX_DUP_CHK);
  765. + m = MASK(recv_seq % MAX_DUP_CHK);
  766. + /*if TST(recv_seq % MAX_DUP_CHK):*/
  767. + if (*b & m) {
  768. + ++G.nrepeats;
  769. + } else {
  770. + /*SET(recv_seq % MAX_DUP_CHK):*/
  771. + *b |= m;
  772. + ++G.nreceived;
  773. + dupmsg += 7;
  774. + }
  775. +
  776. + if (option_mask32 & OPT_QUIET)
  777. + return;
  778. +
  779. + printf("%d bytes from %s: seq=%u ttl=%d", sz,
  780. + from_str, recv_seq, ttl);
  781. + if (tp)
  782. + printf(" time=%u.%03u ms", triptime / 1000, triptime % 1000);
  783. + puts(dupmsg);
  784. + fflush_all();
  785. +}
  786. +static void unpack4(char *buf, int sz, struct sockaddr_in *from)
  787. +{
  788. + struct icmp *icmppkt;
  789. + struct iphdr *iphdr;
  790. + int hlen;
  791. +
  792. + /* discard if too short */
  793. + if (sz < (datalen + ICMP_MINLEN))
  794. + return;
  795. +
  796. + /* check IP header */
  797. + iphdr = (struct iphdr *) buf;
  798. + hlen = iphdr->ihl << 2;
  799. + sz -= hlen;
  800. + icmppkt = (struct icmp *) (buf + hlen);
  801. + if (icmppkt->icmp_id != myid)
  802. + return; /* not our ping */
  803. +
  804. + if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
  805. + uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
  806. + uint32_t *tp = NULL;
  807. +
  808. + if (sz >= ICMP_MINLEN + sizeof(uint32_t))
  809. + tp = (uint32_t *) icmppkt->icmp_data;
  810. + unpack_tail(sz, tp,
  811. + inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
  812. + recv_seq, iphdr->ttl);
  813. + } else if (icmppkt->icmp_type != ICMP_ECHO) {
  814. + bb_error_msg("warning: got ICMP %d (%s)",
  815. + icmppkt->icmp_type,
  816. + icmp_type_name(icmppkt->icmp_type));
  817. + }
  818. +}
  819. +#if ENABLE_PING6
  820. +static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
  821. +{
  822. + struct icmp6_hdr *icmppkt;
  823. + char buf[INET6_ADDRSTRLEN];
  824. +
  825. + /* discard if too short */
  826. + if (sz < (datalen + sizeof(struct icmp6_hdr)))
  827. + return;
  828. +
  829. + icmppkt = (struct icmp6_hdr *) packet;
  830. + if (icmppkt->icmp6_id != myid)
  831. + return; /* not our ping */
  832. +
  833. + if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
  834. + uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
  835. + uint32_t *tp = NULL;
  836. +
  837. + if (sz >= sizeof(struct icmp6_hdr) + sizeof(uint32_t))
  838. + tp = (uint32_t *) &icmppkt->icmp6_data8[4];
  839. + unpack_tail(sz, tp,
  840. + inet_ntop(AF_INET6, &from->sin6_addr,
  841. + buf, sizeof(buf)),
  842. + recv_seq, hoplimit);
  843. + } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) {
  844. + bb_error_msg("warning: got ICMP %d (%s)",
  845. + icmppkt->icmp6_type,
  846. + icmp6_type_name(icmppkt->icmp6_type));
  847. + }
  848. +}
  849. +#endif
  850. +
  851. +static void ping4(len_and_sockaddr *lsa)
  852. +{
  853. + int sockopt;
  854. +
  855. + pingaddr.sin = lsa->u.sin;
  856. + if (source_lsa) {
  857. + if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF,
  858. + &source_lsa->u.sa, source_lsa->len))
  859. + bb_error_msg_and_die("can't set multicast source interface");
  860. + xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
  861. + }
  862. +
  863. + /* enable broadcast pings */
  864. + setsockopt_broadcast(pingsock);
  865. +
  866. + /* set recv buf (needed if we can get lots of responses: flood ping,
  867. + * broadcast ping etc) */
  868. + sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
  869. + setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
  870. +
  871. + if (opt_ttl != 0) {
  872. + setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl));
  873. + /* above doesnt affect packets sent to bcast IP, so... */
  874. + setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl));
  875. + }
  876. +
  877. + signal(SIGINT, print_stats_and_exit);
  878. +
  879. + /* start the ping's going ... */
  880. + sendping4(0);
  881. +
  882. + /* listen for replies */
  883. + while (1) {
  884. + struct sockaddr_in from;
  885. + socklen_t fromlen = (socklen_t) sizeof(from);
  886. + int c;
  887. +
  888. + c = recvfrom(pingsock, G.rcv_packet, G.sizeof_rcv_packet, 0,
  889. + (struct sockaddr *) &from, &fromlen);
  890. + if (c < 0) {
  891. + if (errno != EINTR)
  892. + bb_perror_msg("recvfrom");
  893. + continue;
  894. + }
  895. + unpack4(G.rcv_packet, c, &from);
  896. + if (pingcount && G.nreceived >= pingcount)
  897. + break;
  898. + }
  899. +}
  900. +#if ENABLE_PING6
  901. +extern int BUG_bad_offsetof_icmp6_cksum(void);
  902. +static void ping6(len_and_sockaddr *lsa)
  903. +{
  904. + int sockopt;
  905. + struct msghdr msg;
  906. + struct sockaddr_in6 from;
  907. + struct iovec iov;
  908. + char control_buf[CMSG_SPACE(36)];
  909. +
  910. + pingaddr.sin6 = lsa->u.sin6;
  911. + if (source_lsa)
  912. + xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
  913. +
  914. +#ifdef ICMP6_FILTER
  915. + {
  916. + struct icmp6_filter filt;
  917. + if (!(option_mask32 & OPT_VERBOSE)) {
  918. + ICMP6_FILTER_SETBLOCKALL(&filt);
  919. + ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
  920. + } else {
  921. + ICMP6_FILTER_SETPASSALL(&filt);
  922. + }
  923. + if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
  924. + sizeof(filt)) < 0)
  925. + bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
  926. + }
  927. +#endif /*ICMP6_FILTER*/
  928. +
  929. + /* enable broadcast pings */
  930. + setsockopt_broadcast(pingsock);
  931. +
  932. + /* set recv buf (needed if we can get lots of responses: flood ping,
  933. + * broadcast ping etc) */
  934. + sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
  935. + setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
  936. +
  937. + sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
  938. + if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
  939. + BUG_bad_offsetof_icmp6_cksum();
  940. + setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
  941. +
  942. + /* request ttl info to be returned in ancillary data */
  943. + setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
  944. +
  945. + if (if_index)
  946. + pingaddr.sin6.sin6_scope_id = if_index;
  947. +
  948. + signal(SIGINT, print_stats_and_exit);
  949. +
  950. + /* start the ping's going ... */
  951. + sendping6(0);
  952. +
  953. + /* listen for replies */
  954. + msg.msg_name = &from;
  955. + msg.msg_namelen = sizeof(from);
  956. + msg.msg_iov = &iov;
  957. + msg.msg_iovlen = 1;
  958. + msg.msg_control = control_buf;
  959. + iov.iov_base = G.rcv_packet;
  960. + iov.iov_len = G.sizeof_rcv_packet;
  961. + while (1) {
  962. + int c;
  963. + struct cmsghdr *mp;
  964. + int hoplimit = -1;
  965. + msg.msg_controllen = sizeof(control_buf);
  966. +
  967. + c = recvmsg(pingsock, &msg, 0);
  968. + if (c < 0) {
  969. + if (errno != EINTR)
  970. + bb_perror_msg("recvfrom");
  971. + continue;
  972. + }
  973. + for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
  974. + if (mp->cmsg_level == SOL_IPV6
  975. + && mp->cmsg_type == IPV6_HOPLIMIT
  976. + /* don't check len - we trust the kernel: */
  977. + /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
  978. + ) {
  979. + /*hoplimit = *(int*)CMSG_DATA(mp); - unaligned access */
  980. + move_from_unaligned_int(hoplimit, CMSG_DATA(mp));
  981. + }
  982. + }
  983. + unpack6(G.rcv_packet, c, &from, hoplimit);
  984. + if (pingcount && G.nreceived >= pingcount)
  985. + break;
  986. + }
  987. +}
  988. +#endif
  989. +
  990. +static void ping(len_and_sockaddr *lsa)
  991. +{
  992. + printf("PING %s (%s)", hostname, dotted);
  993. + if (source_lsa) {
  994. + printf(" from %s",
  995. + xmalloc_sockaddr2dotted_noport(&source_lsa->u.sa));
  996. + }
  997. + printf(": %d data bytes\n", datalen);
  998. +
  999. + create_icmp_socket(lsa);
  1000. + /* untested whether "-I addr" really works for IPv6: */
  1001. + if (str_I)
  1002. + setsockopt_bindtodevice(pingsock, str_I);
  1003. +
  1004. + G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN;
  1005. + G.rcv_packet = xzalloc(G.sizeof_rcv_packet);
  1006. +#if ENABLE_PING6
  1007. + if (lsa->u.sa.sa_family == AF_INET6) {
  1008. + /* +4 reserves a place for timestamp, which may end up sitting
  1009. + * _after_ packet. Saves one if() - see sendping4/6() */
  1010. + G.snd_packet = xzalloc(datalen + sizeof(struct icmp6_hdr) + 4);
  1011. + ping6(lsa);
  1012. + } else
  1013. +#endif
  1014. + {
  1015. + G.snd_packet = xzalloc(datalen + ICMP_MINLEN + 4);
  1016. + ping4(lsa);
  1017. + }
  1018. +}
  1019. +
  1020. +static int common_ping_main(int opt, char **argv)
  1021. +{
  1022. + len_and_sockaddr *lsa;
  1023. + char *str_s;
  1024. +
  1025. + INIT_G();
  1026. +
  1027. + /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */
  1028. + opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+";
  1029. + opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I);
  1030. + if (opt & OPT_s)
  1031. + datalen = xatou16(str_s); // -s
  1032. + if (opt & OPT_I) { // -I
  1033. + if_index = if_nametoindex(str_I);
  1034. + if (!if_index) {
  1035. + /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */
  1036. + source_lsa = xdotted2sockaddr(str_I, 0);
  1037. + str_I = NULL; /* don't try to bind to device later */
  1038. + }
  1039. + }
  1040. + myid = (uint16_t) getpid();
  1041. + hostname = argv[optind];
  1042. +#if ENABLE_PING6
  1043. + {
  1044. + sa_family_t af = AF_UNSPEC;
  1045. + if (opt & OPT_IPV4)
  1046. + af = AF_INET;
  1047. + if (opt & OPT_IPV6)
  1048. + af = AF_INET6;
  1049. + lsa = xhost_and_af2sockaddr(hostname, 0, af);
  1050. + }
  1051. +#else
  1052. + lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
  1053. +#endif
  1054. +
  1055. + if (source_lsa && source_lsa->u.sa.sa_family != lsa->u.sa.sa_family)
  1056. + /* leaking it here... */
  1057. + source_lsa = NULL;
  1058. +
  1059. + dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
  1060. + ping(lsa);
  1061. + print_stats_and_exit(EXIT_SUCCESS);
  1062. + /*return EXIT_SUCCESS;*/
  1063. +}
  1064. +#endif /* FEATURE_FANCY_PING */
  1065. +
  1066. +
  1067. +int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1068. +int ping_main(int argc UNUSED_PARAM, char **argv)
  1069. +{
  1070. +#if !ENABLE_FEATURE_FANCY_PING
  1071. + return common_ping_main(AF_UNSPEC, argv);
  1072. +#else
  1073. + return common_ping_main(0, argv);
  1074. +#endif
  1075. +}
  1076. +
  1077. +#if ENABLE_PING6
  1078. +int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1079. +int ping6_main(int argc UNUSED_PARAM, char **argv)
  1080. +{
  1081. +# if !ENABLE_FEATURE_FANCY_PING
  1082. + return common_ping_main(AF_INET6, argv);
  1083. +# else
  1084. + return common_ping_main(OPT_IPV6, argv);
  1085. +# endif
  1086. +}
  1087. +#endif
  1088. +
  1089. +/* from ping6.c:
  1090. + * Copyright (c) 1989 The Regents of the University of California.
  1091. + * All rights reserved.
  1092. + *
  1093. + * This code is derived from software contributed to Berkeley by
  1094. + * Mike Muuss.
  1095. + *
  1096. + * Redistribution and use in source and binary forms, with or without
  1097. + * modification, are permitted provided that the following conditions
  1098. + * are met:
  1099. + * 1. Redistributions of source code must retain the above copyright
  1100. + * notice, this list of conditions and the following disclaimer.
  1101. + * 2. Redistributions in binary form must reproduce the above copyright
  1102. + * notice, this list of conditions and the following disclaimer in the
  1103. + * documentation and/or other materials provided with the distribution.
  1104. + *
  1105. + * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  1106. + * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  1107. + *
  1108. + * 4. Neither the name of the University nor the names of its contributors
  1109. + * may be used to endorse or promote products derived from this software
  1110. + * without specific prior written permission.
  1111. + *
  1112. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1113. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1114. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1115. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1116. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1117. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1118. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1119. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1120. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1121. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1122. + * SUCH DAMAGE.
  1123. + */