- move iprule and ipaddress from matches() to index_in_str_array

text    data     bss     dec     hex filename
   2544       0       0    2544     9f0 networking/libiproute/iprule.o.oorig
   2356       0       0    2356     934 networking/libiproute/iprule.o
   6481       0       0    6481    1951 networking/libiproute/ipaddress.o.oorig
   6464       0       0    6464    1940 networking/libiproute/ipaddress.o
This commit is contained in:
Bernhard Reutner-Fischer 2007-06-20 14:53:49 +00:00
parent 7510384107
commit cd0e80ce90
3 changed files with 50 additions and 43 deletions

View File

@ -30,7 +30,7 @@ void ip_parse_common_args(int *argcp, char ***argvp)
{"-family", "inet", "inet6", "link", {"-family", "inet", "inet6", "link",
"-4", "-6", "-0", "-oneline", 0}; "-4", "-6", "-0", "-oneline", 0};
enum { enum {
ARG_family, ARG_family = 1,
ARG_inet, ARG_inet,
ARG_inet6, ARG_inet6,
ARG_link, ARG_link,

View File

@ -604,7 +604,6 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
"peer", "remote", "broadcast", "brd", "peer", "remote", "broadcast", "brd",
"anycast", "scope", "dev", "label", "local", 0 "anycast", "scope", "dev", "label", "local", 0
}; };
struct rtnl_handle rth; struct rtnl_handle rth;
struct { struct {
struct nlmsghdr n; struct nlmsghdr n;
@ -619,7 +618,7 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
int peer_len = 0; int peer_len = 0;
int brd_len = 0; int brd_len = 0;
int any_len = 0; int any_len = 0;
int scoped = 0; bool scoped = 0;
memset(&req, 0, sizeof(req)); memset(&req, 0, sizeof(req));
@ -724,7 +723,7 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
bb_error_msg(bb_msg_requires_arg,"\"dev\""); bb_error_msg(bb_msg_requires_arg,"\"dev\"");
return -1; return -1;
} }
if (l && matches(d, l) != 0) { if (l && strncmp(d, l, strlen(d)) != 0) {
bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l); bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
} }
@ -775,22 +774,21 @@ int do_ipaddr(int argc, char **argv)
"add", "delete", "list", "show", "lst", "flush", 0 "add", "delete", "list", "show", "lst", "flush", 0
}; };
int command_num = 2; int command_num = 2; /* default command is list */
if (*argv) { if (*argv) {
command_num = index_in_substr_array(commands, *argv); command_num = index_in_substr_array(commands, *argv);
} }
switch (command_num) { if (command_num < 0 || command_num > 5)
case 0: /* add */ bb_error_msg_and_die("unknown command %s", *argv);
return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1); --argc;
case 1: /* delete */ ++argv;
return ipaddr_modify(RTM_DELADDR, argc-1, argv+1); if (command_num == 0) /* add */
case 2: /* list */ return ipaddr_modify(RTM_NEWADDR, argc, argv);
case 3: /* show */ else if (command_num == 1) /* delete */
case 4: /* lst */ return ipaddr_modify(RTM_DELADDR, argc, argv);
return ipaddr_list_or_flush(argc-1, argv+1, 0); else if (command_num == 5) /* flush */
case 5: /* flush */ return ipaddr_list_or_flush(argc, argv, 1);
return ipaddr_list_or_flush(argc-1, argv+1, 1); else /* 2 == list, 3 == show, 4 == lst */
} return ipaddr_list_or_flush(argc, argv, 0);
bb_error_msg_and_die("unknown command %s", *argv);
} }

View File

