blob: 86a3bc80644835bf572c5cf5322b07da42faedff [file] [log] [blame]
Davide Pesavento43ff2a92017-05-18 19:50:57 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0d82d042017-07-07 06:15:27 +00002/*
Davide Pesavento91c15c82024-01-15 17:14:23 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Davide Pesavento43ff2a92017-05-18 19:50:57 -04004 * 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 Shi0d82d042017-07-07 06:15:27 +000030#include "ethernet-protocol.hpp"
Davide Pesavento43ff2a92017-05-18 19:50:57 -040031#include "pcap-helper.hpp"
Davide Pesavento3db98072021-03-09 23:03:27 -050032
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040033#include <boost/asio/posix/stream_descriptor.hpp>
Junxiao Shi0d82d042017-07-07 06:15:27 +000034#include <ndn-cxx/net/network-interface.hpp>
Davide Pesavento43ff2a92017-05-18 19:50:57 -040035
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050036#include <map>
37
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038namespace nfd::face {
Davide Pesavento43ff2a92017-05-18 19:50:57 -040039
40/**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040041 * \brief Class implementing an Ethernet-based channel to create faces.
Davide Pesavento43ff2a92017-05-18 19:50:57 -040042 */
Davide Pesavento3db98072021-03-09 23:03:27 -050043class EthernetChannel final : public Channel
Davide Pesavento43ff2a92017-05-18 19:50:57 -040044{
45public:
46 /**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040047 * \brief EthernetChannel-related error.
Davide Pesavento43ff2a92017-05-18 19:50:57 -040048 */
49 class Error : public std::runtime_error
50 {
51 public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040052 using std::runtime_error::runtime_error;
Davide Pesavento43ff2a92017-05-18 19:50:57 -040053 };
54
55 /**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040056 * \brief Create an Ethernet channel on the given \p localEndpoint (network interface).
Davide Pesavento43ff2a92017-05-18 19:50:57 -040057 *
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040058 * To enable the creation of faces upon incoming connections, one needs to
59 * explicitly call listen().
Davide Pesavento43ff2a92017-05-18 19:50:57 -040060 */
Junxiao Shi0d82d042017-07-07 06:15:27 +000061 EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040062 time::nanoseconds idleTimeout);
63
64 bool
Davide Pesavento3db98072021-03-09 23:03:27 -050065 isListening() const final
Davide Pesavento43ff2a92017-05-18 19:50:57 -040066 {
67 return m_isListening;
68 }
69
70 size_t
Davide Pesavento3db98072021-03-09 23:03:27 -050071 size() const final
Davide Pesavento43ff2a92017-05-18 19:50:57 -040072 {
73 return m_channelFaces.size();
74 }
75
76 /**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040077 * \brief Create a unicast Ethernet face toward \p remoteEndpoint.
Davide Pesavento43ff2a92017-05-18 19:50:57 -040078 */
79 void
80 connect(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050081 const FaceParams& params,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040082 const FaceCreatedCallback& onFaceCreated,
83 const FaceCreationFailedCallback& onConnectFailed);
84
85 /**
Davide Pesavento9bf64cc2023-10-05 02:12:02 -040086 * \brief Start listening.
Davide Pesavento43ff2a92017-05-18 19:50:57 -040087 *
88 * Enable listening on the local endpoint, waiting for incoming frames,
89 * and creating a face when a frame is received from a new remote host.
90 *
91 * Faces created in this way will have on-demand persistency.
92 *
93 * \param onFaceCreated Callback to notify successful creation of a face
94 * \param onFaceCreationFailed Callback to notify errors
95 * \throw Error
96 */
97 void
98 listen(const FaceCreatedCallback& onFaceCreated,
99 const FaceCreationFailedCallback& onFaceCreationFailed);
100
101private:
102 void
103 asyncRead(const FaceCreatedCallback& onFaceCreated,
104 const FaceCreationFailedCallback& onReceiveFailed);
105
106 void
107 handleRead(const boost::system::error_code& error,
108 const FaceCreatedCallback& onFaceCreated,
109 const FaceCreationFailedCallback& onReceiveFailed);
110
111 void
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500112 processIncomingPacket(span<const uint8_t> packet,
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400113 const ethernet::Address& sender,
114 const FaceCreatedCallback& onFaceCreated,
115 const FaceCreationFailedCallback& onReceiveFailed);
116
117 std::pair<bool, shared_ptr<Face>>
118 createFace(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500119 const FaceParams& params);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400120
121 void
122 updateFilter();
123
124private:
Junxiao Shi0d82d042017-07-07 06:15:27 +0000125 shared_ptr<const ndn::net::NetworkInterface> m_localEndpoint;
Davide Pesavento91c15c82024-01-15 17:14:23 -0500126 bool m_isListening = false;
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400127 boost::asio::posix::stream_descriptor m_socket;
128 PcapHelper m_pcap;
129 std::map<ethernet::Address, shared_ptr<Face>> m_channelFaces;
130 const time::nanoseconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces
131
Davide Pesavento91c15c82024-01-15 17:14:23 -0500132#ifndef NDEBUG
133 /// Number of frames dropped by the kernel, as reported by libpcap
134 size_t m_nDropped = 0;
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400135#endif
136};
137
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400138} // namespace nfd::face
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400139
140#endif // NFD_DAEMON_FACE_ETHERNET_CHANNEL_HPP