blob: 7e7fa8787e785b1b3cacb3670d486b5ebb414565 [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 Pesavento3db98072021-03-09 23:03:27 -05003 * Copyright (c) 2014-2021, 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
Junxiao Shi0d82d042017-07-07 06:15:27 +000033#include <ndn-cxx/net/network-interface.hpp>
Davide Pesavento43ff2a92017-05-18 19:50:57 -040034
35namespace nfd {
36namespace face {
37
38/**
39 * \brief Class implementing Ethernet-based channel to create faces
40 */
Davide Pesavento3db98072021-03-09 23:03:27 -050041class EthernetChannel final : public Channel
Davide Pesavento43ff2a92017-05-18 19:50:57 -040042{
43public:
44 /**
45 * \brief EthernetChannel-related error
46 */
47 class Error : public std::runtime_error
48 {
49 public:
50 explicit
51 Error(const std::string& what)
52 : std::runtime_error(what)
53 {
54 }
55 };
56
57 /**
58 * \brief Create an Ethernet channel on the given \p localEndpoint (network interface)
59 *
60 * To enable creation of faces upon incoming connections,
61 * one needs to explicitly call EthernetChannel::listen method.
62 */
Junxiao Shi0d82d042017-07-07 06:15:27 +000063 EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040064 time::nanoseconds idleTimeout);
65
66 bool
Davide Pesavento3db98072021-03-09 23:03:27 -050067 isListening() const final
Davide Pesavento43ff2a92017-05-18 19:50:57 -040068 {
69 return m_isListening;
70 }
71
72 size_t
Davide Pesavento3db98072021-03-09 23:03:27 -050073 size() const final
Davide Pesavento43ff2a92017-05-18 19:50:57 -040074 {
75 return m_channelFaces.size();
76 }
77
78 /**
79 * \brief Create a unicast Ethernet face toward \p remoteEndpoint
Davide Pesavento43ff2a92017-05-18 19:50:57 -040080 */
81 void
82 connect(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050083 const FaceParams& params,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040084 const FaceCreatedCallback& onFaceCreated,
85 const FaceCreationFailedCallback& onConnectFailed);
86
87 /**
88 * \brief Start listening
89 *
90 * Enable listening on the local endpoint, waiting for incoming frames,
91 * and creating a face when a frame is received from a new remote host.
92 *
93 * Faces created in this way will have on-demand persistency.
94 *
95 * \param onFaceCreated Callback to notify successful creation of a face
96 * \param onFaceCreationFailed Callback to notify errors
97 * \throw Error
98 */
99 void
100 listen(const FaceCreatedCallback& onFaceCreated,
101 const FaceCreationFailedCallback& onFaceCreationFailed);
102
103private:
104 void
105 asyncRead(const FaceCreatedCallback& onFaceCreated,
106 const FaceCreationFailedCallback& onReceiveFailed);
107
108 void
109 handleRead(const boost::system::error_code& error,
110 const FaceCreatedCallback& onFaceCreated,
111 const FaceCreationFailedCallback& onReceiveFailed);
112
113 void
114 processIncomingPacket(const uint8_t* packet, size_t length,
115 const ethernet::Address& sender,
116 const FaceCreatedCallback& onFaceCreated,
117 const FaceCreationFailedCallback& onReceiveFailed);
118
119 std::pair<bool, shared_ptr<Face>>
120 createFace(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500121 const FaceParams& params);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400122
123 void
124 updateFilter();
125
126private:
Junxiao Shi0d82d042017-07-07 06:15:27 +0000127 shared_ptr<const ndn::net::NetworkInterface> m_localEndpoint;
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400128 bool m_isListening;
129 boost::asio::posix::stream_descriptor m_socket;
130 PcapHelper m_pcap;
131 std::map<ethernet::Address, shared_ptr<Face>> m_channelFaces;
132 const time::nanoseconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces
133
134#ifdef _DEBUG
135 /// number of frames dropped by the kernel, as reported by libpcap
136 size_t m_nDropped;
137#endif
138};
139
140} // namespace face
141} // namespace nfd
142
143#endif // NFD_DAEMON_FACE_ETHERNET_CHANNEL_HPP