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 static const struct option long_options[] = {
86 {"netmask", no_argument, NULL, 'm'},
87 {"broadcast", no_argument, NULL, 'b'},
88 {"network", no_argument, NULL, 'n'},
89 #ifdef CONFIG_FEATURE_IPCALC_FANCY
90 {"prefix", no_argument, NULL, 'p'},
91 {"hostname", no_argument, NULL, 'h'},
92 {"silent", no_argument, NULL, 's'},
97 bb_applet_long_options = long_options;
98 mode = bb_getopt_ulflags(argc, argv,
99 #ifdef CONFIG_FEATURE_IPCALC_FANCY
108 if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
109 if (argc > 2 || argc <= 0)
116 #ifdef CONFIG_FEATURE_IPCALC_FANCY
117 prefixstr = ipstr = argv[0];
120 if (*prefixstr == '/') {
121 *prefixstr = (char)0;
126 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
127 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
132 while (netprefix > 0) {
137 netmask = htonl(netmask);
138 /* Even if it was 0, we will signify that we have a netmask. This allows */
139 /* for specification of default routes, etc which have a 0 netmask/prefix */
146 ipaddr = inet_aton(ipstr, &a);
148 ipaddr = inet_aton(argv[0], &a);
152 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]),
158 #ifdef CONFIG_FEATURE_IPCALC_FANCY
160 IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"),
165 netmask = inet_aton(argv[1], &a);
167 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]),
172 #ifdef CONFIG_FEATURE_IPCALC_FANCY
176 /* JHC - If the netmask wasn't provided then calculate it */
177 netmask = get_netmask(ipaddr);
180 if (mode & NETMASK) {
181 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
184 if (mode & BROADCAST) {
185 broadcast = (ipaddr & netmask) | ~netmask;
186 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
189 if (mode & NETWORK) {
190 network = ipaddr & netmask;
191 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
194 #ifdef CONFIG_FEATURE_IPCALC_FANCY
195 if (mode & NETPREFIX) {
196 printf("PREFIX=%i\n", get_prefix(netmask));
199 if (mode & HOSTNAME) {
200 struct hostent *hostinfo;
203 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
205 IPCALC_MSG(bb_herror_msg_and_die(
206 "cannot find hostname for %s", argv[0]),);
209 for (x = 0; hostinfo->h_name[x]; x++) {
210 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
213 printf("HOSTNAME=%s\n", hostinfo->h_name);