"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini ipcalc implementation for busybox |
| 4 | * |
| 5 | * By Jordan Crouse <jordan@cosmicpenguin.net> |
| 6 | * Stephan Linz <linz@li-pro.net> |
| 7 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 8 | * 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 McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 10 | * is no denying that this is a loving reimplementation |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 11 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 12 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 13 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 14 | //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: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 21 | //config:config FEATURE_IPCALC_LONG_OPTIONS |
| 22 | //config: bool "Enable long options" |
| 23 | //config: default y |
| 24 | //config: depends on IPCALC && LONG_OPTS |
| 25 | //config: |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 26 | //config:config FEATURE_IPCALC_FANCY |
| 27 | //config: bool "Fancy IPCALC, more options, adds 1 kbyte" |
| 28 | //config: default y |
| 29 | //config: depends on IPCALC |
| 30 | //config: help |
| 31 | //config: Adds the options hostname, prefix and silent to the output of |
| 32 | //config: "ipcalc". |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 33 | |
| 34 | //applet:IF_IPCALC(APPLET(ipcalc, BB_DIR_BIN, BB_SUID_DROP)) |
| 35 | |
| 36 | //kbuild:lib-$(CONFIG_IPCALC) += ipcalc.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 37 | |
| 38 | //usage:#define ipcalc_trivial_usage |
Ron Yorston | 72dcbe4 | 2015-07-12 21:19:28 +0100 | [diff] [blame] | 39 | //usage: "[OPTIONS] ADDRESS" |
| 40 | //usage: IF_FEATURE_IPCALC_FANCY("[/PREFIX]") " [NETMASK]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 41 | //usage:#define ipcalc_full_usage "\n\n" |
| 42 | //usage: "Calculate IP network settings from a IP address\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 43 | //usage: IF_FEATURE_IPCALC_LONG_OPTIONS( |
| 44 | //usage: "\n -b,--broadcast Display calculated broadcast address" |
| 45 | //usage: "\n -n,--network Display calculated network address" |
| 46 | //usage: "\n -m,--netmask Display default netmask for IP" |
| 47 | //usage: IF_FEATURE_IPCALC_FANCY( |
| 48 | //usage: "\n -p,--prefix Display the prefix for IP/NETMASK" |
| 49 | //usage: "\n -h,--hostname Display first resolved host name" |
| 50 | //usage: "\n -s,--silent Don't ever display error messages" |
| 51 | //usage: ) |
| 52 | //usage: ) |
| 53 | //usage: IF_NOT_FEATURE_IPCALC_LONG_OPTIONS( |
| 54 | //usage: "\n -b Display calculated broadcast address" |
| 55 | //usage: "\n -n Display calculated network address" |
| 56 | //usage: "\n -m Display default netmask for IP" |
| 57 | //usage: IF_FEATURE_IPCALC_FANCY( |
| 58 | //usage: "\n -p Display the prefix for IP/NETMASK" |
| 59 | //usage: "\n -h Display first resolved host name" |
| 60 | //usage: "\n -s Don't ever display error messages" |
| 61 | //usage: ) |
| 62 | //usage: ) |
| 63 | |
Dan Fandrich | fdd7b56 | 2010-06-18 22:37:42 -0700 | [diff] [blame] | 64 | #include "libbb.h" |
Denys Vlasenko | 134d0eb | 2010-06-19 20:07:23 +0200 | [diff] [blame] | 65 | /* After libbb.h, because on some systems it needs other includes */ |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 66 | #include <arpa/inet.h> |
| 67 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 68 | #define CLASS_A_NETMASK ntohl(0xFF000000) |
| 69 | #define CLASS_B_NETMASK ntohl(0xFFFF0000) |
| 70 | #define CLASS_C_NETMASK ntohl(0xFFFFFF00) |
Eric Andersen | 7207b88 | 2003-07-05 07:59:30 +0000 | [diff] [blame] | 71 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 72 | static unsigned long get_netmask(unsigned long ipaddr) |
| 73 | { |
Eric Andersen | 7207b88 | 2003-07-05 07:59:30 +0000 | [diff] [blame] | 74 | ipaddr = htonl(ipaddr); |
| 75 | |
| 76 | if ((ipaddr & 0xC0000000) == 0xC0000000) |
| 77 | return CLASS_C_NETMASK; |
| 78 | else if ((ipaddr & 0x80000000) == 0x80000000) |
| 79 | return CLASS_B_NETMASK; |
| 80 | else if ((ipaddr & 0x80000000) == 0) |
| 81 | return CLASS_A_NETMASK; |
| 82 | else |
| 83 | return 0; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 86 | #if ENABLE_FEATURE_IPCALC_FANCY |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 87 | static int get_prefix(unsigned long netmask) |
| 88 | { |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 89 | unsigned long msk = 0x80000000; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 90 | int ret = 0; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 91 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 92 | netmask = htonl(netmask); |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 93 | while (msk) { |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 94 | if (netmask & msk) |
| 95 | ret++; |
| 96 | msk >>= 1; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 97 | } |
| 98 | return ret; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 99 | } |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 100 | #else |
| 101 | int get_prefix(unsigned long netmask); |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 102 | #endif |
| 103 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 104 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 105 | #define NETMASK 0x01 |
| 106 | #define BROADCAST 0x02 |
| 107 | #define NETWORK 0x04 |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 108 | #define NETPREFIX 0x08 |
| 109 | #define HOSTNAME 0x10 |
Glenn L McGrath | d6bdd5d | 2003-09-05 02:37:15 +0000 | [diff] [blame] | 110 | #define SILENT 0x20 |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 111 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 112 | #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 113 | static const char ipcalc_longopts[] ALIGN1 = |
Steve Bennett | 823b636 | 2010-04-07 19:16:12 +0200 | [diff] [blame] | 114 | "netmask\0" No_argument "m" // netmask from IP (assuming complete class A, B, or C network) |
| 115 | "broadcast\0" No_argument "b" // broadcast from IP [netmask] |
| 116 | "network\0" No_argument "n" // network from IP [netmask] |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 117 | # if ENABLE_FEATURE_IPCALC_FANCY |
Steve Bennett | 823b636 | 2010-04-07 19:16:12 +0200 | [diff] [blame] | 118 | "prefix\0" No_argument "p" // prefix from IP[/prefix] [netmask] |
| 119 | "hostname\0" No_argument "h" // hostname from IP |
| 120 | "silent\0" No_argument "s" // don’t ever display error messages |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 121 | # endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 122 | ; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 123 | #endif |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 125 | int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 126 | int ipcalc_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 127 | { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 128 | unsigned opt; |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 129 | bool have_netmask = 0; |
| 130 | struct in_addr s_netmask, s_broadcast, s_network, s_ipaddr; |
| 131 | /* struct in_addr { in_addr_t s_addr; } and in_addr_t |
| 132 | * (which in turn is just a typedef to uint32_t) |
| 133 | * are essentially the same type. A few macros for less verbosity: */ |
| 134 | #define netmask (s_netmask.s_addr) |
| 135 | #define broadcast (s_broadcast.s_addr) |
| 136 | #define network (s_network.s_addr) |
| 137 | #define ipaddr (s_ipaddr.s_addr) |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 138 | char *ipstr; |
| 139 | |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 140 | #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 141 | applet_long_options = ipcalc_longopts; |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 142 | #endif |
Steve Bennett | 823b636 | 2010-04-07 19:16:12 +0200 | [diff] [blame] | 143 | opt_complementary = "-1:?2"; /* minimum 1 arg, maximum 2 args */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 144 | opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs")); |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 145 | argv += optind; |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 146 | if (opt & SILENT) |
| 147 | logmode = LOGMODE_NONE; /* suppress error_msg() output */ |
Steve Bennett | 823b636 | 2010-04-07 19:16:12 +0200 | [diff] [blame] | 148 | opt &= ~SILENT; |
| 149 | if (!(opt & (BROADCAST | NETWORK | NETPREFIX))) { |
| 150 | /* if no options at all or |
| 151 | * (no broadcast,network,prefix) and (two args)... */ |
| 152 | if (!opt || argv[1]) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 153 | bb_show_usage(); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 156 | ipstr = argv[0]; |
| 157 | if (ENABLE_FEATURE_IPCALC_FANCY) { |
| 158 | unsigned long netprefix = 0; |
| 159 | char *prefixstr; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 160 | |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 161 | prefixstr = ipstr; |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 163 | while (*prefixstr) { |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 164 | if (*prefixstr == '/') { |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 165 | *prefixstr++ = '\0'; |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 166 | if (*prefixstr) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 167 | unsigned msk; |
| 168 | netprefix = xatoul_range(prefixstr, 0, 32); |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 169 | netmask = 0; |
| 170 | msk = 0x80000000; |
| 171 | while (netprefix > 0) { |
| 172 | netmask |= msk; |
| 173 | msk >>= 1; |
| 174 | netprefix--; |
| 175 | } |
| 176 | netmask = htonl(netmask); |
| 177 | /* Even if it was 0, we will signify that we have a netmask. This allows */ |
| 178 | /* for specification of default routes, etc which have a 0 netmask/prefix */ |
| 179 | have_netmask = 1; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 180 | } |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 181 | break; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 182 | } |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 183 | prefixstr++; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 184 | } |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 185 | } |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 186 | |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 187 | if (inet_aton(ipstr, &s_ipaddr) == 0) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 188 | bb_error_msg_and_die("bad IP address: %s", argv[0]); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 191 | if (argv[1]) { |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 192 | if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 193 | bb_error_msg_and_die("use prefix or netmask, not both"); |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 194 | } |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 195 | if (inet_aton(argv[1], &s_netmask) == 0) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 196 | bb_error_msg_and_die("bad netmask: %s", argv[1]); |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 197 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 198 | } else { |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 199 | /* JHC - If the netmask wasn't provided then calculate it */ |
| 200 | if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask) |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 201 | netmask = get_netmask(ipaddr); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 204 | if (opt & NETMASK) { |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 205 | printf("NETMASK=%s\n", inet_ntoa(s_netmask)); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 208 | if (opt & BROADCAST) { |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 209 | broadcast = (ipaddr & netmask) | ~netmask; |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 210 | printf("BROADCAST=%s\n", inet_ntoa(s_broadcast)); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 213 | if (opt & NETWORK) { |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 214 | network = ipaddr & netmask; |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 215 | printf("NETWORK=%s\n", inet_ntoa(s_network)); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 216 | } |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 217 | |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 218 | if (ENABLE_FEATURE_IPCALC_FANCY) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 219 | if (opt & NETPREFIX) { |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 220 | printf("PREFIX=%i\n", get_prefix(netmask)); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 223 | if (opt & HOSTNAME) { |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 224 | struct hostent *hostinfo; |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 225 | |
| 226 | hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); |
| 227 | if (!hostinfo) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 228 | bb_herror_msg_and_die("can't find hostname for %s", argv[0]); |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 229 | } |
Denis Vlasenko | fcc6347 | 2008-04-10 02:09:40 +0000 | [diff] [blame] | 230 | str_tolower(hostinfo->h_name); |
Denis Vlasenko | 5b3adae | 2008-04-19 21:57:57 +0000 | [diff] [blame] | 231 | |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 232 | printf("HOSTNAME=%s\n", hostinfo->h_name); |
| 233 | } |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 234 | } |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 235 | |
| 236 | return EXIT_SUCCESS; |
| 237 | } |