Davide Pesavento | a0b2a2c | 2018-07-19 01:01:06 -0400 | [diff] [blame^] | 1 | /* -*- 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> |
| 31 | #include <vector> |
| 32 | |
| 33 | #ifndef NDN_CXX_HAVE_RTNETLINK |
| 34 | #error "This file should not be included ..." |
| 35 | #endif |
| 36 | |
| 37 | namespace ndn { |
| 38 | namespace net { |
| 39 | |
| 40 | class NetlinkMessage; |
| 41 | |
| 42 | class NetlinkSocket : noncopyable |
| 43 | { |
| 44 | public: |
| 45 | using Error = NetworkMonitor::Error; |
| 46 | using MessageCallback = std::function<void(const NetlinkMessage&)>; |
| 47 | |
| 48 | void |
| 49 | joinGroup(int group); |
| 50 | |
| 51 | protected: |
| 52 | explicit |
| 53 | NetlinkSocket(boost::asio::io_service& io); |
| 54 | |
| 55 | ~NetlinkSocket(); |
| 56 | |
| 57 | void |
| 58 | open(int protocol); |
| 59 | |
| 60 | void |
| 61 | startAsyncReceive(MessageCallback cb); |
| 62 | |
| 63 | private: |
| 64 | void |
| 65 | asyncWait(); |
| 66 | |
| 67 | void |
| 68 | receiveAndValidate(); |
| 69 | |
| 70 | protected: |
| 71 | shared_ptr<boost::asio::posix::stream_descriptor> m_sock; ///< netlink socket descriptor |
| 72 | uint32_t m_pid; ///< port ID of this socket |
| 73 | uint32_t m_seqNum; ///< sequence number of the last netlink request sent to the kernel |
| 74 | |
| 75 | private: |
| 76 | std::vector<uint8_t> m_buffer; ///< buffer for netlink messages from the kernel |
| 77 | MessageCallback m_onMessage; ///< callback invoked when a valid netlink message is received |
| 78 | }; |
| 79 | |
| 80 | class RtnlSocket : public NetlinkSocket |
| 81 | { |
| 82 | public: |
| 83 | explicit |
| 84 | RtnlSocket(boost::asio::io_service& io); |
| 85 | |
| 86 | void |
| 87 | open(); |
| 88 | |
| 89 | void |
| 90 | sendDumpRequest(uint16_t nlmsgType); |
| 91 | |
| 92 | using NetlinkSocket::startAsyncReceive; |
| 93 | }; |
| 94 | |
| 95 | } // namespace net |
| 96 | } // namespace ndn |
| 97 | |
| 98 | #endif // NDN_NET_NETLINK_SOCKET_HPP |