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 tarball for details.
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
20 #define CLASS_A_NETMASK ntohl(0xFF000000)
21 #define CLASS_B_NETMASK ntohl(0xFFFF0000)
22 #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
24 static unsigned long get_netmask(unsigned long ipaddr)
26 ipaddr = htonl(ipaddr);
28 if ((ipaddr & 0xC0000000) == 0xC0000000)
29 return CLASS_C_NETMASK;
30 else if ((ipaddr & 0x80000000) == 0x80000000)
31 return CLASS_B_NETMASK;
32 else if ((ipaddr & 0x80000000) == 0)
33 return CLASS_A_NETMASK;
38 #if ENABLE_FEATURE_IPCALC_FANCY
39 static int get_prefix(unsigned long netmask)
41 unsigned long msk = 0x80000000;
44 netmask = htonl(netmask);
53 int get_prefix(unsigned long netmask);
58 #define BROADCAST 0x02
60 #define NETPREFIX 0x08
64 #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
65 static const char ipcalc_longopts[] ALIGN1 =
66 "netmask\0" No_argument "m"
67 "broadcast\0" No_argument "b"
68 "network\0" No_argument "n"
69 # if ENABLE_FEATURE_IPCALC_FANCY
70 "prefix\0" No_argument "p"
71 "hostname\0" No_argument "h"
72 "silent\0" No_argument "s"
77 int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78 int ipcalc_main(int argc, char **argv)
82 in_addr_t netmask, broadcast, network, ipaddr;
86 #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
87 applet_long_options = ipcalc_longopts;
89 opt = getopt32(argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
92 if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
93 if (argc > 2 || argc <= 0)
100 logmode = LOGMODE_NONE; /* Suppress error_msg() output */
103 if (ENABLE_FEATURE_IPCALC_FANCY) {
104 unsigned long netprefix = 0;
110 if (*prefixstr == '/') {
111 *prefixstr = (char)0;
115 netprefix = xatoul_range(prefixstr, 0, 32);
118 while (netprefix > 0) {
123 netmask = htonl(netmask);
124 /* Even if it was 0, we will signify that we have a netmask. This allows */
125 /* for specification of default routes, etc which have a 0 netmask/prefix */
133 ipaddr = inet_aton(ipstr, &a);
136 bb_error_msg_and_die("bad IP address: %s", argv[0]);
141 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
142 bb_error_msg_and_die("use prefix or netmask, not both");
145 netmask = inet_aton(argv[1], &a);
147 bb_error_msg_and_die("bad netmask: %s", argv[1]);
152 /* JHC - If the netmask wasn't provided then calculate it */
153 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
154 netmask = get_netmask(ipaddr);
158 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
161 if (opt & BROADCAST) {
162 broadcast = (ipaddr & netmask) | ~netmask;
163 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
167 network = ipaddr & netmask;
168 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
171 if (ENABLE_FEATURE_IPCALC_FANCY) {
172 if (opt & NETPREFIX) {
173 printf("PREFIX=%i\n", get_prefix(netmask));
176 if (opt & HOSTNAME) {
177 struct hostent *hostinfo;
179 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
181 bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);
183 str_tolower(hostinfo->h_name);
185 printf("HOSTNAME=%s\n", hostinfo->h_name);