@ -184,17 +184,24 @@ static int iprule_list(int argc, char **argv)
return 0; return 0;
} }
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
static int iprule_modify(int cmd, int argc, char **argv) static int iprule_modify(int cmd, int argc, char **argv)
{ {
int table_ok = 0; bool table_ok = 0;
struct rtnl_handle rth; struct rtnl_handle rth;
struct { struct {
struct nlmsghdr n; struct nlmsghdr n;
struct rtmsg r; struct rtmsg r;
char buf[1024]; char buf[1024];
} req; } req;
static const char * const keywords[] =
{ "from", "to", "preference", "order", "priority", "tos", "fwmark",
"realms", "table", "lookup", "dev", "iif", "nat", "map-to", "type",
"help", NULL};
enum { ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help };
smalluint key;
memset(&req, 0, sizeof(req)); memset(&req, 0, sizeof(req));
@ -213,72 +220,74 @@ static int iprule_modify(int cmd, int argc, char **argv)
} }
while (argc > 0) { while (argc > 0) {
if (strcmp(*argv, "from") == 0) { key = index_in_substr_array(keywords, *argv) + 1;
if (key == 0) /* no match found in keywords array, bail out. */
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
if (key == ARG_from) {
inet_prefix dst; inet_prefix dst;
NEXT_ARG(); NEXT_ARG();
get_prefix(&dst, *argv, req.r.rtm_family); get_prefix(&dst, *argv, req.r.rtm_family);
req.r.rtm_src_len = dst.bitlen; req.r.rtm_src_len = dst.bitlen;
addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen); addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
} else if (strcmp(*argv, "to") == 0) { } else if (key == ARG_to) {
inet_prefix dst; inet_prefix dst;
NEXT_ARG(); NEXT_ARG();
get_prefix(&dst, *argv, req.r.rtm_family); get_prefix(&dst, *argv, req.r.rtm_family);
req.r.rtm_dst_len = dst.bitlen; req.r.rtm_dst_len = dst.bitlen;
addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen); addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
} else if (matches(*argv, "preference") == 0 || } else if (key == ARG_preference ||
matches(*argv, "order") == 0 || key == ARG_order ||
matches(*argv, "priority") == 0) { key == ARG_priority) {
uint32_t pref; uint32_t pref;
NEXT_ARG(); NEXT_ARG();
if (get_u32(&pref, *argv, 0)) if (get_u32(&pref, *argv, 0))
invarg("preference value", *argv); invarg(*argv, "preference");
addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref); addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
} else if (strcmp(*argv, "tos") == 0) { } else if (key == ARG_tos) {
uint32_t tos; uint32_t tos;
NEXT_ARG(); NEXT_ARG();
if (rtnl_dsfield_a2n(&tos, *argv)) if (rtnl_dsfield_a2n(&tos, *argv))
invarg("TOS value", *argv); invarg(*argv, "TOS");
req.r.rtm_tos = tos; req.r.rtm_tos = tos;
} else if (strcmp(*argv, "fwmark") == 0) { } else if (key == ARG_fwmark) {
uint32_t fwmark; uint32_t fwmark;
NEXT_ARG(); NEXT_ARG();
if (get_u32(&fwmark, *argv, 0)) if (get_u32(&fwmark, *argv, 0))
invarg("fwmark value", *argv); invarg(*argv, "fwmark");
addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark); addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
} else if (matches(*argv, "realms") == 0) { } else if (key == ARG_realms) {
uint32_t realm; uint32_t realm;
NEXT_ARG(); NEXT_ARG();
if (get_rt_realms(&realm, *argv)) if (get_rt_realms(&realm, *argv))
invarg("realms", *argv); invarg(*argv, "realms");
addattr32(&req.n, sizeof(req), RTA_FLOW, realm); addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
} else if (matches(*argv, "table") == 0 || } else if (key == ARG_table ||
strcmp(*argv, "lookup") == 0) { key == ARG_lookup) {
uint32_t tid; uint32_t tid;
NEXT_ARG(); NEXT_ARG();
if (rtnl_rttable_a2n(&tid, *argv)) if (rtnl_rttable_a2n(&tid, *argv))
invarg("table ID", *argv); invarg(*argv, "table ID");
req.r.rtm_table = tid; req.r.rtm_table = tid;
table_ok = 1; table_ok = 1;
} else if (strcmp(*argv, "dev") == 0 || } else if (key == ARG_dev ||
strcmp(*argv, "iif") == 0) { key == ARG_iif) {
NEXT_ARG(); NEXT_ARG();
addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1); addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
} else if (strcmp(*argv, "nat") == 0 || } else if (key == ARG_nat ||
matches(*argv, "map-to") == 0) { key == ARG_map_to) {
NEXT_ARG(); NEXT_ARG();
addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv)); addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
req.r.rtm_type = RTN_NAT; req.r.rtm_type = RTN_NAT;
} else { } else {
int type; int type;
if (strcmp(*argv, "type") == 0) { if (key == ARG_type) {
NEXT_ARG(); NEXT_ARG();
} }
if (matches(*argv, "help") == 0) if (key == ARG_help)
bb_show_usage(); bb_show_usage();
if (rtnl_rtntype_a2n(&type, *argv)) if (rtnl_rtntype_a2n(&type, *argv))
// bogus-looking error message "invalid argument 'cannot parse rule type' to '<*argv>'" invarg(*argv, "type");
invarg("cannot parse rule type", *argv);
req.r.rtm_type = type; req.r.rtm_type = type;
} }
argc--; argc--;