blob: c8406953ccdfc441843d82142d4097dedc393515 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry2642cd22017-07-13 21:34:53 -04002/*
Eric Newberry0c841642018-01-17 15:01:00 -07003 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi1e46be32015-01-08 20:18:05 -070024 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_TCP_CHANNEL_HPP
27#define NFD_DAEMON_FACE_TCP_CHANNEL_HPP
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080028
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029#include "channel.hpp"
Alexander Afanasyev18861802014-06-13 17:49:03 -070030#include "core/scheduler.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080033
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010034namespace tcp {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070035typedef boost::asio::ip::tcp::endpoint Endpoint;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010036} // namespace tcp
37
Davide Pesavento8fd15e62017-04-06 19:58:54 -040038namespace face {
39
Alexander Afanasyevded17422018-04-03 19:00:23 -040040using DetermineFaceScopeFromAddress = std::function<ndn::nfd::FaceScope(const boost::asio::ip::address& local,
41 const boost::asio::ip::address& remote)>;
42
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080043/**
44 * \brief Class implementing TCP-based channel to create faces
45 *
46 * Channel can create faces as a response to incoming TCP
47 * connections (TcpChannel::listen needs to be called for that
48 * to work) or explicitly after using TcpChannel::connect method.
49 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070050class TcpChannel : public Channel
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080051{
52public:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080053 /**
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080054 * \brief Create TCP channel for the local endpoint
55 *
56 * To enable creation faces upon incoming connections,
57 * one needs to explicitly call TcpChannel::listen method.
58 */
Alexander Afanasyevded17422018-04-03 19:00:23 -040059 TcpChannel(const tcp::Endpoint& localEndpoint, bool wantCongestionMarking,
60 DetermineFaceScopeFromAddress determineFaceScope);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070061
Davide Pesaventoc19408d2017-04-08 00:42:55 -040062 bool
63 isListening() const override
64 {
65 return m_acceptor.is_open();
66 }
67
68 size_t
69 size() const override
70 {
71 return m_channelFaces.size();
72 }
73
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080074 /**
75 * \brief Enable listening on the local endpoint, accept connections,
76 * and create faces when remote host makes a connection
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080077 * \param onFaceCreated Callback to notify successful creation of the face
78 * \param onAcceptFailed Callback to notify when channel fails (accept call
79 * returns an error)
80 * \param backlog The maximum length of the queue of pending incoming
81 * connections
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080082 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080083 void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080084 listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010085 const FaceCreationFailedCallback& onAcceptFailed,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080086 int backlog = boost::asio::ip::tcp::acceptor::max_connections);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080087
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080088 /**
Davide Pesaventoc19408d2017-04-08 00:42:55 -040089 * \brief Create a face by establishing a TCP connection to \p remoteEndpoint
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080090 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080091 void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080092 connect(const tcp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050093 const FaceParams& params,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080094 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010095 const FaceCreationFailedCallback& onConnectFailed,
Davide Pesavento15b55052018-01-27 19:09:28 -050096 time::nanoseconds timeout = 8_s);
Eric Newberry2642cd22017-07-13 21:34:53 -040097
98private:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080099 void
Yukai Tu16aabbc2015-10-06 05:08:42 -0700100 createFace(boost::asio::ip::tcp::socket&& socket,
Davide Pesavento15b55052018-01-27 19:09:28 -0500101 const FaceParams& params,
Eric Newberryf40551a2016-09-05 15:41:16 -0700102 const FaceCreatedCallback& onFaceCreated);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800103
104 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100106 const FaceCreationFailedCallback& onAcceptFailed);
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800107
108 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100109 handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100110 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100111 const FaceCreationFailedCallback& onAcceptFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800112
113 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100114 handleConnect(const boost::system::error_code& error,
Davide Pesavento77911cc2017-04-08 22:12:30 -0400115 const tcp::Endpoint& remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100116 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Davide Pesavento15b55052018-01-27 19:09:28 -0500117 const FaceParams& params,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100118 const scheduler::EventId& connectTimeoutEvent,
119 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100120 const FaceCreationFailedCallback& onConnectFailed);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700121
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800122 void
Davide Pesavento77911cc2017-04-08 22:12:30 -0400123 handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
124 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100125 const FaceCreationFailedCallback& onConnectFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800126
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800127private:
Davide Pesavento77911cc2017-04-08 22:12:30 -0400128 const tcp::Endpoint m_localEndpoint;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100129 boost::asio::ip::tcp::acceptor m_acceptor;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400130 boost::asio::ip::tcp::socket m_socket;
131 std::map<tcp::Endpoint, shared_ptr<Face>> m_channelFaces;
Eric Newberry0c841642018-01-17 15:01:00 -0700132 bool m_wantCongestionMarking;
Alexander Afanasyevded17422018-04-03 19:00:23 -0400133 DetermineFaceScopeFromAddress m_determineFaceScope;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800134};
135
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400136} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800137} // namespace nfd
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700138
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700139#endif // NFD_DAEMON_FACE_TCP_CHANNEL_HPP