From e77b31f53a61a8995cd6baf91a6e557260f401bd Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Tue, 8 May 2018 13:08:36 +0200 Subject: libnftables: Introduce a few helper functions This adds a bunch of functions for conversion of different values into string (and vice-versa). * log_level_parse(): A simple helper to turn log level string representation into log level value. * nat_etype2str(): Translate nat statement type into string representation. * ct_dir2str(): Convert IP_CT_DIR_* values into string representation. * ct_label2str(): Convert ct_label values into string representation. Signed-off-by: Phil Sutter Signed-off-by: Pablo Neira Ayuso --- src/statement.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/statement.c') diff --git a/src/statement.c b/src/statement.c index 6537bbbd..8160e0ad 100644 --- a/src/statement.c +++ b/src/statement.c @@ -233,6 +233,18 @@ const char *log_level(uint32_t level) return syslog_level[level]; } +int log_level_parse(const char *level) +{ + int i; + + for (i = 0; i <= LOG_DEBUG; i++) { + if (syslog_level[i] && + !strcmp(level, syslog_level[i])) + return i; + } + return -1; +} + static void log_stmt_print(const struct stmt *stmt, struct output_ctx *octx) { nft_print(octx, "log"); @@ -499,7 +511,7 @@ static void print_nf_nat_flags(uint32_t flags, struct output_ctx *octx) nft_print(octx, "%spersistent", delim); } -static void nat_stmt_print(const struct stmt *stmt, struct output_ctx *octx) +const char *nat_etype2str(enum nft_nat_etypes type) { static const char * const nat_types[] = { [NFT_NAT_SNAT] = "snat", @@ -508,7 +520,12 @@ static void nat_stmt_print(const struct stmt *stmt, struct output_ctx *octx) [NFT_NAT_REDIR] = "redirect", }; - nft_print(octx, "%s", nat_types[stmt->nat.type]); + return nat_types[type]; +} + +static void nat_stmt_print(const struct stmt *stmt, struct output_ctx *octx) +{ + nft_print(octx, "%s", nat_etype2str(stmt->nat.type)); if (stmt->nat.addr || stmt->nat.proto) nft_print(octx, " to"); -- cgit v1.2.3