"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 | * |
| 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 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 15 | #include <sys/socket.h> |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 16 | #include <arpa/inet.h> |
| 17 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
| 19 | |
Eric Andersen | 7207b88 | 2003-07-05 07:59:30 +0000 | [diff] [blame] | 20 | #define CLASS_A_NETMASK ntohl(0xFF000000) |
| 21 | #define CLASS_B_NETMASK ntohl(0xFFFF0000) |
| 22 | #define CLASS_C_NETMASK ntohl(0xFFFFFF00) |
| 23 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 24 | static unsigned long get_netmask(unsigned long ipaddr) |
| 25 | { |
Eric Andersen | 7207b88 | 2003-07-05 07:59:30 +0000 | [diff] [blame] | 26 | ipaddr = htonl(ipaddr); |
| 27 | |
| 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; |
| 34 | else |
| 35 | return 0; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 38 | #if ENABLE_FEATURE_IPCALC_FANCY |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 39 | static int get_prefix(unsigned long netmask) |
| 40 | { |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 41 | unsigned long msk = 0x80000000; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 42 | int ret = 0; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 43 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 44 | netmask = htonl(netmask); |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 45 | while (msk) { |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 46 | if (netmask & msk) |
| 47 | ret++; |
| 48 | msk >>= 1; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 49 | } |
| 50 | return ret; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 51 | } |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 52 | #else |
| 53 | int get_prefix(unsigned long netmask); |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 54 | #endif |
| 55 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 56 | |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 57 | #define NETMASK 0x01 |
| 58 | #define BROADCAST 0x02 |
| 59 | #define NETWORK 0x04 |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 60 | #define NETPREFIX 0x08 |
| 61 | #define HOSTNAME 0x10 |
Glenn L McGrath | d6bdd5d | 2003-09-05 02:37:15 +0000 | [diff] [blame] | 62 | #define SILENT 0x20 |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 63 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 64 | #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 65 | static const char ipcalc_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 66 | "netmask\0" No_argument "m" |
| 67 | "broadcast\0" No_argument "b" |
| 68 | "network\0" No_argument "n" |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 69 | # if ENABLE_FEATURE_IPCALC_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 70 | "prefix\0" No_argument "p" |
| 71 | "hostname\0" No_argument "h" |
| 72 | "silent\0" No_argument "s" |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 73 | # endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 74 | ; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 75 | #endif |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 77 | int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 78 | int ipcalc_main(int argc, char **argv) |
| 79 | { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 80 | unsigned opt; |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 81 | int have_netmask = 0; |
| 82 | in_addr_t netmask, broadcast, network, ipaddr; |
| 83 | struct in_addr a; |
| 84 | char *ipstr; |
| 85 | |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 86 | #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 87 | applet_long_options = ipcalc_longopts; |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 88 | #endif |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 89 | opt = getopt32(argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs")); |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 90 | argc -= optind; |
| 91 | argv += optind; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 92 | if (opt & (BROADCAST | NETWORK | NETPREFIX)) { |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 93 | if (argc > 2 || argc <= 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | bb_show_usage(); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 95 | } else { |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 96 | if (argc != 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | bb_show_usage(); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 98 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 99 | if (opt & SILENT) |
| 100 | logmode = LOGMODE_NONE; /* Suppress error_msg() output */ |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 101 | |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 102 | ipstr = argv[0]; |
| 103 | if (ENABLE_FEATURE_IPCALC_FANCY) { |
| 104 | unsigned long netprefix = 0; |
| 105 | char *prefixstr; |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 106 | |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 107 | prefixstr = ipstr; |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 108 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 109 | while (*prefixstr) { |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 110 | if (*prefixstr == '/') { |
| 111 | *prefixstr = (char)0; |
| 112 | prefixstr++; |
| 113 | if (*prefixstr) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 114 | unsigned msk; |
| 115 | netprefix = xatoul_range(prefixstr, 0, 32); |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 116 | netmask = 0; |
| 117 | msk = 0x80000000; |
| 118 | while (netprefix > 0) { |
| 119 | netmask |= msk; |
| 120 | msk >>= 1; |
| 121 | netprefix--; |
| 122 | } |
| 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 */ |
| 126 | have_netmask = 1; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 127 | } |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 128 | break; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 129 | } |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 130 | prefixstr++; |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 131 | } |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 132 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 133 | ipaddr = inet_aton(ipstr, &a); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 134 | |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 135 | if (ipaddr == 0) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 136 | bb_error_msg_and_die("bad IP address: %s", argv[0]); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 137 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 138 | ipaddr = a.s_addr; |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 139 | |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 140 | if (argc == 2) { |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 141 | if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 142 | bb_error_msg_and_die("use prefix or netmask, not both"); |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 143 | } |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 144 | |
Mike Frysinger | b7f88e0 | 2005-09-24 23:48:18 +0000 | [diff] [blame] | 145 | netmask = inet_aton(argv[1], &a); |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 146 | if (netmask == 0) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 147 | bb_error_msg_and_die("bad netmask: %s", argv[1]); |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 148 | } |
Glenn L McGrath | 14092a1 | 2003-09-12 00:44:50 +0000 | [diff] [blame] | 149 | netmask = a.s_addr; |
| 150 | } else { |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 151 | |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 152 | /* JHC - If the netmask wasn't provided then calculate it */ |
| 153 | if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask) |
Eric Andersen | a437504 | 2004-04-13 19:25:57 +0000 | [diff] [blame] | 154 | netmask = get_netmask(ipaddr); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 157 | if (opt & NETMASK) { |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 158 | printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask))); |
| 159 | } |
| 160 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 161 | if (opt & BROADCAST) { |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 162 | broadcast = (ipaddr & netmask) | ~netmask; |
| 163 | printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast))); |
| 164 | } |
| 165 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 166 | if (opt & NETWORK) { |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 167 | network = ipaddr & netmask; |
| 168 | printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network))); |
| 169 | } |
Glenn L McGrath | 530ea42 | 2003-09-02 06:59:57 +0000 | [diff] [blame] | 170 | |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 171 | if (ENABLE_FEATURE_IPCALC_FANCY) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 172 | if (opt & NETPREFIX) { |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 173 | printf("PREFIX=%i\n", get_prefix(netmask)); |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 176 | if (opt & HOSTNAME) { |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 177 | struct hostent *hostinfo; |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 178 | |
| 179 | hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); |
| 180 | if (!hostinfo) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 181 | bb_herror_msg_and_die("cannot find hostname for %s", argv[0]); |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 182 | } |
Denis Vlasenko | fcc6347 | 2008-04-10 02:09:40 +0000 | [diff] [blame] | 183 | str_tolower(hostinfo->h_name); |
Denis Vlasenko | 5b3adae | 2008-04-19 21:57:57 +0000 | [diff] [blame] | 184 | |
Rob Landley | ff97ee9 | 2006-06-02 19:03:01 +0000 | [diff] [blame] | 185 | printf("HOSTNAME=%s\n", hostinfo->h_name); |
| 186 | } |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 187 | } |
Glenn L McGrath | c11986d | 2002-11-10 23:42:27 +0000 | [diff] [blame] | 188 | |
| 189 | return EXIT_SUCCESS; |
| 190 | } |