blob: 2dd454da67ffb0429990825284e7bdf0967d0451 [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/*
Junxiao Shib98aa3a2022-01-12 01:14:08 +00003 * Copyright (c) 2014-2022, 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 Shia1937bf2014-11-06 11:43:40 -070024 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080025
26#include "tcp-channel.hpp"
Davide Pesaventocb425e82019-07-14 21:48:22 -040027#include "face.hpp"
Yukai Tu16aabbc2015-10-06 05:08:42 -070028#include "generic-link-service.hpp"
29#include "tcp-transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040030#include "common/global.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040033namespace face {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080034
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(TcpChannel);
Alexander Afanasyev3958b012014-01-31 15:06:13 -080036
Yukai Tu16aabbc2015-10-06 05:08:42 -070037namespace ip = boost::asio::ip;
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080038
Alexander Afanasyevded17422018-04-03 19:00:23 -040039TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint, bool wantCongestionMarking,
40 DetermineFaceScopeFromAddress determineFaceScope)
Davide Pesavento292e5e12015-03-13 02:08:33 +010041 : m_localEndpoint(localEndpoint)
42 , m_acceptor(getGlobalIoService())
Davide Pesavento77911cc2017-04-08 22:12:30 -040043 , m_socket(getGlobalIoService())
Eric Newberry0c841642018-01-17 15:01:00 -070044 , m_wantCongestionMarking(wantCongestionMarking)
Alexander Afanasyevded17422018-04-03 19:00:23 -040045 , m_determineFaceScope(std::move(determineFaceScope))
Junxiao Shi61e3cc52014-03-03 20:40:28 -070046{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010047 setUri(FaceUri(m_localEndpoint));
Davide Pesavento77911cc2017-04-08 22:12:30 -040048 NFD_LOG_CHAN_INFO("Creating channel");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080049}
50
51void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080052TcpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010053 const FaceCreationFailedCallback& onAcceptFailed,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080054 int backlog/* = tcp::acceptor::max_connections*/)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080055{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010056 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040057 NFD_LOG_CHAN_WARN("Already listening");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010058 return;
59 }
60
61 m_acceptor.open(m_localEndpoint.protocol());
62 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040063 if (m_localEndpoint.address().is_v6()) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +010064 m_acceptor.set_option(ip::v6_only(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040065 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010066 m_acceptor.bind(m_localEndpoint);
67 m_acceptor.listen(backlog);
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -070068
Davide Pesavento6ad890a2015-03-09 03:43:17 +010069 accept(onFaceCreated, onAcceptFailed);
Davide Pesavento77911cc2017-04-08 22:12:30 -040070 NFD_LOG_CHAN_DEBUG("Started listening");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080071}
72
73void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080074TcpChannel::connect(const tcp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050075 const FaceParams& params,
Davide Pesavento47ab0292015-11-02 18:45:39 +010076 const FaceCreatedCallback& onFaceCreated,
77 const FaceCreationFailedCallback& onConnectFailed,
Davide Pesaventoc19408d2017-04-08 00:42:55 -040078 time::nanoseconds timeout)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080079{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010080 auto it = m_channelFaces.find(remoteEndpoint);
81 if (it != m_channelFaces.end()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040082 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Davide Pesavento6ad890a2015-03-09 03:43:17 +010083 onFaceCreated(it->second);
Alexander Afanasyev334b7142014-01-28 12:59:39 -080084 return;
85 }
86
Alexander Afanasyeva6bd7da2019-04-25 17:26:11 -040087 auto clientSocket = make_shared<ip::tcp::socket>(getGlobalIoService());
Davide Pesavento3dade002019-03-19 11:29:56 -060088 auto timeoutEvent = getScheduler().schedule(timeout, [=] {
Davide Pesaventoe4b22382018-06-10 14:37:24 -040089 handleConnectTimeout(remoteEndpoint, clientSocket, onConnectFailed);
90 });
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080091
Davide Pesavento77911cc2017-04-08 22:12:30 -040092 NFD_LOG_CHAN_TRACE("Connecting to " << remoteEndpoint);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040093 clientSocket->async_connect(remoteEndpoint, [=] (const auto& e) {
94 this->handleConnect(e, remoteEndpoint, clientSocket, params, timeoutEvent, onFaceCreated, onConnectFailed);
95 });
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080096}
97
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080098void
Yukai Tu16aabbc2015-10-06 05:08:42 -070099TcpChannel::createFace(ip::tcp::socket&& socket,
Davide Pesavento15b55052018-01-27 19:09:28 -0500100 const FaceParams& params,
Junxiao Shib98aa3a2022-01-12 01:14:08 +0000101 const FaceCreatedCallback& onFaceCreated,
102 const FaceCreationFailedCallback& onFaceCreationFailed)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800103{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700104 shared_ptr<Face> face;
Junxiao Shib98aa3a2022-01-12 01:14:08 +0000105 boost::system::error_code ec;
106 tcp::Endpoint remoteEndpoint = socket.remote_endpoint(ec);
107 if (ec) {
108 NFD_LOG_CHAN_DEBUG("Retrieve socket remote endpoint failed: " << ec.message());
109 if (onFaceCreationFailed) {
110 onFaceCreationFailed(500, "Retrieve socket remote endpoint failed: " + ec.message());
111 }
112 return;
113 }
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600114
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100115 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100116 if (it == m_channelFaces.end()) {
Eric Newberry2642cd22017-07-13 21:34:53 -0400117 GenericLinkService::Options options;
Davide Pesavento15b55052018-01-27 19:09:28 -0500118 options.allowLocalFields = params.wantLocalFields;
119 options.reliabilityOptions.isEnabled = params.wantLpReliability;
Eric Newberryf40551a2016-09-05 15:41:16 -0700120
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700121 if (boost::logic::indeterminate(params.wantCongestionMarking)) {
122 // Use default value for this channel if parameter is indeterminate
123 options.allowCongestionMarking = m_wantCongestionMarking;
124 }
125 else {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400126 options.allowCongestionMarking = bool(params.wantCongestionMarking);
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700127 }
128
129 if (params.baseCongestionMarkingInterval) {
130 options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
131 }
132 if (params.defaultCongestionThreshold) {
133 options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
134 }
135
136 auto linkService = make_unique<GenericLinkService>(options);
Alexander Afanasyevded17422018-04-03 19:00:23 -0400137 auto faceScope = m_determineFaceScope(socket.local_endpoint().address(),
138 socket.remote_endpoint().address());
139 auto transport = make_unique<TcpTransport>(std::move(socket), params.persistency, faceScope);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700140 face = make_shared<Face>(std::move(linkService), std::move(transport));
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400141 face->setChannel(weak_from_this());
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600142
Davide Pesavento292e5e12015-03-13 02:08:33 +0100143 m_channelFaces[remoteEndpoint] = face;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400144 connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
Davide Pesavento292e5e12015-03-13 02:08:33 +0100145 }
146 else {
147 // we already have a face for this endpoint, just reuse it
148 face = it->second;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400149 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700150
Davide Pesavento292e5e12015-03-13 02:08:33 +0100151 boost::system::error_code error;
152 socket.shutdown(ip::tcp::socket::shutdown_both, error);
153 socket.close(error);
154 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800155
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600156 // Need to invoke the callback regardless of whether or not we have already created
157 // the face so that control responses and such can be sent.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800158 onFaceCreated(face);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800159}
160
161void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100162TcpChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100163 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800164{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400165 m_acceptor.async_accept(m_socket, [=] (const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800166}
167
168void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100169TcpChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100170 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100171 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800172{
173 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400174 if (error != boost::asio::error::operation_aborted) {
175 NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
176 if (onAcceptFailed)
177 onAcceptFailed(500, "Accept failed: " + error.message());
178 }
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800179 return;
180 }
181
Davide Pesavento77911cc2017-04-08 22:12:30 -0400182 NFD_LOG_CHAN_TRACE("Incoming connection from " << m_socket.remote_endpoint());
Davide Pesavento15b55052018-01-27 19:09:28 -0500183
184 FaceParams params;
185 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
Junxiao Shib98aa3a2022-01-12 01:14:08 +0000186 createFace(std::move(m_socket), params, onFaceCreated, onAcceptFailed);
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800187
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100188 // prepare accepting the next connection
189 accept(onFaceCreated, onAcceptFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800190}
191
192void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100193TcpChannel::handleConnect(const boost::system::error_code& error,
Davide Pesavento77911cc2017-04-08 22:12:30 -0400194 const tcp::Endpoint& remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100195 const shared_ptr<ip::tcp::socket>& socket,
Davide Pesavento15b55052018-01-27 19:09:28 -0500196 const FaceParams& params,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100197 const scheduler::EventId& connectTimeoutEvent,
198 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100199 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800200{
Davide Pesavento3dade002019-03-19 11:29:56 -0600201 connectTimeoutEvent.cancel();
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800202
203 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400204 if (error != boost::asio::error::operation_aborted) {
205 NFD_LOG_CHAN_DEBUG("Connection to " << remoteEndpoint << " failed: " << error.message());
206 if (onConnectFailed)
207 onConnectFailed(504, "Connection failed: " + error.message());
208 }
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800209 return;
210 }
211
Davide Pesavento77911cc2017-04-08 22:12:30 -0400212 NFD_LOG_CHAN_TRACE("Connected to " << socket->remote_endpoint());
Junxiao Shib98aa3a2022-01-12 01:14:08 +0000213 createFace(std::move(*socket), params, onFaceCreated, onConnectFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800214}
215
216void
Davide Pesavento77911cc2017-04-08 22:12:30 -0400217TcpChannel::handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
218 const shared_ptr<ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100219 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800220{
Davide Pesavento77911cc2017-04-08 22:12:30 -0400221 NFD_LOG_CHAN_DEBUG("Connection to " << remoteEndpoint << " timed out");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100222
223 // abort the connection attempt
224 boost::system::error_code error;
225 socket->close(error);
226
227 if (onConnectFailed)
Davide Pesavento77911cc2017-04-08 22:12:30 -0400228 onConnectFailed(504, "Connection timed out");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800229}
230
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400231} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800232} // namespace nfd