ethernet-channel.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
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"
30 #include "ethernet-protocol.hpp"
31 #include "pcap-helper.hpp"
32 
33 #include <boost/asio/posix/stream_descriptor.hpp>
34 #include <ndn-cxx/net/network-interface.hpp>
35 
36 #include <map>
37 
38 namespace nfd::face {
39 
43 class EthernetChannel final : public Channel
44 {
45 public:
49  class Error : public std::runtime_error
50  {
51  public:
52  using std::runtime_error::runtime_error;
53  };
54 
61  EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
62  time::nanoseconds idleTimeout);
63 
64  bool
65  isListening() const final
66  {
67  return m_isListening;
68  }
69 
70  size_t
71  size() const final
72  {
73  return m_channelFaces.size();
74  }
75 
79  void
80  connect(const ethernet::Address& remoteEndpoint,
81  const FaceParams& params,
82  const FaceCreatedCallback& onFaceCreated,
83  const FaceCreationFailedCallback& onConnectFailed);
84 
97  void
98  listen(const FaceCreatedCallback& onFaceCreated,
99  const FaceCreationFailedCallback& onFaceCreationFailed);
100 
101 private:
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
112  processIncomingPacket(span<const uint8_t> packet,
113  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,
119  const FaceParams& params);
120 
121  void
122  updateFilter();
123 
124 private:
125  shared_ptr<const ndn::net::NetworkInterface> m_localEndpoint;
126  bool m_isListening = false;
127  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;
131 
132 #ifndef NDEBUG
134  size_t m_nDropped = 0;
135 #endif
136 };
137 
138 } // namespace nfd::face
139 
140 #endif // NFD_DAEMON_FACE_ETHERNET_CHANNEL_HPP
Represents a channel that listens on a local endpoint.
Definition: channel.hpp:43
EthernetChannel-related error.
Class implementing an Ethernet-based channel to create faces.
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
bool isListening() const final
Returns whether the channel is listening.
void connect(const ethernet::Address &remoteEndpoint, const FaceParams &params, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast Ethernet face toward remoteEndpoint.
size_t size() const final
Returns the number of faces in the channel.
EthernetChannel(shared_ptr< const ndn::net::NetworkInterface > localEndpoint, time::nanoseconds idleTimeout)
Create an Ethernet channel on the given localEndpoint (network interface).
Helper class for dealing with libpcap handles.
Definition: pcap-helper.hpp:45
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
Definition: channel.hpp:94
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:90
Parameters used to set Transport properties or LinkService options on a newly created face.
Definition: face-common.hpp:87