manpages: do not include v4-only modules in ip6tables manpage
[jleu-iptables.git] / extensions / tos_values.c
blob2676d81ed2eb172db6c3eb3e3ab1d730aa34bc1e
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <linux/ip.h>
6 struct tos_value_mask {
7 uint8_t value, mask;
8 };
10 static const struct tos_symbol_info {
11 unsigned char value;
12 const char *name;
13 } tos_symbol_names[] = {
14 {IPTOS_LOWDELAY, "Minimize-Delay"},
15 {IPTOS_THROUGHPUT, "Maximize-Throughput"},
16 {IPTOS_RELIABILITY, "Maximize-Reliability"},
17 {IPTOS_MINCOST, "Minimize-Cost"},
18 {IPTOS_NORMALSVC, "Normal-Service"},
19 { .name = NULL }
23 * tos_parse_numeric - parse sth. like "15/255"
25 * @s: input string
26 * @info: accompanying structure
27 * @bits: number of bits that are allowed
28 * (8 for IPv4 TOS field, 4 for IPv6 Priority Field)
30 static bool tos_parse_numeric(const char *str, struct tos_value_mask *tvm,
31 unsigned int bits)
33 const unsigned int max = (1 << bits) - 1;
34 unsigned int value;
35 char *end;
37 xtables_strtoui(str, &end, &value, 0, max);
38 tvm->value = value;
39 tvm->mask = max;
41 if (*end == '/') {
42 const char *p = end + 1;
44 if (!xtables_strtoui(p, &end, &value, 0, max))
45 xtables_error(PARAMETER_PROBLEM, "Illegal value: \"%s\"",
46 str);
47 tvm->mask = value;
50 if (*end != '\0')
51 xtables_error(PARAMETER_PROBLEM, "Illegal value: \"%s\"", str);
52 return true;
55 static bool tos_parse_symbolic(const char *str, struct tos_value_mask *tvm,
56 unsigned int def_mask)
58 const unsigned int max = UINT8_MAX;
59 const struct tos_symbol_info *symbol;
60 char *tmp;
62 if (xtables_strtoui(str, &tmp, NULL, 0, max))
63 return tos_parse_numeric(str, tvm, max);
65 /* Do not consider ECN bits */
66 tvm->mask = def_mask;
67 for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol)
68 if (strcasecmp(str, symbol->name) == 0) {
69 tvm->value = symbol->value;
70 return true;
73 xtables_error(PARAMETER_PROBLEM, "Symbolic name \"%s\" is unknown", str);
74 return false;
77 static bool tos_try_print_symbolic(const char *prefix,
78 u_int8_t value, u_int8_t mask)
80 const struct tos_symbol_info *symbol;
82 if (mask != 0x3F)
83 return false;
85 for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol)
86 if (value == symbol->value) {
87 printf("%s%s ", prefix, symbol->name);
88 return true;
91 return false;