blob: 265009ad8552e5abd81fbc1daa51c2b77dd43b0e [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 *
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathc11986d2002-11-10 23:42:27 +000013 */
Dan Fandrichfdd7b562010-06-18 22:37:42 -070014#include "libbb.h"
Denys Vlasenko134d0eb2010-06-19 20:07:23 +020015/* After libbb.h, because on some systems it needs other includes */
Glenn L McGrathc11986d2002-11-10 23:42:27 +000016#include <arpa/inet.h>
17
Eric Andersen7207b882003-07-05 07:59:30 +000018#define CLASS_A_NETMASK ntohl(0xFF000000)
19#define CLASS_B_NETMASK ntohl(0xFFFF0000)
20#define CLASS_C_NETMASK ntohl(0xFFFFFF00)
21
Glenn L McGrathc11986d2002-11-10 23:42:27 +000022static unsigned long get_netmask(unsigned long ipaddr)
23{
Eric Andersen7207b882003-07-05 07:59:30 +000024 ipaddr = htonl(ipaddr);
25
26 if ((ipaddr & 0xC0000000) == 0xC0000000)
27 return CLASS_C_NETMASK;
28 else if ((ipaddr & 0x80000000) == 0x80000000)
29 return CLASS_B_NETMASK;
30 else if ((ipaddr & 0x80000000) == 0)
31 return CLASS_A_NETMASK;
32 else
33 return 0;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000034}
35
Denis Vlasenkoe3241842007-08-13 10:36:25 +000036#if ENABLE_FEATURE_IPCALC_FANCY
Glenn L McGrath530ea422003-09-02 06:59:57 +000037static int get_prefix(unsigned long netmask)
38{
Glenn L McGrath14092a12003-09-12 00:44:50 +000039 unsigned long msk = 0x80000000;
Eric Andersena4375042004-04-13 19:25:57 +000040 int ret = 0;
Glenn L McGrath530ea422003-09-02 06:59:57 +000041
Glenn L McGrath14092a12003-09-12 00:44:50 +000042 netmask = htonl(netmask);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +000043 while (msk) {
Glenn L McGrath14092a12003-09-12 00:44:50 +000044 if (netmask & msk)
45 ret++;
46 msk >>= 1;
Eric Andersena4375042004-04-13 19:25:57 +000047 }
48 return ret;
Glenn L McGrath530ea422003-09-02 06:59:57 +000049}
Rob Landleyff97ee92006-06-02 19:03:01 +000050#else
51int get_prefix(unsigned long netmask);
Glenn L McGrath530ea422003-09-02 06:59:57 +000052#endif
53
Denis Vlasenko13858992006-10-08 12:49:22 +000054
Glenn L McGrathc11986d2002-11-10 23:42:27 +000055#define NETMASK 0x01
56#define BROADCAST 0x02
57#define NETWORK 0x04
Glenn L McGrath530ea422003-09-02 06:59:57 +000058#define NETPREFIX 0x08
59#define HOSTNAME 0x10
Glenn L McGrathd6bdd5d2003-09-05 02:37:15 +000060#define SILENT 0x20
Glenn L McGrathc11986d2002-11-10 23:42:27 +000061
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000062#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000063 static const char ipcalc_longopts[] ALIGN1 =
Steve Bennett823b6362010-04-07 19:16:12 +020064 "netmask\0" No_argument "m" // netmask from IP (assuming complete class A, B, or C network)
65 "broadcast\0" No_argument "b" // broadcast from IP [netmask]
66 "network\0" No_argument "n" // network from IP [netmask]
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000067# if ENABLE_FEATURE_IPCALC_FANCY
Steve Bennett823b6362010-04-07 19:16:12 +020068 "prefix\0" No_argument "p" // prefix from IP[/prefix] [netmask]
69 "hostname\0" No_argument "h" // hostname from IP
70 "silent\0" No_argument "s" // don’t ever display error messages
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000071# endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +000072 ;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000073#endif
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000074
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000075int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko12ca0802010-02-04 18:41:18 +010076int ipcalc_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyff97ee92006-06-02 19:03:01 +000077{
Denis Vlasenko13858992006-10-08 12:49:22 +000078 unsigned opt;
Denys Vlasenko12ca0802010-02-04 18:41:18 +010079 bool have_netmask = 0;
80 struct in_addr s_netmask, s_broadcast, s_network, s_ipaddr;
81 /* struct in_addr { in_addr_t s_addr; } and in_addr_t
82 * (which in turn is just a typedef to uint32_t)
83 * are essentially the same type. A few macros for less verbosity: */
84#define netmask (s_netmask.s_addr)
85#define broadcast (s_broadcast.s_addr)
86#define network (s_network.s_addr)
87#define ipaddr (s_ipaddr.s_addr)
Rob Landleyff97ee92006-06-02 19:03:01 +000088 char *ipstr;
89
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000090#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000091 applet_long_options = ipcalc_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000092#endif
Steve Bennett823b6362010-04-07 19:16:12 +020093 opt_complementary = "-1:?2"; /* minimum 1 arg, maximum 2 args */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000094 opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
Mike Frysingerb7f88e02005-09-24 23:48:18 +000095 argv += optind;
Denys Vlasenko12ca0802010-02-04 18:41:18 +010096 if (opt & SILENT)
97 logmode = LOGMODE_NONE; /* suppress error_msg() output */
Steve Bennett823b6362010-04-07 19:16:12 +020098 opt &= ~SILENT;
99 if (!(opt & (BROADCAST | NETWORK | NETPREFIX))) {
100 /* if no options at all or
101 * (no broadcast,network,prefix) and (two args)... */
102 if (!opt || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 bb_show_usage();
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000104 }
105
Rob Landleyff97ee92006-06-02 19:03:01 +0000106 ipstr = argv[0];
107 if (ENABLE_FEATURE_IPCALC_FANCY) {
108 unsigned long netprefix = 0;
109 char *prefixstr;
Glenn L McGrath530ea422003-09-02 06:59:57 +0000110
Rob Landleyff97ee92006-06-02 19:03:01 +0000111 prefixstr = ipstr;
Glenn L McGrath14092a12003-09-12 00:44:50 +0000112
Denis Vlasenko13858992006-10-08 12:49:22 +0000113 while (*prefixstr) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000114 if (*prefixstr == '/') {
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100115 *prefixstr++ = '\0';
Rob Landleyff97ee92006-06-02 19:03:01 +0000116 if (*prefixstr) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000117 unsigned msk;
118 netprefix = xatoul_range(prefixstr, 0, 32);
Rob Landleyff97ee92006-06-02 19:03:01 +0000119 netmask = 0;
120 msk = 0x80000000;
121 while (netprefix > 0) {
122 netmask |= msk;
123 msk >>= 1;
124 netprefix--;
125 }
126 netmask = htonl(netmask);
127 /* Even if it was 0, we will signify that we have a netmask. This allows */
128 /* for specification of default routes, etc which have a 0 netmask/prefix */
129 have_netmask = 1;
Eric Andersena4375042004-04-13 19:25:57 +0000130 }
Rob Landleyff97ee92006-06-02 19:03:01 +0000131 break;
Eric Andersena4375042004-04-13 19:25:57 +0000132 }
Rob Landleyff97ee92006-06-02 19:03:01 +0000133 prefixstr++;
Eric Andersena4375042004-04-13 19:25:57 +0000134 }
Eric Andersena4375042004-04-13 19:25:57 +0000135 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000136
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100137 if (inet_aton(ipstr, &s_ipaddr) == 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000138 bb_error_msg_and_die("bad IP address: %s", argv[0]);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000139 }
140
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100141 if (argv[1]) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000142 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000143 bb_error_msg_and_die("use prefix or netmask, not both");
Eric Andersena4375042004-04-13 19:25:57 +0000144 }
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100145 if (inet_aton(argv[1], &s_netmask) == 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000146 bb_error_msg_and_die("bad netmask: %s", argv[1]);
Eric Andersena4375042004-04-13 19:25:57 +0000147 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000148 } else {
Rob Landleyff97ee92006-06-02 19:03:01 +0000149 /* JHC - If the netmask wasn't provided then calculate it */
150 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
Eric Andersena4375042004-04-13 19:25:57 +0000151 netmask = get_netmask(ipaddr);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000152 }
153
Denis Vlasenko13858992006-10-08 12:49:22 +0000154 if (opt & NETMASK) {
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100155 printf("NETMASK=%s\n", inet_ntoa(s_netmask));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000156 }
157
Denis Vlasenko13858992006-10-08 12:49:22 +0000158 if (opt & BROADCAST) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000159 broadcast = (ipaddr & netmask) | ~netmask;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100160 printf("BROADCAST=%s\n", inet_ntoa(s_broadcast));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000161 }
162
Denis Vlasenko13858992006-10-08 12:49:22 +0000163 if (opt & NETWORK) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000164 network = ipaddr & netmask;
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100165 printf("NETWORK=%s\n", inet_ntoa(s_network));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000166 }
Glenn L McGrath530ea422003-09-02 06:59:57 +0000167
Rob Landleyff97ee92006-06-02 19:03:01 +0000168 if (ENABLE_FEATURE_IPCALC_FANCY) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000169 if (opt & NETPREFIX) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000170 printf("PREFIX=%i\n", get_prefix(netmask));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000171 }
172
Denis Vlasenko13858992006-10-08 12:49:22 +0000173 if (opt & HOSTNAME) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000174 struct hostent *hostinfo;
Rob Landleyff97ee92006-06-02 19:03:01 +0000175
176 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
177 if (!hostinfo) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100178 bb_herror_msg_and_die("can't find hostname for %s", argv[0]);
Rob Landleyff97ee92006-06-02 19:03:01 +0000179 }
Denis Vlasenkofcc63472008-04-10 02:09:40 +0000180 str_tolower(hostinfo->h_name);
Denis Vlasenko5b3adae2008-04-19 21:57:57 +0000181
Rob Landleyff97ee92006-06-02 19:03:01 +0000182 printf("HOSTNAME=%s\n", hostinfo->h_name);
183 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000184 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000185
186 return EXIT_SUCCESS;
187}