blob: b5cc3dec48c13678a3991a25f47ffe60c8606c6f [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrathc11986d2002-11-10 23:42:27 +00002/*
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
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +000011 *
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathc11986d2002-11-10 23:42:27 +000013 */
14
Glenn L McGrathc11986d2002-11-10 23:42:27 +000015#include <sys/socket.h>
Glenn L McGrathc11986d2002-11-10 23:42:27 +000016#include <arpa/inet.h>
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
19
Eric Andersen7207b882003-07-05 07:59:30 +000020#define CLASS_A_NETMASK ntohl(0xFF000000)
21#define CLASS_B_NETMASK ntohl(0xFFFF0000)
22#define CLASS_C_NETMASK ntohl(0xFFFFFF00)
23
Glenn L McGrathc11986d2002-11-10 23:42:27 +000024static unsigned long get_netmask(unsigned long ipaddr)
25{
Eric Andersen7207b882003-07-05 07:59:30 +000026 ipaddr = htonl(ipaddr);
27
28 if ((ipaddr & 0xC0000000) == 0xC0000000)
29 return CLASS_C_NETMASK;
30 else if ((ipaddr & 0x80000000) == 0x80000000)
31 return CLASS_B_NETMASK;
32 else if ((ipaddr & 0x80000000) == 0)
33 return CLASS_A_NETMASK;
34 else
35 return 0;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000036}
37
Denis Vlasenkoe3241842007-08-13 10:36:25 +000038#if ENABLE_FEATURE_IPCALC_FANCY
Glenn L McGrath530ea422003-09-02 06:59:57 +000039static int get_prefix(unsigned long netmask)
40{
Glenn L McGrath14092a12003-09-12 00:44:50 +000041 unsigned long msk = 0x80000000;
Eric Andersena4375042004-04-13 19:25:57 +000042 int ret = 0;
Glenn L McGrath530ea422003-09-02 06:59:57 +000043
Glenn L McGrath14092a12003-09-12 00:44:50 +000044 netmask = htonl(netmask);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +000045 while (msk) {
Glenn L McGrath14092a12003-09-12 00:44:50 +000046 if (netmask & msk)
47 ret++;
48 msk >>= 1;
Eric Andersena4375042004-04-13 19:25:57 +000049 }
50 return ret;
Glenn L McGrath530ea422003-09-02 06:59:57 +000051}
Rob Landleyff97ee92006-06-02 19:03:01 +000052#else
53int get_prefix(unsigned long netmask);
Glenn L McGrath530ea422003-09-02 06:59:57 +000054#endif
55
Denis Vlasenko13858992006-10-08 12:49:22 +000056
Glenn L McGrathc11986d2002-11-10 23:42:27 +000057#define NETMASK 0x01
58#define BROADCAST 0x02
59#define NETWORK 0x04
Glenn L McGrath530ea422003-09-02 06:59:57 +000060#define NETPREFIX 0x08
61#define HOSTNAME 0x10
Glenn L McGrathd6bdd5d2003-09-05 02:37:15 +000062#define SILENT 0x20
Glenn L McGrathc11986d2002-11-10 23:42:27 +000063
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000064#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000065 static const char ipcalc_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000066 "netmask\0" No_argument "m"
67 "broadcast\0" No_argument "b"
68 "network\0" No_argument "n"
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000069# if ENABLE_FEATURE_IPCALC_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000070 "prefix\0" No_argument "p"
71 "hostname\0" No_argument "h"
72 "silent\0" No_argument "s"
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000073# endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +000074 ;
Glenn L McGrathc11986d2002-11-10 23:42:27 +000075#endif
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000076
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000077int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleyff97ee92006-06-02 19:03:01 +000078int ipcalc_main(int argc, char **argv)
79{
Denis Vlasenko13858992006-10-08 12:49:22 +000080 unsigned opt;
Rob Landleyff97ee92006-06-02 19:03:01 +000081 int have_netmask = 0;
82 in_addr_t netmask, broadcast, network, ipaddr;
83 struct in_addr a;
84 char *ipstr;
85
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000086#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000087 applet_long_options = ipcalc_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +000088#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000089 opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
Mike Frysingerb7f88e02005-09-24 23:48:18 +000090 argc -= optind;
91 argv += optind;
Denis Vlasenko13858992006-10-08 12:49:22 +000092 if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
Mike Frysingerb7f88e02005-09-24 23:48:18 +000093 if (argc > 2 || argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 bb_show_usage();
Glenn L McGrathc11986d2002-11-10 23:42:27 +000095 } else {
Mike Frysingerb7f88e02005-09-24 23:48:18 +000096 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 bb_show_usage();
Glenn L McGrathc11986d2002-11-10 23:42:27 +000098 }
Denis Vlasenko13858992006-10-08 12:49:22 +000099 if (opt & SILENT)
100 logmode = LOGMODE_NONE; /* Suppress error_msg() output */
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000101
Rob Landleyff97ee92006-06-02 19:03:01 +0000102 ipstr = argv[0];
103 if (ENABLE_FEATURE_IPCALC_FANCY) {
104 unsigned long netprefix = 0;
105 char *prefixstr;
Glenn L McGrath530ea422003-09-02 06:59:57 +0000106
Rob Landleyff97ee92006-06-02 19:03:01 +0000107 prefixstr = ipstr;
Glenn L McGrath14092a12003-09-12 00:44:50 +0000108
Denis Vlasenko13858992006-10-08 12:49:22 +0000109 while (*prefixstr) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000110 if (*prefixstr == '/') {
111 *prefixstr = (char)0;
112 prefixstr++;
113 if (*prefixstr) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000114 unsigned msk;
115 netprefix = xatoul_range(prefixstr, 0, 32);
Rob Landleyff97ee92006-06-02 19:03:01 +0000116 netmask = 0;
117 msk = 0x80000000;
118 while (netprefix > 0) {
119 netmask |= msk;
120 msk >>= 1;
121 netprefix--;
122 }
123 netmask = htonl(netmask);
124 /* Even if it was 0, we will signify that we have a netmask. This allows */
125 /* for specification of default routes, etc which have a 0 netmask/prefix */
126 have_netmask = 1;
Eric Andersena4375042004-04-13 19:25:57 +0000127 }
Rob Landleyff97ee92006-06-02 19:03:01 +0000128 break;
Eric Andersena4375042004-04-13 19:25:57 +0000129 }
Rob Landleyff97ee92006-06-02 19:03:01 +0000130 prefixstr++;
Eric Andersena4375042004-04-13 19:25:57 +0000131 }
Eric Andersena4375042004-04-13 19:25:57 +0000132 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000133 ipaddr = inet_aton(ipstr, &a);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000134
Glenn L McGrath14092a12003-09-12 00:44:50 +0000135 if (ipaddr == 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000136 bb_error_msg_and_die("bad IP address: %s", argv[0]);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000137 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000138 ipaddr = a.s_addr;
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000139
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000140 if (argc == 2) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000141 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000142 bb_error_msg_and_die("use prefix or netmask, not both");
Eric Andersena4375042004-04-13 19:25:57 +0000143 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000144
Mike Frysingerb7f88e02005-09-24 23:48:18 +0000145 netmask = inet_aton(argv[1], &a);
Glenn L McGrath14092a12003-09-12 00:44:50 +0000146 if (netmask == 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000147 bb_error_msg_and_die("bad netmask: %s", argv[1]);
Eric Andersena4375042004-04-13 19:25:57 +0000148 }
Glenn L McGrath14092a12003-09-12 00:44:50 +0000149 netmask = a.s_addr;
150 } else {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000151
Rob Landleyff97ee92006-06-02 19:03:01 +0000152 /* JHC - If the netmask wasn't provided then calculate it */
153 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
Eric Andersena4375042004-04-13 19:25:57 +0000154 netmask = get_netmask(ipaddr);
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000155 }
156
Denis Vlasenko13858992006-10-08 12:49:22 +0000157 if (opt & NETMASK) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000158 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
159 }
160
Denis Vlasenko13858992006-10-08 12:49:22 +0000161 if (opt & BROADCAST) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000162 broadcast = (ipaddr & netmask) | ~netmask;
163 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
164 }
165
Denis Vlasenko13858992006-10-08 12:49:22 +0000166 if (opt & NETWORK) {
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000167 network = ipaddr & netmask;
168 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
169 }
Glenn L McGrath530ea422003-09-02 06:59:57 +0000170
Rob Landleyff97ee92006-06-02 19:03:01 +0000171 if (ENABLE_FEATURE_IPCALC_FANCY) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000172 if (opt & NETPREFIX) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000173 printf("PREFIX=%i\n", get_prefix(netmask));
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000174 }
175
Denis Vlasenko13858992006-10-08 12:49:22 +0000176 if (opt & HOSTNAME) {
Rob Landleyff97ee92006-06-02 19:03:01 +0000177 struct hostent *hostinfo;
Rob Landleyff97ee92006-06-02 19:03:01 +0000178
179 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
180 if (!hostinfo) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000181 bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);
Rob Landleyff97ee92006-06-02 19:03:01 +0000182 }
Denis Vlasenkofcc63472008-04-10 02:09:40 +0000183 str_tolower(hostinfo->h_name);
Denis Vlasenko5b3adae2008-04-19 21:57:57 +0000184
Rob Landleyff97ee92006-06-02 19:03:01 +0000185 printf("HOSTNAME=%s\n", hostinfo->h_name);
186 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000187 }
Glenn L McGrathc11986d2002-11-10 23:42:27 +0000188
189 return EXIT_SUCCESS;
190}