Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4 ai: */ |
| 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 | * |
| 12 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <ctype.h> |
| 18 | #include <getopt.h> |
| 19 | #include <sys/socket.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <arpa/inet.h> |
| 22 | |
| 23 | #include "busybox.h" |
| 24 | |
| 25 | #define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;} |
| 26 | |
Eric Andersen | 7207b88 | 2003-07-05 07:59:30 +0000 | [diff] [blame] | 27 | #define CLASS_A_NETMASK ntohl(0xFF000000) |
| 28 | #define CLASS_B_NETMASK ntohl(0xFFFF0000) |
| 29 | #define CLASS_C_NETMASK ntohl(0xFFFFFF00) |
| 30 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 31 | static unsigned long get_netmask(unsigned long ipaddr) |
| 32 | { |
Eric Andersen | 7207b88 | 2003-07-05 07:59:30 +0000 | [diff] [blame] | 33 | ipaddr = htonl(ipaddr); |
| 34 | |
| 35 | if ((ipaddr & 0xC0000000) == 0xC0000000) |
| 36 | return CLASS_C_NETMASK; |
| 37 | else if ((ipaddr & 0x80000000) == 0x80000000) |
| 38 | return CLASS_B_NETMASK; |
| 39 | else if ((ipaddr & 0x80000000) == 0) |
| 40 | return CLASS_A_NETMASK; |
| 41 | else |
| 42 | return 0; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 45 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
| 46 | static int get_prefix(unsigned long netmask) |
| 47 | { |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 48 | unsigned long msk = 0x80000000; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 49 | int ret = 0; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 50 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 51 | netmask = htonl(netmask); |
| 52 | while(msk) { |
| 53 | if (netmask & msk) |
| 54 | ret++; |
| 55 | msk >>= 1; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 56 | } |
| 57 | return ret; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 58 | } |
| 59 | #endif |
| 60 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 61 | #define NETMASK 0x01 |
| 62 | #define BROADCAST 0x02 |
| 63 | #define NETWORK 0x04 |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 64 | #define NETPREFIX 0x08 |
| 65 | #define HOSTNAME 0x10 |
Glenn L McGrath | d6bdd5d | 2003-09-05 02:37:15 +0000 | [diff] [blame] | 66 | #define SILENT 0x20 |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 67 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 68 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 69 | int ipcalc_main(int argc, char **argv) |
| 70 | { |
Eric Andersen | e968ee3 | 2003-07-03 10:28:07 +0000 | [diff] [blame] | 71 | unsigned long mode; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 72 | |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 73 | in_addr_t netmask; |
| 74 | in_addr_t broadcast; |
| 75 | in_addr_t network; |
| 76 | in_addr_t ipaddr; |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 77 | struct in_addr a; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 78 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 79 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 80 | unsigned long netprefix = 0; |
| 81 | int have_netmask = 0; |
| 82 | char *ipstr, *prefixstr; |
| 83 | #endif |
| 84 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 85 | #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 86 | static const struct option long_options[] = { |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 87 | {"netmask", no_argument, NULL, 'm'}, |
| 88 | {"broadcast", no_argument, NULL, 'b'}, |
| 89 | {"network", no_argument, NULL, 'n'}, |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 90 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 91 | {"prefix", no_argument, NULL, 'p'}, |
| 92 | {"hostname", no_argument, NULL, 'h'}, |
| 93 | {"silent", no_argument, NULL, 's'}, |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 94 | #endif |
| 95 | {NULL, 0, NULL, 0} |
| 96 | }; |
| 97 | |
Eric Andersen | e968ee3 | 2003-07-03 10:28:07 +0000 | [diff] [blame] | 98 | bb_applet_long_options = long_options; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 99 | #endif |
Eric Andersen | e968ee3 | 2003-07-03 10:28:07 +0000 | [diff] [blame] | 100 | mode = bb_getopt_ulflags(argc, argv, |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 101 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 102 | "mbnphs" |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 103 | #else |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 104 | "mbn" |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 105 | #endif |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 106 | ); |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 107 | |
| 108 | argc -= optind; |
| 109 | argv += optind; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 110 | if (mode & (BROADCAST | NETWORK | NETPREFIX)) { |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 111 | if (argc > 2 || argc <= 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 112 | bb_show_usage(); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 113 | } else { |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 114 | if (argc != 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 115 | bb_show_usage(); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 118 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 119 | prefixstr = ipstr = argv[0]; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 120 | |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 121 | while(*prefixstr) { |
| 122 | if (*prefixstr == '/') { |
| 123 | *prefixstr = (char)0; |
| 124 | prefixstr++; |
| 125 | if (*prefixstr) { |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 126 | unsigned int msk; |
| 127 | |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 128 | if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) { |
| 129 | IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr), |
| 130 | exit(EXIT_FAILURE)); |
| 131 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 132 | netmask = 0; |
| 133 | msk = 0x80000000; |
| 134 | while (netprefix > 0) { |
| 135 | netmask |= msk; |
| 136 | msk >>= 1; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 137 | netprefix--; |
| 138 | } |
| 139 | netmask = htonl(netmask); |
| 140 | /* Even if it was 0, we will signify that we have a netmask. This allows */ |
| 141 | /* for specification of default routes, etc which have a 0 netmask/prefix */ |
| 142 | have_netmask = 1; |
| 143 | } |
| 144 | break; |
| 145 | } |
| 146 | prefixstr++; |
| 147 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 148 | ipaddr = inet_aton(ipstr, &a); |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 149 | #else |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 150 | ipaddr = inet_aton(argv[0], &a); |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 151 | #endif |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 152 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 153 | if (ipaddr == 0) { |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 154 | IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]), |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 155 | exit(EXIT_FAILURE)); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 156 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 157 | ipaddr = a.s_addr; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 158 | |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 159 | if (argc == 2) { |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 160 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 161 | if (have_netmask) { |
| 162 | IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"), |
| 163 | exit(EXIT_FAILURE)); |
| 164 | } |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 165 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 166 | #endif |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 167 | netmask = inet_aton(argv[1], &a); |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 168 | if (netmask == 0) { |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 169 | IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]), |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 170 | exit(EXIT_FAILURE)); |
| 171 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 172 | netmask = a.s_addr; |
| 173 | } else { |
| 174 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 175 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 176 | if (!have_netmask) |
| 177 | #endif |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 178 | /* JHC - If the netmask wasn't provided then calculate it */ |
| 179 | netmask = get_netmask(ipaddr); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (mode & NETMASK) { |
| 183 | printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask))); |
| 184 | } |
| 185 | |
| 186 | if (mode & BROADCAST) { |
| 187 | broadcast = (ipaddr & netmask) | ~netmask; |
| 188 | printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast))); |
| 189 | } |
| 190 | |
| 191 | if (mode & NETWORK) { |
| 192 | network = ipaddr & netmask; |
| 193 | printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network))); |
| 194 | } |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 195 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 196 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 197 | if (mode & NETPREFIX) { |
| 198 | printf("PREFIX=%i\n", get_prefix(netmask)); |
| 199 | } |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 200 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 201 | if (mode & HOSTNAME) { |
| 202 | struct hostent *hostinfo; |
| 203 | int x; |
| 204 | |
| 205 | hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); |
| 206 | if (!hostinfo) { |
Eric Andersen | e968ee3 | 2003-07-03 10:28:07 +0000 | [diff] [blame] | 207 | IPCALC_MSG(bb_herror_msg_and_die( |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 208 | "cannot find hostname for %s", argv[0]),); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 209 | exit(EXIT_FAILURE); |
| 210 | } |
| 211 | for (x = 0; hostinfo->h_name[x]; x++) { |
| 212 | hostinfo->h_name[x] = tolower(hostinfo->h_name[x]); |
| 213 | } |
| 214 | |
| 215 | printf("HOSTNAME=%s\n", hostinfo->h_name); |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | return EXIT_SUCCESS; |
| 220 | } |