summaryrefslogtreecommitdiff
path: root/src/libsystemd/sd-rtnl/rtnl-message.c
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-01-29 21:20:30 +0100
committerTom Gundersen <teg@jklm.no>2014-01-30 14:30:39 +0100
commit3815f36f05f8bc06904777b1eb7f1d22b78bcced (patch)
tree15f3d8e9d3ea908d8edd04016c47c5ca638cf3c1 /src/libsystemd/sd-rtnl/rtnl-message.c
parent377a218f876507fb8be9c21ef4121fa2576ec317 (diff)
sd-rtnl: beef up rtnl-util a bit
Diffstat (limited to 'src/libsystemd/sd-rtnl/rtnl-message.c')
-rw-r--r--src/libsystemd/sd-rtnl/rtnl-message.c99
1 files changed, 17 insertions, 82 deletions
diff --git a/src/libsystemd/sd-rtnl/rtnl-message.c b/src/libsystemd/sd-rtnl/rtnl-message.c
index 85c59bfed1..87ad682349 100644
--- a/src/libsystemd/sd-rtnl/rtnl-message.c
+++ b/src/libsystemd/sd-rtnl/rtnl-message.c
@@ -29,25 +29,15 @@
#include "refcnt.h"
#include "sd-rtnl.h"
+#include "rtnl-util.h"
#include "rtnl-internal.h"
-struct sd_rtnl_message {
- RefCount n_ref;
-
- struct nlmsghdr *hdr;
- size_t container_offsets[RTNL_CONTAINER_DEPTH]; /* offset from hdr to each container's start */
- unsigned n_containers; /* number of containers */
- size_t next_rta_offset; /* offset from hdr to next rta */
-
- bool sealed:1;
-};
-
#define GET_CONTAINER(m, i) (i < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offsets[i]) : NULL)
#define NEXT_RTA(m) ((struct rtattr*)((uint8_t*)(m)->hdr + (m)->next_rta_offset))
#define UPDATE_RTA(m, new) (m)->next_rta_offset = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
#define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
-static int message_new(sd_rtnl_message **ret, size_t initial_size) {
+int message_new(sd_rtnl_message **ret, size_t initial_size) {
sd_rtnl_message *m;
assert_return(ret, -EINVAL);
@@ -73,67 +63,12 @@ static int message_new(sd_rtnl_message **ret, size_t initial_size) {
return 0;
}
-int message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message **ret) {
- struct nlmsgerr *err;
- int r;
-
- assert(error <= 0);
-
- r = message_new(ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
- if (r < 0)
- return r;
-
- (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
- (*ret)->hdr->nlmsg_type = NLMSG_ERROR;
- (*ret)->hdr->nlmsg_seq = serial;
-
- err = NLMSG_DATA((*ret)->hdr);
-
- err->error = error;
-
- return 0;
-}
-
-bool message_type_is_route(uint16_t type) {
- switch (type) {
- case RTM_NEWROUTE:
- case RTM_GETROUTE:
- case RTM_DELROUTE:
- return true;
- default:
- return false;
- }
-}
-
-bool message_type_is_link(uint16_t type) {
- switch (type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_GETLINK:
- case RTM_DELLINK:
- return true;
- default:
- return false;
- }
-}
-
-bool message_type_is_addr(uint16_t type) {
- switch (type) {
- case RTM_NEWADDR:
- case RTM_GETADDR:
- case RTM_DELADDR:
- return true;
- default:
- return false;
- }
-}
-
int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char prefixlen) {
struct rtmsg *rtm;
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
rtm = NLMSG_DATA(m->hdr);
@@ -151,7 +86,7 @@ int sd_rtnl_message_route_new(uint16_t nlmsg_type, unsigned char rtm_family,
struct rtmsg *rtm;
int r;
- assert_return(message_type_is_route(nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_route(nlmsg_type), -EINVAL);
assert_return(rtm_family == AF_INET || rtm_family == AF_INET6, -EINVAL);
assert_return(ret, -EINVAL);
@@ -182,7 +117,7 @@ int sd_rtnl_message_link_set_flags(sd_rtnl_message *m, unsigned flags, unsigned
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
ifi = NLMSG_DATA(m->hdr);
@@ -200,7 +135,7 @@ int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type) {
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
ifi = NLMSG_DATA(m->hdr);
@@ -213,7 +148,7 @@ int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, sd_rtnl_message **r
struct ifinfomsg *ifi;
int r;
- assert_return(message_type_is_link(nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_link(nlmsg_type), -EINVAL);
assert_return(nlmsg_type == RTM_NEWLINK || index > 0, -EINVAL);
assert_return(ret, -EINVAL);
@@ -241,7 +176,7 @@ int sd_rtnl_message_addr_set_prefixlen(sd_rtnl_message *m, unsigned char prefixl
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
ifa = NLMSG_DATA(m->hdr);
@@ -259,7 +194,7 @@ int sd_rtnl_message_addr_set_flags(sd_rtnl_message *m, unsigned char flags) {
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
ifa = NLMSG_DATA(m->hdr);
@@ -273,7 +208,7 @@ int sd_rtnl_message_addr_set_scope(sd_rtnl_message *m, unsigned char scope) {
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
ifa = NLMSG_DATA(m->hdr);
@@ -287,7 +222,7 @@ int sd_rtnl_message_addr_new(uint16_t nlmsg_type, int index, unsigned char famil
struct ifaddrmsg *ifa;
int r;
- assert_return(message_type_is_addr(nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_addr(nlmsg_type), -EINVAL);
assert_return(index > 0, -EINVAL);
assert_return(family == AF_INET || family == AF_INET6, -EINVAL);
assert_return(ret, -EINVAL);
@@ -343,7 +278,7 @@ int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex) {
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
assert_return(ifindex, -EINVAL);
ifi = NLMSG_DATA(m->hdr);
@@ -358,7 +293,7 @@ int sd_rtnl_message_link_get_flags(sd_rtnl_message *m, unsigned *flags) {
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
assert_return(flags, -EINVAL);
ifi = NLMSG_DATA(m->hdr);
@@ -726,7 +661,7 @@ int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
sd_rtnl_message_get_type(m, &rtm_type);
- if (message_type_is_link(rtm_type)) {
+ if (rtnl_message_type_is_link(rtm_type)) {
if ((type == IFLA_LINKINFO && m->n_containers == 0) ||
(type == IFLA_INFO_DATA && m->n_containers == 1 &&
GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO))
@@ -778,7 +713,7 @@ int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data)
*type = NEXT_RTA(m)->rta_type;
- if (message_type_is_link(rtm_type) &&
+ if (rtnl_message_type_is_link(rtm_type) &&
((m->n_containers == 0 &&
NEXT_RTA(m)->rta_type == IFLA_LINKINFO) ||
(m->n_containers == 1 &&
@@ -805,7 +740,7 @@ int sd_rtnl_message_exit_container(sd_rtnl_message *m) {
return 0;
}
-uint32_t message_get_serial(sd_rtnl_message *m) {
+uint32_t rtnl_message_get_serial(sd_rtnl_message *m) {
assert(m);
assert(m->hdr);
@@ -826,7 +761,7 @@ int sd_rtnl_message_get_errno(sd_rtnl_message *m) {
return err->error;
}
-int message_seal(sd_rtnl *nl, sd_rtnl_message *m) {
+int rtnl_message_seal(sd_rtnl *nl, sd_rtnl_message *m) {
int r;
assert(m);