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-2022, 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 namespace nfd::face {
37 
41 class EthernetChannel final : public Channel
42 {
43 public:
47  class Error : public std::runtime_error
48  {
49  public:
50  using std::runtime_error::runtime_error;
51  };
52 
59  EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
60  time::nanoseconds idleTimeout);
61 
62  bool
63  isListening() const final
64  {
65  return m_isListening;
66  }
67 
68  size_t
69  size() const final
70  {
71  return m_channelFaces.size();
72  }
73 
77  void
78  connect(const ethernet::Address& remoteEndpoint,
79  const FaceParams& params,
80  const FaceCreatedCallback& onFaceCreated,
81  const FaceCreationFailedCallback& onConnectFailed);
82 
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
110  processIncomingPacket(span<const uint8_t> packet,
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,
117  const FaceParams& params);
118 
119  void
120  updateFilter();
121 
122 private:
123  shared_ptr<const ndn::net::NetworkInterface> m_localEndpoint;
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;
129 
130 #ifdef _DEBUG
132  size_t m_nDropped;
133 #endif
134 };
135 
136 } // namespace nfd::face
137 
138 #endif // NFD_DAEMON_FACE_ETHERNET_CHANNEL_HPP
Represents a channel that listens on a local endpoint.
Definition: channel.hpp:41
EthernetChannel-related error.
Class implementing 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:92
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:88
Parameters used to set Transport properties or LinkService options on a newly created face.
Definition: face-common.hpp:85