]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network/address: introduce generic config parser for [Address] section
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 28 Aug 2024 05:01:36 +0000 (14:01 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 6 Sep 2024 01:37:23 +0000 (10:37 +0900)
Then, use generic conf parsers defined in conf-parser.[ch].

src/network/networkd-address.c
src/network/networkd-address.h
src/network/networkd-network-gperf.gperf

index 7377c22407d1fd59235158993b5f53935656bab6..c2342c641affb086dc6e2841566924a29d207a18 100644 (file)
@@ -2280,53 +2280,6 @@ int config_parse_lifetime(
         return 0;
 }
 
-int config_parse_address_flags(
-                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) {
-
-        Network *network = userdata;
-        _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
-        int r;
-
-        assert(filename);
-        assert(section);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-
-        r = address_new_static(network, filename, section_line, &n);
-        if (r == -ENOMEM)
-                return log_oom();
-        if (r < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Failed to allocate new address, ignoring assignment: %m");
-                return 0;
-        }
-
-        r = parse_boolean(rvalue);
-        if (r < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
-                return 0;
-        }
-
-        if (streq(lvalue, "AddPrefixRoute"))
-                r = !r;
-
-        SET_FLAG(n->flags, ltype, r);
-
-        TAKE_PTR(n);
-        return 0;
-}
-
 int config_parse_address_scope(
                 const char *unit,
                 const char *filename,
@@ -2371,48 +2324,6 @@ int config_parse_address_scope(
         return 0;
 }
 
-int config_parse_address_route_metric(
-                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) {
-
-        Network *network = userdata;
-        _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
-        int r;
-
-        assert(filename);
-        assert(section);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-
-        r = address_new_static(network, filename, section_line, &n);
-        if (r == -ENOMEM)
-                return log_oom();
-        if (r < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Failed to allocate new address, ignoring assignment: %m");
-                return 0;
-        }
-
-        r = safe_atou32(rvalue, &n->route_metric);
-        if (r < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Could not parse %s=, ignoring assignment: %s", lvalue, rvalue);
-                return 0;
-        }
-
-        TAKE_PTR(n);
-        return 0;
-}
-
 int config_parse_duplicate_address_detection(
                 const char *unit,
                 const char *filename,
@@ -2467,7 +2378,7 @@ int config_parse_duplicate_address_detection(
         return 0;
 }
 
-int config_parse_address_netlabel(
+int config_parse_address_section(
                 const char *unit,
                 const char *filename,
                 unsigned line,
@@ -2479,18 +2390,24 @@ int config_parse_address_netlabel(
                 void *data,
                 void *userdata) {
 
-        Network *network = userdata;
-        _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
+        static const ConfigSectionParser table[_ADDRESS_CONF_PARSER_MAX] = {
+                [ADDRESS_HOME_ADDRESS]             = { .parser = config_parse_uint32_flag,        .ltype = IFA_F_HOMEADDRESS,        .offset = offsetof(Address, flags),                       },
+                [ADDRESS_MANAGE_TEMPORARY_ADDRESS] = { .parser = config_parse_uint32_flag,        .ltype = IFA_F_MANAGETEMPADDR,     .offset = offsetof(Address, flags),                       },
+                [ADDRESS_PREFIX_ROUTE]             = { .parser = config_parse_uint32_flag,        .ltype = IFA_F_NOPREFIXROUTE,      .offset = offsetof(Address, flags),                       },
+                [ADDRESS_ADD_PREFIX_ROUTE]         = { .parser = config_parse_uint32_invert_flag, .ltype = IFA_F_NOPREFIXROUTE,      .offset = offsetof(Address, flags),                       },
+                [ADDRESS_AUTO_JOIN]                = { .parser = config_parse_uint32_flag,        .ltype = IFA_F_MCAUTOJOIN,         .offset = offsetof(Address, flags),                       },
+                [ADDRESS_ROUTE_METRIC]             = { .parser = config_parse_uint32,             .ltype = 0,                        .offset = offsetof(Address, route_metric),                },
+                [ADDRESS_NET_LABEL]                = { .parser = config_parse_string,             .ltype = CONFIG_PARSE_STRING_SAFE, .offset = offsetof(Address, netlabel),                    },
+                [ADDRESS_NFT_SET]                  = { .parser = config_parse_nft_set,            .ltype = NFT_SET_PARSE_NETWORK,    .offset = offsetof(Address, nft_set_context),             },
+        };
+
+        _cleanup_(address_unref_or_set_invalidp) Address *address = NULL;
+        Network *network = ASSERT_PTR(userdata);
         int r;
 
         assert(filename);
-        assert(section);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-        assert(network);
 
-        r = address_new_static(network, filename, section_line, &n);
+        r = address_new_static(network, filename, section_line, &address);
         if (r == -ENOMEM)
                 return log_oom();
         if (r < 0) {
@@ -2499,12 +2416,12 @@ int config_parse_address_netlabel(
                 return 0;
         }
 
-        r = config_parse_string(unit, filename, line, section, section_line,
-                                lvalue, CONFIG_PARSE_STRING_SAFE, rvalue, &n->netlabel, network);
-        if (r < 0)
+        r = config_section_parse(table, ELEMENTSOF(table),
+                                 unit, filename, line, section, section_line, lvalue, ltype, rvalue, address);
+        if (r <= 0)
                 return r;
 
-        TAKE_PTR(n);
+        TAKE_PTR(address);
         return 0;
 }
 
@@ -2655,41 +2572,3 @@ int network_drop_invalid_addresses(Network *network) {
 
         return 0;
 }
-
-int config_parse_address_ip_nft_set(
-                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) {
-
-        Network *network = userdata;
-        _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-        assert(network);
-
-        r = address_new_static(network, filename, section_line, &n);
-        if (r == -ENOMEM)
-                return log_oom();
-        if (r < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Failed to allocate a new address, ignoring assignment: %m");
-                return 0;
-        }
-
-        r = config_parse_nft_set(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->nft_set_context, network);
-        if (r < 0)
-                return r;
-
-        TAKE_PTR(n);
-        return 0;
-}
index fcad445e174f04e7af3d804871a3e080bf2105a0..0c4a4d166ea467e4a13a1eec50f1f4cb9bd41579 100644 (file)
@@ -159,13 +159,23 @@ DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(Address, address);
 
 void link_mark_addresses(Link *link, NetworkConfigSource source);
 
+typedef enum AddressConfParserType {
+        ADDRESS_HOME_ADDRESS,
+        ADDRESS_MANAGE_TEMPORARY_ADDRESS,
+        ADDRESS_PREFIX_ROUTE,
+        ADDRESS_ADD_PREFIX_ROUTE,
+        ADDRESS_AUTO_JOIN,
+        ADDRESS_ROUTE_METRIC,
+        ADDRESS_NET_LABEL,
+        ADDRESS_NFT_SET,
+        _ADDRESS_CONF_PARSER_MAX,
+        _ADDRESS_CONF_PARSER_INVALID = -EINVAL,
+} AddressConfParserType;
+
+CONFIG_PARSER_PROTOTYPE(config_parse_address_section);
 CONFIG_PARSER_PROTOTYPE(config_parse_address);
 CONFIG_PARSER_PROTOTYPE(config_parse_broadcast);
 CONFIG_PARSER_PROTOTYPE(config_parse_label);
 CONFIG_PARSER_PROTOTYPE(config_parse_lifetime);
-CONFIG_PARSER_PROTOTYPE(config_parse_address_flags);
 CONFIG_PARSER_PROTOTYPE(config_parse_address_scope);
-CONFIG_PARSER_PROTOTYPE(config_parse_address_route_metric);
 CONFIG_PARSER_PROTOTYPE(config_parse_duplicate_address_detection);
-CONFIG_PARSER_PROTOTYPE(config_parse_address_netlabel);
-CONFIG_PARSER_PROTOTYPE(config_parse_address_ip_nft_set);
index e39c617500f71b2f68aa0c9256810f2014317139..d6a610e958e0ac73ff5f6418de54234686937788 100644 (file)
@@ -161,16 +161,16 @@ Address.Peer,                                config_parse_address,
 Address.Broadcast,                           config_parse_broadcast,                                   0,                             0
 Address.Label,                               config_parse_label,                                       0,                             0
 Address.PreferredLifetime,                   config_parse_lifetime,                                    0,                             0
-Address.HomeAddress,                         config_parse_address_flags,                               IFA_F_HOMEADDRESS,             0
-Address.ManageTemporaryAddress,              config_parse_address_flags,                               IFA_F_MANAGETEMPADDR,          0
-Address.PrefixRoute,                         config_parse_address_flags,                               IFA_F_NOPREFIXROUTE,           0 /* deprecated */
-Address.AddPrefixRoute,                      config_parse_address_flags,                               IFA_F_NOPREFIXROUTE,           0
-Address.AutoJoin,                            config_parse_address_flags,                               IFA_F_MCAUTOJOIN,              0
+Address.HomeAddress,                         config_parse_address_section,                             ADDRESS_HOME_ADDRESS,          0
+Address.ManageTemporaryAddress,              config_parse_address_section,                             ADDRESS_MANAGE_TEMPORARY_ADDRESS, 0
+Address.PrefixRoute,                         config_parse_address_section,                             ADDRESS_PREFIX_ROUTE,          0 /* deprecated */
+Address.AddPrefixRoute,                      config_parse_address_section,                             ADDRESS_ADD_PREFIX_ROUTE,      0
+Address.AutoJoin,                            config_parse_address_section,                             ADDRESS_AUTO_JOIN,             0
 Address.DuplicateAddressDetection,           config_parse_duplicate_address_detection,                 0,                             0
 Address.Scope,                               config_parse_address_scope,                               0,                             0
-Address.RouteMetric,                         config_parse_address_route_metric,                        0,                             0
-Address.NetLabel,                            config_parse_address_netlabel,                            0,                             0
-Address.NFTSet,                              config_parse_address_ip_nft_set,                          NFT_SET_PARSE_NETWORK,         0
+Address.RouteMetric,                         config_parse_address_section,                             ADDRESS_ROUTE_METRIC,          0
+Address.NetLabel,                            config_parse_address_section,                             ADDRESS_NET_LABEL,             0
+Address.NFTSet,                              config_parse_address_section,                             ADDRESS_NFT_SET,               0
 IPv6AddressLabel.Prefix,                     config_parse_ipv6_address_label_section,                  IPV6_ADDRESS_LABEL_PREFIX,     0
 IPv6AddressLabel.Label,                      config_parse_ipv6_address_label_section,                  IPV6_ADDRESS_LABEL,            0
 Neighbor.Address,                            config_parse_neighbor_address,                            0,                             0