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 message class declaration
25 #ifndef COMMON_NETLINK_NETLINK_MESSAGE_HPP
26 #define COMMON_NETLINK_NETLINK_MESSAGE_HPP
31 #include <type_traits>
33 #include <linux/netlink.h>
39 * NetlinkMessage is used to creatie a netlink messages
41 class NetlinkMessage {
44 * Create netlink message
46 * @param type rtnetlink message type (see man 7 rtnetlink)
47 * @param flags nlmsg flags (see man 7 netlink)
49 NetlinkMessage(std::uint16_t type, std::uint16_t flags);
52 * Add nested atribute type
54 * All future attributes will be nested in this attribute (till call to endNested)
56 * @param ifla attribute name
58 NetlinkMessage& beginNested(int ifla);
63 NetlinkMessage& endNested();
66 /*Add sample attribute */
67 NetlinkMessage& put(int ifla, const std::string& value);
69 NetlinkMessage& put(int ifla, const T& value);
75 * Add raw data to end of netlink message
78 NetlinkMessage& put(const T& value);
81 * Send netlink message
83 * It is not thread safe
85 friend void send(const NetlinkMessage& msg);
87 std::vector<char> mNlmsg;
88 std::stack<void*> mNested;
90 NetlinkMessage& put(int ifla, const void* data, int len);
91 NetlinkMessage& put(const void* data, int len);
93 const nlmsghdr& hdr() const;
94 void setMinCapacity(unsigned int size);
100 NetlinkMessage& NetlinkMessage::put(int ifla, const T& value)
102 static_assert(std::is_pod<T>::value, "Require trivial and standard-layout");
103 return put(ifla, &value, sizeof(value));
107 NetlinkMessage& NetlinkMessage::put(const T& value)
109 static_assert(std::is_pod<T>::value, "Require trivial and standard-layout structure");
110 return put(&value, sizeof(value));
113 } // namespace netlink
116 #endif // COMMON_NETLINK_NETLINK_MESSAGE_HPP