chiark / gitweb /
sd-netlink: make a couple of helper functions static
authorTom Gundersen <teg@jklm.no>
Thu, 25 Jun 2015 22:07:25 +0000 (00:07 +0200)
committerSven Eden <yamakuzure@gmx.net>
Tue, 14 Mar 2017 09:05:52 +0000 (10:05 +0100)
Also rename from rtnl_* to netlink_*.

src/libsystemd/sd-netlink/netlink-internal.h
src/libsystemd/sd-netlink/netlink-message.c

index 6b83764..67e73e9 100644 (file)
@@ -130,14 +130,6 @@ int socket_read_message(sd_netlink *nl);
 int rtnl_rqueue_make_room(sd_netlink *rtnl);
 int rtnl_rqueue_partial_make_room(sd_netlink *rtnl);
 
-int rtnl_message_read_internal(sd_netlink_message *m, unsigned short type, void **data);
-int rtnl_message_parse(sd_netlink_message *m,
-                       size_t **rta_offset_tb,
-                       unsigned short *rta_tb_size,
-                       int max,
-                       struct rtattr *rta,
-                       unsigned int rt_len);
-
 /* Make sure callbacks don't destroy the rtnl connection */
 #define RTNL_DONT_DESTROY(rtnl) \
         _cleanup_netlink_unref_ _unused_ sd_netlink *_dont_destroy_##rtnl = sd_netlink_ref(rtnl)
index 13573dc..23f369c 100644 (file)
@@ -34,7 +34,7 @@
 #include "netlink-internal.h"
 #include "netlink-types.h"
 
-#define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->containers[i].offset) : NULL)
+#define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offsets[i]) : NULL)
 #define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
 
 #define RTA_TYPE(rta) ((rta)->rta_type & NLA_TYPE_MASK)
@@ -88,7 +88,7 @@ int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) {
 
         m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
 
-        type_get_type_system(nl_type, &m->containers[0].type_system);
+        type_get_type_system(nl_type, &m->container_type_system[0]);
         m->hdr->nlmsg_len = size;
         m->hdr->nlmsg_type = type;
 
@@ -129,7 +129,7 @@ sd_netlink_message *sd_netlink_message_unref(sd_netlink_message *m) {
                 free(m->hdr);
 
                 for (i = 0; i <= m->n_containers; i++)
-                        free(m->containers[i].attributes);
+                        free(m->rta_offset_tb[i]);
 
                 sd_netlink_message_unref(m->next);
 
@@ -223,7 +223,7 @@ static int message_attribute_has_type(sd_netlink_message *m, size_t *out_size, u
 
         assert(m);
 
-        r = type_system_get_type(m->containers[m->n_containers].type_system, &type, attribute_type);
+        r = type_system_get_type(m->container_type_system[m->n_containers], &type, attribute_type);
         if (r < 0)
                 return r;
 
@@ -406,18 +406,18 @@ int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type
                 if (r < 0)
                         return r;
 
-                r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type);
+                r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, type);
                 if (r < 0)
                         return r;
 
                 r = type_system_union_protocol_get_type_system(type_system_union,
-                                                               &m->containers[m->n_containers + 1].type_system,
+                                                               &m->container_type_system[m->n_containers + 1],
                                                                family);
                 if (r < 0)
                         return r;
         } else {
-                r = type_system_get_type_system(m->containers[m->n_containers].type_system,
-                                                &m->containers[m->n_containers + 1].type_system,
+                r = type_system_get_type_system(m->container_type_system[m->n_containers],
+                                                &m->container_type_system[m->n_containers + 1],
                                                 type);
                 if (r < 0)
                         return r;
@@ -427,7 +427,7 @@ int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type
         if (r < 0)
                 return r;
 
-        m->containers[m->n_containers ++].offset = r;
+        m->container_offsets[m->n_containers ++] = r;
 
         return 0;
 }
@@ -439,12 +439,12 @@ int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned shor
         assert_return(m, -EINVAL);
         assert_return(!m->sealed, -EPERM);
 
-        r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type);
+        r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, type);
         if (r < 0)
                 return r;
 
         r = type_system_union_get_type_system(type_system_union,
-                                              &m->containers[m->n_containers + 1].type_system,
+                                              &m->container_type_system[m->n_containers + 1],
                                               key);
         if (r < 0)
                 return r;
@@ -458,7 +458,7 @@ int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned shor
         if (r < 0)
                 return r;
 
-        m->containers[m->n_containers ++].offset = r;
+        m->container_offsets[m->n_containers ++] = r;
 
         return 0;
 }
@@ -469,29 +469,26 @@ int sd_netlink_message_close_container(sd_netlink_message *m) {
         assert_return(!m->sealed, -EPERM);
         assert_return(m->n_containers > 0, -EINVAL);
 
-        m->containers[m->n_containers].type_system = NULL;
+        m->container_type_system[m->n_containers] = NULL;
         m->n_containers --;
 
         return 0;
 }
 
 static int netlink_message_read_internal(sd_netlink_message *m, unsigned short type, void **data) {
-        struct netlink_attribute *attribute;
         struct rtattr *rta;
 
         assert_return(m, -EINVAL);
         assert_return(m->sealed, -EPERM);
         assert_return(data, -EINVAL);
         assert(m->n_containers <= RTNL_CONTAINER_DEPTH);
-        assert(m->containers[m->n_containers].attributes);
-        assert(type < m->containers[m->n_containers].n_attributes);
+        assert(m->rta_offset_tb[m->n_containers]);
+        assert(type < m->rta_tb_size[m->n_containers]);
 
-        attribute = &m->containers[m->n_containers].attributes[type];
-
-        if(!attribute->offset)
+        if(!m->rta_offset_tb[m->n_containers][type])
                 return -ENODATA;
 
-        rta = (struct rtattr*)((uint8_t *) m->hdr + attribute->offset);
+        rta = (struct rtattr*)((uint8_t *) m->hdr + m->rta_offset_tb[m->n_containers][type]);
 
         *data = RTA_DATA(rta);
 
@@ -674,36 +671,37 @@ int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type,
         return 0;
 }
 
