blob: 6bdc4b95014d25a2272b678bb35047ef0203ec96 [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/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, 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 Afanasyeva9034b02014-01-26 18:32:02 -080030
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050031#include <ndn-cxx/util/scheduler.hpp>
32
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040033#include <boost/asio/ip/tcp.hpp>
34
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050035#include <map>
36
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::tcp {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040038using Endpoint = boost::asio::ip::tcp::endpoint;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040039} // namespace nfd::tcp
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010040
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040041namespace nfd::face {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040042
Alexander Afanasyevded17422018-04-03 19:00:23 -040043using DetermineFaceScopeFromAddress = std::function<ndn::nfd::FaceScope(const boost::asio::ip::address& local,
44 const boost::asio::ip::address& remote)>;
45
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080046/**
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040047 * \brief Class implementing a TCP-based channel to create faces.
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080048 *
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040049 * The channel can create faces as a response to incoming TCP connections (TcpChannel::listen()
50 * needs to be called for that to work) or explicitly by using TcpChannel::connect().
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080051 */
Davide Pesavento3db98072021-03-09 23:03:27 -050052class TcpChannel final : public Channel
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080053{
54public:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080055 /**
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040056 * \brief Create a TCP channel for the specified \p localEndpoint.
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080057 *
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040058 * To enable the creation of faces upon incoming connections, one needs to
59 * explicitly call listen().
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080060 */
Alexander Afanasyevded17422018-04-03 19:00:23 -040061 TcpChannel(const tcp::Endpoint& localEndpoint, bool wantCongestionMarking,
62 DetermineFaceScopeFromAddress determineFaceScope);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070063
Davide Pesaventoc19408d2017-04-08 00:42:55 -040064 bool
Davide Pesavento3db98072021-03-09 23:03:27 -050065 isListening() const final
Davide Pesaventoc19408d2017-04-08 00:42:55 -040066 {
67 return m_acceptor.is_open();
68 }
69
70 size_t
Davide Pesavento3db98072021-03-09 23:03:27 -050071 size() const final
Davide Pesaventoc19408d2017-04-08 00:42:55 -040072 {
73 return m_channelFaces.size();
74 }
75
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080076 /**
77 * \brief Enable listening on the local endpoint, accept connections,
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040078 * and create faces when remote host makes a connection.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080079 * \param onFaceCreated Callback to notify successful creation of the face
80 * \param onAcceptFailed Callback to notify when channel fails (accept call
81 * returns an error)
82 * \param backlog The maximum length of the queue of pending incoming
83 * connections
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080084 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080085 void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080086 listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010087 const FaceCreationFailedCallback& onAcceptFailed,
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040088 int backlog = boost::asio::socket_base::max_listen_connections);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080089
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080090 /**
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040091 * \brief Create a face by establishing a TCP connection to \p remoteEndpoint.
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080092 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080093 void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080094 connect(const tcp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050095 const FaceParams& params,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080096 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010097 const FaceCreationFailedCallback& onConnectFailed,
Davide Pesavento15b55052018-01-27 19:09:28 -050098 time::nanoseconds timeout = 8_s);
Eric Newberry2642cd22017-07-13 21:34:53 -040099
100private:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800101 void
Yukai Tu16aabbc2015-10-06 05:08:42 -0700102 createFace(boost::asio::ip::tcp::socket&& socket,
Davide Pesavento15b55052018-01-27 19:09:28 -0500103 const FaceParams& params,
Junxiao Shib98aa3a2022-01-12 01:14:08 +0000104 const FaceCreatedCallback& onFaceCreated,
105 const FaceCreationFailedCallback& onFaceCreationFailed);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800106
107 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100108 accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100109 const FaceCreationFailedCallback& onAcceptFailed);
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800110
111 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100112 handleConnect(const boost::system::error_code& error,
Davide Pesavento77911cc2017-04-08 22:12:30 -0400113 const tcp::Endpoint& remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100114 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Davide Pesavento15b55052018-01-27 19:09:28 -0500115 const FaceParams& params,
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500116 const ndn::scheduler::EventId& connectTimeoutEvent,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100117 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100118 const FaceCreationFailedCallback& onConnectFailed);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700119
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800120 void
Davide Pesavento77911cc2017-04-08 22:12:30 -0400121 handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
122 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100123 const FaceCreationFailedCallback& onConnectFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800124
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800125private:
Davide Pesavento77911cc2017-04-08 22:12:30 -0400126 const tcp::Endpoint m_localEndpoint;
Davide Pesavento9bf64cc2023-10-05 02:12:02 -0400127 const bool m_wantCongestionMarking;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100128 boost::asio::ip::tcp::acceptor m_acceptor;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400129 std::map<tcp::Endpoint, shared_ptr<Face>> m_channelFaces;
Alexander Afanasyevded17422018-04-03 19:00:23 -0400130 DetermineFaceScopeFromAddress m_determineFaceScope;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800131};
132
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400133} // namespace nfd::face
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700134
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700135#endif // NFD_DAEMON_FACE_TCP_CHANNEL_HPP