blob: 15036ec0f24e858359a60766bedd0746a3b0076e [file] [log] [blame]
Davide Pesaventoa0b2a2c2018-07-19 01:01:06 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
4 *
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.
20 *
21 * @author Davide Pesavento <davide.pesavento@lip6.fr>
22 */
23
24#ifndef NDN_NET_NETLINK_SOCKET_HPP
25#define NDN_NET_NETLINK_SOCKET_HPP
26
27#include "../../common.hpp"
28#include "../network-monitor.hpp"
29
30#include <boost/asio/posix/stream_descriptor.hpp>
Davide Pesaventodc5bb962018-08-12 23:40:38 -040031#include <map>
Davide Pesaventoa0b2a2c2018-07-19 01:01:06 -040032#include <vector>
33
34#ifndef NDN_CXX_HAVE_RTNETLINK
35#error "This file should not be included ..."
36#endif
37
38namespace ndn {
39namespace net {
40
41class NetlinkMessage;
42
43class NetlinkSocket : noncopyable
44{
45public:
46 using Error = NetworkMonitor::Error;
47 using MessageCallback = std::function<void(const NetlinkMessage&)>;
48
49 void
50 joinGroup(int group);
51
Davide Pesaventodc5bb962018-08-12 23:40:38 -040052 void
53 registerNotificationCallback(MessageCallback cb);
54
Davide Pesaventoa0b2a2c2018-07-19 01:01:06 -040055protected:
56 explicit
57 NetlinkSocket(boost::asio::io_service& io);
58
59 ~NetlinkSocket();
60
61 void
62 open(int protocol);
63
64 void
Davide Pesaventodc5bb962018-08-12 23:40:38 -040065 registerRequestCallback(uint32_t seq, MessageCallback cb);
Davide Pesaventoa0b2a2c2018-07-19 01:01:06 -040066
67private:
68 void
69 asyncWait();
70
71 void
72 receiveAndValidate();
73
74protected:
75 shared_ptr<boost::asio::posix::stream_descriptor> m_sock; ///< netlink socket descriptor
76 uint32_t m_pid; ///< port ID of this socket
77 uint32_t m_seqNum; ///< sequence number of the last netlink request sent to the kernel
78
79private:
80 std::vector<uint8_t> m_buffer; ///< buffer for netlink messages from the kernel
Davide Pesaventodc5bb962018-08-12 23:40:38 -040081 std::map<uint32_t, MessageCallback> m_pendingRequests; ///< request sequence number => callback
Davide Pesaventoa0b2a2c2018-07-19 01:01:06 -040082};
83
84class RtnlSocket : public NetlinkSocket
85{
86public:
87 explicit
88 RtnlSocket(boost::asio::io_service& io);
89
90 void
91 open();
92
93 void
Davide Pesaventodc5bb962018-08-12 23:40:38 -040094 sendDumpRequest(uint16_t nlmsgType, MessageCallback cb);
Davide Pesaventoa0b2a2c2018-07-19 01:01:06 -040095};
96
97} // namespace net
98} // namespace ndn
99
100#endif // NDN_NET_NETLINK_SOCKET_HPP