blob: 21219424fc6e09d619b4537c52d8816410287e9f [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrathc11986d2002-11-10 23:42:27 +00002/*
3 * Mini ipcalc implementation for busybox
4 *
5 * By Jordan Crouse <jordan@cosmicpenguin.net>
6 * Stephan Linz <linz@li-pro.net>
7 *
Eric Andersenaff114c2004-04-14 17:51:38 +00008 * This is a complete reimplementation of the ipcalc program
9 * from Red Hat. I didn't look at their source code, but there
Glenn L McGrathc11986d2002-11-10 23:42:27 +000010 * is no denying that this is a loving reimplementation
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +000011 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020012 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrathc11986d2002-11-10 23:42:27 +000013 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010014//config:config IPCALC
15//config: bool "ipcalc"
16//config: default y
17//config: help
18//config: ipcalc takes an IP address and netmask and calculates the
19//config: resulting broadcast, network, and host range.
20//config:
21//config:config FEATURE_IPCALC_FANCY
22//config: bool "Fancy IPCALC, more options, adds 1 kbyte"
23//config: default y
24//config: depends on IPCALC
25//config: help
26//config: Adds the options hostname, prefix and silent to the output of
27//config: "ipcalc".
28//config:
29//config:config FEATURE_IPCALC_LONG_OPTIONS
30//config: bool "Enable long options"
31//config: default y
32//config: depends on IPCALC && LONG_OPTS
33//config: help
34//config: Support long options for the ipcalc applet.
35
36//applet:IF_IPCALC(APPLET(ipcalc, BB_DIR_BIN, BB_SUID_DROP))
37
38//kbuild:lib-$(CONFIG_IPCALC) += ipcalc.o
Pere Orga5bc8c002011-04-11 03:29:49 +020039
40//usage:#define ipcalc_trivial_usage
Ron Yorston72dcbe42015-07-12 21:19:28 +010041//usage: "[OPTIONS] ADDRESS"
42//usage: IF_FEATURE_IPCALC_FANCY("[/PREFIX]") " [NETMASK]"
Pere Orga5bc8c002011-04-11 03:29:49 +020043//usage:#define ipcalc_full_usage "\n\n"
44//usage: "Calculate IP network settings from a IP address\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020045//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"
53//usage: )
54//usage: )
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"
63//usage: )
64//usage: )
65
Dan Fandrichfdd7b562010-06-18 22:37:42 -070066#include "libbb.h"
Denys Vlasenko134d0eb2010-06-19 20:07:23 +020067/* After libbb.h, because on some systems it needs other includes */
Glenn L McGrathc11986d2002-11-10 23:42:27 +000068#include <arpa/inet.h>
69
Denys Vlasenkofb132e42010-10-29 11:46:52 +020070#define CLASS_A_NETMASK ntohl(0xFF000000)
71#define CLASS_B_NETMASK ntohl(0xFFFF0000)
72#define CLASS_C_NETMASK ntohl(0xFFFFFF00)
Eric Andersen7207b882003-07-05 07:59:30 +000073
Glenn L McGrathc11986d2002-11-10 23:42:27 +000074static unsigned long get_netmask(unsigned long ipaddr)
75{
Eric Andersen7207b882003-07-05 07:59:30 +000076 ipaddr = htonl(ipaddr);
77
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;
84 else
85 return 0;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000086}
87
Denis Vlasenkoe3241842007-08-13 10:36:25 +000088#if ENABLE_FEATURE_IPCALC_FANCY
Glenn L McGrath530ea422003-09-02 06:59:57 +000089static int get_prefix(unsigned long netmask)
90{
Glenn L McGrath14092a12003-09-12 00:44:50 +000091 unsigned long msk = 0x80000000;
Eric Andersena4375042004-04-13 19:25:57 +000092 int ret = 0;
Glenn L McGrath530ea422003-09-02 06:59:57 +000093
Glenn L McGrath14092a12003-09-12 00:44:50 +000094 netmask = htonl(netmask);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +000095 while (msk) {
Glenn L McGrath14092a12003-09-12 00:44:50 +000096 if (netmask & msk)
97 ret++;
98 msk >>= 1;
Eric Andersena4375042004-04-13 19:25:57 +000099 }
100 return ret;
Glenn L McGrath530ea422003-09-02 06:59:57 +0000101}
Rob Landleyff97ee92006-06-02 19:03:01 +0000102#else
103int get_prefix(unsigned long netmask);
Glenn L McGrath530ea422003-09-02 06:59:57 +0000104#endif
105
Denis Vlasenko13858992006-10-08 12:49:22 +0000106
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000107#define NETMASK 0x01
108#define BROADCAST 0x02
109#define NETWORK 0x04
Glenn L McGrath530ea422003-09-02 06:59:57 +0000110#define NETPREFIX 0x08
111#define HOSTNAME 0x10
Glenn L McGrathd6bdd5d2003-09-05 02:37:15 +0000112#define SILENT 0x20
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000113
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000114#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000115 static const char ipcalc_longopts[] ALIGN1 =
Steve Bennett823b6362010-04-07 19:16:12 +0200116 "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]
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000119# if ENABLE_FEATURE_IPCALC_FANCY
Steve Bennett823b6362010-04-07 19:16:12 +0200120 "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
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000123# endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000124 ;
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000125#endif
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000126
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000127int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100128int ipcalc_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyff97ee92006-06-02 19:03:01 +0000129{
Denis Vlasenko13858992006-10-08 12:49:22 +0000130 unsigned opt;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100131 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)
Rob Landleyff97ee92006-06-02 19:03:01 +0000140 char *ipstr;
141
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000142#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000143 applet_long_options = ipcalc_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000144#endif
Steve Bennett823b6362010-04-07 19:16:12 +0200145 opt_complementary = "-1:?2"; /* minimum 1 arg, maximum 2 args */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000146 opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000147 argv += optind;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100148 if (opt & SILENT)
149 logmode = LOGMODE_NONE; /* suppress error_msg() output */
Steve Bennett823b6362010-04-07 19:16:12 +0200150 opt &= ~SILENT;
151 if (!(opt & (BROADCAST | NETWORK | NETPREFIX))) {
152 /* if no options at all or
153 * (no broadcast,network,prefix) and (two args)... */
154 if (!opt || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155 bb_show_usage();
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000156 }
157
Rob Landleyff97ee92006-06-02 19:03:01 +0000158 ipstr = argv[0];
159 if (ENABLE_FEATURE_IPCALC_FANCY) {
160 unsigned long netprefix = 0;
161 char *prefixstr;
Glenn L McGrath530ea422003-09-02 06:59:57 +0000162
Rob Landleyff97ee92006-06-02 19:03:01 +0000163 prefixstr = ipstr;
Glenn L McGrath14092a12003-09-12 00:44:50 +0000164
Denis Vlasenko13858992006-10-08 12:49:22 +0000165 while (*prefixstr) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000166 if (*prefixstr == '/') {
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100167 *prefixstr++ = '\0';
Rob Landleyff97ee92006-06-02 19:03:01 +0000168 if (*prefixstr) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000169 unsigned msk;
170 netprefix = xatoul_range(prefixstr, 0, 32);
Rob Landleyff97ee92006-06-02 19:03:01 +0000171 netmask = 0;
172 msk = 0x80000000;
173 while (netprefix > 0) {
174 netmask |= msk;
175 msk >>= 1;
176 netprefix--;
177 }
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 */
181 have_netmask = 1;
Eric Andersena4375042004-04-13 19:25:57 +0000182 }
Rob Landleyff97ee92006-06-02 19:03:01 +0000183 break;
Eric Andersena4375042004-04-13 19:25:57 +0000184 }
Rob Landleyff97ee92006-06-02 19:03:01 +0000185 prefixstr++;
Eric Andersena4375042004-04-13 19:25:57 +0000186 }
Eric Andersena4375042004-04-13 19:25:57 +0000187 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000188
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100189 if (inet_aton(ipstr, &s_ipaddr) == 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000190 bb_error_msg_and_die("bad IP address: %s", argv[0]);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000191 }
192
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100193 if (argv[1]) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000194 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000195 bb_error_msg_and_die("use prefix or netmask, not both");
Eric Andersena4375042004-04-13 19:25:57 +0000196 }
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100197 if (inet_aton(argv[1], &s_netmask) == 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000198 bb_error_msg_and_die("bad netmask: %s", argv[1]);
Eric Andersena4375042004-04-13 19:25:57 +0000199 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000200 } else {
Rob Landleyff97ee92006-06-02 19:03:01 +0000201 /* JHC - If the netmask wasn't provided then calculate it */
202 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
Eric Andersena4375042004-04-13 19:25:57 +0000203 netmask = get_netmask(ipaddr);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000204 }
205
Denis Vlasenko13858992006-10-08 12:49:22 +0000206 if (opt & NETMASK) {
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100207 printf("NETMASK=%s\n", inet_ntoa(s_netmask));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000208 }
209
Denis Vlasenko13858992006-10-08 12:49:22 +0000210 if (opt & BROADCAST) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000211 broadcast = (ipaddr & netmask) | ~netmask;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100212 printf("BROADCAST=%s\n", inet_ntoa(s_broadcast));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000213 }
214
Denis Vlasenko13858992006-10-08 12:49:22 +0000215 if (opt & NETWORK) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000216 network = ipaddr & netmask;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100217 printf("NETWORK=%s\n", inet_ntoa(s_network));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000218 }
Glenn L McGrath530ea422003-09-02 06:59:57 +0000219
Rob Landleyff97ee92006-06-02 19:03:01 +0000220 if (ENABLE_FEATURE_IPCALC_FANCY) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000221 if (opt & NETPREFIX) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000222 printf("PREFIX=%i\n", get_prefix(netmask));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000223 }
224
Denis Vlasenko13858992006-10-08 12:49:22 +0000225 if (opt & HOSTNAME) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000226 struct hostent *hostinfo;
Rob Landleyff97ee92006-06-02 19:03:01 +0000227
228 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
229 if (!hostinfo) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100230 bb_herror_msg_and_die("can't find hostname for %s", argv[0]);
Rob Landleyff97ee92006-06-02 19:03:01 +0000231 }
Denis Vlasenkofcc63472008-04-10 02:09:40 +0000232 str_tolower(hostinfo->h_name);
Denis Vlasenko5b3adae2008-04-19 21:57:57 +0000233
Rob Landleyff97ee92006-06-02 19:03:01 +0000234 printf("HOSTNAME=%s\n", hostinfo->h_name);
235 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000236 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000237
238 return EXIT_SUCCESS;
239}