blob: 4075437236104443003e642fdcb50459fb33eaad [file] [log] [blame]
Davide Pesavento9a8bae52016-02-24 20:33:08 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento50b92262018-07-11 12:28:31 -04002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Davide Pesavento9a8bae52016-02-24 20:33:08 +01004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Davide Pesavento2bf35a62017-04-02 00:41:06 -040020 *
21 * @author Davide Pesavento <davide.pesavento@lip6.fr>
Davide Pesavento9a8bae52016-02-24 20:33:08 +010022 */
23
Davide Pesavento50b92262018-07-11 12:28:31 -040024#ifndef NDN_NET_NETWORK_MONITOR_IMPL_NETLINK_HPP
25#define NDN_NET_NETWORK_MONITOR_IMPL_NETLINK_HPP
Davide Pesavento9a8bae52016-02-24 20:33:08 +010026
Davide Pesavento2bf35a62017-04-02 00:41:06 -040027#include "ndn-cxx-config.hpp"
Davide Pesavento9a8bae52016-02-24 20:33:08 +010028#include "../network-monitor.hpp"
29
Davide Pesavento2bf35a62017-04-02 00:41:06 -040030#ifndef NDN_CXX_HAVE_RTNETLINK
Davide Pesavento50b92262018-07-11 12:28:31 -040031#error "This file should not be included ..."
Davide Pesavento2bf35a62017-04-02 00:41:06 -040032#endif
33
Davide Pesavento9a8bae52016-02-24 20:33:08 +010034#include <boost/asio/posix/stream_descriptor.hpp>
35
Davide Pesavento2bf35a62017-04-02 00:41:06 -040036#include <array>
37#include <map>
38
Davide Pesavento9a8bae52016-02-24 20:33:08 +010039namespace ndn {
Junxiao Shi25467942017-06-30 02:53:14 +000040namespace net {
Davide Pesavento9a8bae52016-02-24 20:33:08 +010041
Davide Pesavento50b92262018-07-11 12:28:31 -040042class NetlinkMessage;
43
44class NetworkMonitorImplNetlink : public NetworkMonitorImpl
Davide Pesavento9a8bae52016-02-24 20:33:08 +010045{
46public:
Junxiao Shi2dc416d2017-07-03 04:46:16 +000047 using Error = NetworkMonitor::Error;
48
Davide Pesavento2bf35a62017-04-02 00:41:06 -040049 /** \brief initialize netlink socket and start enumerating interfaces
50 */
Junxiao Shi2dc416d2017-07-03 04:46:16 +000051 explicit
Davide Pesavento50b92262018-07-11 12:28:31 -040052 NetworkMonitorImplNetlink(boost::asio::io_service& io);
Davide Pesavento9a8bae52016-02-24 20:33:08 +010053
Davide Pesavento50b92262018-07-11 12:28:31 -040054 ~NetworkMonitorImplNetlink();
Davide Pesavento2bf35a62017-04-02 00:41:06 -040055
Junxiao Shif4c1eb32017-05-30 17:05:10 +000056 uint32_t
Junxiao Shi2dc416d2017-07-03 04:46:16 +000057 getCapabilities() const final
Junxiao Shif4c1eb32017-05-30 17:05:10 +000058 {
59 return NetworkMonitor::CAP_ENUM |
60 NetworkMonitor::CAP_IF_ADD_REMOVE |
61 NetworkMonitor::CAP_STATE_CHANGE |
62 NetworkMonitor::CAP_MTU_CHANGE |
63 NetworkMonitor::CAP_ADDR_ADD_REMOVE;
64 }
65
Junxiao Shi2dc416d2017-07-03 04:46:16 +000066 shared_ptr<const NetworkInterface>
67 getNetworkInterface(const std::string& ifname) const final;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040068
Junxiao Shi2dc416d2017-07-03 04:46:16 +000069 std::vector<shared_ptr<const NetworkInterface>>
70 listNetworkInterfaces() const final;
Davide Pesavento2bf35a62017-04-02 00:41:06 -040071
Davide Pesavento9a8bae52016-02-24 20:33:08 +010072private:
Davide Pesavento2bf35a62017-04-02 00:41:06 -040073 bool
74 isEnumerating() const;
75
Davide Pesavento9a8bae52016-02-24 20:33:08 +010076 void
Davide Pesavento50b92262018-07-11 12:28:31 -040077 initSocket(int family);
78
79 void
80 joinGroup(int group);
Davide Pesavento2bf35a62017-04-02 00:41:06 -040081
82 void
83 sendDumpRequest(uint16_t nlmsgType);
84
85 void
86 asyncRead();
87
88 void
Davide Pesavento2aec97a2018-07-12 23:59:00 -040089 receiveMessage();
Davide Pesavento2bf35a62017-04-02 00:41:06 -040090
91 void
Davide Pesavento50b92262018-07-11 12:28:31 -040092 parseNetlinkMessage(const NetlinkMessage& nlmsg);
Davide Pesavento2bf35a62017-04-02 00:41:06 -040093
94 void
Davide Pesavento50b92262018-07-11 12:28:31 -040095 parseLinkMessage(const NetlinkMessage& nlmsg);
Davide Pesavento2bf35a62017-04-02 00:41:06 -040096
97 void
Davide Pesavento50b92262018-07-11 12:28:31 -040098 parseAddressMessage(const NetlinkMessage& nlmsg);
Davide Pesavento2bf35a62017-04-02 00:41:06 -040099
100 void
Davide Pesavento50b92262018-07-11 12:28:31 -0400101 parseRouteMessage(const NetlinkMessage& nlmsg);
102
103 void
104 parseErrorMessage(const NetlinkMessage& nlmsg);
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400105
106 static void
107 updateInterfaceState(NetworkInterface& interface, uint8_t operState);
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100108
109private:
Junxiao Shi2dc416d2017-07-03 04:46:16 +0000110 std::map<int, shared_ptr<NetworkInterface>> m_interfaces; ///< ifindex => interface
111 std::array<uint8_t, 16384> m_buffer; ///< netlink messages received from the kernel
Davide Pesavento2bf35a62017-04-02 00:41:06 -0400112 shared_ptr<boost::asio::posix::stream_descriptor> m_socket; ///< the netlink socket
113 uint32_t m_pid; ///< our port ID (unicast address for netlink sockets)
114 uint32_t m_sequenceNo; ///< sequence number of the last netlink request sent to the kernel
115 bool m_isEnumeratingLinks; ///< true if a dump of all links is in progress
116 bool m_isEnumeratingAddresses; ///< true if a dump of all addresses is in progress
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100117};
118
Junxiao Shi25467942017-06-30 02:53:14 +0000119} // namespace net
Davide Pesavento9a8bae52016-02-24 20:33:08 +0100120} // namespace ndn
121
Davide Pesavento50b92262018-07-11 12:28:31 -0400122#endif // NDN_NET_NETWORK_MONITOR_IMPL_NETLINK_HPP