2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Mateusz Malicki <m.malicki2@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Mateusz Malicki (m.malicki2@samsung.com)
22 * @brief Netlink class definition
26 #include "netlink.hpp"
28 #include "base-exception.hpp"
30 #include <logger/logger.hpp>
33 #include <sys/socket.h>
35 #include <linux/netlink.h>
44 static_assert(std::is_pod<T>::value, "make_clean require trivial and standard-layout");
46 std::fill_n(reinterpret_cast<char*>(&value), sizeof(value), 0);
52 Netlink::Netlink() : mFd(-1)
64 mFd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
66 LOGE("Can't open socket (" << getSystemErrorMessage() << ")");
67 throw VasumException("Can't open netlink connection");
70 sockaddr_nl local = make_clean<sockaddr_nl>();
71 local.nl_family = AF_NETLINK;
73 if (bind(mFd, (struct sockaddr *)&local, sizeof(local)) < 0) {
76 LOGE("Can't bind to socket (" << getSystemErrorMessage(err) << ")");
77 throw VasumException("Can't set up netlink connection");
89 void Netlink::send(const nlmsghdr *nlmsg)
91 msghdr msg = make_clean<msghdr>();
92 sockaddr_nl nladdr = make_clean<sockaddr_nl>();
93 iovec iov = make_clean<iovec>();
95 iov.iov_base = (void *)nlmsg;
96 iov.iov_len = nlmsg->nlmsg_len;
97 msg.msg_name = &nladdr;
98 msg.msg_namelen = sizeof(nladdr);
101 nladdr.nl_family = AF_NETLINK;
103 int ret = sendmsg(mFd, &msg, 0);
105 LOGE("Can't send message (" << getSystemErrorMessage() << ")");
106 throw VasumException("Can't send netlink message");
110 int Netlink::rcv(nlmsghdr *answer)
112 //TODO: Handle too small buffer situation (buffer resizing)
113 msghdr msg = make_clean<msghdr>();
114 sockaddr_nl nladdr = make_clean<sockaddr_nl>();
115 iovec iov = make_clean<iovec>();
117 iov.iov_base = answer;
118 iov.iov_len = answer->nlmsg_len;
119 msg.msg_name = &nladdr;
120 msg.msg_namelen = sizeof(nladdr);
123 nladdr.nl_family = AF_NETLINK;
125 int ret = recvmsg(mFd, &msg, 0);
127 LOGE("Can't receive message (" + getSystemErrorMessage() + ")");
128 throw VasumException("Can't receive netlink message");
131 LOGE("Peer has performed an orderly shutdown");
132 throw VasumException("Can't receive netlink message");
134 if (msg.msg_flags & MSG_TRUNC) {
135 LOGE("Can't receive message (" + getSystemErrorMessage(EMSGSIZE) + ")");
136 throw VasumException("Can't receive netlink message");
138 if (msg.msg_flags & MSG_ERRQUEUE) {
139 LOGE("No data was received but an extended error");
140 throw VasumException("Can't receive netlink message");
142 if (msg.msg_flags & MSG_OOB) {
143 LOGE("Internal error (expedited or out-of-band data were received)");
144 throw VasumException("Can't receive netlink message");
146 if (msg.msg_flags & (MSG_EOR | MSG_CTRUNC)) {
147 assert(!"This should not happen!");
148 LOGE("Internal error (" << std::to_string(msg.msg_flags) << ")");
149 throw VasumException("Internal error while recaiving netlink message");