1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

sd-netlink: make NLType internal

If we extend NLType to support arrays and further extended types, we
really want to avoid hard-coding the type-layout outside of
netlink-types.c. We already avoid accessing nl_type->type_system outside
of netlink-types.c, extend this to also avoid accessing any other fields.

Provide accessor functions for nl_type->type and nl_type->size and then
move NLType away from the type-system header.

With this in place, follow-up patches can safely turn "type_system" and
"type_system_union" into a real "union { }", and then add another type for
arrays.
This commit is contained in:
David Herrmann 2015-06-23 11:03:10 +02:00
parent c658008f50
commit 817d1cd824
4 changed files with 45 additions and 26 deletions

View File

@ -76,7 +76,7 @@ int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) {
if (r < 0)
return r;
size = NLMSG_SPACE(nl_type->size);
size = NLMSG_SPACE(type_get_size(nl_type));
assert(size >= sizeof(struct nlmsghdr));
m->hdr = malloc0(size);
@ -85,7 +85,7 @@ int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) {
m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
if (nl_type->type == NETLINK_TYPE_NESTED)
if (type_get_type(nl_type) == NETLINK_TYPE_NESTED)
type_get_type_system(nl_type, &m->container_type_system[0]);
m->hdr->nlmsg_len = size;
m->hdr->nlmsg_type = type;
@ -223,10 +223,10 @@ static int message_attribute_has_type(sd_netlink_message *m, uint16_t attribute_
if (r < 0)
return r;
if (type->type != data_type)
if (type_get_type(type) != data_type)
return -EINVAL;
return type->size;
return type_get_size(type);
}
int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type, const char *data) {
@ -670,10 +670,11 @@ int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type,
return 0;
}
int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type) {
int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type_id) {
const NLType *nl_type;
const NLTypeSystem *type_system;
void *container;
uint16_t type;
size_t size;
int r;
@ -682,22 +683,24 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
r = type_system_get_type(m->container_type_system[m->n_containers],
&nl_type,
type);
type_id);
if (r < 0)
return r;
if (nl_type->type == NETLINK_TYPE_NESTED) {
type = type_get_type(nl_type);
if (type == NETLINK_TYPE_NESTED) {
r = type_system_get_type_system(m->container_type_system[m->n_containers],
&type_system,
type);
type_id);
if (r < 0)
return r;
} else if (nl_type->type == NETLINK_TYPE_UNION) {
} else if (type == NETLINK_TYPE_UNION) {
const NLTypeSystemUnion *type_system_union;
r = type_system_get_type_system_union(m->container_type_system[m->n_containers],
&type_system_union,
type);
type_id);
if (r < 0)
return r;
@ -740,7 +743,7 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
} else
return -EINVAL;
r = rtnl_message_read_internal(m, type, &container);
r = rtnl_message_read_internal(m, type_id, &container);
if (r < 0)
return r;
else
@ -842,7 +845,9 @@ int rtnl_message_parse(sd_netlink_message *m,
}
int sd_netlink_message_rewind(sd_netlink_message *m) {
const NLType *type;
const NLType *nl_type;
uint16_t type;
size_t size;
unsigned i;
int r;
@ -868,14 +873,17 @@ int sd_netlink_message_rewind(sd_netlink_message *m) {
assert(m->hdr);
r = type_system_get_type(NULL, &type, m->hdr->nlmsg_type);
r = type_system_get_type(NULL, &nl_type, m->hdr->nlmsg_type);
if (r < 0)
return r;
if (type->type == NETLINK_TYPE_NESTED) {
type = type_get_type(nl_type);
size = type_get_size(nl_type);
if (type == NETLINK_TYPE_NESTED) {
const NLTypeSystem *type_system;
type_get_type_system(type, &type_system);
type_get_type_system(nl_type, &type_system);
m->container_type_system[0] = type_system;
@ -883,9 +891,8 @@ int sd_netlink_message_rewind(sd_netlink_message *m) {
&m->rta_offset_tb[m->n_containers],
&m->rta_tb_size[m->n_containers],
type_system->max,
(struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) +
NLMSG_ALIGN(type->size)),
NLMSG_PAYLOAD(m->hdr, type->size));
(struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) + NLMSG_ALIGN(size)),
NLMSG_PAYLOAD(m->hdr, size));
if (r < 0)
return r;
}

View File

@ -253,7 +253,7 @@ int socket_read_message(sd_netlink *rtnl) {
}
/* check that the size matches the message type */
if (new_msg->nlmsg_len < NLMSG_LENGTH(nl_type->size)) {
if (new_msg->nlmsg_len < NLMSG_LENGTH(type_get_size(nl_type))) {
log_debug("sd-netlink: message larger than expected, dropping");
continue;
}

View File

@ -39,6 +39,13 @@
#include "netlink-types.h"
#include "missing.h"
struct NLType {
uint16_t type;
size_t size;
const NLTypeSystem *type_system;
const NLTypeSystemUnion *type_system_union;
};
static const NLTypeSystem rtnl_link_type_system;
static const NLType rtnl_link_info_data_veth_types[VETH_INFO_MAX + 1] = {
@ -460,6 +467,16 @@ const NLTypeSystem rtnl_type_system = {
.types = rtnl_types,
};
uint16_t type_get_type(const NLType *type) {
assert(type);
return type->type;
}
size_t type_get_size(const NLType *type) {
assert(type);
return type->size;
}
void type_get_type_system(const NLType *nl_type, const NLTypeSystem **ret) {
assert(nl_type);
assert(ret);

View File

@ -58,13 +58,8 @@ struct NLTypeSystem {
const NLType *types;
};
struct NLType {
uint16_t type;
size_t size;
const NLTypeSystem *type_system;
const NLTypeSystemUnion *type_system_union;
};
uint16_t type_get_type(const NLType *type);
size_t type_get_size(const NLType *type);
void type_get_type_system(const NLType *type, const NLTypeSystem **ret);
void type_get_type_system_union(const NLType *type, const NLTypeSystemUnion **ret);
int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type);