blob: f8ca9723c0406d3c710f82b98058337e3d47c19d [file] [log] [blame]
Glenn L McGrathc11986d2002-11-10 23:42:27 +00001/* 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 *
Eric Andersenaff114c2004-04-14 17:51:38 +00008 * This is a complete reimplementation of the ipcalc program
9 * from Red Hat. I didn't look at their source code, but there
Glenn L McGrathc11986d2002-11-10 23:42:27 +000010 * 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
Eric Andersen7207b882003-07-05 07:59:30 +000025#define CLASS_A_NETMASK ntohl(0xFF000000)
26#define CLASS_B_NETMASK ntohl(0xFFFF0000)
27#define CLASS_C_NETMASK ntohl(0xFFFFFF00)
28
Glenn L McGrathc11986d2002-11-10 23:42:27 +000029static unsigned long get_netmask(unsigned long ipaddr)
30{
Eric Andersen7207b882003-07-05 07:59:30 +000031 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;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000041}
42
Glenn L McGrath530ea422003-09-02 06:59:57 +000043#ifdef CONFIG_FEATURE_IPCALC_FANCY
44static int get_prefix(unsigned long netmask)
45{
Glenn L McGrath14092a12003-09-12 00:44:50 +000046 unsigned long msk = 0x80000000;
Eric Andersena4375042004-04-13 19:25:57 +000047 int ret = 0;
Glenn L McGrath530ea422003-09-02 06:59:57 +000048
Glenn L McGrath14092a12003-09-12 00:44:50 +000049 netmask = htonl(netmask);
50 while(msk) {
51 if (netmask & msk)
52 ret++;
53 msk >>= 1;
Eric Andersena4375042004-04-13 19:25:57 +000054 }
55 return ret;
Glenn L McGrath530ea422003-09-02 06:59:57 +000056}
57#endif
58
Glenn L McGrathc11986d2002-11-10 23:42:27 +000059#define NETMASK 0x01
60#define BROADCAST 0x02
61#define NETWORK 0x04
Glenn L McGrath530ea422003-09-02 06:59:57 +000062#define NETPREFIX 0x08
63#define HOSTNAME 0x10
Glenn L McGrathd6bdd5d2003-09-05 02:37:15 +000064#define SILENT 0x20
Glenn L McGrathc11986d2002-11-10 23:42:27 +000065
Glenn L McGrath14092a12003-09-12 00:44:50 +000066
Glenn L McGrathc11986d2002-11-10 23:42:27 +000067int ipcalc_main(int argc, char **argv)
68{
Eric Andersene968ee32003-07-03 10:28:07 +000069 unsigned long mode;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000070
Glenn L McGrath14092a12003-09-12 00:44:50 +000071 unsigned long netmask;
Eric Andersene968ee32003-07-03 10:28:07 +000072 unsigned long broadcast;
73 unsigned long network;
74 unsigned long ipaddr;
Glenn L McGrath14092a12003-09-12 00:44:50 +000075 struct in_addr a;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000076
Glenn L McGrathc11986d2002-11-10 23:42:27 +000077#ifdef CONFIG_FEATURE_IPCALC_FANCY
Glenn L McGrath530ea422003-09-02 06:59:57 +000078 unsigned long netprefix = 0;
79 int have_netmask = 0;
80 char *ipstr, *prefixstr;
81#endif
82
83 static const struct option long_options[] = {
Eric Andersena4375042004-04-13 19:25:57 +000084 {"netmask", no_argument, NULL, 'm'},
85 {"broadcast", no_argument, NULL, 'b'},
86 {"network", no_argument, NULL, 'n'},
Glenn L McGrath530ea422003-09-02 06:59:57 +000087#ifdef CONFIG_FEATURE_IPCALC_FANCY
Eric Andersena4375042004-04-13 19:25:57 +000088 {"prefix", no_argument, NULL, 'p'},
89 {"hostname", no_argument, NULL, 'h'},
90 {"silent", no_argument, NULL, 's'},
Glenn L McGrathc11986d2002-11-10 23:42:27 +000091#endif
92 {NULL, 0, NULL, 0}
93 };
94
Eric Andersene968ee32003-07-03 10:28:07 +000095 bb_applet_long_options = long_options;
96 mode = bb_getopt_ulflags(argc, argv,
Glenn L McGrathc11986d2002-11-10 23:42:27 +000097#ifdef CONFIG_FEATURE_IPCALC_FANCY
Eric Andersena4375042004-04-13 19:25:57 +000098 "mbnphs"
Glenn L McGrathc11986d2002-11-10 23:42:27 +000099#else
Eric Andersena4375042004-04-13 19:25:57 +0000100 "mbn"
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000101#endif
Eric Andersena4375042004-04-13 19:25:57 +0000102 );
Glenn L McGrath530ea422003-09-02 06:59:57 +0000103 if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000104 if (argc - optind > 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_show_usage();
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000106 }
107 } else {
108 if (argc - optind != 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 bb_show_usage();
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000110 }
111 }
112
Glenn L McGrath530ea422003-09-02 06:59:57 +0000113#ifdef CONFIG_FEATURE_IPCALC_FANCY
Eric Andersena4375042004-04-13 19:25:57 +0000114 prefixstr = ipstr = argv[optind];
Glenn L McGrath530ea422003-09-02 06:59:57 +0000115
Eric Andersena4375042004-04-13 19:25:57 +0000116 while(*prefixstr) {
117 if (*prefixstr == '/') {
118 *prefixstr = (char)0;
119 prefixstr++;
120 if (*prefixstr) {
Glenn L McGrath14092a12003-09-12 00:44:50 +0000121 unsigned int msk;
122
Eric Andersena4375042004-04-13 19:25:57 +0000123 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
124 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
125 exit(EXIT_FAILURE));
126 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000127 netmask = 0;
128 msk = 0x80000000;
129 while (netprefix > 0) {
130 netmask |= msk;
131 msk >>= 1;
Eric Andersena4375042004-04-13 19:25:57 +0000132 netprefix--;
133 }
134 netmask = htonl(netmask);
135 /* Even if it was 0, we will signify that we have a netmask. This allows */
136 /* for specification of default routes, etc which have a 0 netmask/prefix */
137 have_netmask = 1;
138 }
139 break;
140 }
141 prefixstr++;
142 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000143 ipaddr = inet_aton(ipstr, &a);
Glenn L McGrath530ea422003-09-02 06:59:57 +0000144#else
Glenn L McGrath14092a12003-09-12 00:44:50 +0000145 ipaddr = inet_aton(argv[optind], &a);
Glenn L McGrath530ea422003-09-02 06:59:57 +0000146#endif
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000147
Glenn L McGrath14092a12003-09-12 00:44:50 +0000148 if (ipaddr == 0) {
Eric Andersene968ee32003-07-03 10:28:07 +0000149 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[optind]),
Eric Andersena4375042004-04-13 19:25:57 +0000150 exit(EXIT_FAILURE));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000151 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000152 ipaddr = a.s_addr;
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000153
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000154 if (argc - optind == 2) {
Glenn L McGrath530ea422003-09-02 06:59:57 +0000155#ifdef CONFIG_FEATURE_IPCALC_FANCY
Eric Andersena4375042004-04-13 19:25:57 +0000156 if (have_netmask) {
157 IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"),
158 exit(EXIT_FAILURE));
159 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000160
Glenn L McGrath14092a12003-09-12 00:44:50 +0000161#endif
162 netmask = inet_aton(argv[optind + 1], &a);
163 if (netmask == 0) {
Eric Andersena4375042004-04-13 19:25:57 +0000164 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[optind + 1]),
165 exit(EXIT_FAILURE));
166 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000167 netmask = a.s_addr;
168 } else {
169#ifdef CONFIG_FEATURE_IPCALC_FANCY
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000170
Glenn L McGrath14092a12003-09-12 00:44:50 +0000171 if (!have_netmask)
172#endif
Eric Andersena4375042004-04-13 19:25:57 +0000173 /* JHC - If the netmask wasn't provided then calculate it */
174 netmask = get_netmask(ipaddr);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000175 }
176
177 if (mode & NETMASK) {
178 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
179 }
180
181 if (mode & BROADCAST) {
182 broadcast = (ipaddr & netmask) | ~netmask;
183 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
184 }
185
186 if (mode & NETWORK) {
187 network = ipaddr & netmask;
188 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
189 }
Glenn L McGrath530ea422003-09-02 06:59:57 +0000190
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000191#ifdef CONFIG_FEATURE_IPCALC_FANCY
Eric Andersena4375042004-04-13 19:25:57 +0000192 if (mode & NETPREFIX) {
193 printf("PREFIX=%i\n", get_prefix(netmask));
194 }
Glenn L McGrath530ea422003-09-02 06:59:57 +0000195
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000196 if (mode & HOSTNAME) {
197 struct hostent *hostinfo;
198 int x;
199
200 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
201 if (!hostinfo) {
Eric Andersene968ee32003-07-03 10:28:07 +0000202 IPCALC_MSG(bb_herror_msg_and_die(
Eric Andersena4375042004-04-13 19:25:57 +0000203 "cannot find hostname for %s", argv[optind]),);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000204 exit(EXIT_FAILURE);
205 }
206 for (x = 0; hostinfo->h_name[x]; x++) {
207 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
208 }
209
210 printf("HOSTNAME=%s\n", hostinfo->h_name);
211 }
212#endif
213
214 return EXIT_SUCCESS;
215}
216
217/* END CODE */
218/*
219Local Variables:
220c-file-style: "linux"
221c-basic-offset: 4
222tab-width: 4
223End:
224*/