blob: cb4fbe971bba4428dedd68a7839668f90df1d9ef [file] [log] [blame]
Davide Pesavento43ff2a92017-05-18 19:50:57 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, 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 "pcap-helper.hpp"
31#include "core/network-interface.hpp"
32
33namespace nfd {
34namespace face {
35
36/**
37 * \brief Class implementing Ethernet-based channel to create faces
38 */
39class EthernetChannel : public Channel
40{
41public:
42 /**
43 * \brief EthernetChannel-related error
44 */
45 class Error : public std::runtime_error
46 {
47 public:
48 explicit
49 Error(const std::string& what)
50 : std::runtime_error(what)
51 {
52 }
53 };
54
55 /**
56 * \brief Create an Ethernet channel on the given \p localEndpoint (network interface)
57 *
58 * To enable creation of faces upon incoming connections,
59 * one needs to explicitly call EthernetChannel::listen method.
60 */
61 EthernetChannel(const NetworkInterfaceInfo& localEndpoint,
62 time::nanoseconds idleTimeout);
63
64 bool
65 isListening() const override
66 {
67 return m_isListening;
68 }
69
70 size_t
71 size() const override
72 {
73 return m_channelFaces.size();
74 }
75
76 /**
77 * \brief Create a unicast Ethernet face toward \p remoteEndpoint
78 *
79 * \param remoteEndpoint The remote Ethernet endpoint
80 * \param persistency Persistency of the newly created face
81 * \param onFaceCreated Callback to notify successful creation of the face
82 * \param onConnectFailed Callback to notify errors
83 */
84 void
85 connect(const ethernet::Address& remoteEndpoint,
86 ndn::nfd::FacePersistency persistency,
87 const FaceCreatedCallback& onFaceCreated,
88 const FaceCreationFailedCallback& onConnectFailed);
89
90 /**
91 * \brief Start listening
92 *
93 * Enable listening on the local endpoint, waiting for incoming frames,
94 * and creating a face when a frame is received from a new remote host.
95 *
96 * Faces created in this way will have on-demand persistency.
97 *
98 * \param onFaceCreated Callback to notify successful creation of a face
99 * \param onFaceCreationFailed Callback to notify errors
100 * \throw Error
101 */
102 void
103 listen(const FaceCreatedCallback& onFaceCreated,
104 const FaceCreationFailedCallback& onFaceCreationFailed);
105
106private:
107 void
108 asyncRead(const FaceCreatedCallback& onFaceCreated,
109 const FaceCreationFailedCallback& onReceiveFailed);
110
111 void
112 handleRead(const boost::system::error_code& error,
113 const FaceCreatedCallback& onFaceCreated,
114 const FaceCreationFailedCallback& onReceiveFailed);
115
116 void
117 processIncomingPacket(const uint8_t* packet, size_t length,
118 const ethernet::Address& sender,
119 const FaceCreatedCallback& onFaceCreated,
120 const FaceCreationFailedCallback& onReceiveFailed);
121
122 std::pair<bool, shared_ptr<Face>>
123 createFace(const ethernet::Address& remoteEndpoint,
124 ndn::nfd::FacePersistency persistency);
125
126 void
127 updateFilter();
128
129private:
130 const NetworkInterfaceInfo m_localEndpoint;
131 bool m_isListening;
132 boost::asio::posix::stream_descriptor m_socket;
133 PcapHelper m_pcap;
134 std::map<ethernet::Address, shared_ptr<Face>> m_channelFaces;
135 const time::nanoseconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces
136
137#ifdef _DEBUG
138 /// number of frames dropped by the kernel, as reported by libpcap
139 size_t m_nDropped;
140#endif
141};
142
143} // namespace face
144} // namespace nfd
145
146#endif // NFD_DAEMON_FACE_ETHERNET_CHANNEL_HPP