softwarecontainer  0.18.0-739e8d7 2017-05-04
netlink.h
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  *
19  */
20 
21 #include "softwarecontainer-common.h"
22 
23 #include <linux/rtnetlink.h>
24 #include <arpa/inet.h>
25 
26 #include <string>
27 #include <vector>
28 
29 namespace softwarecontainer {
30 
37 class Netlink
38 {
39 public:
40  LOG_DECLARE_CLASS_CONTEXT("NETL", "Netlink");
41 
42  typedef std::pair<rtattr, void*> AttributeInfo;
43  typedef std::vector<AttributeInfo> AttributeList;
44  typedef std::pair<ifinfomsg, AttributeList> LinkInfo;
45  typedef std::pair<ifaddrmsg, AttributeList> AddressInfo;
46  typedef std::pair<rtmsg, AttributeList> RouteInfo;
47 
54  Netlink();
55 
65  virtual ~Netlink();
66 
80  bool getKernelDump();
81 
88  virtual bool checkKernelDump();
89 
97  bool setDefaultGateway(const char *gatewayAddress);
98 
108  bool linkUp(const int ifaceIndex);
109 
117  bool linkDown(const int ifaceIndex);
118 
129  bool setIP(const int ifaceIndex, const in_addr ip, const unsigned char netmask);
130 
139  bool findLink(const char *ifaceName, LinkInfo &linkInfo);
140 
150  bool findAddresses(const unsigned int interfaceIndex, std::vector<AddressInfo> &result);
151 
162  bool hasAddress(const std::vector<AddressInfo> &haystack, const int addressFamily, const char *needle);
163 
164 private:
172  bool setupNetlink();
173 
182  void clearCache();
183 
191  template<typename msgtype, typename InfoType>
192  bool saveMessage(const struct nlmsghdr &header, std::vector<InfoType> &result);
193 
208  template<typename msgtype>
209  bool getAttributes(const struct nlmsghdr &header, AttributeList &result);
210 
223  template<typename payload>struct netlink_request {
224  nlmsghdr hdr;
225  payload pay;
226  char attr[2048];
227  };
228 
240  template<typename payload>
241  netlink_request<payload> createMessage(const int type, const int flags);
242 
262  template<typename payload>
263  bool addAttribute(netlink_request<payload> &req, const int type, const size_t length, const void *data);
264 
276  template<typename payload>
277  bool sendMessage(netlink_request<payload> &request);
278 
286  int readMessage();
287 
291  void freeAttributes(AttributeList &attrList);
292 
293  // Netlink communication variables
294  struct sockaddr_nl m_local;
295  struct sockaddr_nl m_kernel;
296  int m_fd;
297  pid_t m_pid;
298  unsigned int m_sequenceNumber;
299 
300  // The local cache of links, addresses and routes
301  std::vector< LinkInfo > m_links;
302  std::vector< AddressInfo > m_addresses;
303  std::vector< RouteInfo > m_routes;
304 
305  // Status flags
306  bool m_hasKernelDump;
307  bool m_netlinkInitialized;
308 };
309 
310 } // namespace softwarecontainer
Developers guide to adding a config item: