aboutsummaryrefslogtreecommitdiff
path: root/networking/ipcalc.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-08 12:49:22 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-08 12:49:22 +0000
commit1385899416a4396385ad421ae1f532be7103738a (patch)
treefc4d14a910593d1235318bb36abe5e9f72d2039e /networking/ipcalc.c
parent5625415085e68ac5e150f54e685417c866620d76 (diff)
downloadbusybox-w32-1385899416a4396385ad421ae1f532be7103738a.tar.gz
busybox-w32-1385899416a4396385ad421ae1f532be7103738a.tar.bz2
busybox-w32-1385899416a4396385ad421ae1f532be7103738a.zip
attempt to regularize atoi mess.
Diffstat (limited to 'networking/ipcalc.c')
-rw-r--r--networking/ipcalc.c48
1 files changed, 18 insertions, 30 deletions
diff --git a/networking/ipcalc.c b/networking/ipcalc.c
index 576dfc853..909373cbb 100644
--- a/networking/ipcalc.c
+++ b/networking/ipcalc.c
@@ -18,8 +18,6 @@
18#include <sys/socket.h> 18#include <sys/socket.h>
19#include <arpa/inet.h> 19#include <arpa/inet.h>
20 20
21#define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;}
22
23#define CLASS_A_NETMASK ntohl(0xFF000000) 21#define CLASS_A_NETMASK ntohl(0xFF000000)
24#define CLASS_B_NETMASK ntohl(0xFFFF0000) 22#define CLASS_B_NETMASK ntohl(0xFFFF0000)
25#define CLASS_C_NETMASK ntohl(0xFFFFFF00) 23#define CLASS_C_NETMASK ntohl(0xFFFFFF00)
@@ -56,6 +54,7 @@ static int get_prefix(unsigned long netmask)
56int get_prefix(unsigned long netmask); 54int get_prefix(unsigned long netmask);
57#endif 55#endif
58 56
57
59#define NETMASK 0x01 58#define NETMASK 0x01
60#define BROADCAST 0x02 59#define BROADCAST 0x02
61#define NETWORK 0x04 60#define NETWORK 0x04
@@ -78,12 +77,9 @@ int get_prefix(unsigned long netmask);
78#else 77#else
79#define long_options 0 78#define long_options 0
80#endif 79#endif
81
82
83
84int ipcalc_main(int argc, char **argv) 80int ipcalc_main(int argc, char **argv)
85{ 81{
86 unsigned long mode; 82 unsigned opt;
87 int have_netmask = 0; 83 int have_netmask = 0;
88 in_addr_t netmask, broadcast, network, ipaddr; 84 in_addr_t netmask, broadcast, network, ipaddr;
89 struct in_addr a; 85 struct in_addr a;
@@ -92,17 +88,18 @@ int ipcalc_main(int argc, char **argv)
92 if (ENABLE_FEATURE_IPCALC_LONG_OPTIONS) 88 if (ENABLE_FEATURE_IPCALC_LONG_OPTIONS)
93 applet_long_options = long_options; 89 applet_long_options = long_options;
94 90
95 mode = getopt32(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs")); 91 opt = getopt32(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
96
97 argc -= optind; 92 argc -= optind;
98 argv += optind; 93 argv += optind;
99 if (mode & (BROADCAST | NETWORK | NETPREFIX)) { 94 if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
100 if (argc > 2 || argc <= 0) 95 if (argc > 2 || argc <= 0)
101 bb_show_usage(); 96 bb_show_usage();
102 } else { 97 } else {
103 if (argc != 1) 98 if (argc != 1)
104 bb_show_usage(); 99 bb_show_usage();
105 } 100 }
101 if (opt & SILENT)
102 logmode = LOGMODE_NONE; /* Suppress error_msg() output */
106 103
107 ipstr = argv[0]; 104 ipstr = argv[0];
108 if (ENABLE_FEATURE_IPCALC_FANCY) { 105 if (ENABLE_FEATURE_IPCALC_FANCY) {
@@ -111,17 +108,13 @@ int ipcalc_main(int argc, char **argv)
111 108
112 prefixstr = ipstr; 109 prefixstr = ipstr;
113 110
114 while(*prefixstr) { 111 while (*prefixstr) {
115 if (*prefixstr == '/') { 112 if (*prefixstr == '/') {
116 *prefixstr = (char)0; 113 *prefixstr = (char)0;
117 prefixstr++; 114 prefixstr++;
118 if (*prefixstr) { 115 if (*prefixstr) {
119 unsigned int msk; 116 unsigned msk;
120 117 netprefix = xatoul_range(prefixstr, 0, 32);
121 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
122 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s", prefixstr),
123 exit(EXIT_FAILURE));
124 }
125 netmask = 0; 118 netmask = 0;
126 msk = 0x80000000; 119 msk = 0x80000000;
127 while (netprefix > 0) { 120 while (netprefix > 0) {
@@ -142,21 +135,18 @@ int ipcalc_main(int argc, char **argv)
142 ipaddr = inet_aton(ipstr, &a); 135 ipaddr = inet_aton(ipstr, &a);
143 136
144 if (ipaddr == 0) { 137 if (ipaddr == 0) {
145 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]), 138 bb_error_msg_and_die("bad IP address: %s", argv[0]);
146 exit(EXIT_FAILURE));
147 } 139 }
148 ipaddr = a.s_addr; 140 ipaddr = a.s_addr;
149 141
150 if (argc == 2) { 142 if (argc == 2) {
151 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) { 143 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
152 IPCALC_MSG(bb_error_msg_and_die("Use prefix or netmask, not both"), 144 bb_error_msg_and_die("use prefix or netmask, not both");
153 exit(EXIT_FAILURE));
154 } 145 }
155 146
156 netmask = inet_aton(argv[1], &a); 147 netmask = inet_aton(argv[1], &a);
157 if (netmask == 0) { 148 if (netmask == 0) {
158 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]), 149 bb_error_msg_and_die("bad netmask: %s", argv[1]);
159 exit(EXIT_FAILURE));
160 } 150 }
161 netmask = a.s_addr; 151 netmask = a.s_addr;
162 } else { 152 } else {
@@ -166,34 +156,32 @@ int ipcalc_main(int argc, char **argv)
166 netmask = get_netmask(ipaddr); 156 netmask = get_netmask(ipaddr);
167 } 157 }
168 158
169 if (mode & NETMASK) { 159 if (opt & NETMASK) {
170 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask))); 160 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
171 } 161 }
172 162
173 if (mode & BROADCAST) { 163 if (opt & BROADCAST) {
174 broadcast = (ipaddr & netmask) | ~netmask; 164 broadcast = (ipaddr & netmask) | ~netmask;
175 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast))); 165 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
176 } 166 }
177 167
178 if (mode & NETWORK) { 168 if (opt & NETWORK) {
179 network = ipaddr & netmask; 169 network = ipaddr & netmask;
180 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network))); 170 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
181 } 171 }
182 172
183 if (ENABLE_FEATURE_IPCALC_FANCY) { 173 if (ENABLE_FEATURE_IPCALC_FANCY) {
184 if (mode & NETPREFIX) { 174 if (opt & NETPREFIX) {
185 printf("PREFIX=%i\n", get_prefix(netmask)); 175 printf("PREFIX=%i\n", get_prefix(netmask));
186 } 176 }
187 177
188 if (mode & HOSTNAME) { 178 if (opt & HOSTNAME) {
189 struct hostent *hostinfo; 179 struct hostent *hostinfo;
190 int x; 180 int x;
191 181
192 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); 182 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
193 if (!hostinfo) { 183 if (!hostinfo) {
194 IPCALC_MSG(bb_herror_msg_and_die( 184 bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);
195 "cannot find hostname for %s", argv[0]),);
196 exit(EXIT_FAILURE);
197 } 185 }
198 for (x = 0; hostinfo->h_name[x]; x++) { 186 for (x = 0; hostinfo->h_name[x]; x++) {
199 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]); 187 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);