aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-04-06 07:17:02 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-04-06 07:17:02 +0000
commit278a1c22645263f1a82bb3437345e3d96c3f13eb (patch)
tree1e9f967ca896674212d8aecb17919bacbb4c1515
parentad4da989e3767cdf4620725c16908b4f99dbe2c9 (diff)
downloadbusybox-w32-278a1c22645263f1a82bb3437345e3d96c3f13eb.tar.gz
busybox-w32-278a1c22645263f1a82bb3437345e3d96c3f13eb.tar.bz2
busybox-w32-278a1c22645263f1a82bb3437345e3d96c3f13eb.zip
brctl: optional support for "show" cmd (by L. Gabriel Somlo <somlo AT cmu.edu>)
function old new delta brctl_main 739 1186 +447 if_indextoname - 104 +104 static.keywords 827 841 +14 static.ops - 7 +7 packed_usage 23978 23976 -2
-rw-r--r--include/libbb.h6
-rw-r--r--include/usage.h6
-rw-r--r--libbb/xfuncs.c23
-rw-r--r--networking/Config.in16
-rw-r--r--networking/brctl.c180
5 files changed, 147 insertions, 84 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 202574adb..1c54a32d5 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1017,15 +1017,15 @@ extern int update_passwd(const char *filename, const char *username,
1017int get_terminal_width_height(int fd, int *width, int *height); 1017int get_terminal_width_height(int fd, int *width, int *height);
1018 1018
1019int ioctl_or_perror(int fd, int request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5))); 1019int ioctl_or_perror(int fd, int request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5)));
1020void ioctl_or_perror_and_die(int fd, int request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5))); 1020int ioctl_or_perror_and_die(int fd, int request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5)));
1021#if ENABLE_IOCTL_HEX2STR_ERROR 1021#if ENABLE_IOCTL_HEX2STR_ERROR
1022int bb_ioctl_or_warn(int fd, int request, void *argp, const char *ioctl_name); 1022int bb_ioctl_or_warn(int fd, int request, void *argp, const char *ioctl_name);
1023void bb_xioctl(int fd, int request, void *argp, const char *ioctl_name); 1023int bb_xioctl(int fd, int request, void *argp, const char *ioctl_name);
1024#define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp,#request) 1024#define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp,#request)
1025#define xioctl(fd,request,argp) bb_xioctl(fd,request,argp,#request) 1025#define xioctl(fd,request,argp) bb_xioctl(fd,request,argp,#request)
1026#else 1026#else
1027int bb_ioctl_or_warn(int fd, int request, void *argp); 1027int bb_ioctl_or_warn(int fd, int request, void *argp);
1028void bb_xioctl(int fd, int request, void *argp); 1028int bb_xioctl(int fd, int request, void *argp);
1029#define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp) 1029#define ioctl_or_warn(fd,request,argp) bb_ioctl_or_warn(fd,request,argp)
1030#define xioctl(fd,request,argp) bb_xioctl(fd,request,argp) 1030#define xioctl(fd,request,argp) bb_xioctl(fd,request,argp)
1031#endif 1031#endif
diff --git a/include/usage.h b/include/usage.h
index 0849c6831..e007acdfa 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -138,6 +138,9 @@
138#define brctl_full_usage \ 138#define brctl_full_usage \
139 "Manage ethernet bridges.\n" \ 139 "Manage ethernet bridges.\n" \
140 "\nCommands:" \ 140 "\nCommands:" \
141 USE_FEATURE_BRCTL_SHOW( \
142 "\n show Show a list of bridges" \
143 ) \
141 "\n addbr BRIDGE Create BRIDGE" \ 144 "\n addbr BRIDGE Create BRIDGE" \
142 "\n delbr BRIDGE Delete BRIDGE" \ 145 "\n delbr BRIDGE Delete BRIDGE" \
143 "\n addif BRIDGE IFACE Add IFACE to BRIDGE" \ 146 "\n addif BRIDGE IFACE Add IFACE to BRIDGE" \
@@ -151,7 +154,8 @@
151 "\n setportprio BRIDGE PRIO Set port priority" \ 154 "\n setportprio BRIDGE PRIO Set port priority" \
152 "\n setbridgeprio BRIDGE PRIO Set bridge priority" \ 155 "\n setbridgeprio BRIDGE PRIO Set bridge priority" \
153 "\n stp BRIDGE [1|0] STP on/off" \ 156 "\n stp BRIDGE [1|0] STP on/off" \
154 ) 157 ) \
158
155#define bunzip2_trivial_usage \ 159#define bunzip2_trivial_usage \
156 "[OPTION]... [FILE]" 160 "[OPTION]... [FILE]"
157#define bunzip2_full_usage \ 161#define bunzip2_full_usage \
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index ca7f94173..125063935 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -704,17 +704,20 @@ int get_terminal_width_height(int fd, int *width, int *height)
704 return ret; 704 return ret;
705} 705}
706 706
707void ioctl_or_perror_and_die(int fd, int request, void *argp, const char *fmt,...) 707int ioctl_or_perror_and_die(int fd, int request, void *argp, const char *fmt,...)
708{ 708{
709 int ret;
709 va_list p; 710 va_list p;
710 711
711 if (ioctl(fd, request, argp) < 0) { 712 ret = ioctl(fd, request, argp);
713 if (ret < 0) {
712 va_start(p, fmt); 714 va_start(p, fmt);
713 bb_verror_msg(fmt, p, strerror(errno)); 715 bb_verror_msg(fmt, p, strerror(errno));
714 /* xfunc_die can actually longjmp, so be nice */ 716 /* xfunc_die can actually longjmp, so be nice */
715 va_end(p); 717 va_end(p);
716 xfunc_die(); 718 xfunc_die();
717 } 719 }
720 return ret;
718} 721}
719 722
720int ioctl_or_perror(int fd, int request, void *argp, const char *fmt,...) 723int ioctl_or_perror(int fd, int request, void *argp, const char *fmt,...)
@@ -740,10 +743,14 @@ int bb_ioctl_or_warn(int fd, int request, void *argp, const char *ioctl_name)
740 bb_simple_perror_msg(ioctl_name); 743 bb_simple_perror_msg(ioctl_name);
741 return ret; 744 return ret;
742} 745}
743void bb_xioctl(int fd, int request, void *argp, const char *ioctl_name) 746int bb_xioctl(int fd, int request, void *argp, const char *ioctl_name)
744{ 747{
745 if (ioctl(fd, request, argp) < 0) 748 int ret;
749
750 ret = ioctl(fd, request, argp);
751 if (ret < 0)
746 bb_simple_perror_msg_and_die(ioctl_name); 752 bb_simple_perror_msg_and_die(ioctl_name);
753 return ret;
747} 754}
748#else 755#else
749int bb_ioctl_or_warn(int fd, int request, void *argp) 756int bb_ioctl_or_warn(int fd, int request, void *argp)
@@ -755,9 +762,13 @@ int bb_ioctl_or_warn(int fd, int request, void *argp)
755 bb_perror_msg("ioctl %#x failed", request); 762 bb_perror_msg("ioctl %#x failed", request);
756 return ret; 763 return ret;
757} 764}
758void bb_xioctl(int fd, int request, void *argp) 765int bb_xioctl(int fd, int request, void *argp)
759{ 766{
760 if (ioctl(fd, request, argp) < 0) 767 int ret;
768
769 ret = ioctl(fd, request, argp);
770 if (ret < 0)
761 bb_perror_msg_and_die("ioctl %#x failed", request); 771 bb_perror_msg_and_die("ioctl %#x failed", request);
772 return ret;
762} 773}
763#endif 774#endif
diff --git a/networking/Config.in b/networking/Config.in
index f0a93078e..f7f189cd6 100644
--- a/networking/Config.in
+++ b/networking/Config.in
@@ -54,14 +54,6 @@ config BRCTL
54 Manage ethernet bridges. 54 Manage ethernet bridges.
55 Supports addbr/delbr and addif/delif. 55 Supports addbr/delbr and addif/delif.
56 56
57#config FEATURE_BRCTL_SHOW
58# bool "Support show, showmac and showstp"
59# default n
60# depends on BRCTL
61# help
62# Add support for option which print the current config:
63# showmacs, showstp, show
64
65config FEATURE_BRCTL_FANCY 57config FEATURE_BRCTL_FANCY
66 bool "Fancy options" 58 bool "Fancy options"
67 default n 59 default n
@@ -73,6 +65,14 @@ config FEATURE_BRCTL_FANCY
73 stp 65 stp
74 This adds about 600 bytes. 66 This adds about 600 bytes.
75 67
68config FEATURE_BRCTL_SHOW
69 bool "Support show, showmac and showstp"
70 default n
71 depends on BRCTL && FEATURE_BRCTL_FANCY
72 help
73 Add support for option which prints the current config:
74 showmacs, showstp, show
75
76config DNSD 76config DNSD
77 bool "dnsd" 77 bool "dnsd"
78 default n 78 default n
diff --git a/networking/brctl.c b/networking/brctl.c
index 2bb03dd82..bd13878b2 100644
--- a/networking/brctl.c
+++ b/networking/brctl.c
@@ -25,12 +25,6 @@
25/* #define BRCTL_USE_INTERNAL 0 */ /* use exact conversion */ 25/* #define BRCTL_USE_INTERNAL 0 */ /* use exact conversion */
26#define BRCTL_USE_INTERNAL 1 26#define BRCTL_USE_INTERNAL 1
27 27
28#ifdef ENABLE_FEATURE_BRCTL_SHOW
29#error Remove these
30#endif
31#define ENABLE_FEATURE_BRCTL_SHOW 0
32#define USE_FEATURE_BRCTL_SHOW(...)
33
34#if ENABLE_FEATURE_BRCTL_FANCY 28#if ENABLE_FEATURE_BRCTL_FANCY
35#include <linux/if_bridge.h> 29#include <linux/if_bridge.h>
36 30
@@ -96,7 +90,7 @@ int brctl_main(int argc ATTRIBUTE_UNUSED, char **argv)
96 "setageing\0" "setfd\0" "sethello\0" "setmaxage\0" 90 "setageing\0" "setfd\0" "sethello\0" "setmaxage\0"
97 "setpathcost\0" "setportprio\0" "setbridgeprio\0" 91 "setpathcost\0" "setportprio\0" "setbridgeprio\0"
98 ) 92 )
99 USE_FEATURE_BRCTL_SHOW("showmacs\0" "show\0"); 93 USE_FEATURE_BRCTL_SHOW("showmacs\0" "show\0");
100 94
101 enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif 95 enum { ARG_addbr = 0, ARG_delbr, ARG_addif, ARG_delif
102 USE_FEATURE_BRCTL_FANCY(, 96 USE_FEATURE_BRCTL_FANCY(,
@@ -104,31 +98,87 @@ int brctl_main(int argc ATTRIBUTE_UNUSED, char **argv)
104 ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage, 98 ARG_setageing, ARG_setfd, ARG_sethello, ARG_setmaxage,
105 ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio 99 ARG_setpathcost, ARG_setportprio, ARG_setbridgeprio
106 ) 100 )
107 USE_FEATURE_BRCTL_SHOW(, ARG_showmacs, ARG_show) 101 USE_FEATURE_BRCTL_SHOW(, ARG_showmacs, ARG_show)
108 }; 102 };
109 103
110 int fd; 104 int fd;
111 smallint key; 105 smallint key;
112 struct ifreq ifr; 106 struct ifreq ifr;
113 char *br, *brif; 107 char *br, *brif;
114#if ENABLE_FEATURE_BRCTL_FANCY
115 unsigned long args[4] = {0, 0, 0, 0};
116 int port;
117 int tmp;
118#endif
119 108
120 argv++; 109 argv++;
121 while (*argv) { 110 while (*argv) {
111#if ENABLE_FEATURE_BRCTL_FANCY
112 int ifidx[MAX_PORTS];
113 unsigned long args[4];
114 ifr.ifr_data = (char *) &args;
115#endif
116
122 key = index_in_strings(keywords, *argv); 117 key = index_in_strings(keywords, *argv);
123 if (key == -1) /* no match found in keywords array, bail out. */ 118 if (key == -1) /* no match found in keywords array, bail out. */
124 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); 119 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
125 argv++; 120 argv++;
121 fd = xsocket(AF_INET, SOCK_STREAM, 0);
122
126#if ENABLE_FEATURE_BRCTL_SHOW 123#if ENABLE_FEATURE_BRCTL_SHOW
127 if (key == ARG_show) { /* show */ 124 if (key == ARG_show) { /* show */
128 goto out; /* FIXME: implement me! :) */ 125 char brname[IFNAMSIZ];
126 int bridx[MAX_PORTS];
127 int i, num;
128 arm_ioctl(args, BRCTL_GET_BRIDGES,
129 (unsigned long) bridx, MAX_PORTS);
130 num = xioctl(fd, SIOCGIFBR, args);
131 printf("bridge name\tbridge id\t\tSTP enabled\tinterfaces\n");
132 for (i = 0; i < num; i++) {
133 char ifname[IFNAMSIZ];
134 int j, tabs;
135 struct __bridge_info bi;
136 unsigned char *x;
137
138 if (!if_indextoname(bridx[i], brname))
139 bb_perror_msg_and_die("can't get bridge name for index %d", i);
140 safe_strncpy(ifr.ifr_name, brname, IFNAMSIZ);
141
142 arm_ioctl(args, BRCTL_GET_BRIDGE_INFO,
143 (unsigned long) &bi, 0);
144 xioctl(fd, SIOCDEVPRIVATE, &ifr);
145 printf("%s\t\t", brname);
146
147 /* print bridge id */
148 x = (unsigned char *) &bi.bridge_id;
149 for (j = 0; j < 8; j++) {
150 printf("%.2x", x[j]);
151 if (j == 1)
152 bb_putchar('.');
153 }
154 printf(bi.stp_enabled ? "\tyes" : "\tno");
155
156 /* print interface list */
157 arm_ioctl(args, BRCTL_GET_PORT_LIST,
158 (unsigned long) ifidx, MAX_PORTS);
159 xioctl(fd, SIOCDEVPRIVATE, &ifr);
160 tabs = 0;
161 for (j = 0; j < MAX_PORTS; j++) {
162 if (!ifidx[j])
163 continue;
164 if (!if_indextoname(ifidx[j], ifname))
165 bb_perror_msg_and_die("can't get interface name for index %d", j);
166 if (tabs)
167 printf("\t\t\t\t\t");
168 else
169 tabs = 1;
170 printf("\t\t%s\n", ifname);
171 }
172 if (!tabs) /* bridge has no interfaces */
173 bb_putchar('\n');
174 }
175 goto done;
129 } 176 }
130#endif 177#endif
131 fd = xsocket(AF_INET, SOCK_STREAM, 0); 178
179 if (!*argv) /* all but 'show' need at least one argument */
180 bb_show_usage();
181
132 br = *argv++; 182 br = *argv++;
133 183
134 if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */ 184 if (key == ARG_addbr || key == ARG_delbr) { /* addbr or delbr */
@@ -137,11 +187,13 @@ int brctl_main(int argc ATTRIBUTE_UNUSED, char **argv)
137 br, "bridge %s", br); 187 br, "bridge %s", br);
138 goto done; 188 goto done;
139 } 189 }
140 if (!*argv) /* all but 'show' need at least one argument */ 190
191 if (!*argv) /* all but 'addif/delif' need at least two arguments */
141 bb_show_usage(); 192 bb_show_usage();
193
142 safe_strncpy(ifr.ifr_name, br, IFNAMSIZ); 194 safe_strncpy(ifr.ifr_name, br, IFNAMSIZ);
143 if (key == ARG_addif || key == ARG_delif) { /* addif or delif */ 195 if (key == ARG_addif || key == ARG_delif) { /* addif or delif */
144 brif = *argv++; 196 brif = *argv;
145 ifr.ifr_ifindex = if_nametoindex(brif); 197 ifr.ifr_ifindex = if_nametoindex(brif);
146 if (!ifr.ifr_ifindex) { 198 if (!ifr.ifr_ifindex) {
147 bb_perror_msg_and_die("iface %s", brif); 199 bb_perror_msg_and_die("iface %s", brif);
@@ -149,76 +201,72 @@ int brctl_main(int argc ATTRIBUTE_UNUSED, char **argv)
149 ioctl_or_perror_and_die(fd, 201 ioctl_or_perror_and_die(fd,
150 key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF, 202 key == ARG_addif ? SIOCBRADDIF : SIOCBRDELIF,
151 &ifr, "bridge %s", br); 203 &ifr, "bridge %s", br);
152 goto done; 204 goto done_next_argv;
153 } 205 }
154#if ENABLE_FEATURE_BRCTL_FANCY 206#if ENABLE_FEATURE_BRCTL_FANCY
155 ifr.ifr_data = (char *) &args;
156 if (key == ARG_stp) { /* stp */ 207 if (key == ARG_stp) { /* stp */
157 /* FIXME: parsing yes/y/on/1 versus no/n/off/0 is too involved */ 208 /* FIXME: parsing yes/y/on/1 versus no/n/off/0 is too involved */
158 arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE, 209 arm_ioctl(args, BRCTL_SET_BRIDGE_STP_STATE,
159 (unsigned)(**argv - '0'), 0); 210 (unsigned)(**argv - '0'), 0);
160 goto fire; 211 goto fire;
161 } 212 }
162 if ((unsigned)(key - ARG_stp) < 5) { /* time related ops */ 213 if ((unsigned)(key - ARG_setageing) < 4) { /* time related ops */
163 unsigned long op = (key == ARG_setageing) ? BRCTL_SET_AGEING_TIME : 214 static const uint8_t ops[] ALIGN1 = {
164 (key == ARG_setfd) ? BRCTL_SET_BRIDGE_FORWARD_DELAY : 215 BRCTL_SET_AGEING_TIME, /* ARG_setageing */
165 (key == ARG_sethello) ? BRCTL_SET_BRIDGE_HELLO_TIME : 216 BRCTL_SET_BRIDGE_FORWARD_DELAY, /* ARG_setfd */
166 /*key == ARG_setmaxage*/ BRCTL_SET_BRIDGE_MAX_AGE; 217 BRCTL_SET_BRIDGE_HELLO_TIME, /* ARG_sethello */
167 arm_ioctl(args, op, str_to_jiffies(*argv), 0); 218 BRCTL_SET_BRIDGE_MAX_AGE /* ARG_setmaxage */
219 };
220 arm_ioctl(args, ops[key - ARG_setageing], str_to_jiffies(*argv), 0);
168 goto fire; 221 goto fire;
169 } 222 }
170 port = -1;
171 if (key == ARG_setpathcost || key == ARG_setportprio) {/* get portnum */
172 int ifidx[MAX_PORTS];
173 unsigned i;
174
175 port = if_nametoindex(*argv);
176 if (!port)
177 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, "port");
178 argv++;
179 memset(ifidx, 0, sizeof ifidx);
180 arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx,
181 MAX_PORTS);
182 xioctl(fd, SIOCDEVPRIVATE, &ifr);
183 for (i = 0; i < MAX_PORTS; i++) {
184 if (ifidx[i] == port) {
185 port = i;
186 break;
187 }
188 }
189 }
190 if (key == ARG_setpathcost 223 if (key == ARG_setpathcost
191 || key == ARG_setportprio 224 || key == ARG_setportprio
192 || key == ARG_setbridgeprio 225 || key == ARG_setbridgeprio
193 ) { 226 ) {
194 unsigned long op = (key == ARG_setpathcost) ? BRCTL_SET_PATH_COST : 227 static const uint8_t ops[] ALIGN1 = {
195 (key == ARG_setportprio) ? BRCTL_SET_PORT_PRIORITY : 228 BRCTL_SET_PATH_COST, /* ARG_setpathcost */
196 /*key == ARG_setbridgeprio*/ BRCTL_SET_BRIDGE_PRIORITY; 229 BRCTL_SET_PORT_PRIORITY, /* ARG_setportprio */
197 unsigned long arg1 = port; 230 BRCTL_SET_BRIDGE_PRIORITY /* ARG_setbridgeprio */
198 unsigned long arg2; 231 };
199# if BRCTL_USE_INTERNAL 232 int port = -1;
200 tmp = xatoi(*argv); 233 unsigned arg1, arg2;
201# else 234
202 if (sscanf(*argv, "%i", &tmp) != 1) 235 if (key != ARG_setbridgeprio) {
203 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, 236 /* get portnum */
204 key == ARG_setpathcost ? "cost" : "prio"); 237 unsigned i;
205# endif 238
239 port = if_nametoindex(*argv++);
240 if (!port)
241 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, "port");
242 memset(ifidx, 0, sizeof ifidx);
243 arm_ioctl(args, BRCTL_GET_PORT_LIST, (unsigned long)ifidx,
244 MAX_PORTS);
245 xioctl(fd, SIOCDEVPRIVATE, &ifr);
246 for (i = 0; i < MAX_PORTS; i++) {
247 if (ifidx[i] == port) {
248 port = i;
249 break;
250 }
251 }
252 }
253 arg1 = port;
254 arg2 = xatoi_u(*argv);
206 if (key == ARG_setbridgeprio) { 255 if (key == ARG_setbridgeprio) {
207 arg1 = tmp; 256 arg1 = arg2;
208 arg2 = 0; 257 arg2 = 0;
209 } else 258 }
210 arg2 = tmp; 259 arm_ioctl(args, ops[key - ARG_setpathcost], arg1, arg2);
211 arm_ioctl(args, op, arg1, arg2);
212 } 260 }
213 fire: 261 fire:
214 /* Execute the previously set command. */ 262 /* Execute the previously set command */
215 xioctl(fd, SIOCDEVPRIVATE, &ifr); 263 xioctl(fd, SIOCDEVPRIVATE, &ifr);
216 argv++;
217#endif 264#endif
265 done_next_argv:
266 argv++;
218 done: 267 done:
219 if (ENABLE_FEATURE_CLEAN_UP) 268 close(fd);
220 close(fd);
221 } 269 }
222 USE_FEATURE_BRCTL_SHOW(out:) 270
223 return EXIT_SUCCESS; 271 return EXIT_SUCCESS;
224} 272}