diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-05-14 16:35:34 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-05-18 20:19:37 +0900 |
commit | cf0747725ddb96cd664ea1f9b169febfa53d1615 (patch) | |
tree | 8e070ceca8b1fbe777b1998eb615d85875642423 | |
parent | c54cfef3968613f9e86e76a3337148360e20150e (diff) | |
download | systemd-cf0747725ddb96cd664ea1f9b169febfa53d1615.tar.gz |
conf-parser: introduce config_parse_in_addr_non_null()
-rw-r--r-- | src/shared/conf-parser.c | 53 | ||||
-rw-r--r-- | src/shared/conf-parser.h | 1 |
2 files changed, 54 insertions, 0 deletions
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index fa4079cff7..ce3af64962 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -16,6 +16,7 @@ #include "fd-util.h" #include "fileio.h" #include "fs-util.h" +#include "in-addr-util.h" #include "log.h" #include "macro.h" #include "missing_network.h" @@ -1359,5 +1360,57 @@ int config_parse_hwaddrs( } } +int config_parse_in_addr_non_null( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + /* data must be a pointer to struct in_addr or in6_addr, and the type is determined by ltype. */ + struct in_addr *ipv4 = data; + struct in6_addr *ipv6 = data; + union in_addr_union a; + int r; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + assert(IN_SET(ltype, AF_INET, AF_INET6)); + + if (isempty(rvalue)) { + if (ltype == AF_INET) + *ipv4 = (struct in_addr) {}; + else + *ipv6 = (struct in6_addr) {}; + return 0; + } + + r = in_addr_from_string(ltype, rvalue, &a); + if (r < 0) { + log_syntax(unit, LOG_WARNING, filename, line, r, + "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue); + return 0; + } + + if (!in_addr_is_set(ltype, &a)) { + log_syntax(unit, LOG_WARNING, filename, line, 0, + "%s= cannot be the ANY address, ignoring: %s", lvalue, rvalue); + return 0; + } + + if (ltype == AF_INET) + *ipv4 = a.in; + else + *ipv6 = a.in6; + return 0; +} + DEFINE_CONFIG_PARSE(config_parse_percent, parse_percent, "Failed to parse percent value"); DEFINE_CONFIG_PARSE(config_parse_permyriad, parse_permyriad, "Failed to parse permyriad value"); diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h index c4b9891428..c3a138274d 100644 --- a/src/shared/conf-parser.h +++ b/src/shared/conf-parser.h @@ -149,6 +149,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_rlimit); CONFIG_PARSER_PROTOTYPE(config_parse_vlanprotocol); CONFIG_PARSER_PROTOTYPE(config_parse_hwaddr); CONFIG_PARSER_PROTOTYPE(config_parse_hwaddrs); +CONFIG_PARSER_PROTOTYPE(config_parse_in_addr_non_null); CONFIG_PARSER_PROTOTYPE(config_parse_percent); CONFIG_PARSER_PROTOTYPE(config_parse_permyriad); |