-static int netlink_container_parse(sd_netlink_message *m,
-                                   struct netlink_container *container,
-                                   int count,
-                                   struct rtattr *rta,
-                                   unsigned int rt_len) {
-        _cleanup_free_ struct netlink_attribute *attributes = NULL;
+static int netlink_message_parse(sd_netlink_message *m,
+                       size_t **rta_offset_tb,
+                       unsigned short *rta_tb_size,
+                       int count,
+                       struct rtattr *rta,
+                       unsigned int rt_len) {
+        unsigned short type;
+        size_t *tb;
 
-        attributes = new0(struct netlink_attribute, count);
-        if(!attributes)
+        tb = new0(size_t, count);
+        if(!tb)
                 return -ENOMEM;
 
-        for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
-                unsigned short type;
+        *rta_tb_size = count;
 
+        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 */
+                   when building, we ignore out-of-range attributes
+                 */
                 if (type >= count)
                         continue;
 
-                if (attributes[type].offset)
+                if (tb[type])
                         log_debug("rtnl: message parse - overwriting repeated attribute");
 
-                attributes[type].offset = (uint8_t *) rta - (uint8_t *) m->hdr;
+                tb[type] = (uint8_t *) rta - (uint8_t *) m->hdr;
         }
 
-        container->attributes = attributes;
-        attributes = NULL;
-        container->n_attributes = count;
+        *rta_offset_tb = tb;
 
         return 0;
 }
@@ -719,7 +717,7 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
         assert_return(m, -EINVAL);
         assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -EINVAL);
 
-        r = type_system_get_type(m->containers[m->n_containers].type_system,
+        r = type_system_get_type(m->container_type_system[m->n_containers],
                                  &nl_type,
                                  type_id);
         if (r < 0)
@@ -728,7 +726,7 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
         type = type_get_type(nl_type);
 
         if (type == NETLINK_TYPE_NESTED) {
-                r = type_system_get_type_system(m->containers[m->n_containers].type_system,
+                r = type_system_get_type_system(m->container_type_system[m->n_containers],
                                                 &type_system,
                                                 type_id);
                 if (r < 0)
@@ -736,7 +734,7 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
         } else if (type == NETLINK_TYPE_UNION) {
                 const NLTypeSystemUnion *type_system_union;
 
-                r = type_system_get_type_system_union(m->containers[m->n_containers].type_system,
+                r = type_system_get_type_system_union(m->container_type_system[m->n_containers],
                                                       &type_system_union,
                                                       type_id);
                 if (r < 0)
@@ -789,17 +787,18 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
 
         m->n_containers ++;
 
-        r = netlink_container_parse(m,
-                                    &m->containers[m->n_containers],
-                                    type_system_get_count(type_system),
-                                    container,
-                                    size);
+        r = netlink_message_parse(m,
+                                  &m->rta_offset_tb[m->n_containers],
+                                  &m->rta_tb_size[m->n_containers],
+                                  type_system_get_count(type_system),
+                                  container,
+                                  size);
         if (r < 0) {
                 m->n_containers --;
                 return r;
         }
 
-        m->containers[m->n_containers].type_system = type_system;
+        m->container_type_system[m->n_containers] = type_system;
 
         return 0;
 }
@@ -809,9 +808,9 @@ int sd_netlink_message_exit_container(sd_netlink_message *m) {
         assert_return(m->sealed, -EINVAL);
         assert_return(m->n_containers > 0, -EINVAL);
 
-        free(m->containers[m->n_containers].attributes);
-        m->containers[m->n_containers].attributes = NULL;
-        m->containers[m->n_containers].type_system = NULL;
+        free(m->rta_offset_tb[m->n_containers]);
+        m->rta_offset_tb[m->n_containers] = NULL;
+        m->container_type_system[m->n_containers] = NULL;
 
         m->n_containers --;
 
@@ -860,13 +859,15 @@ int sd_netlink_message_rewind(sd_netlink_message *m) {
                 rtnl_message_seal(m);
 
         for (i = 1; i <= m->n_containers; i++) {
-                free(m->containers[i].attributes);
-                m->containers[i].attributes = NULL;
+                free(m->rta_offset_tb[i]);
+                m->rta_offset_tb[i] = NULL;
+                m->rta_tb_size[i] = 0;
+                m->container_type_system[i] = NULL;
         }
 
         m->n_containers = 0;
 
-        if (m->containers[0].attributes) {
+        if (m->rta_offset_tb[0]) {
                 /* top-level attributes have already been parsed */
                 return 0;
         }
@@ -885,13 +886,14 @@ int sd_netlink_message_rewind(sd_netlink_message *m) {
 
                 type_get_type_system(nl_type, &type_system);
 
-                m->containers[0].type_system = type_system;
+                m->container_type_system[0] = type_system;
 
-                r = netlink_container_parse(m,
-                                            &m->containers[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));
+                r = netlink_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));
                 if (r < 0)
                         return r;
         }