network-monitor-impl-rtnl.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
24 #ifndef NDN_UTIL_NETWORK_MONITOR_IMPL_RTNL_HPP
25 #define NDN_UTIL_NETWORK_MONITOR_IMPL_RTNL_HPP
26 
27 #include "ndn-cxx-config.hpp"
28 #include "../network-monitor.hpp"
29 
30 #ifndef NDN_CXX_HAVE_RTNETLINK
31 #error "This file should not be compiled ..."
32 #endif
33 
34 #include <boost/asio/posix/stream_descriptor.hpp>
35 
36 #include <array>
37 #include <map>
38 
39 #include <linux/netlink.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/if_addr.h>
42 #include <linux/if_link.h>
43 
44 namespace ndn {
45 namespace util {
46 
47 class NetworkMonitor::Impl
48 {
49 public:
52  Impl(NetworkMonitor& nm, boost::asio::io_service& io);
53 
54  ~Impl();
55 
56  uint32_t
58  {
64  }
65 
66  shared_ptr<NetworkInterface>
67  getNetworkInterface(const std::string& ifname) const;
68 
69  std::vector<shared_ptr<NetworkInterface>>
70  listNetworkInterfaces() const;
71 
72 private:
73  struct RtnlRequest
74  {
75  nlmsghdr nlh;
76  ifinfomsg ifi;
77  rtattr rta __attribute__((aligned(NLMSG_ALIGNTO))); // rtattr has to be aligned
78  uint32_t rtext; // space for IFLA_EXT_MASK
79  };
80 
81  bool
82  isEnumerating() const;
83 
84  void
85  initSocket();
86 
87  void
88  sendDumpRequest(uint16_t nlmsgType);
89 
90  void
91  asyncRead();
92 
93  void
94  handleRead(const boost::system::error_code& error, size_t nBytesReceived,
95  const shared_ptr<boost::asio::posix::stream_descriptor>& socket);
96 
97  void
98  parseNetlinkMessage(const nlmsghdr* nlh, size_t len);
99 
100  void
101  parseLinkMessage(const nlmsghdr* nlh, const ifinfomsg* ifi);
102 
103  void
104  parseAddressMessage(const nlmsghdr* nlh, const ifaddrmsg* ifa);
105 
106  void
107  parseRouteMessage(const nlmsghdr* nlh, const rtmsg* rtm);
108 
109  static void
110  updateInterfaceState(NetworkInterface& interface, uint8_t operState);
111 
112 private:
113  NetworkMonitor& m_nm;
114  std::map<int /*ifindex*/, shared_ptr<NetworkInterface>> m_interfaces;
115  std::array<uint8_t, 16384> m_buffer;
116  shared_ptr<boost::asio::posix::stream_descriptor> m_socket;
117  uint32_t m_pid;
118  uint32_t m_sequenceNo;
119  bool m_isEnumeratingLinks;
120  bool m_isEnumeratingAddresses;
121 };
122 
123 } // namespace util
124 } // namespace ndn
125 
126 #endif // NDN_UTIL_NETWORK_MONITOR_IMPL_RTNL_HPP
Impl(NetworkMonitor &nm, boost::asio::io_service &io)
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:75
NetworkMonitor onInterfaceAdded and onInterfaceRemoved signals are supported.
Network interfaces monitor.
listNetworkInterfaces() and getNetworkInterface() are supported
NetworkMonitor(boost::asio::io_service &io)
Construct instance, request enumeration of all network interfaces, and start monitoring for network s...
Represents one network interface attached to the host.
NetworkInterface onStateChanged signal is supported.
std::vector< shared_ptr< NetworkInterface > > listNetworkInterfaces() const
NetworkInterface onMtuChanged signal is supported.
shared_ptr< NetworkInterface > getNetworkInterface(const std::string &) const
NetworkInterface onAddressAdded and onAddressRemoved signals are supported.