summaryrefslogtreecommitdiff
path: root/networking/ipcalc.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-06-02 19:03:01 +0000
committerRob Landley <rob@landley.net>2006-06-02 19:03:01 +0000
commitff97ee9abda67ac0b85e0dc97a3d78ee85071cbf (patch)
tree2bad8febd7442a36910c0c0f6ad43f8bbd33e530 /networking/ipcalc.c
parentcf1f2ace391269655a80e42b692749da0c3d4047 (diff)
downloadbusybox-w32-ff97ee9abda67ac0b85e0dc97a3d78ee85071cbf.tar.gz
busybox-w32-ff97ee9abda67ac0b85e0dc97a3d78ee85071cbf.tar.bz2
busybox-w32-ff97ee9abda67ac0b85e0dc97a3d78ee85071cbf.zip
#ifdef reduction.
Diffstat (limited to 'networking/ipcalc.c')
-rw-r--r--networking/ipcalc.c157
1 files changed, 72 insertions, 85 deletions
diff --git a/networking/ipcalc.c b/networking/ipcalc.c
index ade4ed101..8c61e3a71 100644
--- a/networking/ipcalc.c
+++ b/networking/ipcalc.c
@@ -12,16 +12,12 @@
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 */ 13 */
14 14
15#include <stdio.h> 15#include "busybox.h"
16#include <stdlib.h>
17#include <ctype.h> 16#include <ctype.h>
18#include <getopt.h> 17#include <getopt.h>
19#include <sys/socket.h> 18#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h> 19#include <arpa/inet.h>
22 20
23#include "busybox.h"
24
25#define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;} 21#define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;}
26 22
27#define CLASS_A_NETMASK ntohl(0xFF000000) 23#define CLASS_A_NETMASK ntohl(0xFF000000)
@@ -56,6 +52,8 @@ static int get_prefix(unsigned long netmask)
56 } 52 }
57 return ret; 53 return ret;
58} 54}
55#else
56int get_prefix(unsigned long netmask);
59#endif 57#endif
60 58
61#define NETMASK 0x01 59#define NETMASK 0x01
@@ -65,23 +63,6 @@ static int get_prefix(unsigned long netmask)
65#define HOSTNAME 0x10 63#define HOSTNAME 0x10
66#define SILENT 0x20 64#define SILENT 0x20
67 65
68
69int 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#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS 66#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
86 static const struct option long_options[] = { 67 static const struct option long_options[] = {
87 {"netmask", no_argument, NULL, 'm'}, 68 {"netmask", no_argument, NULL, 'm'},
@@ -94,16 +75,24 @@ int ipcalc_main(int argc, char **argv)
94#endif 75#endif
95 {NULL, 0, NULL, 0} 76 {NULL, 0, NULL, 0}
96 }; 77 };
97
98 bb_applet_long_options = long_options;
99#endif
100 mode = bb_getopt_ulflags(argc, argv,
101#ifdef CONFIG_FEATURE_IPCALC_FANCY
102 "mbnphs"
103#else 78#else
104 "mbn" 79#define long_options 0
105#endif 80#endif
106 ); 81
82
83
84int ipcalc_main(int argc, char **argv)
85{
86 unsigned long mode;
87 int have_netmask = 0;
88 in_addr_t netmask, broadcast, network, ipaddr;
89 struct in_addr a;
90 char *ipstr;
91
92 if (ENABLE_FEATURE_IPCALC_LONG_OPTIONS)
93 bb_applet_long_options = long_options;
94
95 mode = bb_getopt_ulflags(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
107 96
108 argc -= optind; 97 argc -= optind;
109 argv += optind; 98 argv += optind;
@@ -115,40 +104,42 @@ int ipcalc_main(int argc, char **argv)
115 bb_show_usage(); 104 bb_show_usage();
116 } 105 }
117 106
118#ifdef CONFIG_FEATURE_IPCALC_FANCY 107 ipstr = argv[0];
119 prefixstr = ipstr = argv[0]; 108 if (ENABLE_FEATURE_IPCALC_FANCY) {
120 109 unsigned long netprefix = 0;
121 while(*prefixstr) { 110 char *prefixstr;
122 if (*prefixstr == '/') { 111
123 *prefixstr = (char)0; 112 prefixstr = ipstr;
124 prefixstr++; 113
125 if (*prefixstr) { 114 while(*prefixstr) {
126 unsigned int msk; 115 if (*prefixstr == '/') {
127 116 *prefixstr = (char)0;
128 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) { 117 prefixstr++;
129 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr), 118 if (*prefixstr) {
130 exit(EXIT_FAILURE)); 119 unsigned int msk;
131 } 120
132 netmask = 0; 121 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
133 msk = 0x80000000; 122 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
134 while (netprefix > 0) { 123 exit(EXIT_FAILURE));
135 netmask |= msk; 124 }
136 msk >>= 1; 125 netmask = 0;
137 netprefix--; 126 msk = 0x80000000;
127 while (netprefix > 0) {
128 netmask |= msk;
129 msk >>= 1;
130 netprefix--;
131 }
132 netmask = htonl(netmask);
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;
138 } 136 }
139 netmask = htonl(netmask); 137 break;
140 /* Even if it was 0, we will signify that we have a netmask. This allows */
141 /* for specification of default routes, etc which have a 0 netmask/prefix */
142 have_netmask = 1;
143 } 138 }
144 break; 139 prefixstr++;
145 } 140 }
146 prefixstr++;
147 } 141 }
148 ipaddr = inet_aton(ipstr, &a); 142 ipaddr = inet_aton(ipstr, &a);
149#else
150 ipaddr = inet_aton(argv[0], &a);
151#endif
152 143
153 if (ipaddr == 0) { 144 if (ipaddr == 0) {
154 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]), 145 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]),
@@ -157,13 +148,11 @@ int ipcalc_main(int argc, char **argv)
157 ipaddr = a.s_addr; 148 ipaddr = a.s_addr;
158 149
159 if (argc == 2) { 150 if (argc == 2) {
160#ifdef CONFIG_FEATURE_IPCALC_FANCY 151 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
161 if (have_netmask) { 152 IPCALC_MSG(bb_error_msg_and_die("Use prefix or netmask, not both.\n"),
162 IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"),
163 exit(EXIT_FAILURE)); 153 exit(EXIT_FAILURE));
164 } 154 }
165 155
166#endif
167 netmask = inet_aton(argv[1], &a); 156 netmask = inet_aton(argv[1], &a);
168 if (netmask == 0) { 157 if (netmask == 0) {
169 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]), 158 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]),
@@ -171,11 +160,9 @@ int ipcalc_main(int argc, char **argv)
171 } 160 }
172 netmask = a.s_addr; 161 netmask = a.s_addr;
173 } else { 162 } else {
174#ifdef CONFIG_FEATURE_IPCALC_FANCY
175 163
176 if (!have_netmask) 164 /* JHC - If the netmask wasn't provided then calculate it */
177#endif 165 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
178 /* JHC - If the netmask wasn't provided then calculate it */
179 netmask = get_netmask(ipaddr); 166 netmask = get_netmask(ipaddr);
180 } 167 }
181 168
@@ -193,28 +180,28 @@ int ipcalc_main(int argc, char **argv)
193 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network))); 180 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
194 } 181 }
195 182
196#ifdef CONFIG_FEATURE_IPCALC_FANCY 183 if (ENABLE_FEATURE_IPCALC_FANCY) {
197 if (mode & NETPREFIX) { 184 if (mode & NETPREFIX) {
198 printf("PREFIX=%i\n", get_prefix(netmask)); 185 printf("PREFIX=%i\n", get_prefix(netmask));
199 } 186 }
200 187
201 if (mode & HOSTNAME) { 188 if (mode & HOSTNAME) {
202 struct hostent *hostinfo; 189 struct hostent *hostinfo;
203 int x; 190 int x;
204 191
205 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); 192 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
206 if (!hostinfo) { 193 if (!hostinfo) {
207 IPCALC_MSG(bb_herror_msg_and_die( 194 IPCALC_MSG(bb_herror_msg_and_die(
208 "cannot find hostname for %s", argv[0]),); 195 "cannot find hostname for %s", argv[0]),);
209 exit(EXIT_FAILURE); 196 exit(EXIT_FAILURE);
210 } 197 }
211 for (x = 0; hostinfo->h_name[x]; x++) { 198 for (x = 0; hostinfo->h_name[x]; x++) {
212 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]); 199 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
213 } 200 }
214 201
215 printf("HOSTNAME=%s\n", hostinfo->h_name); 202 printf("HOSTNAME=%s\n", hostinfo->h_name);
203 }
216 } 204 }
217#endif
218 205
219 return EXIT_SUCCESS; 206 return EXIT_SUCCESS;
220} 207}