blob: 3c8b8bfc910026c270161fd829178d61f7c623f6 [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 */
Pere Orga5bc8c002011-04-11 03:29:49 +020014
15//usage:#define ipcalc_trivial_usage
16//usage: "[OPTIONS] ADDRESS[[/]NETMASK] [NETMASK]"
17//usage:#define ipcalc_full_usage "\n\n"
18//usage: "Calculate IP network settings from a IP address\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020019//usage: IF_FEATURE_IPCALC_LONG_OPTIONS(
20//usage: "\n -b,--broadcast Display calculated broadcast address"
21//usage: "\n -n,--network Display calculated network address"
22//usage: "\n -m,--netmask Display default netmask for IP"
23//usage: IF_FEATURE_IPCALC_FANCY(
24//usage: "\n -p,--prefix Display the prefix for IP/NETMASK"
25//usage: "\n -h,--hostname Display first resolved host name"
26//usage: "\n -s,--silent Don't ever display error messages"
27//usage: )
28//usage: )
29//usage: IF_NOT_FEATURE_IPCALC_LONG_OPTIONS(
30//usage: "\n -b Display calculated broadcast address"
31//usage: "\n -n Display calculated network address"
32//usage: "\n -m Display default netmask for IP"
33//usage: IF_FEATURE_IPCALC_FANCY(
34//usage: "\n -p Display the prefix for IP/NETMASK"
35//usage: "\n -h Display first resolved host name"
36//usage: "\n -s Don't ever display error messages"
37//usage: )
38//usage: )
39
Dan Fandrichfdd7b562010-06-18 22:37:42 -070040#include "libbb.h"
Denys Vlasenko134d0eb2010-06-19 20:07:23 +020041/* After libbb.h, because on some systems it needs other includes */
Glenn L McGrathc11986d2002-11-10 23:42:27 +000042#include <arpa/inet.h>
43
Denys Vlasenkofb132e42010-10-29 11:46:52 +020044#define CLASS_A_NETMASK ntohl(0xFF000000)
45#define CLASS_B_NETMASK ntohl(0xFFFF0000)
46#define CLASS_C_NETMASK ntohl(0xFFFFFF00)
Eric Andersen7207b882003-07-05 07:59:30 +000047
Glenn L McGrathc11986d2002-11-10 23:42:27 +000048static unsigned long get_netmask(unsigned long ipaddr)
49{
Eric Andersen7207b882003-07-05 07:59:30 +000050 ipaddr = htonl(ipaddr);
51
52 if ((ipaddr & 0xC0000000) == 0xC0000000)
53 return CLASS_C_NETMASK;
54 else if ((ipaddr & 0x80000000) == 0x80000000)
55 return CLASS_B_NETMASK;
56 else if ((ipaddr & 0x80000000) == 0)
57 return CLASS_A_NETMASK;
58 else
59 return 0;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000060}
61
Denis Vlasenkoe3241842007-08-13 10:36:25 +000062#if ENABLE_FEATURE_IPCALC_FANCY
Glenn L McGrath530ea422003-09-02 06:59:57 +000063static int get_prefix(unsigned long netmask)
64{
Glenn L McGrath14092a12003-09-12 00:44:50 +000065 unsigned long msk = 0x80000000;
Eric Andersena4375042004-04-13 19:25:57 +000066 int ret = 0;
Glenn L McGrath530ea422003-09-02 06:59:57 +000067
Glenn L McGrath14092a12003-09-12 00:44:50 +000068 netmask = htonl(netmask);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +000069 while (msk) {
Glenn L McGrath14092a12003-09-12 00:44:50 +000070 if (netmask & msk)
71 ret++;
72 msk >>= 1;
Eric Andersena4375042004-04-13 19:25:57 +000073 }
74 return ret;
Glenn L McGrath530ea422003-09-02 06:59:57 +000075}
Rob Landleyff97ee92006-06-02 19:03:01 +000076#else
77int get_prefix(unsigned long netmask);
Glenn L McGrath530ea422003-09-02 06:59:57 +000078#endif
79
Denis Vlasenko13858992006-10-08 12:49:22 +000080
Glenn L McGrathc11986d2002-11-10 23:42:27 +000081#define NETMASK 0x01
82#define BROADCAST 0x02
83#define NETWORK 0x04
Glenn L McGrath530ea422003-09-02 06:59:57 +000084#define NETPREFIX 0x08
85#define HOSTNAME 0x10
Glenn L McGrathd6bdd5d2003-09-05 02:37:15 +000086#define SILENT 0x20
Glenn L McGrathc11986d2002-11-10 23:42:27 +000087
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000088#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000089 static const char ipcalc_longopts[] ALIGN1 =
Steve Bennett823b6362010-04-07 19:16:12 +020090 "netmask\0" No_argument "m" // netmask from IP (assuming complete class A, B, or C network)
91 "broadcast\0" No_argument "b" // broadcast from IP [netmask]
92 "network\0" No_argument "n" // network from IP [netmask]
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000093# if ENABLE_FEATURE_IPCALC_FANCY
Steve Bennett823b6362010-04-07 19:16:12 +020094 "prefix\0" No_argument "p" // prefix from IP[/prefix] [netmask]
95 "hostname\0" No_argument "h" // hostname from IP
96 "silent\0" No_argument "s" // don’t ever display error messages
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000097# endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +000098 ;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000099#endif
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000100
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000101int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100102int ipcalc_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyff97ee92006-06-02 19:03:01 +0000103{
Denis Vlasenko13858992006-10-08 12:49:22 +0000104 unsigned opt;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100105 bool have_netmask = 0;
106 struct in_addr s_netmask, s_broadcast, s_network, s_ipaddr;
107 /* struct in_addr { in_addr_t s_addr; } and in_addr_t
108 * (which in turn is just a typedef to uint32_t)
109 * are essentially the same type. A few macros for less verbosity: */
110#define netmask (s_netmask.s_addr)
111#define broadcast (s_broadcast.s_addr)
112#define network (s_network.s_addr)
113#define ipaddr (s_ipaddr.s_addr)
Rob Landleyff97ee92006-06-02 19:03:01 +0000114 char *ipstr;
115
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000116#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000117 applet_long_options = ipcalc_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000118#endif
Steve Bennett823b6362010-04-07 19:16:12 +0200119 opt_complementary = "-1:?2"; /* minimum 1 arg, maximum 2 args */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000120 opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000121 argv += optind;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100122 if (opt & SILENT)
123 logmode = LOGMODE_NONE; /* suppress error_msg() output */
Steve Bennett823b6362010-04-07 19:16:12 +0200124 opt &= ~SILENT;
125 if (!(opt & (BROADCAST | NETWORK | NETPREFIX))) {
126 /* if no options at all or
127 * (no broadcast,network,prefix) and (two args)... */
128 if (!opt || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 bb_show_usage();
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000130 }
131
Rob Landleyff97ee92006-06-02 19:03:01 +0000132 ipstr = argv[0];
133 if (ENABLE_FEATURE_IPCALC_FANCY) {
134 unsigned long netprefix = 0;
135 char *prefixstr;
Glenn L McGrath530ea422003-09-02 06:59:57 +0000136
Rob Landleyff97ee92006-06-02 19:03:01 +0000137 prefixstr = ipstr;
Glenn L McGrath14092a12003-09-12 00:44:50 +0000138
Denis Vlasenko13858992006-10-08 12:49:22 +0000139 while (*prefixstr) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000140 if (*prefixstr == '/') {
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100141 *prefixstr++ = '\0';
Rob Landleyff97ee92006-06-02 19:03:01 +0000142 if (*prefixstr) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000143 unsigned msk;
144 netprefix = xatoul_range(prefixstr, 0, 32);
Rob Landleyff97ee92006-06-02 19:03:01 +0000145 netmask = 0;
146 msk = 0x80000000;
147 while (netprefix > 0) {
148 netmask |= msk;
149 msk >>= 1;
150 netprefix--;
151 }
152 netmask = htonl(netmask);
153 /* Even if it was 0, we will signify that we have a netmask. This allows */
154 /* for specification of default routes, etc which have a 0 netmask/prefix */
155 have_netmask = 1;
Eric Andersena4375042004-04-13 19:25:57 +0000156 }
Rob Landleyff97ee92006-06-02 19:03:01 +0000157 break;
Eric Andersena4375042004-04-13 19:25:57 +0000158 }
Rob Landleyff97ee92006-06-02 19:03:01 +0000159 prefixstr++;
Eric Andersena4375042004-04-13 19:25:57 +0000160 }
Eric Andersena4375042004-04-13 19:25:57 +0000161 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000162
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100163 if (inet_aton(ipstr, &s_ipaddr) == 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000164 bb_error_msg_and_die("bad IP address: %s", argv[0]);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000165 }
166
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100167 if (argv[1]) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000168 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000169 bb_error_msg_and_die("use prefix or netmask, not both");
Eric Andersena4375042004-04-13 19:25:57 +0000170 }
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100171 if (inet_aton(argv[1], &s_netmask) == 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000172 bb_error_msg_and_die("bad netmask: %s", argv[1]);
Eric Andersena4375042004-04-13 19:25:57 +0000173 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000174 } else {
Rob Landleyff97ee92006-06-02 19:03:01 +0000175 /* JHC - If the netmask wasn't provided then calculate it */
176 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
Eric Andersena4375042004-04-13 19:25:57 +0000177 netmask = get_netmask(ipaddr);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000178 }
179
Denis Vlasenko13858992006-10-08 12:49:22 +0000180 if (opt & NETMASK) {
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100181 printf("NETMASK=%s\n", inet_ntoa(s_netmask));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000182 }
183
Denis Vlasenko13858992006-10-08 12:49:22 +0000184 if (opt & BROADCAST) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000185 broadcast = (ipaddr & netmask) | ~netmask;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100186 printf("BROADCAST=%s\n", inet_ntoa(s_broadcast));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000187 }
188
Denis Vlasenko13858992006-10-08 12:49:22 +0000189 if (opt & NETWORK) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000190 network = ipaddr & netmask;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100191 printf("NETWORK=%s\n", inet_ntoa(s_network));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000192 }
Glenn L McGrath530ea422003-09-02 06:59:57 +0000193
Rob Landleyff97ee92006-06-02 19:03:01 +0000194 if (ENABLE_FEATURE_IPCALC_FANCY) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000195 if (opt & NETPREFIX) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000196 printf("PREFIX=%i\n", get_prefix(netmask));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000197 }
198
Denis Vlasenko13858992006-10-08 12:49:22 +0000199 if (opt & HOSTNAME) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000200 struct hostent *hostinfo;
Rob Landleyff97ee92006-06-02 19:03:01 +0000201
202 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
203 if (!hostinfo) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100204 bb_herror_msg_and_die("can't find hostname for %s", argv[0]);
Rob Landleyff97ee92006-06-02 19:03:01 +0000205 }
Denis Vlasenkofcc63472008-04-10 02:09:40 +0000206 str_tolower(hostinfo->h_name);
Denis Vlasenko5b3adae2008-04-19 21:57:57 +0000207
Rob Landleyff97ee92006-06-02 19:03:01 +0000208 printf("HOSTNAME=%s\n", hostinfo->h_name);
209 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000210 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000211
212 return EXIT_SUCCESS;
213}