1 /* vi: set sw=4 ts=4 ai: */
3 * Mini ipcalc implementation for busybox
5 * By Jordan Crouse <jordan@cosmicpenguin.net>
6 * Stephan Linz <linz@li-pro.net>
8 * This is a complete reimplementation of the ipcalc program
9 * from Red Hat. I didn't look at their source code, but there
10 * is no denying that this is a loving reimplementation
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
25 #define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;}
27 #define CLASS_A_NETMASK ntohl(0xFF000000)
28 #define CLASS_B_NETMASK ntohl(0xFFFF0000)
29 #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
31 static unsigned long get_netmask(unsigned long ipaddr)
33 ipaddr = htonl(ipaddr);
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;
45 #ifdef CONFIG_FEATURE_IPCALC_FANCY
46 static int get_prefix(unsigned long netmask)
48 unsigned long msk = 0x80000000;
51 netmask = htonl(netmask);
62 #define BROADCAST 0x02
64 #define NETPREFIX 0x08
69 int ipcalc_main(int argc, char **argv)
79 #ifdef CONFIG_FEATURE_IPCALC_FANCY
80 unsigned long netprefix = 0;
82 char *ipstr, *prefixstr;
85 #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
86 static const struct option long_options[] = {
87 {"netmask", no_argument, NULL, 'm'},
88 {"broadcast", no_argument, NULL, 'b'},
89 {"network", no_argument, NULL, 'n'},
90 #ifdef CONFIG_FEATURE_IPCALC_FANCY
91 {"prefix", no_argument, NULL, 'p'},
92 {"hostname", no_argument, NULL, 'h'},
93 {"silent", no_argument, NULL, 's'},
98 bb_applet_long_options = long_options;
100 mode = bb_getopt_ulflags(argc, argv,
101 #ifdef CONFIG_FEATURE_IPCALC_FANCY
110 if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
111 if (argc > 2 || argc <= 0)
118 #ifdef CONFIG_FEATURE_IPCALC_FANCY
119 prefixstr = ipstr = argv[0];
122 if (*prefixstr == '/') {
123 *prefixstr = (char)0;
128 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
129 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
134 while (netprefix > 0) {
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 */
148 ipaddr = inet_aton(ipstr, &a);
150 ipaddr = inet_aton(argv[0], &a);
154 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]),
160 #ifdef CONFIG_FEATURE_IPCALC_FANCY
162 IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"),
167 netmask = inet_aton(argv[1], &a);
169 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]),
174 #ifdef CONFIG_FEATURE_IPCALC_FANCY
178 /* JHC - If the netmask wasn't provided then calculate it */
179 netmask = get_netmask(ipaddr);
182 if (mode & NETMASK) {
183 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
186 if (mode & BROADCAST) {
187 broadcast = (ipaddr & netmask) | ~netmask;
188 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
191 if (mode & NETWORK) {
192 network = ipaddr & netmask;
193 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
196 #ifdef CONFIG_FEATURE_IPCALC_FANCY
197 if (mode & NETPREFIX) {
198 printf("PREFIX=%i\n", get_prefix(netmask));
201 if (mode & HOSTNAME) {
202 struct hostent *hostinfo;
205 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
207 IPCALC_MSG(bb_herror_msg_and_die(
208 "cannot find hostname for %s", argv[0]),);
211 for (x = 0; hostinfo->h_name[x]; x++) {
212 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
215 printf("HOSTNAME=%s\n", hostinfo->h_name);