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
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
23 #define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;}
25 #define CLASS_A_NETMASK ntohl(0xFF000000)
26 #define CLASS_B_NETMASK ntohl(0xFFFF0000)
27 #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
29 static unsigned long get_netmask(unsigned long ipaddr)
31 ipaddr = htonl(ipaddr);
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;
43 #ifdef CONFIG_FEATURE_IPCALC_FANCY
44 static int get_prefix(unsigned long netmask)
46 unsigned long msk = 0x80000000;
49 netmask = htonl(netmask);
60 #define BROADCAST 0x02
62 #define NETPREFIX 0x08
67 int ipcalc_main(int argc, char **argv)
77 #ifdef CONFIG_FEATURE_IPCALC_FANCY
78 unsigned long netprefix = 0;
80 char *ipstr, *prefixstr;
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'},
89 {"hostname", no_argument, NULL, 'h'},
90 {"silent", no_argument, NULL, 's'},
95 bb_applet_long_options = long_options;
96 mode = bb_getopt_ulflags(argc, argv,
97 #ifdef CONFIG_FEATURE_IPCALC_FANCY
106 if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
107 if (argc > 2 || argc <= 0)
114 #ifdef CONFIG_FEATURE_IPCALC_FANCY
115 prefixstr = ipstr = argv[0];
118 if (*prefixstr == '/') {
119 *prefixstr = (char)0;
124 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
125 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
130 while (netprefix > 0) {
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 */
144 ipaddr = inet_aton(ipstr, &a);
146 ipaddr = inet_aton(argv[0], &a);
150 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]),
156 #ifdef CONFIG_FEATURE_IPCALC_FANCY
158 IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"),
163 netmask = inet_aton(argv[1], &a);
165 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]),
170 #ifdef CONFIG_FEATURE_IPCALC_FANCY
174 /* JHC - If the netmask wasn't provided then calculate it */
175 netmask = get_netmask(ipaddr);
178 if (mode & NETMASK) {
179 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
182 if (mode & BROADCAST) {
183 broadcast = (ipaddr & netmask) | ~netmask;
184 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
187 if (mode & NETWORK) {
188 network = ipaddr & netmask;
189 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
192 #ifdef CONFIG_FEATURE_IPCALC_FANCY
193 if (mode & NETPREFIX) {
194 printf("PREFIX=%i\n", get_prefix(netmask));
197 if (mode & HOSTNAME) {
198 struct hostent *hostinfo;
201 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
203 IPCALC_MSG(bb_herror_msg_and_die(
204 "cannot find hostname for %s", argv[0]),);
207 for (x = 0; hostinfo->h_name[x]; x++) {
208 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
211 printf("HOSTNAME=%s\n", hostinfo->h_name);
221 c-file-style: "linux"