Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
netlink-message.hpp
Go to the documentation of this file.
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 
25 #ifndef COMMON_NETLINK_NETLINK_MESSAGE_HPP
26 #define COMMON_NETLINK_NETLINK_MESSAGE_HPP
27 
28 #include <limits>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 #include <stack>
33 #include <type_traits>
34 #include <cstdlib>
35 #include <sys/socket.h>
36 #include <linux/netlink.h>
37 
38 //FIXME remove from namespace vasum
39 namespace vasum {
40 namespace netlink {
41 
42 class NetlinkResponse;
43 class NetlinkMessage;
44 
49 public:
56  NetlinkMessage(std::uint16_t type, std::uint16_t flags);
57 
65  NetlinkMessage& beginNested(int ifla);
66 
71 
73  /*Add sample attribute */
74  NetlinkMessage& put(int ifla, const std::string& value);
75  template<class T>
76  NetlinkMessage& put(int ifla, const T& value);
78 
84  template<class T>
85  NetlinkMessage& put(const T& value);
86 
94  friend NetlinkResponse send(const NetlinkMessage& msg, int pid);
95 private:
96  std::vector<char> mNlmsg;
97  std::stack<int> mNested;
98 
99  NetlinkMessage& put(int ifla, const void* data, int len);
100  NetlinkMessage& put(const void* data, int len);
101  nlmsghdr& hdr();
102  const nlmsghdr& hdr() const;
103  void setMinCapacity(unsigned int size);
104 };
105 
110 public:
114  bool hasMessage() const;
115 
119  void fetchNextMessage();
120 
124  int getMessageType() const;
125 
129  bool hasAttribute() const;
130 
134  bool isNestedAttribute() const;
135 
139  void skipAttribute();
140 
144  NetlinkResponse& openNested(int ifla);
145 
150 
152 
155  NetlinkResponse& fetch(int ifla, std::string& value, unsigned maxLen = std::numeric_limits<unsigned>::max());
156  template<class T>
157  NetlinkResponse& fetch(int ifla, T& value);
159 
163  int getAttributeType() const;
164 
168  int getAttributeLength() const;
169 
173  template<class T>
174  NetlinkResponse& fetch(T& value);
175 
179  template<class T>
181 
189  friend NetlinkResponse send(const NetlinkMessage& msg, int pid);
190 private:
191  NetlinkResponse(std::unique_ptr<std::vector<char>>&& message);
192 
193  std::unique_ptr<std::vector<char>> mNlmsg;
194  std::stack<int> mNested;
195  nlmsghdr* mNlmsgHdr;
197 
198  const char* get(int ifla, int iflasize) const;
199  const char* get(int size = 0) const;
200  NetlinkResponse& fetch(int ifla, char* data, int len);
201  NetlinkResponse& fetch(char* data, int len);
202  NetlinkResponse& seek(int len);
203  int size() const;
204  int getHdrPosition() const;
205 };
206 
213 
214 template<class T>
215 NetlinkMessage& NetlinkMessage::put(int ifla, const T& value)
216 {
217  static_assert(std::is_pod<T>::value, "Require trivial and standard-layout");
218  return put(ifla, &value, sizeof(value));
219 }
220 
221 template<class T>
223 {
224  static_assert(std::is_pod<T>::value, "Require trivial and standard-layout structure");
225  return put(&value, sizeof(value));
226 }
227 
228 template<class T>
230 {
231  static_assert(std::is_pod<T>::value, "Require trivial and standard-layout");
232  return fetch(ifla, reinterpret_cast<char*>(&value), sizeof(value));
233 }
234 
235 template<class T>
237 {
238  static_assert(std::is_pod<T>::value, "Require trivial and standard-layout structure");
239  return fetch(reinterpret_cast<char*>(&value), sizeof(value));
240 }
241 
242 template<class T>
244 {
245  static_assert(std::is_pod<T>::value, "Require trivial and standard-layout structure");
246  return seek(sizeof(T));
247 }
248 
249 } // namespace netlink
250 } // namespace vasum
251 
252 #endif // COMMON_NETLINK_NETLINK_MESSAGE_HPP
char data[368]
Definition: initctl.cpp:41