1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-26 14:04:03 +03:00

sd-netlink: introduce two helper functions for type system union

This commit is contained in:
Yu Watanabe 2021-07-04 15:11:08 +09:00
parent ea073c8fce
commit b019c545e9
3 changed files with 32 additions and 18 deletions

View File

@ -616,7 +616,7 @@ int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned shor
if (r < 0) if (r < 0)
return r; return r;
r = sd_netlink_message_append_string(m, type_system_union->match, key); r = sd_netlink_message_append_string(m, type_system_union_get_match_attribute(type_system_union), key);
if (r < 0) if (r < 0)
return r; return r;
@ -1148,12 +1148,14 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
if (r < 0) if (r < 0)
return r; return r;
switch (type_system_union->match_type) { switch (type_system_union_get_match_type(type_system_union)) {
case NL_MATCH_SIBLING: case NL_MATCH_SIBLING: {
{
const char *key; const char *key;
r = sd_netlink_message_read_string(m, type_system_union->match, &key); r = sd_netlink_message_read_string(
m,
type_system_union_get_match_attribute(type_system_union),
&key);
if (r < 0) if (r < 0)
return r; return r;
@ -1166,8 +1168,7 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
break; break;
} }
case NL_MATCH_PROTOCOL: case NL_MATCH_PROTOCOL: {
{
int family; int family;
r = sd_rtnl_message_get_family(m, &family); r = sd_rtnl_message_get_family(m, &family);

View File

@ -72,6 +72,14 @@ struct NLTypeSystem {
const NLType *types; const NLType *types;
}; };
struct NLTypeSystemUnion {
int num;
NLMatchType match_type;
uint16_t match_attribute;
int (*lookup)(const char *);
const NLTypeSystem *type_systems;
};
static const NLTypeSystem rtnl_link_type_system; static const NLTypeSystem rtnl_link_type_system;
static const NLType empty_types[1] = { static const NLType empty_types[1] = {
@ -476,7 +484,7 @@ static const NLTypeSystemUnion rtnl_link_info_data_type_system_union = {
.lookup = nl_union_link_info_data_from_string, .lookup = nl_union_link_info_data_from_string,
.type_systems = rtnl_link_info_data_type_systems, .type_systems = rtnl_link_info_data_type_systems,
.match_type = NL_MATCH_SIBLING, .match_type = NL_MATCH_SIBLING,
.match = IFLA_INFO_KIND, .match_attribute = IFLA_INFO_KIND,
}; };
static const NLType rtnl_link_info_types[] = { static const NLType rtnl_link_info_types[] = {
@ -1013,7 +1021,7 @@ static const NLTypeSystemUnion rtnl_tca_option_data_type_system_union = {
.lookup = nl_union_tca_option_data_from_string, .lookup = nl_union_tca_option_data_from_string,
.type_systems = rtnl_tca_option_data_type_systems, .type_systems = rtnl_tca_option_data_type_systems,
.match_type = NL_MATCH_SIBLING, .match_type = NL_MATCH_SIBLING,
.match = TCA_KIND, .match_attribute = TCA_KIND,
}; };
static const NLType rtnl_tca_types[] = { static const NLType rtnl_tca_types[] = {
@ -1570,7 +1578,7 @@ static const NLTypeSystemUnion nfnl_nft_data_expr_type_system_union = {
.lookup = nl_union_nft_expr_data_from_string, .lookup = nl_union_nft_expr_data_from_string,
.type_systems = nfnl_expr_data_type_systems, .type_systems = nfnl_expr_data_type_systems,
.match_type = NL_MATCH_SIBLING, .match_type = NL_MATCH_SIBLING,
.match = NFTA_EXPR_NAME, .match_attribute = NFTA_EXPR_NAME,
}; };
static const NLType nfnl_nft_rule_expr_types[] = { static const NLType nfnl_nft_rule_expr_types[] = {
@ -1805,6 +1813,17 @@ int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLT
return 0; return 0;
} }
NLMatchType type_system_union_get_match_type(const NLTypeSystemUnion *type_system_union) {
assert(type_system_union);
return type_system_union->match_type;
}
uint16_t type_system_union_get_match_attribute(const NLTypeSystemUnion *type_system_union) {
assert(type_system_union);
assert(type_system_union->match_type == NL_MATCH_SIBLING);
return type_system_union->match_attribute;
}
int type_system_union_get_type_system_by_string(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key) { int type_system_union_get_type_system_by_string(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key) {
int type; int type;

View File

@ -35,14 +35,6 @@ typedef struct NLTypeSystemUnion NLTypeSystemUnion;
typedef struct NLTypeSystem NLTypeSystem; typedef struct NLTypeSystem NLTypeSystem;
typedef struct NLType NLType; typedef struct NLType NLType;
struct NLTypeSystemUnion {
int num;
NLMatchType match_type;
uint16_t match;
int (*lookup)(const char *);
const NLTypeSystem *type_systems;
};
extern const NLTypeSystem genl_family_type_system_root; extern const NLTypeSystem genl_family_type_system_root;
uint16_t type_get_type(const NLType *type); uint16_t type_get_type(const NLType *type);
@ -56,6 +48,8 @@ int type_system_root_get_type(sd_netlink *nl, const NLType **ret, uint16_t type)
int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type); int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type);
int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type); int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type);
int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type); int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type);
NLMatchType type_system_union_get_match_type(const NLTypeSystemUnion *type_system_union);
uint16_t type_system_union_get_match_attribute(const NLTypeSystemUnion *type_system_union);
int type_system_union_get_type_system_by_string(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key); int type_system_union_get_type_system_by_string(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key);
int type_system_union_get_type_system_by_protocol(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, uint16_t protocol); int type_system_union_get_type_system_by_protocol(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, uint16_t protocol);