]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
conf-parser: introduce config_parse_in_addr_prefix()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 22 Sep 2024 16:50:44 +0000 (01:50 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 11 Oct 2024 18:26:04 +0000 (03:26 +0900)
It is not used currently, but will be used later.

src/shared/conf-parser.c
src/shared/conf-parser.h

index 8b32f2a9bb2480c17f5983419c4b1d66bbebf73f..8c5a4a7013a0dc31f51d2ff1f559c37e07669832 100644 (file)
@@ -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,
index 8767902a501da6d514030253f7adfe15f87c7289..1599738f8492385dde4fe35f4da8b84f2d91b384 100644 (file)
@@ -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);