aboutsummaryrefslogtreecommitdiff
path: root/networking/ipcalc.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/ipcalc.c')
-rw-r--r--networking/ipcalc.c44
1 files changed, 21 insertions, 23 deletions
diff --git a/networking/ipcalc.c b/networking/ipcalc.c
index 18abc12eb..78558495e 100644
--- a/networking/ipcalc.c
+++ b/networking/ipcalc.c
@@ -75,29 +75,34 @@ int get_prefix(unsigned long netmask);
75#endif 75#endif
76 76
77int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 77int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78int ipcalc_main(int argc, char **argv) 78int ipcalc_main(int argc UNUSED_PARAM, char **argv)
79{ 79{
80 unsigned opt; 80 unsigned opt;
81 int have_netmask = 0; 81 bool have_netmask = 0;
82 in_addr_t netmask, broadcast, network, ipaddr; 82 struct in_addr s_netmask, s_broadcast, s_network, s_ipaddr;
83 struct in_addr a; 83 /* struct in_addr { in_addr_t s_addr; } and in_addr_t
84 * (which in turn is just a typedef to uint32_t)
85 * are essentially the same type. A few macros for less verbosity: */
86#define netmask (s_netmask.s_addr)
87#define broadcast (s_broadcast.s_addr)
88#define network (s_network.s_addr)
89#define ipaddr (s_ipaddr.s_addr)
84 char *ipstr; 90 char *ipstr;
85 91
86#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS 92#if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
87 applet_long_options = ipcalc_longopts; 93 applet_long_options = ipcalc_longopts;
88#endif 94#endif
89 opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs")); 95 opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
90 argc -= optind;
91 argv += optind; 96 argv += optind;
97 if (opt & SILENT)
98 logmode = LOGMODE_NONE; /* suppress error_msg() output */
92 if (opt & (BROADCAST | NETWORK | NETPREFIX)) { 99 if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
93 if (argc > 2 || argc <= 0) 100 if (!argv[0] || !argv[1] || argv[2])
94 bb_show_usage(); 101 bb_show_usage();
95 } else { 102 } else {
96 if (argc != 1) 103 if (!argv[0] || argv[1])
97 bb_show_usage(); 104 bb_show_usage();
98 } 105 }
99 if (opt & SILENT)
100 logmode = LOGMODE_NONE; /* Suppress error_msg() output */
101 106
102 ipstr = argv[0]; 107 ipstr = argv[0];
103 if (ENABLE_FEATURE_IPCALC_FANCY) { 108 if (ENABLE_FEATURE_IPCALC_FANCY) {
@@ -108,8 +113,7 @@ int ipcalc_main(int argc, char **argv)
108 113
109 while (*prefixstr) { 114 while (*prefixstr) {
110 if (*prefixstr == '/') { 115 if (*prefixstr == '/') {
111 *prefixstr = (char)0; 116 *prefixstr++ = '\0';
112 prefixstr++;
113 if (*prefixstr) { 117 if (*prefixstr) {
114 unsigned msk; 118 unsigned msk;
115 netprefix = xatoul_range(prefixstr, 0, 32); 119 netprefix = xatoul_range(prefixstr, 0, 32);
@@ -130,42 +134,36 @@ int ipcalc_main(int argc, char **argv)
130 prefixstr++; 134 prefixstr++;
131 } 135 }
132 } 136 }
133 ipaddr = inet_aton(ipstr, &a);
134 137
135 if (ipaddr == 0) { 138 if (inet_aton(ipstr, &s_ipaddr) == 0) {
136 bb_error_msg_and_die("bad IP address: %s", argv[0]); 139 bb_error_msg_and_die("bad IP address: %s", argv[0]);
137 } 140 }
138 ipaddr = a.s_addr;
139 141
140 if (argc == 2) { 142 if (argv[1]) {
141 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) { 143 if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
142 bb_error_msg_and_die("use prefix or netmask, not both"); 144 bb_error_msg_and_die("use prefix or netmask, not both");
143 } 145 }
144 146 if (inet_aton(argv[1], &s_netmask) == 0) {
145 netmask = inet_aton(argv[1], &a);
146 if (netmask == 0) {
147 bb_error_msg_and_die("bad netmask: %s", argv[1]); 147 bb_error_msg_and_die("bad netmask: %s", argv[1]);
148 } 148 }
149 netmask = a.s_addr;
150 } else { 149 } else {
151
152 /* JHC - If the netmask wasn't provided then calculate it */ 150 /* JHC - If the netmask wasn't provided then calculate it */
153 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask) 151 if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
154 netmask = get_netmask(ipaddr); 152 netmask = get_netmask(ipaddr);
155 } 153 }
156 154
157 if (opt & NETMASK) { 155 if (opt & NETMASK) {
158 printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask))); 156 printf("NETMASK=%s\n", inet_ntoa(s_netmask));
159 } 157 }
160 158
161 if (opt & BROADCAST) { 159 if (opt & BROADCAST) {
162 broadcast = (ipaddr & netmask) | ~netmask; 160 broadcast = (ipaddr & netmask) | ~netmask;
163 printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast))); 161 printf("BROADCAST=%s\n", inet_ntoa(s_broadcast));
164 } 162 }
165 163
166 if (opt & NETWORK) { 164 if (opt & NETWORK) {
167 network = ipaddr & netmask; 165 network = ipaddr & netmask;
168 printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network))); 166 printf("NETWORK=%s\n", inet_ntoa(s_network));
169 } 167 }
170 168
171 if (ENABLE_FEATURE_IPCALC_FANCY) { 169 if (ENABLE_FEATURE_IPCALC_FANCY) {