blob: 5847af545391cff44d121ac1c78cdebf786d7bff [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 Pesavento43ff2a92017-05-18 19:50:57 -04003 * 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"
Junxiao Shi0d82d042017-07-07 06:15:27 +000030#include "ethernet-protocol.hpp"
Davide Pesavento43ff2a92017-05-18 19:50:57 -040031#include "pcap-helper.hpp"
Junxiao Shi0d82d042017-07-07 06:15:27 +000032#include <ndn-cxx/net/network-interface.hpp>
Davide Pesavento43ff2a92017-05-18 19:50:57 -040033
34namespace nfd {
35namespace face {
36
37/**
38 * \brief Class implementing Ethernet-based channel to create faces
39 */
40class EthernetChannel : public Channel
41{
42public:
43 /**
44 * \brief EthernetChannel-related error
45 */
46 class Error : public std::runtime_error
47 {
48 public:
49 explicit
50 Error(const std::string& what)
51 : std::runtime_error(what)
52 {
53 }
54 };
55
56 /**
57 * \brief Create an Ethernet channel on the given \p localEndpoint (network interface)
58 *
59 * To enable creation of faces upon incoming connections,
60 * one needs to explicitly call EthernetChannel::listen method.
61 */
Junxiao Shi0d82d042017-07-07 06:15:27 +000062 EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040063 time::nanoseconds idleTimeout);
64
65 bool
66 isListening() const override
67 {
68 return m_isListening;
69 }
70
71 size_t
72 size() const override
73 {
74 return m_channelFaces.size();
75 }
76
77 /**
78 * \brief Create a unicast Ethernet face toward \p remoteEndpoint
79 *
80 * \param remoteEndpoint The remote Ethernet endpoint
81 * \param persistency Persistency of the newly created face
Eric Newberry2642cd22017-07-13 21:34:53 -040082 * \param wantLpReliability whether LpReliability should be enabled
Davide Pesavento43ff2a92017-05-18 19:50:57 -040083 * \param onFaceCreated Callback to notify successful creation of the face
84 * \param onConnectFailed Callback to notify errors
85 */
86 void
87 connect(const ethernet::Address& remoteEndpoint,
88 ndn::nfd::FacePersistency persistency,
Eric Newberry2642cd22017-07-13 21:34:53 -040089 bool wantLpReliability,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040090 const FaceCreatedCallback& onFaceCreated,
91 const FaceCreationFailedCallback& onConnectFailed);
92
93 /**
94 * \brief Start listening
95 *
96 * Enable listening on the local endpoint, waiting for incoming frames,
97 * and creating a face when a frame is received from a new remote host.
98 *
99 * Faces created in this way will have on-demand persistency.
100 *
101 * \param onFaceCreated Callback to notify successful creation of a face
102 * \param onFaceCreationFailed Callback to notify errors
103 * \throw Error
104 */
105 void
106 listen(const FaceCreatedCallback& onFaceCreated,
107 const FaceCreationFailedCallback& onFaceCreationFailed);
108
109private:
110 void
111 asyncRead(const FaceCreatedCallback& onFaceCreated,
112 const FaceCreationFailedCallback& onReceiveFailed);
113
114 void
115 handleRead(const boost::system::error_code& error,
116 const FaceCreatedCallback& onFaceCreated,
117 const FaceCreationFailedCallback& onReceiveFailed);
118
119 void
120 processIncomingPacket(const uint8_t* packet, size_t length,
121 const ethernet::Address& sender,
122 const FaceCreatedCallback& onFaceCreated,
123 const FaceCreationFailedCallback& onReceiveFailed);
124
125 std::pair<bool, shared_ptr<Face>>
126 createFace(const ethernet::Address& remoteEndpoint,
Eric Newberry2642cd22017-07-13 21:34:53 -0400127 ndn::nfd::FacePersistency persistency,
128 bool wantLpReliability);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400129
130 void
131 updateFilter();
132
133private:
Junxiao Shi0d82d042017-07-07 06:15:27 +0000134 shared_ptr<const ndn::net::NetworkInterface> m_localEndpoint;
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400135 bool m_isListening;
136 boost::asio::posix::stream_descriptor m_socket;
137 PcapHelper m_pcap;
138 std::map<ethernet::Address, shared_ptr<Face>> m_channelFaces;
139 const time::nanoseconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces
140
141#ifdef _DEBUG
142 /// number of frames dropped by the kernel, as reported by libpcap
143 size_t m_nDropped;
144#endif
145};
146
147} // namespace face
148} // namespace nfd
149
150#endif // NFD_DAEMON_FACE_ETHERNET_CHANNEL_HPP