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 | * |
| 8 | * This is a complete reimplentation of the ipcalc program |
| 9 | * from Redhat. I didn't look at their source code, but there |
| 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; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 47 | int ret = 0; |
| 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; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 54 | } |
| 55 | return ret; |
| 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 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 71 | unsigned long netmask; |
Eric Andersen | e968ee3 | 2003-07-03 10:28:07 +0000 | [diff] [blame] | 72 | unsigned long broadcast; |
| 73 | unsigned long network; |
| 74 | unsigned long 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[] = { |
| 84 | {"netmask", no_argument, NULL, 'm'}, |
| 85 | {"broadcast", no_argument, NULL, 'b'}, |
| 86 | {"network", no_argument, NULL, 'n'}, |
| 87 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
| 88 | {"prefix", no_argument, NULL, 'p'}, |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 89 | {"hostname", no_argument, NULL, 'h'}, |
| 90 | {"silent", no_argument, NULL, 's'}, |
| 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 |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 98 | "mbnphs"); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 99 | #else |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 100 | "mbn"); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 101 | #endif |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 102 | if (mode & (BROADCAST | NETWORK | NETPREFIX)) { |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 103 | if (argc - optind > 2) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 104 | bb_show_usage(); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 105 | } |
| 106 | } else { |
| 107 | if (argc - optind != 1) { |
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 | } |
| 110 | } |
| 111 | |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 112 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
| 113 | prefixstr = ipstr = argv[optind]; |
| 114 | |
| 115 | while(*prefixstr) { |
| 116 | if (*prefixstr == '/') { |
| 117 | *prefixstr = (char)0; |
| 118 | prefixstr++; |
| 119 | if (*prefixstr) { |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 120 | unsigned int msk; |
| 121 | |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 122 | netprefix = atol(prefixstr); |
| 123 | if (netprefix > 32) { |
| 124 | IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr), |
| 125 | exit(EXIT_FAILURE)); |
| 126 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 127 | netmask = 0; |
| 128 | msk = 0x80000000; |
| 129 | while (netprefix > 0) { |
| 130 | netmask |= msk; |
| 131 | msk >>= 1; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 132 | netprefix--; |
| 133 | } |
| 134 | netmask = htonl(netmask); |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 135 | /* Even if it was 0, we will signify that we have a netmask. This allows */ |
| 136 | /* for specification of default routes, etc which have a 0 netmask/prefix */ |
| 137 | have_netmask = 1; |
| 138 | } |
| 139 | break; |
| 140 | } |
| 141 | prefixstr++; |
| 142 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 143 | ipaddr = inet_aton(ipstr, &a); |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 144 | #else |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 145 | ipaddr = inet_aton(argv[optind], &a); |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 146 | #endif |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 147 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 148 | if (ipaddr == 0) { |
Eric Andersen | e968ee3 | 2003-07-03 10:28:07 +0000 | [diff] [blame] | 149 | IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[optind]), |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 150 | exit(EXIT_FAILURE)); |
| 151 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 152 | ipaddr = a.s_addr; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 153 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 154 | if (argc - optind == 2) { |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 155 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 156 | if (have_netmask) { |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 157 | IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"), |
| 158 | exit(EXIT_FAILURE)); |
| 159 | } |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 160 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 161 | #endif |
| 162 | netmask = inet_aton(argv[optind + 1], &a); |
| 163 | if (netmask == 0) { |
Eric Andersen | e968ee3 | 2003-07-03 10:28:07 +0000 | [diff] [blame] | 164 | IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[optind + 1]), |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 165 | exit(EXIT_FAILURE)); |
| 166 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 167 | netmask = a.s_addr; |
| 168 | } else { |
| 169 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 170 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 171 | if (!have_netmask) |
| 172 | #endif |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 173 | /* JHC - If the netmask wasn't provided then calculate it */ |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 174 | netmask = get_netmask(ipaddr); |
| 175 | } |
| 176 | |
| 177 | if (mode & NETMASK) { |
| 178 | printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask))); |
| 179 | } |
| 180 | |
| 181 | if (mode & BROADCAST) { |
| 182 | broadcast = (ipaddr & netmask) | ~netmask; |
| 183 | printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast))); |
| 184 | } |
| 185 | |
| 186 | if (mode & NETWORK) { |
| 187 | network = ipaddr & netmask; |
| 188 | printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network))); |
| 189 | } |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 190 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 191 | #ifdef CONFIG_FEATURE_IPCALC_FANCY |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 192 | if (mode & NETPREFIX) { |
| 193 | printf("PREFIX=%i\n", get_prefix(netmask)); |
| 194 | } |
| 195 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 196 | if (mode & HOSTNAME) { |
| 197 | struct hostent *hostinfo; |
| 198 | int x; |
| 199 | |
| 200 | hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); |
| 201 | if (!hostinfo) { |
Eric Andersen | e968ee3 | 2003-07-03 10:28:07 +0000 | [diff] [blame] | 202 | IPCALC_MSG(bb_herror_msg_and_die( |
| 203 | "cannot find hostname for %s", argv[optind]),); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 204 | exit(EXIT_FAILURE); |
| 205 | } |
| 206 | for (x = 0; hostinfo->h_name[x]; x++) { |
| 207 | hostinfo->h_name[x] = tolower(hostinfo->h_name[x]); |
| 208 | } |
| 209 | |
| 210 | printf("HOSTNAME=%s\n", hostinfo->h_name); |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | return EXIT_SUCCESS; |
| 215 | } |
| 216 | |
| 217 | /* END CODE */ |
| 218 | /* |
| 219 | Local Variables: |
| 220 | c-file-style: "linux" |
| 221 | c-basic-offset: 4 |
| 222 | tab-width: 4 |
| 223 | End: |
| 224 | */ |