summaryrefslogtreecommitdiffstats
path: root/libxtables/xtables.c
diff options
context:
space:
mode:
authorSerhey Popovych <serhe.popovych@gmail.com>2018-03-01 13:03:11 +0200
committerFlorian Westphal <fw@strlen.de>2018-04-27 18:56:20 +0200
commit29b1d97764d1849651388d870565b3fa815a0bd8 (patch)
tree1e9c5e71eaf91ca360949b020602390280df90d9 /libxtables/xtables.c
parent56aadc01b258ef7849463723ab5ddc4885db22f6 (diff)
xtables: Introduce and use common function to parse val[/mask] arguments
There are a couple of places in both core and extensions where arguments in the form of val[/mask] is parsed (see XTTYPE_MARKMASK32). In some cases symbolic name might be used which is mapped in code to numeric value. Introduce common function to handle both cases where value given is either val[/mask] or symbolic name. Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com> Signed-off-by: Florian Westphal <fw@strlen.de>
Diffstat (limited to 'libxtables/xtables.c')
-rw-r--r--libxtables/xtables.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index 57a11022..f93e88dd 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -1960,6 +1960,58 @@ void xtables_print_num(uint64_t number, unsigned int format)
printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
}
+void xtables_parse_val_mask(struct xt_option_call *cb,
+ unsigned int *val, unsigned int *mask,
+ const struct xtables_lmap *lmap)
+{
+ char *end;
+
+ *mask = ~0U;
+
+ if (!xtables_strtoui(cb->arg, &end, val, 0, UINT32_MAX)) {
+ if (lmap)
+ goto name2val;
+ else
+ goto bad_val;
+ }
+
+ if (*end == '\0')
+ return;
+
+ if (*end != '/') {
+ if (lmap)
+ goto name2val;
+ else
+ goto garbage;
+ }
+
+ if (!xtables_strtoui(end + 1, &end, mask, 0, UINT32_MAX))
+ goto bad_val;
+
+ if (*end == '\0')
+ return;
+
+garbage:
+ xt_params->exit_err(PARAMETER_PROBLEM,
+ "%s: trailing garbage after value "
+ "for option \"--%s\".\n",
+ cb->ext_name, cb->entry->name);
+
+bad_val:
+ xt_params->exit_err(PARAMETER_PROBLEM,
+ "%s: bad integer value for option \"--%s\", "
+ "or out of range.\n",
+ cb->ext_name, cb->entry->name);
+
+name2val:
+ *val = xtables_lmap_name2id(lmap, cb->arg);
+ if ((int)*val == -1)
+ xt_params->exit_err(PARAMETER_PROBLEM,
+ "%s: could not map name %s to an integer value "
+ "for option \"--%s\".\n",
+ cb->ext_name, cb->arg, cb->entry->name);
+}
+
int kernel_version;
void get_kernel_version(void)