if (r < 0)
return r;
- if (type_get_type(nl_type) != NETLINK_TYPE_NESTED)
- return -EINVAL;
-
r = message_new_empty(rtnl, &m);
if (r < 0)
return r;
- size = NLMSG_SPACE(type_get_size(nl_type));
+ size = NLMSG_SPACE(nl_type->size);
assert(size >= sizeof(struct nlmsghdr));
m->hdr = malloc0(size);
m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
- type_get_type_system(nl_type, &m->container_type_system[0]);
+ if (nl_type->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;
return offset;
}
-static int message_attribute_has_type(sd_netlink_message *m, size_t *out_size, uint16_t attribute_type, uint16_t data_type) {
+static int message_attribute_has_type(sd_netlink_message *m, uint16_t attribute_type, uint16_t data_type) {
const NLType *type;
int r;
- assert(m);
-
r = type_system_get_type(m->container_type_system[m->n_containers], &type, attribute_type);
if (r < 0)
return r;
- if (type_get_type(type) != data_type)
+ if (type->type != data_type)
return -EINVAL;
- if (out_size)
- *out_size = type_get_size(type);
- return 0;
+ return type->size;
}
int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type, const char *data) {
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_STRING);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_STRING);
if (r < 0)
return r;
+ else
+ size = (size_t)r;
if (size) {
length = strnlen(data, size+1);
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_U8);
if (r < 0)
return r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_U16);
if (r < 0)
return r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_U32);
if (r < 0)
return r;
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_IN_ADDR);
if (r < 0)
return r;
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_IN_ADDR);
if (r < 0)
return r;
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_ETHER_ADDR);
if (r < 0)
return r;
assert_return(!m->sealed, -EPERM);
assert_return(info, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_CACHE_INFO);
if (r < 0)
return r;
assert_return(!m->sealed, -EPERM);
assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -ERANGE);
- r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_NESTED);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_NESTED);
if (r < 0) {
const NLTypeSystemUnion *type_system_union;
int family;
- r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_UNION);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_UNION);
if (r < 0)
return r;
+ size = (size_t) r;
r = sd_rtnl_message_get_family(m, &family);
if (r < 0)
if (r < 0)
return r;
} else {
+ size = (size_t)r;
+
r = type_system_get_type_system(m->container_type_system[m->n_containers],
&m->container_type_system[m->n_containers + 1],
type);
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_STRING);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_STRING);
if (r < 0)
return r;
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_U8);
if (r < 0)
return r;
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_U16);
if (r < 0)
return r;
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_U32);
if (r < 0)
return r;
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_ETHER_ADDR);
if (r < 0)
return r;
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_CACHE_INFO);
if (r < 0)
return r;
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_IN_ADDR);
if (r < 0)
return r;
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
+ r = message_attribute_has_type(m, type, NETLINK_TYPE_IN_ADDR);
if (r < 0)
return r;
return 0;
}
-int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type_id) {
+int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type) {
const NLType *nl_type;
const NLTypeSystem *type_system;
void *container;
- uint16_t type;
size_t size;
int r;
r = type_system_get_type(m->container_type_system[m->n_containers],
&nl_type,
- type_id);
+ type);
if (r < 0)
return r;
- type = type_get_type(nl_type);
-
- if (type == NETLINK_TYPE_NESTED) {
+ if (nl_type->type == NETLINK_TYPE_NESTED) {
r = type_system_get_type_system(m->container_type_system[m->n_containers],
&type_system,
- type_id);
+ type);
if (r < 0)
return r;
- } else if (type == NETLINK_TYPE_UNION) {
+ } else if (nl_type->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_id);
+ type);
if (r < 0)
return r;
} else
return -EINVAL;
- r = rtnl_message_read_internal(m, type_id, &container);
+ r = rtnl_message_read_internal(m, type, &container);
if (r < 0)
return r;
else
r = rtnl_message_parse(m,
&m->rta_offset_tb[m->n_containers],
&m->rta_tb_size[m->n_containers],
- type_system_get_count(type_system),
+ type_system->max,
container,
size);
if (r < 0) {
int rtnl_message_parse(sd_netlink_message *m,
size_t **rta_offset_tb,
unsigned short *rta_tb_size,
- int count,
+ int max,
struct rtattr *rta,
unsigned int rt_len) {
unsigned short type;
size_t *tb;
- tb = new0(size_t, count);
+ tb = new0(size_t, max + 1);
if(!tb)
return -ENOMEM;
- *rta_tb_size = count;
+ *rta_tb_size = max + 1;
for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
type = RTA_TYPE(rta);
/* if the kernel is newer than the headers we used
when building, we ignore out-of-range attributes
*/
- if (type >= count)
+ if (type > max)
continue;
if (tb[type])
}
int sd_netlink_message_rewind(sd_netlink_message *m) {
- const NLType *nl_type;
- uint16_t type;
- size_t size;
+ const NLType *type;
unsigned i;
int r;
assert(m->hdr);
- r = type_system_get_type(NULL, &nl_type, m->hdr->nlmsg_type);
+ r = type_system_get_type(NULL, &type, m->hdr->nlmsg_type);
if (r < 0)
return r;
- type = type_get_type(nl_type);
- size = type_get_size(nl_type);
-
- if (type == NETLINK_TYPE_NESTED) {
+ if (type->type == NETLINK_TYPE_NESTED) {
const NLTypeSystem *type_system;
- type_get_type_system(nl_type, &type_system);
+ type_get_type_system(type, &type_system);
m->container_type_system[0] = type_system;
r = rtnl_message_parse(m,
&m->rta_offset_tb[m->n_containers],
&m->rta_tb_size[m->n_containers],
- type_system_get_count(type_system),
- (struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) + NLMSG_ALIGN(size)),
- NLMSG_PAYLOAD(m->hdr, size));
+ type_system->max,
+ (struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) +
+ NLMSG_ALIGN(type->size)),
+ NLMSG_PAYLOAD(m->hdr, type->size));
if (r < 0)
return r;
}