This commit is contained in:
Eric Andersen 1999-12-07 23:14:59 +00:00
parent d00c262847
commit 485b9550fd
14 changed files with 1029 additions and 34 deletions

View File

@ -1,3 +1,12 @@
0.38
* New Apps: ping and hostname contributed by Randolph Chung
<tausq@debian.org>
* on reboot, init called 'umount -a -n', which caused errors
when BB_MTAB was not enabled. Changed to 'umount -a',
which does the right thing.
-Erik Andrsen
0.38 0.38
* Fixed a segfault in 'umount -a' when a badly formed /etc/fstab * Fixed a segfault in 'umount -a' when a badly formed /etc/fstab
file existed. file existed.

4
TODO
View File

@ -14,8 +14,8 @@ around to it some time. If you have any good ideas, please let me know.
* poweroff * poweroff
* du * du
* mkfifo * mkfifo
* hostname/dnsdomainname * dnsdomainname
* ping/traceroute/nslookup/netstat * traceroute/nslookup/netstat
* rdate * rdate
* hwclock * hwclock
* login/getty * login/getty

View File

@ -69,6 +69,9 @@ static const struct Applet applets[] = {
#ifdef BB_HALT //sbin #ifdef BB_HALT //sbin
{"halt", halt_main}, {"halt", halt_main},
#endif #endif
#ifdef BB_HOSTNAME //bin
{"hostname", hostname_main},
#endif
#ifdef BB_INIT //sbin #ifdef BB_INIT //sbin
{"init", init_main}, {"init", init_main},
#endif #endif
@ -123,6 +126,9 @@ static const struct Applet applets[] = {
#ifdef BB_MV //bin #ifdef BB_MV //bin
{"mv", mv_main}, {"mv", mv_main},
#endif #endif
#ifdef BB_PING //bin
{"ping", ping_main},
#endif
#ifdef BB_PRINTF //usr/bin #ifdef BB_PRINTF //usr/bin
{"printf", printf_main}, {"printf", printf_main},
#endif #endif

View File

@ -69,6 +69,9 @@ static const struct Applet applets[] = {
#ifdef BB_HALT //sbin #ifdef BB_HALT //sbin
{"halt", halt_main}, {"halt", halt_main},
#endif #endif
#ifdef BB_HOSTNAME //bin
{"hostname", hostname_main},
#endif
#ifdef BB_INIT //sbin #ifdef BB_INIT //sbin
{"init", init_main}, {"init", init_main},
#endif #endif
@ -123,6 +126,9 @@ static const struct Applet applets[] = {
#ifdef BB_MV //bin #ifdef BB_MV //bin
{"mv", mv_main}, {"mv", mv_main},
#endif #endif
#ifdef BB_PING //bin
{"ping", ping_main},
#endif
#ifdef BB_PRINTF //usr/bin #ifdef BB_PRINTF //usr/bin
{"printf", printf_main}, {"printf", printf_main},
#endif #endif

View File

@ -22,6 +22,7 @@
#define BB_DEALLOCVT #define BB_DEALLOCVT
#define BB_GREP #define BB_GREP
//#define BB_HALT //#define BB_HALT
#define BB_HOSTNAME
#define BB_INIT #define BB_INIT
#define BB_KILL #define BB_KILL
//#define BB_LENGTH //#define BB_LENGTH
@ -42,6 +43,7 @@
//#define BB_MT //#define BB_MT
//#define BB_MTAB //#define BB_MTAB
#define BB_MV #define BB_MV
#define BB_PING
//#define BB_PRINTF //#define BB_PRINTF
#define BB_PS #define BB_PS
#define BB_PWD #define BB_PWD

126
hostname.c Normal file
View File

@ -0,0 +1,126 @@
/*
* $Id: hostname.c,v 1.1 1999/12/07 23:14:59 andersen Exp $
* Mini hostname implementation for busybox
*
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
*
* 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
*/
#include "internal.h"
#include <errno.h>
#include <getopt.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
static const char* hostname_usage = "hostname [OPTION] {hostname | -F file}\n\n"
"Options:\n"
"\t-s, --short\t\tshort\n"
"\t-i, --ip-address\t\taddresses for the hostname\n"
"\t-d, --domain\t\tDNS domain name\n"
"If a hostname is given, or a file is given with the -F parameter, the host\n"
"name will be set\n";
static char short_opts[] = "sidF:";
static const struct option long_opts[] = {
{ "short", no_argument, NULL, 's' },
{ "ip-address", no_argument, NULL, 'i' },
{ "domain", no_argument, NULL, 'd' },
{ NULL, 0, NULL, 0 }
};
void do_sethostname(char *s, int isfile)
{
FILE *f;
char buf[255];
if (!s) return;
if (!isfile) {
if (sethostname(s, strlen(s)) < 0) {
if (errno == EPERM)
fprintf(stderr, "hostname: you must be root to change the hostname\n");
else
perror("sethostname");
exit(1);
}
} else {
if ((f = fopen(s, "r")) == NULL) {
perror(s);
exit(1);
} else {
fgets(buf, 255, f);
fclose(f);
if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
if (sethostname(buf, strlen(buf)) < 0) {
perror("sethostname");
exit(1);
}
}
}
}
int hostname_main(int argc, char **argv)
{
int c;
int opt_short = 0;
int opt_domain = 0;
int opt_ip = 0;
int opt_file = 0;
struct hostent *h;
char *filename = NULL;
char buf[255];
char *s = NULL;
if (argc < 1) usage(hostname_usage);
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
switch (c) {
case 's': opt_short = 1; break;
case 'i': opt_ip = 1; break;
case 'd': opt_domain = 1; break;
case 'F': opt_file = 1; filename = optarg; break;
default: usage(hostname_usage);
}
}
if (optind < argc) {
do_sethostname(argv[optind], 0);
} else if (opt_file) {
do_sethostname(filename, 1);
} else {
gethostname(buf, 255);
if (opt_short) {
s = strchr(buf, '.');
if (!s) s = buf; *s = 0;
printf("%s\n", buf);
} else if (opt_domain) {
s = strchr(buf, '.');
printf("%s\n", (s ? s+1 : ""));
} else if (opt_ip) {
h = gethostbyname(buf);
if (!h) {
printf("Host not found\n");
exit(1);
}
printf("%s\n", inet_ntoa(*(struct in_addr *)(h->h_addr)));
} else {
printf("%s\n", buf);
}
}
return 0;
}

10
init.c
View File

@ -151,19 +151,11 @@ void message(int device, char *fmt, ...)
void set_term( int fd) void set_term( int fd)
{ {
struct termios tty; struct termios tty;
#if 0
static const char control_characters[] = {
'\003', '\034', '\177', '\030', '\004', '\0',
'\1', '\0', '\021', '\023', '\032', '\0', '\022',
'\017', '\027', '\026', '\0'
};
#else
static const char control_characters[] = { static const char control_characters[] = {
'\003', '\034', '\177', '\025', '\004', '\0', '\003', '\034', '\177', '\025', '\004', '\0',
'\1', '\0', '\021', '\023', '\032', '\0', '\022', '\1', '\0', '\021', '\023', '\032', '\0', '\022',
'\017', '\027', '\026', '\0' '\017', '\027', '\026', '\0'
}; };
#endif
tcgetattr(fd, &tty); tcgetattr(fd, &tty);
@ -386,7 +378,7 @@ goodnight:
static void shutdown_system(void) static void shutdown_system(void)
{ {
const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0}; const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0}; const char* const umount_cmd[] = { "umount", "umount", "-a", 0};
#ifndef DEBUG_INIT #ifndef DEBUG_INIT
/* Allow Ctrl-Alt-Del to reboot system. */ /* Allow Ctrl-Alt-Del to reboot system. */

View File

@ -151,19 +151,11 @@ void message(int device, char *fmt, ...)
void set_term( int fd) void set_term( int fd)
{ {
struct termios tty; struct termios tty;
#if 0
static const char control_characters[] = {
'\003', '\034', '\177', '\030', '\004', '\0',
'\1', '\0', '\021', '\023', '\032', '\0', '\022',
'\017', '\027', '\026', '\0'
};
#else
static const char control_characters[] = { static const char control_characters[] = {
'\003', '\034', '\177', '\025', '\004', '\0', '\003', '\034', '\177', '\025', '\004', '\0',
'\1', '\0', '\021', '\023', '\032', '\0', '\022', '\1', '\0', '\021', '\023', '\032', '\0', '\022',
'\017', '\027', '\026', '\0' '\017', '\027', '\026', '\0'
}; };
#endif
tcgetattr(fd, &tty); tcgetattr(fd, &tty);
@ -386,7 +378,7 @@ goodnight:
static void shutdown_system(void) static void shutdown_system(void)
{ {
const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0}; const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0}; const char* const umount_cmd[] = { "umount", "umount", "-a", 0};
#ifndef DEBUG_INIT #ifndef DEBUG_INIT
/* Allow Ctrl-Alt-Del to reboot system. */ /* Allow Ctrl-Alt-Del to reboot system. */

View File

@ -72,6 +72,7 @@ extern int mkfs_minix_main(int argc, char **argv);
extern int find_main(int argc, char** argv); extern int find_main(int argc, char** argv);
extern int grep_main(int argc, char** argv); extern int grep_main(int argc, char** argv);
extern int halt_main(int argc, char** argv); extern int halt_main(int argc, char** argv);
extern int hostname_main(int argc, char** argv);
extern int init_main(int argc, char** argv); extern int init_main(int argc, char** argv);
extern int kill_main(int argc, char** argv); extern int kill_main(int argc, char** argv);
extern int length_main(int argc, char** argv); extern int length_main(int argc, char** argv);
@ -91,6 +92,7 @@ extern int mnc_main(int argc, char** argv);
extern int mount_main(int argc, char** argv); extern int mount_main(int argc, char** argv);
extern int mt_main(int argc, char** argv); extern int mt_main(int argc, char** argv);
extern int mv_main(int argc, char** argv); extern int mv_main(int argc, char** argv);
extern int ping_main(int argc, char **argv);
extern int printf_main(int argc, char** argv); extern int printf_main(int argc, char** argv);
extern int ps_main(int argc, char** argv); extern int ps_main(int argc, char** argv);
extern int pwd_main(int argc, char** argv); extern int pwd_main(int argc, char** argv);

126
networking/hostname.c Normal file
View File

@ -0,0 +1,126 @@
/*
* $Id: hostname.c,v 1.1 1999/12/07 23:14:59 andersen Exp $
* Mini hostname implementation for busybox
*
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
*
* 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
*/
#include "internal.h"
#include <errno.h>
#include <getopt.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
static const char* hostname_usage = "hostname [OPTION] {hostname | -F file}\n\n"
"Options:\n"
"\t-s, --short\t\tshort\n"
"\t-i, --ip-address\t\taddresses for the hostname\n"
"\t-d, --domain\t\tDNS domain name\n"
"If a hostname is given, or a file is given with the -F parameter, the host\n"
"name will be set\n";
static char short_opts[] = "sidF:";
static const struct option long_opts[] = {
{ "short", no_argument, NULL, 's' },
{ "ip-address", no_argument, NULL, 'i' },
{ "domain", no_argument, NULL, 'd' },
{ NULL, 0, NULL, 0 }
};
void do_sethostname(char *s, int isfile)
{
FILE *f;
char buf[255];
if (!s) return;
if (!isfile) {
if (sethostname(s, strlen(s)) < 0) {
if (errno == EPERM)
fprintf(stderr, "hostname: you must be root to change the hostname\n");
else
perror("sethostname");
exit(1);
}
} else {
if ((f = fopen(s, "r")) == NULL) {
perror(s);
exit(1);
} else {
fgets(buf, 255, f);
fclose(f);
if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
if (sethostname(buf, strlen(buf)) < 0) {
perror("sethostname");
exit(1);
}
}
}
}
int hostname_main(int argc, char **argv)
{
int c;
int opt_short = 0;
int opt_domain = 0;
int opt_ip = 0;
int opt_file = 0;
struct hostent *h;
char *filename = NULL;
char buf[255];
char *s = NULL;
if (argc < 1) usage(hostname_usage);
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
switch (c) {
case 's': opt_short = 1; break;
case 'i': opt_ip = 1; break;
case 'd': opt_domain = 1; break;
case 'F': opt_file = 1; filename = optarg; break;
default: usage(hostname_usage);
}
}
if (optind < argc) {
do_sethostname(argv[optind], 0);
} else if (opt_file) {
do_sethostname(filename, 1);
} else {
gethostname(buf, 255);
if (opt_short) {
s = strchr(buf, '.');
if (!s) s = buf; *s = 0;
printf("%s\n", buf);
} else if (opt_domain) {
s = strchr(buf, '.');
printf("%s\n", (s ? s+1 : ""));
} else if (opt_ip) {
h = gethostbyname(buf);
if (!h) {
printf("Host not found\n");
exit(1);
}
printf("%s\n", inet_ntoa(*(struct in_addr *)(h->h_addr)));
} else {
printf("%s\n", buf);
}
}
return 0;
}

374
networking/ping.c Normal file
View File

@ -0,0 +1,374 @@
/*
* $Id: ping.c,v 1.1 1999/12/07 23:14:59 andersen Exp $
* Mini ping implementation for busybox
*
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
*
* 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
*
* This version of ping is adapted from the ping in netkit-base 0.10,
* which is:
*
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Mike Muuss.
*
* Original copyright notice is retained at the end of this file.
*/
#include "internal.h"
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define DEFDATALEN 56
#define MAXIPLEN 60
#define MAXICMPLEN 76
#define MAXPACKET 65468
#define MAX_DUP_CHK (8 * 128)
#define MAXWAIT 10
#define O_QUIET (1 << 0)
#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
#define SET(bit) (A(bit) |= B(bit))
#define CLR(bit) (A(bit) &= (~B(bit)))
#define TST(bit) (A(bit) & B(bit))
static const char* ping_usage = "ping [OPTION]... host\n\n"
"Options:\n"
"\t-q\t\tquiet\n"
"\t-c\t\tping count\n";
static char *hostname = NULL;
static struct sockaddr_in pingaddr;
static int pingsock = -1;
static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0;
static int myid = 0, options = 0;
static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0;
static char rcvd_tbl[MAX_DUP_CHK / 8];
static void pingstats(int);
static void sendping(int);
static void unpack(char *, int, struct sockaddr_in *);
static void ping(char *);
static int in_cksum(unsigned short *, int);
/**************************************************************************/
static int in_cksum(unsigned short *buf, int sz)
{
int nleft = sz;
int sum = 0;
unsigned short *w = buf;
unsigned short ans = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(unsigned char *)(&ans) = *(unsigned char *)w;
sum += ans;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
ans = ~sum;
return(ans);
}
static void pingstats(int ign) {
signal(SIGINT, SIG_IGN);
printf("\n--- %s ping statistics ---\n", hostname);
printf("%ld packets transmitted, ", ntransmitted);
printf("%ld packets received, ", nreceived);
if (nrepeats)
printf("%ld duplicates, ", nrepeats);
if (ntransmitted)
printf("%ld%% packet loss\n",
(ntransmitted - nreceived)*100/ntransmitted);
if (nreceived)
printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
tmin/10, tmin%10,
(tsum/(nreceived+nrepeats))/10,
(tsum/(nreceived+nrepeats))%10,
tmax/10, tmax%10);
exit(0);
}
static void sendping(int ign)
{
struct icmp *pkt;
int i;
char packet[DEFDATALEN + 8];
pkt = (struct icmp *)packet;
pkt->icmp_type = ICMP_ECHO;
pkt->icmp_code = 0;
pkt->icmp_cksum = 0;
pkt->icmp_seq = ntransmitted++;
pkt->icmp_id = myid;
CLR(pkt->icmp_seq % MAX_DUP_CHK);
gettimeofday((struct timeval *)&packet[8], NULL);
pkt->icmp_cksum = in_cksum((unsigned short *)pkt, sizeof(packet));
i = sendto(pingsock, packet, sizeof(packet), 0,
(struct sockaddr *)&pingaddr, sizeof(struct sockaddr_in));
if (i < 0 || i != sizeof(packet)) {
if (i < 0) perror("ping");
fprintf(stderr, "ping wrote %d chars; %d expected\n", i, sizeof(packet));
exit(1);
}
signal(SIGALRM, sendping);
if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next */
alarm(1);
} else { /* done, wait for the last ping to come back */
/* todo, don't necessarily need to wait so long... */
signal(SIGALRM, pingstats);
alarm(MAXWAIT);
}
}
static void unpack(char *buf, int sz, struct sockaddr_in *from)
{
struct icmp *icmppkt;
struct iphdr *iphdr;
struct timeval tv, *tp;
int hlen, dupflag;
unsigned long triptime;
gettimeofday(&tv, NULL);
/* check IP header */
iphdr = (struct iphdr *)buf;
hlen = iphdr->ihl << 2;
/* discard if too short */
if (sz < (DEFDATALEN + ICMP_MINLEN)) return;
sz -= hlen;
icmppkt = (struct icmp *)(buf + hlen);
if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
if (icmppkt->icmp_id != myid) return; /* not our ping */
++nreceived;
tp = (struct timeval *)icmppkt->icmp_data;
if ((tv.tv_usec -= tp->tv_usec) < 0) {
--tv.tv_sec;
tv.tv_usec += 1000000;
}
tv.tv_sec -= tp->tv_sec;
triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
tsum += triptime;
if (triptime < tmin) tmin = triptime;
if (triptime > tmax) tmax = triptime;
if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) {
++nrepeats;
--nreceived;
dupflag = 1;
} else {
SET(icmppkt->icmp_seq % MAX_DUP_CHK);
dupflag = 0;
}
if (options & O_QUIET) return;
printf("%d bytes from %s: icmp_seq=%u", sz,
inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
icmppkt->icmp_seq);
printf(" ttl=%d", iphdr->ttl);
printf(" time=%lu.%lu ms", triptime/10, triptime%10);
if (dupflag) printf(" (DUP!)");
printf("\n");
} else {
fprintf(stderr, "Warning: unknown ICMP packet received (not echo-reply)\n");
}
}
static void ping(char *host)
{
struct protoent *proto;
struct hostent *h;
char buf[MAXHOSTNAMELEN];
char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
int sockopt;
if (!(proto = getprotobyname("icmp"))) {
fprintf(stderr, "ping: unknown protocol icmp\n");
exit(1);
}
if ((pingsock = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
if (errno == EPERM) {
fprintf(stderr, "ping: permission denied. (are you root?)\n");
} else {
perror("ping");
}
exit(1);
}
#ifdef SUID_BUSYBOX
setuid(getuid());
#endif
memset(&pingaddr, 0, sizeof(struct sockaddr_in));
pingaddr.sin_family = AF_INET;
if (inet_aton(host, &pingaddr.sin_addr)) {
hostname = host;
} else {
if (!(h = gethostbyname(host))) {
fprintf(stderr, "ping: unknown host %s\n", host);
exit(1);
}
if (h->h_addrtype != AF_INET) {
fprintf(stderr, "ping: unknown address type; only AF_INET is currently supported.\n");
exit(1);
}
pingaddr.sin_family = AF_INET; /* h->h_addrtype */
memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
strncpy(buf, h->h_name, sizeof(buf)-1);
hostname = buf;
}
/* enable broadcast pings */
sockopt = 1;
setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *)&sockopt, sizeof(sockopt));
/* set recv buf for broadcast pings */
sockopt = 48 * 1024;
setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *)&sockopt, sizeof(sockopt));
printf("PING %s (%s): %d data bytes\n",
hostname, inet_ntoa(*(struct in_addr *)&pingaddr.sin_addr.s_addr),
DEFDATALEN);
signal(SIGINT, pingstats);
/* start the ping's going ... */
sendping(0);
/* listen for replies */
while (1) {
struct sockaddr_in from;
size_t fromlen = sizeof(from);
int c;
if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
(struct sockaddr *)&from, &fromlen)) < 0) {
if (errno == EINTR) continue;
perror("ping");
continue;
}
unpack(packet, c, &from);
if (pingcount > 0 && nreceived >= pingcount) break;
}
pingstats(0);
}
extern int ping_main(int argc, char **argv)
{
argc--;
argv++;
options = 0;
/* Parse any options */
if (argc < 1) usage(ping_usage);
while (**argv == '-') {
while (*++(*argv))
switch (**argv) {
case 'c':
argc--; argv++;
if (argc < 1) usage(ping_usage);
pingcount = atoi(*argv);
break;
case 'q':
options |= O_QUIET;
break;
default:
usage(ping_usage);
}
argc--;
argv++;
}
if (argc < 1) usage(ping_usage);
myid = getpid() & 0xFFFF;
ping(*(argv++));
exit( TRUE);
}
/*
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Mike Muuss.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 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
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

374
ping.c Normal file
View File

@ -0,0 +1,374 @@
/*
* $Id: ping.c,v 1.1 1999/12/07 23:14:59 andersen Exp $
* Mini ping implementation for busybox
*
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
*
* 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
*
* This version of ping is adapted from the ping in netkit-base 0.10,
* which is:
*
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Mike Muuss.
*
* Original copyright notice is retained at the end of this file.
*/
#include "internal.h"
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define DEFDATALEN 56
#define MAXIPLEN 60
#define MAXICMPLEN 76
#define MAXPACKET 65468
#define MAX_DUP_CHK (8 * 128)
#define MAXWAIT 10
#define O_QUIET (1 << 0)
#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
#define SET(bit) (A(bit) |= B(bit))
#define CLR(bit) (A(bit) &= (~B(bit)))
#define TST(bit) (A(bit) & B(bit))
static const char* ping_usage = "ping [OPTION]... host\n\n"
"Options:\n"
"\t-q\t\tquiet\n"
"\t-c\t\tping count\n";
static char *hostname = NULL;
static struct sockaddr_in pingaddr;
static int pingsock = -1;
static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0;
static int myid = 0, options = 0;
static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0;
static char rcvd_tbl[MAX_DUP_CHK / 8];
static void pingstats(int);
static void sendping(int);
static void unpack(char *, int, struct sockaddr_in *);
static void ping(char *);
static int in_cksum(unsigned short *, int);
/**************************************************************************/
static int in_cksum(unsigned short *buf, int sz)
{
int nleft = sz;
int sum = 0;
unsigned short *w = buf;
unsigned short ans = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(unsigned char *)(&ans) = *(unsigned char *)w;
sum += ans;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
ans = ~sum;
return(ans);
}
static void pingstats(int ign) {
signal(SIGINT, SIG_IGN);
printf("\n--- %s ping statistics ---\n", hostname);
printf("%ld packets transmitted, ", ntransmitted);
printf("%ld packets received, ", nreceived);
if (nrepeats)
printf("%ld duplicates, ", nrepeats);
if (ntransmitted)
printf("%ld%% packet loss\n",
(ntransmitted - nreceived)*100/ntransmitted);
if (nreceived)
printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
tmin/10, tmin%10,
(tsum/(nreceived+nrepeats))/10,
(tsum/(nreceived+nrepeats))%10,
tmax/10, tmax%10);
exit(0);
}
static void sendping(int ign)
{
struct icmp *pkt;
int i;
char packet[DEFDATALEN + 8];
pkt = (struct icmp *)packet;
pkt->icmp_type = ICMP_ECHO;
pkt->icmp_code = 0;
pkt->icmp_cksum = 0;
pkt->icmp_seq = ntransmitted++;
pkt->icmp_id = myid;
CLR(pkt->icmp_seq % MAX_DUP_CHK);
gettimeofday((struct timeval *)&packet[8], NULL);
pkt->icmp_cksum = in_cksum((unsigned short *)pkt, sizeof(packet));
i = sendto(pingsock, packet, sizeof(packet), 0,
(struct sockaddr *)&pingaddr, sizeof(struct sockaddr_in));
if (i < 0 || i != sizeof(packet)) {
if (i < 0) perror("ping");
fprintf(stderr, "ping wrote %d chars; %d expected\n", i, sizeof(packet));
exit(1);
}
signal(SIGALRM, sendping);
if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next */
alarm(1);
} else { /* done, wait for the last ping to come back */
/* todo, don't necessarily need to wait so long... */
signal(SIGALRM, pingstats);
alarm(MAXWAIT);
}
}
static void unpack(char *buf, int sz, struct sockaddr_in *from)
{
struct icmp *icmppkt;
struct iphdr *iphdr;
struct timeval tv, *tp;
int hlen, dupflag;
unsigned long triptime;
gettimeofday(&tv, NULL);
/* check IP header */
iphdr = (struct iphdr *)buf;
hlen = iphdr->ihl << 2;
/* discard if too short */
if (sz < (DEFDATALEN + ICMP_MINLEN)) return;
sz -= hlen;
icmppkt = (struct icmp *)(buf + hlen);
if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
if (icmppkt->icmp_id != myid) return; /* not our ping */
++nreceived;
tp = (struct timeval *)icmppkt->icmp_data;
if ((tv.tv_usec -= tp->tv_usec) < 0) {
--tv.tv_sec;
tv.tv_usec += 1000000;
}
tv.tv_sec -= tp->tv_sec;
triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
tsum += triptime;
if (triptime < tmin) tmin = triptime;
if (triptime > tmax) tmax = triptime;
if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) {
++nrepeats;
--nreceived;
dupflag = 1;
} else {
SET(icmppkt->icmp_seq % MAX_DUP_CHK);
dupflag = 0;
}
if (options & O_QUIET) return;
printf("%d bytes from %s: icmp_seq=%u", sz,
inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
icmppkt->icmp_seq);
printf(" ttl=%d", iphdr->ttl);
printf(" time=%lu.%lu ms", triptime/10, triptime%10);
if (dupflag) printf(" (DUP!)");
printf("\n");
} else {
fprintf(stderr, "Warning: unknown ICMP packet received (not echo-reply)\n");
}
}
static void ping(char *host)
{
struct protoent *proto;
struct hostent *h;
char buf[MAXHOSTNAMELEN];
char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
int sockopt;
if (!(proto = getprotobyname("icmp"))) {
fprintf(stderr, "ping: unknown protocol icmp\n");
exit(1);
}
if ((pingsock = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
if (errno == EPERM) {
fprintf(stderr, "ping: permission denied. (are you root?)\n");
} else {
perror("ping");
}
exit(1);
}
#ifdef SUID_BUSYBOX
setuid(getuid());
#endif
memset(&pingaddr, 0, sizeof(struct sockaddr_in));
pingaddr.sin_family = AF_INET;
if (inet_aton(host, &pingaddr.sin_addr)) {
hostname = host;
} else {
if (!(h = gethostbyname(host))) {
fprintf(stderr, "ping: unknown host %s\n", host);
exit(1);
}
if (h->h_addrtype != AF_INET) {
fprintf(stderr, "ping: unknown address type; only AF_INET is currently supported.\n");
exit(1);
}
pingaddr.sin_family = AF_INET; /* h->h_addrtype */
memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
strncpy(buf, h->h_name, sizeof(buf)-1);
hostname = buf;
}
/* enable broadcast pings */
sockopt = 1;
setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *)&sockopt, sizeof(sockopt));
/* set recv buf for broadcast pings */
sockopt = 48 * 1024;
setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *)&sockopt, sizeof(sockopt));
printf("PING %s (%s): %d data bytes\n",
hostname, inet_ntoa(*(struct in_addr *)&pingaddr.sin_addr.s_addr),
DEFDATALEN);
signal(SIGINT, pingstats);
/* start the ping's going ... */
sendping(0);
/* listen for replies */
while (1) {
struct sockaddr_in from;
size_t fromlen = sizeof(from);
int c;
if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
(struct sockaddr *)&from, &fromlen)) < 0) {
if (errno == EINTR) continue;
perror("ping");
continue;
}
unpack(packet, c, &from);
if (pingcount > 0 && nreceived >= pingcount) break;
}
pingstats(0);
}
extern int ping_main(int argc, char **argv)
{
argc--;
argv++;
options = 0;
/* Parse any options */
if (argc < 1) usage(ping_usage);
while (**argv == '-') {
while (*++(*argv))
switch (**argv) {
case 'c':
argc--; argv++;
if (argc < 1) usage(ping_usage);
pingcount = atoi(*argv);
break;
case 'q':
options |= O_QUIET;
break;
default:
usage(ping_usage);
}
argc--;
argv++;
}
if (argc < 1) usage(ping_usage);
myid = getpid() & 0xFFFF;
ping(*(argv++));
exit( TRUE);
}
/*
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Mike Muuss.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 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
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

View File

@ -107,13 +107,6 @@ umount_all(int useMtab)
extern int extern int
umount_main(int argc, char** argv) umount_main(int argc, char** argv)
{ {
int i=0;
char **foo=argv;
while(*foo) {
fprintf(stderr, "argv[%d]='%s'\n", i++, *foo);
foo++;
}
if (argc < 2) { if (argc < 2) {
usage( umount_usage); usage( umount_usage);
} }

View File

@ -107,13 +107,6 @@ umount_all(int useMtab)
extern int extern int
umount_main(int argc, char** argv) umount_main(int argc, char** argv)
{ {
int i=0;
char **foo=argv;
while(*foo) {
fprintf(stderr, "argv[%d]='%s'\n", i++, *foo);
foo++;
}
if (argc < 2) { if (argc < 2) {
usage( umount_usage); usage( umount_usage);
} }