tc: util: bore up action_a2n()
It's a pitty this function is used nowhere, so let's polish it for use: * Loop over branch names, makes it clear that every former conditional was exactly identical. * Support 'pipe' branch name, too. * Make number parsing optional. Signed-off-by: Phil Sutter <phil@nwl.cc>
This commit is contained in:
parent
9ffc80b1e4
commit
53aadc5286
54
tc/tc_util.c
54
tc/tc_util.c
|
|
@ -435,29 +435,43 @@ char *action_n2a(int action, char *buf, int len)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int action_a2n(char *arg, int *result)
|
/* Convert action branch name into numeric format.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* @arg - string to parse
|
||||||
|
* @result - pointer to output variable
|
||||||
|
* @allow_num - whether @arg may be in numeric format already
|
||||||
|
*
|
||||||
|
* In error case, returns -1 and does not touch @result. Otherwise returns 0.
|
||||||
|
*/
|
||||||
|
int action_a2n(char *arg, int *result, bool allow_num)
|
||||||
{
|
{
|
||||||
int res;
|
int n;
|
||||||
|
char dummy;
|
||||||
|
struct {
|
||||||
|
const char *a;
|
||||||
|
int n;
|
||||||
|
} a2n[] = {
|
||||||
|
{"continue", TC_ACT_UNSPEC},
|
||||||
|
{"drop", TC_ACT_SHOT},
|
||||||
|
{"shot", TC_ACT_SHOT},
|
||||||
|
{"pass", TC_ACT_OK},
|
||||||
|
{"ok", TC_ACT_OK},
|
||||||
|
{"reclassify", TC_ACT_RECLASSIFY},
|
||||||
|
{"pipe", TC_ACT_PIPE},
|
||||||
|
{ NULL },
|
||||||
|
}, *iter;
|
||||||
|
|
||||||
if (matches(arg, "continue") == 0)
|
for (iter = a2n; iter->a; iter++) {
|
||||||
res = -1;
|
if (matches(arg, iter->a) != 0)
|
||||||
else if (matches(arg, "drop") == 0)
|
continue;
|
||||||
res = TC_ACT_SHOT;
|
*result = iter->n;
|
||||||
else if (matches(arg, "shot") == 0)
|
return 0;
|
||||||
res = TC_ACT_SHOT;
|
|
||||||
else if (matches(arg, "pass") == 0)
|
|
||||||
res = TC_ACT_OK;
|
|
||||||
else if (strcmp(arg, "ok") == 0)
|
|
||||||
res = TC_ACT_OK;
|
|
||||||
else if (matches(arg, "reclassify") == 0)
|
|
||||||
res = TC_ACT_RECLASSIFY;
|
|
||||||
else {
|
|
||||||
char dummy;
|
|
||||||
|
|
||||||
if (sscanf(arg, "%d%c", &res, &dummy) != 1)
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
*result = res;
|
if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
*result = n;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ int tc_print_police(FILE *f, struct rtattr *tb);
|
||||||
int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n);
|
int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n);
|
||||||
|
|
||||||
char *action_n2a(int action, char *buf, int len);
|
char *action_n2a(int action, char *buf, int len);
|
||||||
int action_a2n(char *arg, int *result);
|
int action_a2n(char *arg, int *result, bool allow_num);
|
||||||
int act_parse_police(struct action_util *a, int *argc_p,
|
int act_parse_police(struct action_util *a, int *argc_p,
|
||||||
char ***argv_p, int tca_id, struct nlmsghdr *n);
|
char ***argv_p, int tca_id, struct nlmsghdr *n);
|
||||||
int print_police(struct action_util *a, FILE *f, struct rtattr *tb);
|
int print_police(struct action_util *a, FILE *f, struct rtattr *tb);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue