blob: c5f2ba121ec4520b5f90d380c30dfc5cc2d4c9cc [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
Eric Andersenb225e2a2004-08-28 00:43:07 +000071 in_addr_t netmask;
72 in_addr_t broadcast;
73 in_addr_t network;
74 in_addr_t 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 );
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000103
104 argc -= optind;
105 argv += optind;
Glenn L McGrath530ea422003-09-02 06:59:57 +0000106 if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000107 if (argc > 2 || argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 bb_show_usage();
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000109 } else {
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000110 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111 bb_show_usage();
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000112 }
113
Glenn L McGrath530ea422003-09-02 06:59:57 +0000114#ifdef CONFIG_FEATURE_IPCALC_FANCY
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000115 prefixstr = ipstr = argv[0];
Glenn L McGrath530ea422003-09-02 06:59:57 +0000116
Eric Andersena4375042004-04-13 19:25:57 +0000117 while(*prefixstr) {
118 if (*prefixstr == '/') {
119 *prefixstr = (char)0;
120 prefixstr++;
121 if (*prefixstr) {
Glenn L McGrath14092a12003-09-12 00:44:50 +0000122 unsigned int msk;
123
Eric Andersena4375042004-04-13 19:25:57 +0000124 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
125 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
126 exit(EXIT_FAILURE));
127 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000128 netmask = 0;
129 msk = 0x80000000;
130 while (netprefix > 0) {
131 netmask |= msk;
132 msk >>= 1;
Eric Andersena4375042004-04-13 19:25:57 +0000133 netprefix--;
134 }
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 */
138 have_netmask = 1;
139 }
140 break;
141 }
142 prefixstr++;
143 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000144 ipaddr = inet_aton(ipstr, &a);
Glenn L McGrath530ea422003-09-02 06:59:57 +0000145#else
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000146 ipaddr = inet_aton(argv[0], &a);
Glenn L McGrath530ea422003-09-02 06:59:57 +0000147#endif
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000148
Glenn L McGrath14092a12003-09-12 00:44:50 +0000149 if (ipaddr == 0) {
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000150 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]),
Eric Andersena4375042004-04-13 19:25:57 +0000151 exit(EXIT_FAILURE));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000152 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000153 ipaddr = a.s_addr;
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000154
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000155 if (argc == 2) {
Glenn L McGrath530ea422003-09-02 06:59:57 +0000156#ifdef CONFIG_FEATURE_IPCALC_FANCY
Eric Andersena4375042004-04-13 19:25:57 +0000157 if (have_netmask) {
158 IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"),
159 exit(EXIT_FAILURE));
160 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000161
Glenn L McGrath14092a12003-09-12 00:44:50 +0000162#endif
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000163 netmask = inet_aton(argv[1], &a);
Glenn L McGrath14092a12003-09-12 00:44:50 +0000164 if (netmask == 0) {
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000165 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]),
Eric Andersena4375042004-04-13 19:25:57 +0000166 exit(EXIT_FAILURE));
167 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000168 netmask = a.s_addr;
169 } else {
170#ifdef CONFIG_FEATURE_IPCALC_FANCY
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000171
Glenn L McGrath14092a12003-09-12 00:44:50 +0000172 if (!have_netmask)
173#endif
Eric Andersena4375042004-04-13 19:25:57 +0000174 /* JHC - If the netmask wasn't provided then calculate it */
175 netmask = get_netmask(ipaddr);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000176 }
177
178 if (mode & NETMASK) {
179 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
180 }
181
182 if (mode & BROADCAST) {
183 broadcast = (ipaddr & netmask) | ~netmask;
184 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
185 }
186
187 if (mode & NETWORK) {
188 network = ipaddr & netmask;
189 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
190 }
Glenn L McGrath530ea422003-09-02 06:59:57 +0000191
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000192#ifdef CONFIG_FEATURE_IPCALC_FANCY
Eric Andersena4375042004-04-13 19:25:57 +0000193 if (mode & NETPREFIX) {
194 printf("PREFIX=%i\n", get_prefix(netmask));
195 }
Glenn L McGrath530ea422003-09-02 06:59:57 +0000196
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000197 if (mode & HOSTNAME) {
198 struct hostent *hostinfo;
199 int x;
200
201 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
202 if (!hostinfo) {
Eric Andersene968ee32003-07-03 10:28:07 +0000203 IPCALC_MSG(bb_herror_msg_and_die(
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000204 "cannot find hostname for %s", argv[0]),);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000205 exit(EXIT_FAILURE);
206 }
207 for (x = 0; hostinfo->h_name[x]; x++) {
208 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
209 }
210
211 printf("HOSTNAME=%s\n", hostinfo->h_name);
212 }
213#endif
214
215 return EXIT_SUCCESS;
216}
217
218/* END CODE */
219/*
220Local Variables:
221c-file-style: "linux"
222c-basic-offset: 4
223tab-width: 4
224End:
225*/