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