a24629a9d9f2c04d132b040d0b41d1b918d254f8
[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 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
11  *
12  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <ctype.h>
18 #include <getopt.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22
23 #include "busybox.h"
24
25 #define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;}
26
27 #define CLASS_A_NETMASK ntohl(0xFF000000)
28 #define CLASS_B_NETMASK ntohl(0xFFFF0000)
29 #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
30
31 static unsigned long get_netmask(unsigned long ipaddr)
32 {
33         ipaddr = htonl(ipaddr);
34
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;
41         else
42                 return 0;
43 }
44
45 #ifdef CONFIG_FEATURE_IPCALC_FANCY
46 static int get_prefix(unsigned long netmask)
47 {
48         unsigned long msk = 0x80000000;
49         int ret = 0;
50
51         netmask = htonl(netmask);
52         while(msk) {
53                 if (netmask & msk)
54                         ret++;
55                 msk >>= 1;
56         }
57         return ret;
58 }
59 #endif
60
61 #define NETMASK   0x01
62 #define BROADCAST 0x02
63 #define NETWORK   0x04
64 #define NETPREFIX 0x08
65 #define HOSTNAME  0x10
66 #define SILENT    0x20
67
68
69 int ipcalc_main(int argc, char **argv)
70 {
71         unsigned long mode;
72
73         in_addr_t netmask;
74         in_addr_t broadcast;
75         in_addr_t network;
76         in_addr_t ipaddr;
77         struct in_addr a;
78
79 #ifdef CONFIG_FEATURE_IPCALC_FANCY
80         unsigned long netprefix = 0;
81         int have_netmask = 0;
82         char *ipstr, *prefixstr;
83 #endif
84
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'},
93 #endif
94                 {NULL, 0, NULL, 0}
95         };
96
97         bb_applet_long_options = long_options;
98         mode = bb_getopt_ulflags(argc, argv,
99 #ifdef CONFIG_FEATURE_IPCALC_FANCY
100                         "mbnphs"
101 #else
102                         "mbn"
103 #endif
104                         );
105
106         argc -= optind;
107         argv += optind;
108         if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
109                 if (argc > 2 || argc <= 0)
110                         bb_show_usage();
111         } else {
112                 if (argc != 1)
113                         bb_show_usage();
114         }
115
116 #ifdef CONFIG_FEATURE_IPCALC_FANCY
117         prefixstr = ipstr = argv[0];
118
119         while(*prefixstr) {
120                 if (*prefixstr == '/') {
121                         *prefixstr = (char)0;
122                         prefixstr++;
123                         if (*prefixstr) {
124                                 unsigned int msk;
125
126                                 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
127                                         IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
128                                                         exit(EXIT_FAILURE));
129                                 }
130                                 netmask = 0;
131                                 msk = 0x80000000;
132                                 while (netprefix > 0) {
133                                         netmask |= msk;
134                                         msk >>= 1;
135                                         netprefix--;
136                                 }
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 */
140                                 have_netmask = 1;
141                         }
142                         break;
143                 }
144                 prefixstr++;
145         }
146         ipaddr = inet_aton(ipstr, &a);
147 #else
148         ipaddr = inet_aton(argv[0], &a);
149 #endif
150
151         if (ipaddr == 0) {
152                 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]),
153                                 exit(EXIT_FAILURE));
154         }
155         ipaddr = a.s_addr;
156
157         if (argc == 2) {
158 #ifdef CONFIG_FEATURE_IPCALC_FANCY
159                 if (have_netmask) {
160                         IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"),
161                                         exit(EXIT_FAILURE));
162                 }
163
164 #endif
165                 netmask = inet_aton(argv[1], &a);
166                 if (netmask == 0) {
167                         IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]),
168                                         exit(EXIT_FAILURE));
169                 }
170                 netmask = a.s_addr;
171         } else {
172 #ifdef CONFIG_FEATURE_IPCALC_FANCY
173
174                 if (!have_netmask)
175 #endif
176                         /* JHC - If the netmask wasn't provided then calculate it */
177                         netmask = get_netmask(ipaddr);
178         }
179
180         if (mode & NETMASK) {
181                 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
182         }
183
184         if (mode & BROADCAST) {
185                 broadcast = (ipaddr & netmask) | ~netmask;
186                 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
187         }
188
189         if (mode & NETWORK) {
190                 network = ipaddr & netmask;
191                 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
192         }
193
194 #ifdef CONFIG_FEATURE_IPCALC_FANCY
195         if (mode & NETPREFIX) {
196                 printf("PREFIX=%i\n", get_prefix(netmask));
197         }
198
199         if (mode & HOSTNAME) {
200                 struct hostent *hostinfo;
201                 int x;
202
203                 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
204                 if (!hostinfo) {
205                         IPCALC_MSG(bb_herror_msg_and_die(
206                                                 "cannot find hostname for %s", argv[0]),);
207                         exit(EXIT_FAILURE);
208                 }
209                 for (x = 0; hostinfo->h_name[x]; x++) {
210                         hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
211                 }
212
213                 printf("HOSTNAME=%s\n", hostinfo->h_name);
214         }
215 #endif
216
217         return EXIT_SUCCESS;
218 }