aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2019-10-12 19:24:38 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2019-10-12 19:24:38 +0200
commit339875381800668df26922dd0fb8789e9862698d (patch)
tree0f2fb9034c8a44eef792778c051012dd3ed7af4d
parent6dcf563633d3dae95c4ba4df3a5acc8fdd3f3107 (diff)
downloadbusybox-w32-339875381800668df26922dd0fb8789e9862698d.tar.gz
busybox-w32-339875381800668df26922dd0fb8789e9862698d.tar.bz2
busybox-w32-339875381800668df26922dd0fb8789e9862698d.zip
brctl: code shrink
packed_usage 33253 33255 +2 write_uint 96 90 -6 show_bridge_timer 93 69 -24 brctl_main 2338 2235 -103 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/3 up/down: 2/-133) Total: -131 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/brctl.c60
1 files changed, 28 insertions, 32 deletions
diff --git a/networking/brctl.c b/networking/brctl.c
index e15710b5e..04ea08253 100644
--- a/networking/brctl.c
+++ b/networking/brctl.c
@@ -54,7 +54,7 @@
54//usage: "\n delif BRIDGE IFACE Delete IFACE from BRIDGE" 54//usage: "\n delif BRIDGE IFACE Delete IFACE from BRIDGE"
55//usage: IF_FEATURE_BRCTL_FANCY( 55//usage: IF_FEATURE_BRCTL_FANCY(
56//usage: "\n stp BRIDGE 1/yes/on|0/no/off STP on/off" 56//usage: "\n stp BRIDGE 1/yes/on|0/no/off STP on/off"
57//usage: "\n showstp BRIDGE Show stp info" 57//usage: "\n showstp BRIDGE Show STP info"
58//usage: "\n setageing BRIDGE SECONDS Set ageing time" 58//usage: "\n setageing BRIDGE SECONDS Set ageing time"
59//usage: "\n setfd BRIDGE SECONDS Set bridge forward delay" 59//usage: "\n setfd BRIDGE SECONDS Set bridge forward delay"
60//usage: "\n sethello BRIDGE SECONDS Set hello time" 60//usage: "\n sethello BRIDGE SECONDS Set hello time"
@@ -67,7 +67,6 @@
67// Not yet implemented: 67// Not yet implemented:
68// hairpin BRIDGE IFACE on|off Hairpin on/off 68// hairpin BRIDGE IFACE on|off Hairpin on/off
69 69
70
71#include "libbb.h" 70#include "libbb.h"
72#include "common_bufsiz.h" 71#include "common_bufsiz.h"
73#include <linux/sockios.h> 72#include <linux/sockios.h>
@@ -130,7 +129,7 @@ static int show_bridge(const char *name, int need_hdr)
130 *bridge name bridge id STP enabled interfaces 129 *bridge name bridge id STP enabled interfaces
131 *br0 8000.000000000000 no eth0 130 *br0 8000.000000000000 no eth0
132 */ 131 */
133 char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 32]; 132 char pathbuf[IFNAMSIZ + sizeof("/bridge/bridge_id") + 8];
134 int tabs; 133 int tabs;
135 DIR *ifaces; 134 DIR *ifaces;
136 struct dirent *ent; 135 struct dirent *ent;
@@ -194,7 +193,11 @@ static void write_uint(const char *name, const char *leaf, unsigned val)
194 n = sprintf(filedata, "%u\n", val); 193 n = sprintf(filedata, "%u\n", val);
195 if (write(fd, filedata, n) < 0) 194 if (write(fd, filedata, n) < 0)
196 bb_simple_perror_msg_and_die(name); 195 bb_simple_perror_msg_and_die(name);
197 close(fd); 196 /* So far all callers exit very soon after calling us.
197 * Do not bother closing fd (unless debugging):
198 */
199 if (ENABLE_FEATURE_CLEAN_UP)
200 close(fd);
198} 201}
199 202
200struct fdb_entry { 203struct fdb_entry {
@@ -217,15 +220,18 @@ static int compare_fdbs(const void *_f0, const void *_f1)
217 220
218static size_t read_bridge_forward_db(const char *name, struct fdb_entry **_fdb) 221static size_t read_bridge_forward_db(const char *name, struct fdb_entry **_fdb)
219{ 222{
223 char pathbuf[IFNAMSIZ + sizeof("/brforward") + 8];
220 struct fdb_entry *fdb; 224 struct fdb_entry *fdb;
221 size_t nentries; 225 size_t nentries;
222 char *path;
223 int fd; 226 int fd;
224 ssize_t cc; 227 ssize_t cc;
225 228
226 path = concat_path_file(name, "brforward"); 229#if IFNAMSIZ == 16
227 fd = open(path, O_RDONLY); 230 sprintf(pathbuf, "%.16s/brforward", name);
228 free(path); 231#else
232 sprintf(pathbuf, "%.*s/brforward", (int)IFNAMSIZ, name);
233#endif
234 fd = open(pathbuf, O_RDONLY);
229 if (fd < 0) 235 if (fd < 0)
230 bb_error_msg_and_die("bridge %s does not exist", name); 236 bb_error_msg_and_die("bridge %s does not exist", name);
231 237
@@ -243,7 +249,8 @@ static size_t read_bridge_forward_db(const char *name, struct fdb_entry **_fdb)
243 ++nentries; 249 ++nentries;
244 } 250 }
245 251
246 close(fd); 252 if (ENABLE_FEATURE_CLEAN_UP)
253 close(fd);
247 254
248 qsort(fdb, nentries, sizeof(*fdb), compare_fdbs); 255 qsort(fdb, nentries, sizeof(*fdb), compare_fdbs);
249 256
@@ -262,9 +269,8 @@ static void show_bridge_macs(const char *name)
262 printf("port no\tmac addr\t\tis local?\tageing timer\n"); 269 printf("port no\tmac addr\t\tis local?\tageing timer\n");
263 for (i = 0; i < nentries; ++i) { 270 for (i = 0; i < nentries; ++i) {
264 const struct fdb_entry *f = &fdb[i]; 271 const struct fdb_entry *f = &fdb[i];
265 unsigned long tvmsec = 10UL * f->ageing_timer_value; 272 unsigned tv_sec = f->ageing_timer_value / 100;
266 unsigned tv_sec = tvmsec / 1000; 273 unsigned tv_csec = f->ageing_timer_value % 100;
267 unsigned tv_msec = tvmsec % 1000;
268 printf("%3u\t" 274 printf("%3u\t"
269 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\t" 275 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\t"
270 "%s\t\t" 276 "%s\t\t"
@@ -273,25 +279,26 @@ static void show_bridge_macs(const char *name)
273 f->mac_addr[0], f->mac_addr[1], f->mac_addr[2], 279 f->mac_addr[0], f->mac_addr[1], f->mac_addr[2],
274 f->mac_addr[3], f->mac_addr[4], f->mac_addr[5], 280 f->mac_addr[3], f->mac_addr[4], f->mac_addr[5],
275 (f->is_local ? "yes" : "no"), 281 (f->is_local ? "yes" : "no"),
276 tv_sec, tv_msec / 10 282 tv_sec, tv_csec
277 ); 283 );
278 } 284 }
279 285
280 free(fdb); 286 if (ENABLE_FEATURE_CLEAN_UP)
287 free(fdb);
281} 288}
282 289
283static void show_bridge_timer(const char *msg) 290static void show_bridge_timer(const char *msg)
284{ 291{
285 unsigned long long tvmsec = 10 * xstrtoull(filedata, 0); 292 unsigned long long centisec = xstrtoull(filedata, 0);
286 unsigned tv_sec = tvmsec / 1000; 293 unsigned tv_sec = centisec / 100;
287 unsigned tv_msec = tvmsec % 1000; 294 unsigned tv_csec = centisec % 100;
288 printf("%s%4u.%.2u", msg, tv_sec, tv_msec / 10); 295 printf("%s%4u.%.2u", msg, tv_sec, tv_csec);
289} 296}
290 297
291static const char *show_bridge_state(unsigned state) 298static const char *show_bridge_state(unsigned state)
292{ 299{
293 /* See linux/if_bridge.h, BR_STATE_ constants */ 300 /* See linux/if_bridge.h, BR_STATE_ constants */
294 static const char state_names[] = 301 static const char state_names[] ALIGN1 =
295 "disabled\0" //BR_STATE_DISABLED 0 302 "disabled\0" //BR_STATE_DISABLED 0
296 "listening\0" //BR_STATE_LISTENING 1 303 "listening\0" //BR_STATE_LISTENING 1
297 "learning\0" //BR_STATE_LEARNING 2 304 "learning\0" //BR_STATE_LEARNING 2
@@ -310,7 +317,7 @@ static void printf_xstrtou(const char *fmt)
310 317
311static void show_bridge_port(const char *name) 318static void show_bridge_port(const char *name)
312{ 319{
313 char pathbuf[IFNAMSIZ + sizeof("/brport/forward_delay_timer") + 32]; 320 char pathbuf[IFNAMSIZ + sizeof("/brport/forward_delay_timer") + 8];
314 char *sfx; 321 char *sfx;
315 322
316#if IFNAMSIZ == 16 323#if IFNAMSIZ == 16
@@ -407,7 +414,7 @@ static void show_bridge_ports(const char *name)
407 414
408static void show_bridge_stp(const char *name) 415static void show_bridge_stp(const char *name)
409{ 416{
410 char pathbuf[IFNAMSIZ + sizeof("/bridge/topology_change_timer") + 32]; 417 char pathbuf[IFNAMSIZ + sizeof("/bridge/topology_change_timer") + 8];
411 char *sfx; 418 char *sfx;
412 419
413#if IFNAMSIZ == 16 420#if IFNAMSIZ == 16
@@ -614,7 +621,6 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
614 bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name); 621 bb_error_msg_and_die(bb_msg_invalid_arg_to, *argv, applet_name);
615 onoff = (unsigned)onoff / 4; 622 onoff = (unsigned)onoff / 4;
616 write_uint(br, "bridge/stp_state", onoff); 623 write_uint(br, "bridge/stp_state", onoff);
617 //goto done_next_argv;
618 return EXIT_SUCCESS; 624 return EXIT_SUCCESS;
619 } 625 }
620 626
@@ -634,13 +640,11 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
634 ), 640 ),
635 str_to_jiffies(*argv) 641 str_to_jiffies(*argv)
636 ); 642 );
637 //goto done_next_argv;
638 return EXIT_SUCCESS; 643 return EXIT_SUCCESS;
639 } 644 }
640 645
641 if (key == ARG_setbridgeprio) { 646 if (key == ARG_setbridgeprio) {
642 write_uint(br, "bridge/priority", xatoi_positive(*argv)); 647 write_uint(br, "bridge/priority", xatoi_positive(*argv));
643 //goto done_next_argv;
644 return EXIT_SUCCESS; 648 return EXIT_SUCCESS;
645 } 649 }
646 650
@@ -663,8 +667,6 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
663 ), 667 ),
664 xatoi_positive(argv[1]) 668 xatoi_positive(argv[1])
665 ); 669 );
666 //argv++;
667 //goto done_next_argv;
668 return EXIT_SUCCESS; 670 return EXIT_SUCCESS;
669 } 671 }
670#endif 672#endif
@@ -682,16 +684,10 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
682 key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF, 684 key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
683 &ifr, "bridge %s", br 685 &ifr, "bridge %s", br
684 ); 686 );
685 //close(fd);
686 //goto done_next_argv;
687 if (ENABLE_FEATURE_CLEAN_UP) 687 if (ENABLE_FEATURE_CLEAN_UP)
688 close(fd); 688 close(fd);
689 return EXIT_SUCCESS; 689 return EXIT_SUCCESS;
690 } 690 }
691
692// done_next_argv:
693// argv++;
694// done:
695 } 691 }
696 692
697 return EXIT_SUCCESS; 693 return EXIT_SUCCESS;