Creating netdev
[platform/core/security/vasum.git] / common / netlink / netlink-message.hpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Mateusz Malicki <m.malicki2@samsung.com>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18
19 /**
20  * @file
21  * @author  Mateusz Malicki (m.malicki2@samsung.com)
22  * @brief   Netlink message class declaration
23  */
24
25 #ifndef COMMON_NETLINK_NETLINK_MESSAGE_HPP
26 #define COMMON_NETLINK_NETLINK_MESSAGE_HPP
27
28 #include <string>
29 #include <vector>
30 #include <stack>
31 #include <type_traits>
32 #include <cstdlib>
33 #include <linux/netlink.h>
34
35 namespace vasum {
36 namespace netlink {
37
38 /**
39  *  NetlinkMessage is used to creatie a netlink messages
40  */
41 class NetlinkMessage {
42 public:
43     /**
44      * Create netlink message
45      *
46      * @param type rtnetlink message type (see man 7 rtnetlink)
47      * @param flags nlmsg flags (see man 7 netlink)
48      */
49     NetlinkMessage(std::uint16_t type, std::uint16_t flags);
50
51     /**
52      * Add nested atribute type
53      *
54      * All future attributes will be nested in this attribute (till call to endNested)
55      *
56      * @param ifla attribute name
57      */
58     NetlinkMessage& beginNested(int ifla);
59
60     /**
61      * End nested atribute
62      */
63     NetlinkMessage& endNested();
64
65     ///@{
66     /*Add sample attribute */
67     NetlinkMessage& put(int ifla, const std::string& value);
68     template<class T>
69     NetlinkMessage& put(int ifla, const T& value);
70     ///@}
71
72     /**
73      * Add raw data
74      *
75      * Add raw data to end of netlink message
76      */
77     template<class T>
78     NetlinkMessage& put(const T& value);
79
80     /**
81      * Send netlink message
82      *
83      * It is not thread safe
84      */
85     friend void send(const NetlinkMessage& msg);
86 private:
87     std::vector<char> mNlmsg;
88     std::stack<void*> mNested;
89
90     NetlinkMessage& put(int ifla, const void* data, int len);
91     NetlinkMessage& put(const void* data, int len);
92     nlmsghdr& hdr();
93     const nlmsghdr& hdr() const;
94     void setMinCapacity(unsigned int size);
95
96
97 };
98
99 template<class T>
100 NetlinkMessage& NetlinkMessage::put(int ifla, const T& value)
101 {
102     static_assert(std::is_pod<T>::value, "Require trivial and standard-layout");
103     return put(ifla, &value, sizeof(value));
104 }
105
106 template<class T>
107 NetlinkMessage& NetlinkMessage::put(const T& value)
108 {
109     static_assert(std::is_pod<T>::value, "Require trivial and standard-layout structure");
110     return put(&value, sizeof(value));
111 }
112
113 } // namespace netlink
114 } // namespace vasum
115
116 #endif // COMMON_NETLINK_NETLINK_MESSAGE_HPP