X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fping6.c;h=1cff59d4b1ca0869e0c2baa0070580926bfc5c09;hb=5929edc1fac4340f99ed84e92bf3a2bedd4177c2;hp=8e14647ebc1d37dc970893030d96801744e4734d;hpb=efc6fbd452211fb1695f7f0abbf8c7477c54f7fa;p=oweals%2Fbusybox.git diff --git a/networking/ping6.c b/networking/ping6.c index 8e14647eb..1cff59d4b 100644 --- a/networking/ping6.c +++ b/networking/ping6.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * $Id: ping6.c,v 1.2 2002/11/26 03:03:41 bug1 Exp $ + * $Id: ping6.c,v 1.6 2004/03/15 08:28:48 andersen Exp $ * Mini ping implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -27,7 +27,7 @@ * * This code is derived from software contributed to Berkeley by * Mike Muuss. - * + * * Original copyright notice is retained at the end of this file. * * This version is an adaptation of ping.c from busybox. @@ -37,9 +37,8 @@ #include #include #include -#include #include -#include +#include #include #include @@ -56,13 +55,15 @@ #include /* offsetof */ #include "busybox.h" -static const int DEFDATALEN = 56; -static const int MAXIPLEN = 60; -static const int MAXICMPLEN = 76; -static const int MAXPACKET = 65468; -#define MAX_DUP_CHK (8 * 128) -static const int MAXWAIT = 10; -static const int PINGINTERVAL = 1; /* second */ +enum { + DEFDATALEN = 56, + MAXIPLEN = 60, + MAXICMPLEN = 76, + MAXPACKET = 65468, + MAX_DUP_CHK = (8 * 128), + MAXWAIT = 10, + PINGINTERVAL = 1 /* second */ +}; #define O_QUIET (1 << 0) #define O_VERBOSE (1 << 1) @@ -77,22 +78,22 @@ static void ping(const char *host); /* simple version */ #ifndef CONFIG_FEATURE_FANCY_PING6 +static struct hostent *h; + +void noresp(int ign) +{ + printf("No response from %s\n", h->h_name); + exit(EXIT_FAILURE); +} static void ping(const char *host) { - struct hostent *h; struct sockaddr_in6 pingaddr; struct icmp6_hdr *pkt; int pingsock, c; int sockopt; char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; - void noresp(int ign) - { - printf("No response from %s\n", h->h_name); - exit(0); - } - pingsock = create_icmp6_socket(); memset(&pingaddr, 0, sizeof(struct sockaddr_in)); @@ -109,11 +110,11 @@ static void ping(const char *host) setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt, sizeof(sockopt)); - c = sendto(pingsock, packet, sizeof(packet), 0, + c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0, (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6)); if (c < 0 || c != sizeof(packet)) - perror_msg_and_die("sendto"); + bb_perror_msg_and_die("sendto"); signal(SIGALRM, noresp); alarm(5); /* give the host 5000ms to respond */ @@ -126,7 +127,7 @@ static void ping(const char *host) (struct sockaddr *) &from, &fromlen)) < 0) { if (errno == EINTR) continue; - perror_msg("recvfrom"); + bb_perror_msg("recvfrom"); continue; } if (c >= 8) { /* icmp6_hdr */ @@ -139,12 +140,12 @@ static void ping(const char *host) return; } -extern int ping6_main(int argc, char **argv) +int ping6_main(int argc, char **argv) { argc--; argv++; if (argc < 1) - show_usage(); + bb_show_usage(); ping(*argv); return EXIT_SUCCESS; } @@ -162,7 +163,7 @@ static unsigned long tmin = ULONG_MAX, tmax, tsum; static char rcvd_tbl[MAX_DUP_CHK / 8]; # ifdef CONFIG_FEATURE_FANCY_PING -extern +extern # endif struct hostent *hostent; @@ -202,7 +203,7 @@ static void sendping(int junk) { struct icmp6_hdr *pkt; int i; - char packet[datalen + 8]; + char packet[datalen + sizeof (struct icmp6_hdr)]; pkt = (struct icmp6_hdr *) packet; @@ -219,9 +220,9 @@ static void sendping(int junk) (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6)); if (i < 0) - perror_msg_and_die("sendto"); + bb_perror_msg_and_die("sendto"); else if ((size_t)i != sizeof(packet)) - error_msg_and_die("ping wrote %d chars; %d expected", i, + bb_error_msg_and_die("ping wrote %d chars; %d expected", i, (int)sizeof(packet)); signal(SIGALRM, sendping); @@ -234,6 +235,17 @@ static void sendping(int junk) } } +/* RFC3542 changed some definitions from RFC2292 for no good reason, whee ! + * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */ +#ifndef MLD_LISTENER_QUERY +# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY +#endif +#ifndef MLD_LISTENER_REPORT +# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT +#endif +#ifndef MLD_LISTENER_REDUCTION +# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION +#endif static char *icmp6_type_name (int id) { switch (id) { @@ -243,10 +255,10 @@ static char *icmp6_type_name (int id) case ICMP6_PARAM_PROB: return "Parameter Problem"; case ICMP6_ECHO_REPLY: return "Echo Reply"; case ICMP6_ECHO_REQUEST: return "Echo Request"; - case ICMP6_MEMBERSHIP_QUERY: return "Membership Query"; - case ICMP6_MEMBERSHIP_REPORT: return "Membership Report"; - case ICMP6_MEMBERSHIP_REDUCTION: return "Membership Reduction"; - default: return "unknown ICMP type"; + case MLD_LISTENER_QUERY: return "Listener Query"; + case MLD_LISTENER_REPORT: return "Listener Report"; + case MLD_LISTENER_REDUCTION: return "Listener Reduction"; + default: return "unknown ICMP type"; } } @@ -302,14 +314,14 @@ static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr, buf, sizeof(buf)), icmppkt->icmp6_seq); - printf(" ttl=%d time=%lu.%lu ms", hoplimit, + printf(" ttl=%d time=%lu.%lu ms", hoplimit, triptime / 10, triptime % 10); if (dupflag) printf(" (DUP!)"); printf("\n"); - } else + } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) - error_msg("Warning: Got ICMP %d (%s)", + bb_error_msg("Warning: Got ICMP %d (%s)", icmppkt->icmp6_type, icmp6_type_name (icmppkt->icmp6_type)); } @@ -330,7 +342,7 @@ static void ping(const char *host) pingaddr.sin6_family = AF_INET6; hostent = xgethostbyname2(host, AF_INET6); if (hostent->h_addrtype != AF_INET6) - error_msg_and_die("unknown address type; only AF_INET6 is currently supported."); + bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported."); memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr)); @@ -351,7 +363,7 @@ static void ping(const char *host) } if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt)) < 0) - error_msg_and_die("setsockopt(ICMP6_FILTER)"); + bb_error_msg_and_die("setsockopt(ICMP6_FILTER)"); } #endif /*ICMP6_FILTER*/ @@ -375,7 +387,7 @@ static void ping(const char *host) if (ifname) { if ((pingaddr.sin6_scope_id = if_nametoindex(ifname)) == 0) - error_msg_and_die("%s: invalid interface name", ifname); + bb_error_msg_and_die("%s: invalid interface name", ifname); } printf("PING %s (%s): %d data bytes\n", @@ -406,7 +418,7 @@ static void ping(const char *host) if ((c = recvmsg(pingsock, &msg, 0)) < 0) { if (errno == EINTR) continue; - perror_msg("recvfrom"); + bb_perror_msg("recvfrom"); continue; } for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; @@ -423,7 +435,7 @@ static void ping(const char *host) pingstats(0); } -extern int ping6_main(int argc, char **argv) +int ping6_main(int argc, char **argv) { char *thisarg; @@ -447,30 +459,30 @@ extern int ping6_main(int argc, char **argv) break; case 'c': if (--argc <= 0) - show_usage(); + bb_show_usage(); argv++; pingcount = atoi(*argv); break; case 's': if (--argc <= 0) - show_usage(); + bb_show_usage(); argv++; datalen = atoi(*argv); break; case 'I': if (--argc <= 0) - show_usage(); + bb_show_usage(); argv++; ifname = *argv; break; default: - show_usage(); + bb_show_usage(); } argc--; argv++; } if (argc < 1) - show_usage(); + bb_show_usage(); myid = getpid() & 0xFFFF; ping(*argv); @@ -494,8 +506,8 @@ extern int ping6_main(int argc, char **argv) * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - * 3. + * 3. * * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software