Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_DAEMON_FACE_ETHERNET_CHANNEL_HPP |
| 27 | #define NFD_DAEMON_FACE_ETHERNET_CHANNEL_HPP |
| 28 | |
| 29 | #include "channel.hpp" |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 30 | #include "ethernet-protocol.hpp" |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 31 | #include "pcap-helper.hpp" |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 32 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 33 | #include <boost/asio/posix/stream_descriptor.hpp> |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 34 | #include <ndn-cxx/net/network-interface.hpp> |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::face { |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * \brief Class implementing Ethernet-based channel to create faces |
| 40 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 41 | class EthernetChannel final : public Channel |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 42 | { |
| 43 | public: |
| 44 | /** |
| 45 | * \brief EthernetChannel-related error |
| 46 | */ |
| 47 | class Error : public std::runtime_error |
| 48 | { |
| 49 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 50 | using std::runtime_error::runtime_error; |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * \brief Create an Ethernet channel on the given \p localEndpoint (network interface) |
| 55 | * |
| 56 | * To enable creation of faces upon incoming connections, |
| 57 | * one needs to explicitly call EthernetChannel::listen method. |
| 58 | */ |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 59 | EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint, |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 60 | time::nanoseconds idleTimeout); |
| 61 | |
| 62 | bool |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 63 | isListening() const final |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 64 | { |
| 65 | return m_isListening; |
| 66 | } |
| 67 | |
| 68 | size_t |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 69 | size() const final |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 70 | { |
| 71 | return m_channelFaces.size(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * \brief Create a unicast Ethernet face toward \p remoteEndpoint |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 76 | */ |
| 77 | void |
| 78 | connect(const ethernet::Address& remoteEndpoint, |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 79 | const FaceParams& params, |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 80 | const FaceCreatedCallback& onFaceCreated, |
| 81 | const FaceCreationFailedCallback& onConnectFailed); |
| 82 | |
| 83 | /** |
| 84 | * \brief Start listening |
| 85 | * |
| 86 | * Enable listening on the local endpoint, waiting for incoming frames, |
| 87 | * and creating a face when a frame is received from a new remote host. |
| 88 | * |
| 89 | * Faces created in this way will have on-demand persistency. |
| 90 | * |
| 91 | * \param onFaceCreated Callback to notify successful creation of a face |
| 92 | * \param onFaceCreationFailed Callback to notify errors |
| 93 | * \throw Error |
| 94 | */ |
| 95 | void |
| 96 | listen(const FaceCreatedCallback& onFaceCreated, |
| 97 | const FaceCreationFailedCallback& onFaceCreationFailed); |
| 98 | |
| 99 | private: |
| 100 | void |
| 101 | asyncRead(const FaceCreatedCallback& onFaceCreated, |
| 102 | const FaceCreationFailedCallback& onReceiveFailed); |
| 103 | |
| 104 | void |
| 105 | handleRead(const boost::system::error_code& error, |
| 106 | const FaceCreatedCallback& onFaceCreated, |
| 107 | const FaceCreationFailedCallback& onReceiveFailed); |
| 108 | |
| 109 | void |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 110 | processIncomingPacket(span<const uint8_t> packet, |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 111 | const ethernet::Address& sender, |
| 112 | const FaceCreatedCallback& onFaceCreated, |
| 113 | const FaceCreationFailedCallback& onReceiveFailed); |
| 114 | |
| 115 | std::pair<bool, shared_ptr<Face>> |
| 116 | createFace(const ethernet::Address& remoteEndpoint, |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 117 | const FaceParams& params); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 118 | |
| 119 | void |
| 120 | updateFilter(); |
| 121 | |
| 122 | private: |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 123 | shared_ptr<const ndn::net::NetworkInterface> m_localEndpoint; |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 124 | bool m_isListening; |
| 125 | boost::asio::posix::stream_descriptor m_socket; |
| 126 | PcapHelper m_pcap; |
| 127 | std::map<ethernet::Address, shared_ptr<Face>> m_channelFaces; |
| 128 | const time::nanoseconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces |
| 129 | |
| 130 | #ifdef _DEBUG |
| 131 | /// number of frames dropped by the kernel, as reported by libpcap |
| 132 | size_t m_nDropped; |
| 133 | #endif |
| 134 | }; |
| 135 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 136 | } // namespace nfd::face |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 137 | |
| 138 | #endif // NFD_DAEMON_FACE_ETHERNET_CHANNEL_HPP |