From 19db07b3d4ca4c333f9a53bfbc113b44f3c55750 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Sat, 11 Dec 1999 08:41:28 +0000 Subject: [PATCH] Ok, so this is reallt 0.38... -Erik --- Changelog | 4 +- applets/busybox.c | 3 + busybox.c | 3 + busybox.def.h | 3 + coreutils/mkfifo.c | 61 +++++++++++++++++ internal.h | 3 +- mkfifo.c | 61 +++++++++++++++++ networking/ping.c | 166 +++++++++++++++++++++++++++++++++------------ ping.c | 166 +++++++++++++++++++++++++++++++++------------ 9 files changed, 383 insertions(+), 87 deletions(-) create mode 100644 coreutils/mkfifo.c create mode 100644 mkfifo.c diff --git a/Changelog b/Changelog index b5bbddcf0..6db788d4d 100644 --- a/Changelog +++ b/Changelog @@ -1,6 +1,6 @@ 0.39 - * New Apps: ping and hostname contributed by Randolph Chung - . 2 items off the TODO list! + * New Apps: ping, hostname, and mkfifo contributed by Randolph Chung + . 3 items off the TODO list! * I wrote free (just calls "cat /proc/meminfo"). * Added tail, based on tail from GNU textutils-1.19, but adjusted to suit my evil purposes. Costs 6k. I'll make it smaller sometime. diff --git a/applets/busybox.c b/applets/busybox.c index d1eb38a0b..4d676e7c0 100644 --- a/applets/busybox.c +++ b/applets/busybox.c @@ -114,6 +114,9 @@ static const struct Applet applets[] = { #ifdef BB_MKDIR //bin {"mkdir", mkdir_main}, #endif +#ifdef BB_MKFIFO //usr/bin + {"mkfifo", mkfifo_main}, +#endif #ifdef BB_MKNOD //bin {"mknod", mknod_main}, #endif diff --git a/busybox.c b/busybox.c index d1eb38a0b..4d676e7c0 100644 --- a/busybox.c +++ b/busybox.c @@ -114,6 +114,9 @@ static const struct Applet applets[] = { #ifdef BB_MKDIR //bin {"mkdir", mkdir_main}, #endif +#ifdef BB_MKFIFO //usr/bin + {"mkfifo", mkfifo_main}, +#endif #ifdef BB_MKNOD //bin {"mknod", mknod_main}, #endif diff --git a/busybox.def.h b/busybox.def.h index 30533616a..402f2bce3 100644 --- a/busybox.def.h +++ b/busybox.def.h @@ -38,6 +38,7 @@ //#define BB_MAKEDEVS //#define BB_MATH #define BB_MKDIR +//#define BB_MKFIFO #define BB_MKNOD #define BB_MKSWAP //#define BB_MNC @@ -99,3 +100,5 @@ #define BB_FEATURE_LS_TIMESTAMPS // enable ls -p and -F #define BB_FEATURE_LS_FILETYPES +// simplified ping +//#define BB_SIMPLE_PING diff --git a/coreutils/mkfifo.c b/coreutils/mkfifo.c new file mode 100644 index 000000000..676592ac7 --- /dev/null +++ b/coreutils/mkfifo.c @@ -0,0 +1,61 @@ +/* + * Mini mkfifo 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 + * + */ + +#include "internal.h" +#include +#include +#include +#include + +static const char mkfifo_usage[] = "mkfifo [OPTIONS] name\n\n" +"Create the named fifo\n\n" +"Options:\n" +"\t-m\tcreate the fifo with the specified mode; default = a=rw-umask\n"; + +extern int mkfifo_main(int argc, char **argv) +{ + char *thisarg; + mode_t mode = 0666; + argc--; + argv++; + + /* Parse any options */ + while (argc > 1) { + if (**argv != '-') usage(mkfifo_usage); + thisarg = *argv; thisarg++; + switch (*thisarg) { + case 'm': + argc--; argv++; + parse_mode(*argv, &mode); + break; + default: + usage (mkfifo_usage); + } + argc--; argv++; + } + if (argc < 1) usage (mkfifo_usage); + if (mkfifo(*argv, mode) < 0) { + perror("mkfifo"); + exit(255); + } else { + exit(TRUE); + } +} diff --git a/internal.h b/internal.h index 95df64c63..3cf777418 100644 --- a/internal.h +++ b/internal.h @@ -69,7 +69,6 @@ extern int false_main(int argc, char** argv); extern int fdisk_main(int argc, char** argv); extern int fdflush_main(int argc, char **argv); extern int fsck_minix_main(int argc, char **argv); -extern int mkfs_minix_main(int argc, char **argv); extern int find_main(int argc, char** argv); extern int free_main(int argc, char** argv); extern int grep_main(int argc, char** argv); @@ -89,6 +88,8 @@ extern int deallocvt_main(int argc, char** argv); extern int makedevs_main(int argc, char** argv); extern int math_main(int argc, char** argv); extern int mkdir_main(int argc, char** argv); +extern int mkfifo_main(int argc, char **argv); +extern int mkfs_minix_main(int argc, char **argv); extern int mknod_main(int argc, char** argv); extern int mkswap_main(int argc, char** argv); extern int mnc_main(int argc, char** argv); diff --git a/mkfifo.c b/mkfifo.c new file mode 100644 index 000000000..676592ac7 --- /dev/null +++ b/mkfifo.c @@ -0,0 +1,61 @@ +/* + * Mini mkfifo 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 + * + */ + +#include "internal.h" +#include +#include +#include +#include + +static const char mkfifo_usage[] = "mkfifo [OPTIONS] name\n\n" +"Create the named fifo\n\n" +"Options:\n" +"\t-m\tcreate the fifo with the specified mode; default = a=rw-umask\n"; + +extern int mkfifo_main(int argc, char **argv) +{ + char *thisarg; + mode_t mode = 0666; + argc--; + argv++; + + /* Parse any options */ + while (argc > 1) { + if (**argv != '-') usage(mkfifo_usage); + thisarg = *argv; thisarg++; + switch (*thisarg) { + case 'm': + argc--; argv++; + parse_mode(*argv, &mode); + break; + default: + usage (mkfifo_usage); + } + argc--; argv++; + } + if (argc < 1) usage (mkfifo_usage); + if (mkfifo(*argv, mode) < 0) { + perror("mkfifo"); + exit(255); + } else { + exit(TRUE); + } +} diff --git a/networking/ping.c b/networking/ping.c index 4176ab390..92b62def3 100644 --- a/networking/ping.c +++ b/networking/ping.c @@ -1,5 +1,5 @@ /* - * $Id: ping.c,v 1.5 1999/12/09 06:11:36 andersen Exp $ + * $Id: ping.c,v 1.6 1999/12/11 08:41:28 andersen Exp $ * Mini ping implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -63,28 +63,7 @@ #define CLR(bit) (A(bit) &= (~B(bit))) #define TST(bit) (A(bit) & B(bit)) -static const char* ping_usage = "ping [OPTION]... host\n\n" -"Send ICMP ECHO_REQUEST packets to network hosts.\n\n" -"Options:\n" -"\t-q\t\tQuiet mode, only displays output at start and when finished.\n" -"\t-c COUNT\tSend only COUNT pings.\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); - -/**************************************************************************/ - +/* common routines */ static int in_cksum(unsigned short *buf, int sz) { int nleft = sz; @@ -108,6 +87,114 @@ static int in_cksum(unsigned short *buf, int sz) return(ans); } +/* simple version */ +#ifdef BB_SIMPLE_PING +static const char* ping_usage = "ping host\n\n"; + +static char* hostname = NULL; + +static void noresp(int ign) +{ + printf("No response from %s\n", hostname); + exit(0); +} + +static int ping(const char *host) +{ + struct hostent *h; + struct sockaddr_in pingaddr; + struct icmp *pkt; + int pingsock, c; + char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; + + if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */ + perror("ping"); + exit(1); + } + + /* drop root privs if running setuid */ + setuid(getuid()); + + memset(&pingaddr, 0, sizeof(struct sockaddr_in)); + pingaddr.sin_family = AF_INET; + if (!(h = gethostbyname(host))) { + fprintf(stderr, "ping: unknown host %s\n", host); + exit(1); + } + memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); + hostname = h->h_name; + + pkt = (struct icmp *)packet; + memset(pkt, 0, sizeof(packet)); + pkt->icmp_type = ICMP_ECHO; + pkt->icmp_cksum = in_cksum((unsigned short *)pkt, sizeof(packet)); + + c = sendto(pingsock, packet, sizeof(packet), 0, + (struct sockaddr *)&pingaddr, sizeof(struct sockaddr_in)); + + if (c < 0 || c != sizeof(packet)) { + if (c < 0) perror("ping"); + fprintf(stderr, "ping: write incomplete\n"); + exit(1); + } + + signal(SIGALRM, noresp); + alarm(5); /* give the host 5000ms to respond */ + /* listen for replies */ + while (1) { + struct sockaddr_in from; + size_t fromlen = sizeof(from); + + if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, + (struct sockaddr *)&from, &fromlen)) < 0) { + if (errno == EINTR) continue; + perror("ping"); + continue; + } + if (c >= 76) { /* ip + icmp */ + struct iphdr *iphdr = (struct iphdr *)packet; + pkt = (struct icmp *)(packet + (iphdr->ihl << 2)); /* skip ip hdr */ + if (pkt->icmp_type == ICMP_ECHOREPLY) break; + } + } + printf("%s is alive!\n", hostname); + return(TRUE); +} + +extern int ping_main(int argc, char **argv) +{ + argc--; + argv++; + if (argc < 1) usage(ping_usage); + ping(*argv); + exit(TRUE); +} + +#else +/* full(er) version */ +static const char* ping_usage = "ping [OPTION]... host\n\n" +"Send ICMP ECHO_REQUEST packets to network hosts.\n\n" +"Options:\n" +"\t-q\t\tQuiet mode, only displays output at start and when finished.\n" +"\t-c COUNT\tSend only COUNT pings.\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 sendping(int); +static void pingstats(int); +static void unpack(char *, int, struct sockaddr_in *); + +static void ping(char *); + +/**************************************************************************/ + static void pingstats(int ign) { signal(SIGINT, SIG_IGN); @@ -249,25 +336,21 @@ static void ping(char *host) 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; + 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)); @@ -331,6 +414,7 @@ extern int ping_main(int argc, char **argv) ping(*argv); exit(TRUE); } +#endif /* * Copyright (c) 1989 The Regents of the University of California. @@ -367,5 +451,3 @@ extern int ping_main(int argc, char **argv) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ - - diff --git a/ping.c b/ping.c index 4176ab390..92b62def3 100644 --- a/ping.c +++ b/ping.c @@ -1,5 +1,5 @@ /* - * $Id: ping.c,v 1.5 1999/12/09 06:11:36 andersen Exp $ + * $Id: ping.c,v 1.6 1999/12/11 08:41:28 andersen Exp $ * Mini ping implementation for busybox * * Copyright (C) 1999 by Randolph Chung @@ -63,28 +63,7 @@ #define CLR(bit) (A(bit) &= (~B(bit))) #define TST(bit) (A(bit) & B(bit)) -static const char* ping_usage = "ping [OPTION]... host\n\n" -"Send ICMP ECHO_REQUEST packets to network hosts.\n\n" -"Options:\n" -"\t-q\t\tQuiet mode, only displays output at start and when finished.\n" -"\t-c COUNT\tSend only COUNT pings.\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); - -/**************************************************************************/ - +/* common routines */ static int in_cksum(unsigned short *buf, int sz) { int nleft = sz; @@ -108,6 +87,114 @@ static int in_cksum(unsigned short *buf, int sz) return(ans); } +/* simple version */ +#ifdef BB_SIMPLE_PING +static const char* ping_usage = "ping host\n\n"; + +static char* hostname = NULL; + +static void noresp(int ign) +{ + printf("No response from %s\n", hostname); + exit(0); +} + +static int ping(const char *host) +{ + struct hostent *h; + struct sockaddr_in pingaddr; + struct icmp *pkt; + int pingsock, c; + char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; + + if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */ + perror("ping"); + exit(1); + } + + /* drop root privs if running setuid */ + setuid(getuid()); + + memset(&pingaddr, 0, sizeof(struct sockaddr_in)); + pingaddr.sin_family = AF_INET; + if (!(h = gethostbyname(host))) { + fprintf(stderr, "ping: unknown host %s\n", host); + exit(1); + } + memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); + hostname = h->h_name; + + pkt = (struct icmp *)packet; + memset(pkt, 0, sizeof(packet)); + pkt->icmp_type = ICMP_ECHO; + pkt->icmp_cksum = in_cksum((unsigned short *)pkt, sizeof(packet)); + + c = sendto(pingsock, packet, sizeof(packet), 0, + (struct sockaddr *)&pingaddr, sizeof(struct sockaddr_in)); + + if (c < 0 || c != sizeof(packet)) { + if (c < 0) perror("ping"); + fprintf(stderr, "ping: write incomplete\n"); + exit(1); + } + + signal(SIGALRM, noresp); + alarm(5); /* give the host 5000ms to respond */ + /* listen for replies */ + while (1) { + struct sockaddr_in from; + size_t fromlen = sizeof(from); + + if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, + (struct sockaddr *)&from, &fromlen)) < 0) { + if (errno == EINTR) continue; + perror("ping"); + continue; + } + if (c >= 76) { /* ip + icmp */ + struct iphdr *iphdr = (struct iphdr *)packet; + pkt = (struct icmp *)(packet + (iphdr->ihl << 2)); /* skip ip hdr */ + if (pkt->icmp_type == ICMP_ECHOREPLY) break; + } + } + printf("%s is alive!\n", hostname); + return(TRUE); +} + +extern int ping_main(int argc, char **argv) +{ + argc--; + argv++; + if (argc < 1) usage(ping_usage); + ping(*argv); + exit(TRUE); +} + +#else +/* full(er) version */ +static const char* ping_usage = "ping [OPTION]... host\n\n" +"Send ICMP ECHO_REQUEST packets to network hosts.\n\n" +"Options:\n" +"\t-q\t\tQuiet mode, only displays output at start and when finished.\n" +"\t-c COUNT\tSend only COUNT pings.\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 sendping(int); +static void pingstats(int); +static void unpack(char *, int, struct sockaddr_in *); + +static void ping(char *); + +/**************************************************************************/ + static void pingstats(int ign) { signal(SIGINT, SIG_IGN); @@ -249,25 +336,21 @@ static void ping(char *host) 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; + 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)); @@ -331,6 +414,7 @@ extern int ping_main(int argc, char **argv) ping(*argv); exit(TRUE); } +#endif /* * Copyright (c) 1989 The Regents of the University of California. @@ -367,5 +451,3 @@ extern int ping_main(int argc, char **argv) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ - -