From 8cde9f6c5a5722d72d6a0869b343603a34c2e869 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 23 Sep 2024 01:50:44 +0900 Subject: [PATCH] conf-parser: introduce config_parse_in_addr_prefix() It is not used currently, but will be used later. --- src/shared/conf-parser.c | 39 ++++++++++++++++++++++++++++++++++++++- src/shared/conf-parser.h | 1 + 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index 8b32f2a9bb2..8c5a4a7013a 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -24,7 +24,7 @@ #include "hash-funcs.h" #include "hostname-util.h" #include "id128-util.h" -#include "in-addr-util.h" +#include "in-addr-prefix-util.h" #include "ip-protocol-list.h" #include "log.h" #include "macro.h" @@ -2002,6 +2002,43 @@ int config_parse_in_addr_data( return 1; } +int config_parse_in_addr_prefix( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, /* takes boolean, whether we warn about missing prefixlen */ + const char *rvalue, + void *data, + void *userdata) { + + struct in_addr_prefix *p = ASSERT_PTR(data); + int r; + + assert(filename); + assert(lvalue); + + if (isempty(rvalue)) { + *p = (struct in_addr_prefix) {}; + return 1; + } + + r = in_addr_prefix_from_string_auto_full(rvalue, ltype ? PREFIXLEN_REFUSE : PREFIXLEN_FULL, &p->family, &p->address, &p->prefixlen); + if (r == -ENOANO) { + r = in_addr_prefix_from_string_auto(rvalue, &p->family, &p->address, &p->prefixlen); + if (r >= 0) + log_syntax(unit, LOG_WARNING, filename, line, r, + "%s=%s is specified without prefix length. Assuming the prefix length is %u. " + "Please specify the prefix length explicitly.", lvalue, rvalue, p->prefixlen); + } + if (r < 0) + return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); + + return 1; +} + int config_parse_unsigned_bounded( const char *unit, const char *filename, diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h index 8767902a501..1599738f849 100644 --- a/src/shared/conf-parser.h +++ b/src/shared/conf-parser.h @@ -305,6 +305,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_ether_addr); CONFIG_PARSER_PROTOTYPE(config_parse_ether_addrs); CONFIG_PARSER_PROTOTYPE(config_parse_in_addr_non_null); CONFIG_PARSER_PROTOTYPE(config_parse_in_addr_data); +CONFIG_PARSER_PROTOTYPE(config_parse_in_addr_prefix); CONFIG_PARSER_PROTOTYPE(config_parse_percent); CONFIG_PARSER_PROTOTYPE(config_parse_permyriad); CONFIG_PARSER_PROTOTYPE(config_parse_pid); -- 2.47.3