X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fping6.c;h=767aafd2a91b76eb1b791d0e7def276e6005cc8d;hb=59fe8b90890a07c87ec9c2943bae515d5c6d959d;hp=8e14647ebc1d37dc970893030d96801744e4734d;hpb=efc6fbd452211fb1695f7f0abbf8c7477c54f7fa;p=oweals%2Fbusybox.git diff --git a/networking/ping6.c b/networking/ping6.c index 8e14647eb..767aafd2a 100644 --- a/networking/ping6.c +++ b/networking/ping6.c @@ -1,23 +1,11 @@ /* 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 * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * * This version of ping is adapted from the ping in netkit-base 0.10, * which is: @@ -27,7 +15,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 +25,8 @@ #include #include #include -#include #include -#include +#include #include #include @@ -56,13 +43,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 +66,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 +98,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 +115,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 +128,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 +151,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 +191,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 +208,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 +223,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 +243,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"; } } @@ -299,17 +299,17 @@ static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit return; printf("%d bytes from %s: icmp6_seq=%u", sz, - inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr, + inet_ntop(AF_INET6, &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 +330,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)); @@ -339,19 +339,13 @@ static void ping(const char *host) struct icmp6_filter filt; if (!(options & O_VERBOSE)) { ICMP6_FILTER_SETBLOCKALL(&filt); -#if 0 - if ((options & F_FQDN) || (options & F_FQDNOLD) || - (options & F_NODEADDR) || (options & F_SUPTYPES)) - ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt); - else -#endif ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt); } else { ICMP6_FILTER_SETPASSALL(&filt); } 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,12 +369,12 @@ 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", hostent->h_name, - inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr, + inet_ntop(AF_INET6, &pingaddr.sin6_addr, buf, sizeof(buf)), datalen); @@ -406,7 +400,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 +417,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 +441,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 +488,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