aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2002-11-10 23:42:27 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2002-11-10 23:42:27 +0000
commitc11986d89eba564945b226660031e624bf897561 (patch)
tree916cab407d266dbbf8f0069e65ab0434583b336c
parent912d8f4cbc724ce6ef9d8bd3f73d16d7ef4cf069 (diff)
downloadbusybox-w32-c11986d89eba564945b226660031e624bf897561.tar.gz
busybox-w32-c11986d89eba564945b226660031e624bf897561.tar.bz2
busybox-w32-c11986d89eba564945b226660031e624bf897561.zip
ipcalc applet by Stephan Linz and Jordan Crouse
-rw-r--r--AUTHORS13
-rw-r--r--include/applets.h3
-rw-r--r--include/usage.h30
-rw-r--r--networking/Makefile.in1
-rw-r--r--networking/config.in4
-rw-r--r--networking/ipcalc.c164
6 files changed, 211 insertions, 4 deletions
diff --git a/AUTHORS b/AUTHORS
index 6cd8ab80f..88fa562b2 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -8,6 +8,9 @@ incorect, _please_ let me know.
8 8
9----------- 9-----------
10 10
11Emanuele Aina <emanuele.aina@tiscali.it>
12 run-parts
13
11Erik Andersen <andersen@codepoet.org>, <andersee@debian.org> 14Erik Andersen <andersen@codepoet.org>, <andersee@debian.org>
12 Tons of new stuff, major rewrite of most of the 15 Tons of new stuff, major rewrite of most of the
13 core apps, tons of new apps as noted in header files. 16 core apps, tons of new apps as noted in header files.
@@ -28,6 +31,9 @@ Dave Cinege <dcinege@psychosis.com>
28 more(v2), makedevs, dutmp, modularization, auto links file, 31 more(v2), makedevs, dutmp, modularization, auto links file,
29 various fixes, Linux Router Project maintenance 32 various fixes, Linux Router Project maintenance
30 33
34Jordan Crouse <jordan@cosmicpenguin.net>
35 ipcalc
36
31Magnus Damm <damm@opensource.se> 37Magnus Damm <damm@opensource.se>
32 tftp client 38 tftp client
33 insmod powerpc support 39 insmod powerpc support
@@ -48,6 +54,9 @@ Daniel Jacobowitz <dan@debian.org>
48Matt Kraai <kraai@alumni.cmu.edu> 54Matt Kraai <kraai@alumni.cmu.edu>
49 documentation, bugfixes, test suite 55 documentation, bugfixes, test suite
50 56
57Stephan Linz <linz@li-pro.net>
58 ipcalc, Red Hat equivalence
59
51John Lombardo <john@deltanet.com> 60John Lombardo <john@deltanet.com>
52 dirname, tr 61 dirname, tr
53 62
@@ -89,7 +98,3 @@ Charles P. Wright <cpwright@villagenet.com>
89 98
90Enrique Zanardi <ezanardi@ull.es> 99Enrique Zanardi <ezanardi@ull.es>
91 tarcat (since removed), loadkmap, various fixes, Debian maintenance 100 tarcat (since removed), loadkmap, various fixes, Debian maintenance
92
93Emanuele Aina <emanuele.aina@tiscali.it>
94 run-parts
95
diff --git a/include/applets.h b/include/applets.h
index a9f93cc49..e8bcb0e4b 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -254,6 +254,9 @@
254#ifdef CONFIG_IP 254#ifdef CONFIG_IP
255 APPLET(ip, ip_main, _BB_DIR_BIN, _BB_SUID_NEVER) 255 APPLET(ip, ip_main, _BB_DIR_BIN, _BB_SUID_NEVER)
256#endif 256#endif
257#ifdef CONFIG_IPCALC
258 APPLET(ipcalc, ipcalc_main, _BB_DIR_BIN, _BB_SUID_NEVER)
259#endif
257#ifdef CONFIG_KILL 260#ifdef CONFIG_KILL
258 APPLET(kill, kill_main, _BB_DIR_BIN, _BB_SUID_NEVER) 261 APPLET(kill, kill_main, _BB_DIR_BIN, _BB_SUID_NEVER)
259#endif 262#endif
diff --git a/include/usage.h b/include/usage.h
index c140a88ba..b741360cf 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -982,6 +982,36 @@
982#define ip_full_usage \ 982#define ip_full_usage \
983 "not written yet" 983 "not written yet"
984 984
985#ifndef CONFIG_FEATURE_IPCALC_FANCY
986#define ipcalc_trivial_usage \
987 "[--broadcast] [--netmask] [--network] ipaddr <netmask>"
988
989#define ipcalc_full_usage \
990 "Calculate IP network settings from a IP address\n\n" \
991 "Options:\n" \
992 "\t-b\t--broadcast\tDisplay calculated broadcast address.\n" \
993 "\t-n\t--netmask\tDisplay default netmask for IP.\n" \
994 "\t-w\t--network\tDisplay calculated network address."
995#else
996#define ipcalc_trivial_usage \
997 "[OPTION]... ipaddr <netmask>"
998
999#define ipcalc_full_usage \
1000 "Calculate IP network settings from a IP address\n\n" \
1001 "Options:\n" \
1002 "\t-b\t--broadcast\tDisplay calculated broadcast address.\n" \
1003 "\t-n\t--netmask\tDisplay default netmask for IP.\n" \
1004 "\t-w\t--network\tDisplay calculated network address.\n" \
1005 "\t-h\t--hostname\tDisplay first resolved host name.\n" \
1006 "\t-s\t--silent\tDon't ever display error messages."
1007#endif
1008
1009#define ipcalc_notes_usage \
1010 "ipcalc provides a simple way to calculate IP information for\n" \
1011 "a host. The various options specify what information ipcalc\n" \
1012 "should display on standard out. Multiple options may be\n" \
1013 "specified.\n"
1014
985#define kill_trivial_usage \ 1015#define kill_trivial_usage \
986 "[-signal] process-id [process-id ...]" 1016 "[-signal] process-id [process-id ...]"
987#define kill_full_usage \ 1017#define kill_full_usage \
diff --git a/networking/Makefile.in b/networking/Makefile.in
index d249c0423..7404b041d 100644
--- a/networking/Makefile.in
+++ b/networking/Makefile.in
@@ -27,6 +27,7 @@ NETWORKING-$(CONFIG_HOSTNAME) += hostname.o
27NETWORKING-$(CONFIG_IFCONFIG) += ifconfig.o 27NETWORKING-$(CONFIG_IFCONFIG) += ifconfig.o
28NETWORKING-$(CONFIG_IFUPDOWN) += ifupdown.o 28NETWORKING-$(CONFIG_IFUPDOWN) += ifupdown.o
29NETWORKING-$(CONFIG_IP) += ip.o 29NETWORKING-$(CONFIG_IP) += ip.o
30NETWORKING-$(CONFIG_IPCALC) += ipcalc.o
30NETWORKING-$(CONFIG_NC) += nc.o 31NETWORKING-$(CONFIG_NC) += nc.o
31NETWORKING-$(CONFIG_NETSTAT) += netstat.o 32NETWORKING-$(CONFIG_NETSTAT) += netstat.o
32NETWORKING-$(CONFIG_NSLOOKUP) += nslookup.o 33NETWORKING-$(CONFIG_NSLOOKUP) += nslookup.o
diff --git a/networking/config.in b/networking/config.in
index 7393febae..27a8ab571 100644
--- a/networking/config.in
+++ b/networking/config.in
@@ -30,6 +30,10 @@ if [ "$CONFIG_IP" = "y" ]; then
30 bool ' route' CONFIG_FEATURE_IP_ROUTE 30 bool ' route' CONFIG_FEATURE_IP_ROUTE
31 bool ' tunnel' CONFIG_FEATURE_IP_TUNNEL 31 bool ' tunnel' CONFIG_FEATURE_IP_TUNNEL
32fi 32fi
33bool 'ipcalc' CONFIG_IPCALC
34if [ "$CONFIG_IPCALC" = "y" ]; then
35 bool ' Fancy IPCALC, more options, adds 300 bytes' CONFIG_FEATURE_IPCALC_FANCY
36fi
33bool 'nc' CONFIG_NC 37bool 'nc' CONFIG_NC
34bool 'netstat' CONFIG_NETSTAT 38bool 'netstat' CONFIG_NETSTAT
35bool 'nslookup' CONFIG_NSLOOKUP 39bool 'nslookup' CONFIG_NSLOOKUP
diff --git a/networking/ipcalc.c b/networking/ipcalc.c
new file mode 100644
index 000000000..f130f3b7c
--- /dev/null
+++ b/networking/ipcalc.c
@@ -0,0 +1,164 @@
1/* vi: set sw=4 ts=4 ai: */
2/*
3 * Mini ipcalc implementation for busybox
4 *
5 * By Jordan Crouse <jordan@cosmicpenguin.net>
6 * Stephan Linz <linz@li-pro.net>
7 *
8 * This is a complete reimplentation of the ipcalc program
9 * from Redhat. I didn't look at their source code, but there
10 * is no denying that this is a loving reimplementation
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <ctype.h>
16#include <getopt.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20
21#include "busybox.h"
22
23#define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;}
24
25static unsigned long get_netmask(unsigned long ipaddr)
26{
27 if (ipaddr & 0xC0) {
28 return 0x00FFFFFF; /* Class C */
29 }
30 if (ipaddr & 0x10) {
31 return 0x0000FFFF; /* Class B */
32 }
33 return 0x000000FF; /* Class A */
34}
35
36#define NETMASK 0x01
37#define BROADCAST 0x02
38#define NETWORK 0x04
39#define HOSTNAME 0x08
40#define SILENT 0x80
41
42int ipcalc_main(int argc, char **argv)
43{
44 unsigned char mode = 0;
45
46 unsigned long netmask = 0;
47 unsigned long broadcast = 0;
48 unsigned long network = 0;
49 unsigned long ipaddr = 0;
50
51 int opt = 0;
52
53 struct option long_options[] = {
54 {"netmask", no_argument, NULL, 'n'},
55 {"broadcast", no_argument, NULL, 'b'},
56 {"network", no_argument, NULL, 'w'},
57#ifdef CONFIG_FEATURE_IPCALC_FANCY
58 {"hostname", no_argument, NULL, 'h'},
59 {"silent", no_argument, NULL, 's'},
60#endif
61 {NULL, 0, NULL, 0}
62 };
63
64
65 while ((opt = getopt_long(argc, argv,
66#ifdef CONFIG_FEATURE_IPCALC_FANCY
67 "nbwhs",
68#else
69 "nbw",
70#endif
71 long_options, NULL)) != EOF) {
72 if (opt == 'n')
73 mode |= NETMASK;
74 else if (opt == 'b')
75 mode |= BROADCAST;
76 else if (opt == 'w')
77 mode |= NETWORK;
78#ifdef CONFIG_FEATURE_IPCALC_FANCY
79 else if (opt == 'h')
80 mode |= HOSTNAME;
81 else if (opt == 's')
82 mode |= SILENT;
83#endif
84 else {
85 show_usage();
86 }
87 }
88
89 if (mode & (BROADCAST | NETWORK)) {
90 if (argc - optind > 2) {
91 show_usage();
92 }
93 } else {
94 if (argc - optind != 1) {
95 show_usage();
96 }
97 }
98
99 ipaddr = inet_addr(argv[optind]);
100
101 if (ipaddr == INADDR_NONE) {
102 IPCALC_MSG(error_msg_and_die("bad IP address: %s\n", argv[optind]),
103 exit(EXIT_FAILURE));
104 }
105
106
107 if (argc - optind == 2) {
108 netmask = inet_addr(argv[optind + 1]);
109 }
110
111 if (ipaddr == INADDR_NONE) {
112 IPCALC_MSG(error_msg_and_die("bad netmask: %s\n", argv[optind + 1]),
113 exit(EXIT_FAILURE));
114 }
115
116 /* JHC - If the netmask wasn't provided then calculate it */
117 if (!netmask) {
118 netmask = get_netmask(ipaddr);
119 }
120
121 if (mode & NETMASK) {
122 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
123 }
124
125 if (mode & BROADCAST) {
126 broadcast = (ipaddr & netmask) | ~netmask;
127 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
128 }
129
130 if (mode & NETWORK) {
131 network = ipaddr & netmask;
132 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
133 }
134#ifdef CONFIG_FEATURE_IPCALC_FANCY
135 if (mode & HOSTNAME) {
136 struct hostent *hostinfo;
137 int x;
138
139 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
140 if (!hostinfo) {
141 IPCALC_MSG(error_msg("cannot find hostname for %s", argv[optind]);
142 herror(NULL);
143 putc('\n', stderr);,);
144 exit(EXIT_FAILURE);
145 }
146 for (x = 0; hostinfo->h_name[x]; x++) {
147 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
148 }
149
150 printf("HOSTNAME=%s\n", hostinfo->h_name);
151 }
152#endif
153
154 return EXIT_SUCCESS;
155}
156
157/* END CODE */
158/*
159Local Variables:
160c-file-style: "linux"
161c-basic-offset: 4
162tab-width: 4
163End:
164*/