Small bug in silent option
[oweals/busybox.git] / networking / ipcalc.c
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
25 #define CLASS_A_NETMASK ntohl(0xFF000000)
26 #define CLASS_B_NETMASK ntohl(0xFFFF0000)
27 #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
28
29 static unsigned long get_netmask(unsigned long ipaddr)
30 {
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;
41 }
42
43 #ifdef CONFIG_FEATURE_IPCALC_FANCY
44 static int get_prefix(unsigned long netmask)
45 {
46         unsigned long t;
47         int ret = 0;
48
49         for (t = 0; t < 32; t++) {
50                 if (htonl(netmask) & (0x80000000 >> t)) {
51                         ret ++;
52                 } else {
53                         break;
54                 }
55         }
56         return ret;
57 }
58 #endif
59
60 #define NETMASK   0x01
61 #define BROADCAST 0x02
62 #define NETWORK   0x04
63 #define NETPREFIX 0x08
64 #define HOSTNAME  0x10
65 #define SILENT    0x20
66
67 int ipcalc_main(int argc, char **argv)
68 {
69         unsigned long mode;
70
71         unsigned long netmask = 0;
72         unsigned long broadcast;
73         unsigned long network;
74         unsigned long ipaddr;
75
76 #ifdef CONFIG_FEATURE_IPCALC_FANCY
77         unsigned long netprefix = 0;
78         int have_netmask = 0;
79         char *ipstr, *prefixstr;
80 #endif
81
82         static const struct option long_options[] = {
83                 {"netmask", no_argument, NULL, 'm'},
84                 {"broadcast", no_argument, NULL, 'b'},
85                 {"network", no_argument, NULL, 'n'},
86 #ifdef CONFIG_FEATURE_IPCALC_FANCY
87                 {"prefix", no_argument, NULL, 'p'},
88                 {"hostname", no_argument, NULL, 'h'},
89                 {"silent", no_argument, NULL, 's'},
90 #endif
91                 {NULL, 0, NULL, 0}
92         };
93
94         bb_applet_long_options = long_options;
95         mode = bb_getopt_ulflags(argc, argv,
96 #ifdef CONFIG_FEATURE_IPCALC_FANCY
97                                                           "mbnphs");
98 #else
99                                                           "mbn");
100 #endif
101         if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
102                 if (argc - optind > 2) {
103                         bb_show_usage();
104                 }
105         } else {
106                 if (argc - optind != 1) {
107                         bb_show_usage();
108                 }
109         }
110
111 #ifdef CONFIG_FEATURE_IPCALC_FANCY
112         prefixstr = ipstr = argv[optind];
113
114         while(*prefixstr) {
115                 if (*prefixstr == '/') {
116                         *prefixstr = (char)0;
117                         prefixstr++;
118                         if (*prefixstr) {
119                                 netprefix = atol(prefixstr);
120                                 if (netprefix > 32) {
121                                         IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
122                                                 exit(EXIT_FAILURE));
123                                 }
124                                 if (netprefix) {
125                                         netmask = 0x80000000;
126                                         netprefix--;
127                                         while(netprefix) {
128                                                 netmask = 0x80000000 | (netmask >> 1);
129                                                 netprefix--;
130                                         }
131                                         netmask = htonl(netmask);
132                                 }
133                                 /* Even if it was 0, we will signify that we have a netmask. This allows */
134                                 /* for specification of default routes, etc which have a 0 netmask/prefix */
135                                 have_netmask = 1;
136                         }
137                         break;
138                 }
139                 prefixstr++;
140         }
141         ipaddr = inet_addr(ipstr);
142 #else
143         ipaddr = inet_addr(argv[optind]);
144 #endif
145
146         if (ipaddr == INADDR_NONE) {
147                 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[optind]),
148                                    exit(EXIT_FAILURE));
149         }
150
151         if (argc - optind == 2) {
152 #ifdef CONFIG_FEATURE_IPCALC_FANCY
153                if (have_netmask == 1) {
154                         IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"),
155                                 exit(EXIT_FAILURE));
156                 }
157                 have_netmask = 1;
158 #endif
159                 netmask = inet_addr(argv[optind + 1]);
160         }
161
162         if (ipaddr == INADDR_NONE) {
163                 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[optind + 1]),
164                                    exit(EXIT_FAILURE));
165         }
166
167         /* JHC - If the netmask wasn't provided then calculate it */
168         if (!netmask) {
169                 netmask = get_netmask(ipaddr);
170         }
171
172         if (mode & NETMASK) {
173                 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
174         }
175
176         if (mode & BROADCAST) {
177                 broadcast = (ipaddr & netmask) | ~netmask;
178                 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
179         }
180
181         if (mode & NETWORK) {
182                 network = ipaddr & netmask;
183                 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
184         }
185
186 #ifdef CONFIG_FEATURE_IPCALC_FANCY
187         if (mode & NETPREFIX) {
188                 printf("PREFIX=%i\n", get_prefix(netmask));
189         }
190
191         if (mode & HOSTNAME) {
192                 struct hostent *hostinfo;
193                 int x;
194
195                 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
196                 if (!hostinfo) {
197                         IPCALC_MSG(bb_herror_msg_and_die(
198                                 "cannot find hostname for %s", argv[optind]),);
199                         exit(EXIT_FAILURE);
200                 }
201                 for (x = 0; hostinfo->h_name[x]; x++) {
202                         hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
203                 }
204
205                 printf("HOSTNAME=%s\n", hostinfo->h_name);
206         }
207 #endif
208
209         return EXIT_SUCCESS;
210 }
211
212 /* END CODE */
213 /*
214 Local Variables:
215 c-file-style: "linux"
216 c-basic-offset: 4
217 tab-width: 4
218 End:
219 */