1 /* vi: set sw=4 ts=4: */
3 * Mini ipcalc implementation for busybox
5 * By Jordan Crouse <jordan@cosmicpenguin.net>
6 * Stephan Linz <linz@li-pro.net>
8 * This is a complete reimplementation of the ipcalc program
9 * from Red Hat. I didn't look at their source code, but there
10 * is no denying that this is a loving reimplementation
12 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
14 //config:config IPCALC
15 //config: bool "ipcalc"
18 //config: ipcalc takes an IP address and netmask and calculates the
19 //config: resulting broadcast, network, and host range.
21 //config:config FEATURE_IPCALC_FANCY
22 //config: bool "Fancy IPCALC, more options, adds 1 kbyte"
24 //config: depends on IPCALC
26 //config: Adds the options hostname, prefix and silent to the output of
29 //config:config FEATURE_IPCALC_LONG_OPTIONS
30 //config: bool "Enable long options"
32 //config: depends on IPCALC && LONG_OPTS
34 //config: Support long options for the ipcalc applet.
36 //applet:IF_IPCALC(APPLET(ipcalc, BB_DIR_BIN, BB_SUID_DROP))
38 //kbuild:lib-$(CONFIG_IPCALC) += ipcalc.o
40 //usage:#define ipcalc_trivial_usage
41 //usage: "[OPTIONS] ADDRESS"
42 //usage: IF_FEATURE_IPCALC_FANCY("[/PREFIX]") " [NETMASK]"
43 //usage:#define ipcalc_full_usage "\n\n"
44 //usage: "Calculate IP network settings from a IP address\n"
45 //usage: IF_FEATURE_IPCALC_LONG_OPTIONS(
46 //usage: "\n -b,--broadcast Display calculated broadcast address"
47 //usage: "\n -n,--network Display calculated network address"
48 //usage: "\n -m,--netmask Display default netmask for IP"
49 //usage: IF_FEATURE_IPCALC_FANCY(
50 //usage: "\n -p,--prefix Display the prefix for IP/NETMASK"
51 //usage: "\n -h,--hostname Display first resolved host name"
52 //usage: "\n -s,--silent Don't ever display error messages"
55 //usage: IF_NOT_FEATURE_IPCALC_LONG_OPTIONS(
56 //usage: "\n -b Display calculated broadcast address"
57 //usage: "\n -n Display calculated network address"
58 //usage: "\n -m Display default netmask for IP"
59 //usage: IF_FEATURE_IPCALC_FANCY(
60 //usage: "\n -p Display the prefix for IP/NETMASK"
61 //usage: "\n -h Display first resolved host name"
62 //usage: "\n -s Don't ever display error messages"
67 /* After libbb.h, because on some systems it needs other includes */
68 #include <arpa/inet.h>
70 #define CLASS_A_NETMASK ntohl(0xFF000000)
71 #define CLASS_B_NETMASK ntohl(0xFFFF0000)
72 #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
74 static unsigned long get_netmask(unsigned long ipaddr)
76 ipaddr = htonl(ipaddr);
78 if ((ipaddr & 0xC0000000) == 0xC0000000)
79 return CLASS_C_NETMASK;
80 else if ((ipaddr & 0x80000000) == 0x80000000)
81 return CLASS_B_NETMASK;
82 else if ((ipaddr & 0x80000000) == 0)
83 return CLASS_A_NETMASK;
88 #if ENABLE_FEATURE_IPCALC_FANCY
89 static int get_prefix(unsigned long netmask)
91 unsigned long msk = 0x80000000;
94 netmask = htonl(netmask);
103 int get_prefix(unsigned long netmask);
108 #define BROADCAST 0x02
110 #define NETPREFIX 0x08
111 #define HOSTNAME 0x10
114 #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
115 static const char ipcalc_longopts[] ALIGN1 =
116 "netmask\0" No_argument "m" // netmask from IP (assuming complete class A, B, or C network)
117 "broadcast\0" No_argument "b" // broadcast from IP [netmask]
118 "network\0" No_argument "n" // network from IP [netmask]
119 # if ENABLE_FEATURE_IPCALC_FANCY
120 "prefix\0" No_argument "p" // prefix from IP[/prefix] [netmask]
121 "hostname\0" No_argument "h" // hostname from IP
122 "silent\0" No_argument "s" // don’t ever display error messages
127 int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
128 int ipcalc_main(int argc UNUSED_PARAM, char **argv)
131 bool have_netmask = 0;
132 struct in_addr s_netmask, s_broadcast, s_network, s_ipaddr;
133 /* struct in_addr { in_addr_t s_addr; } and in_addr_t
134 * (which in turn is just a typedef to uint32_t)
135 * are essentially the same type. A few macros for less verbosity: */
136 #define netmask (s_netmask.s_addr)
137 #define broadcast (s_broadcast.s_addr)
138 #define network (s_network.s_addr)
139 #define ipaddr (s_ipaddr.s_addr)
142 #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
143 applet_long_options = ipcalc_longopts;
145 opt_complementary = "-1:?2"; /* minimum 1 arg, maximum 2 args */
146 opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
149 logmode = LOGMODE_NONE; /* suppress error_msg() output */
151 if (!(opt & (BROADCAST | NETWORK | NETPREFIX))) {
152 /* if no options at all or
153 * (no broadcast,network,prefix) and (two args)... */
159 if (ENABLE_FEATURE_IPCALC_FANCY) {
160 unsigned long netprefix = 0;
166 if (*prefixstr == '/') {
170 netprefix = xatoul_range(prefixstr, 0, 32);
173 while (netprefix > 0) {
178 netmask = htonl(netmask);
179 /* Even if it was 0, we will signify that we have a netmask. This allows */
180 /* for specification of default routes, etc which have a 0 netmask/prefix */
189 if (inet_aton(ipstr, &s_ipaddr) == 0) {
190 bb_error_msg_and_die("bad IP address: %s", argv[0]);
194 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
195 bb_error_msg_and_die("use prefix or netmask, not both");
197 if (inet_aton(argv[1], &s_netmask) == 0) {
198 bb_error_msg_and_die("bad netmask: %s", argv[1]);
201 /* JHC - If the netmask wasn't provided then calculate it */
202 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
203 netmask = get_netmask(ipaddr);
207 printf("NETMASK=%s\n", inet_ntoa(s_netmask));
210 if (opt & BROADCAST) {
211 broadcast = (ipaddr & netmask) | ~netmask;
212 printf("BROADCAST=%s\n", inet_ntoa(s_broadcast));
216 network = ipaddr & netmask;
217 printf("NETWORK=%s\n", inet_ntoa(s_network));
220 if (ENABLE_FEATURE_IPCALC_FANCY) {
221 if (opt & NETPREFIX) {
222 printf("PREFIX=%i\n", get_prefix(netmask));
225 if (opt & HOSTNAME) {
226 struct hostent *hostinfo;
228 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
230 bb_herror_msg_and_die("can't find hostname for %s", argv[0]);
232 str_tolower(hostinfo->h_name);
234 printf("HOSTNAME=%s\n", hostinfo->h_name);