network: use string table to parse route type

This commit is contained in:
Yu Watanabe 2019-07-07 08:43:33 +09:00
parent 5424fd9573
commit 7a22312d68
2 changed files with 21 additions and 13 deletions

View File

@ -12,6 +12,7 @@
#include "networkd-route.h"
#include "parse-util.h"
#include "set.h"
#include "string-table.h"
#include "string-util.h"
#include "sysctl-util.h"
#include "util.h"
@ -749,6 +750,17 @@ int network_add_default_route_on_device(Network *network) {
return 0;
}
static const char * const route_type_table[__RTN_MAX] = {
[RTN_UNICAST] = "unicast",
[RTN_BLACKHOLE] = "blackhole",
[RTN_UNREACHABLE] = "unreachable",
[RTN_PROHIBIT] = "prohibit",
[RTN_THROW] = "throw",
};
assert_cc(__RTN_MAX <= UCHAR_MAX);
DEFINE_STRING_TABLE_LOOKUP(route_type, int);
int config_parse_gateway(
const char *unit,
const char *filename,
@ -1127,28 +1139,21 @@ int config_parse_route_type(
Network *network = userdata;
_cleanup_(route_free_or_set_invalidp) Route *n = NULL;
int r;
int t, r;
r = route_new_static(network, filename, section_line, &n);
if (r < 0)
return r;
if (streq(rvalue, "unicast"))
n->type = RTN_UNICAST;
else if (streq(rvalue, "blackhole"))
n->type = RTN_BLACKHOLE;
else if (streq(rvalue, "unreachable"))
n->type = RTN_UNREACHABLE;
else if (streq(rvalue, "prohibit"))
n->type = RTN_PROHIBIT;
else if (streq(rvalue, "throw"))
n->type = RTN_THROW;
else {
log_syntax(unit, LOG_ERR, filename, line, r,
t = route_type_from_string(rvalue);
if (t < 0) {
log_syntax(unit, LOG_ERR, filename, line, 0,
"Could not parse route type \"%s\", ignoring assignment: %m", rvalue);
return 0;
}
n->type = (unsigned char) t;
TAKE_PTR(n);
return 0;
}

View File

@ -65,6 +65,9 @@ DEFINE_NETWORK_SECTION_FUNCTIONS(Route, route_free);
int network_add_ipv4ll_route(Network *network);
int network_add_default_route_on_device(Network *network);
const char* route_type_to_string(int t) _const_;
int route_type_from_string(const char *s) _pure_;
CONFIG_PARSER_PROTOTYPE(config_parse_gateway);
CONFIG_PARSER_PROTOTYPE(config_parse_preferred_src);
CONFIG_PARSER_PROTOTYPE(config_parse_destination);