blob: dc0576f6ae8b67b4ddeff71a15fd36e29723bd17 [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 Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, 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"
Yukai Tu16aabbc2015-10-06 05:08:42 -070027#include "generic-link-service.hpp"
28#include "tcp-transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "common/global.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080030
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080031namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040032namespace face {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080033
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(TcpChannel);
Alexander Afanasyev3958b012014-01-31 15:06:13 -080035
Yukai Tu16aabbc2015-10-06 05:08:42 -070036namespace ip = boost::asio::ip;
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080037
Alexander Afanasyevded17422018-04-03 19:00:23 -040038TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint, bool wantCongestionMarking,
39 DetermineFaceScopeFromAddress determineFaceScope)
Davide Pesavento292e5e12015-03-13 02:08:33 +010040 : m_localEndpoint(localEndpoint)
41 , m_acceptor(getGlobalIoService())
Davide Pesavento77911cc2017-04-08 22:12:30 -040042 , m_socket(getGlobalIoService())
Eric Newberry0c841642018-01-17 15:01:00 -070043 , m_wantCongestionMarking(wantCongestionMarking)
Alexander Afanasyevded17422018-04-03 19:00:23 -040044 , m_determineFaceScope(std::move(determineFaceScope))
Junxiao Shi61e3cc52014-03-03 20:40:28 -070045{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010046 setUri(FaceUri(m_localEndpoint));
Davide Pesavento77911cc2017-04-08 22:12:30 -040047 NFD_LOG_CHAN_INFO("Creating channel");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080048}
49
50void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080051TcpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010052 const FaceCreationFailedCallback& onAcceptFailed,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080053 int backlog/* = tcp::acceptor::max_connections*/)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080054{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010055 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040056 NFD_LOG_CHAN_WARN("Already listening");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010057 return;
58 }
59
60 m_acceptor.open(m_localEndpoint.protocol());
61 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040062 if (m_localEndpoint.address().is_v6()) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +010063 m_acceptor.set_option(ip::v6_only(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040064 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010065 m_acceptor.bind(m_localEndpoint);
66 m_acceptor.listen(backlog);
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -070067
Davide Pesavento6ad890a2015-03-09 03:43:17 +010068 accept(onFaceCreated, onAcceptFailed);
Davide Pesavento77911cc2017-04-08 22:12:30 -040069 NFD_LOG_CHAN_DEBUG("Started listening");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080070}
71
72void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080073TcpChannel::connect(const tcp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050074 const FaceParams& params,
Davide Pesavento47ab0292015-11-02 18:45:39 +010075 const FaceCreatedCallback& onFaceCreated,
76 const FaceCreationFailedCallback& onConnectFailed,
Davide Pesaventoc19408d2017-04-08 00:42:55 -040077 time::nanoseconds timeout)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080078{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010079 auto it = m_channelFaces.find(remoteEndpoint);
80 if (it != m_channelFaces.end()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040081 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Davide Pesavento6ad890a2015-03-09 03:43:17 +010082 onFaceCreated(it->second);
Alexander Afanasyev334b7142014-01-28 12:59:39 -080083 return;
84 }
85
Davide Pesaventoe4b22382018-06-10 14:37:24 -040086 auto clientSocket = make_shared<ip::tcp::socket>(std::ref(getGlobalIoService()));
Davide Pesavento3dade002019-03-19 11:29:56 -060087 auto timeoutEvent = getScheduler().schedule(timeout, [=] {
Davide Pesaventoe4b22382018-06-10 14:37:24 -040088 handleConnectTimeout(remoteEndpoint, clientSocket, onConnectFailed);
89 });
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080090
Davide Pesavento77911cc2017-04-08 22:12:30 -040091 NFD_LOG_CHAN_TRACE("Connecting to " << remoteEndpoint);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040092 clientSocket->async_connect(remoteEndpoint, [=] (const auto& e) {
93 this->handleConnect(e, remoteEndpoint, clientSocket, params, timeoutEvent, onFaceCreated, onConnectFailed);
94 });
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080095}
96
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080097void
Yukai Tu16aabbc2015-10-06 05:08:42 -070098TcpChannel::createFace(ip::tcp::socket&& socket,
Davide Pesavento15b55052018-01-27 19:09:28 -050099 const FaceParams& params,
Eric Newberryf40551a2016-09-05 15:41:16 -0700100 const FaceCreatedCallback& onFaceCreated)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800101{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700102 shared_ptr<Face> face;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100103 tcp::Endpoint remoteEndpoint = socket.remote_endpoint();
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600104
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100106 if (it == m_channelFaces.end()) {
Eric Newberry2642cd22017-07-13 21:34:53 -0400107 GenericLinkService::Options options;
Davide Pesavento15b55052018-01-27 19:09:28 -0500108 options.allowLocalFields = params.wantLocalFields;
109 options.reliabilityOptions.isEnabled = params.wantLpReliability;
Eric Newberryf40551a2016-09-05 15:41:16 -0700110
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700111 if (boost::logic::indeterminate(params.wantCongestionMarking)) {
112 // Use default value for this channel if parameter is indeterminate
113 options.allowCongestionMarking = m_wantCongestionMarking;
114 }
115 else {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400116 options.allowCongestionMarking = bool(params.wantCongestionMarking);
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700117 }
118
119 if (params.baseCongestionMarkingInterval) {
120 options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
121 }
122 if (params.defaultCongestionThreshold) {
123 options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
124 }
125
126 auto linkService = make_unique<GenericLinkService>(options);
Alexander Afanasyevded17422018-04-03 19:00:23 -0400127 auto faceScope = m_determineFaceScope(socket.local_endpoint().address(),
128 socket.remote_endpoint().address());
129 auto transport = make_unique<TcpTransport>(std::move(socket), params.persistency, faceScope);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700130 face = make_shared<Face>(std::move(linkService), std::move(transport));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600131
Davide Pesavento292e5e12015-03-13 02:08:33 +0100132 m_channelFaces[remoteEndpoint] = face;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400133 connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
Davide Pesavento292e5e12015-03-13 02:08:33 +0100134 }
135 else {
136 // we already have a face for this endpoint, just reuse it
137 face = it->second;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400138 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700139
Davide Pesavento292e5e12015-03-13 02:08:33 +0100140 boost::system::error_code error;
141 socket.shutdown(ip::tcp::socket::shutdown_both, error);
142 socket.close(error);
143 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800144
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600145 // Need to invoke the callback regardless of whether or not we have already created
146 // the face so that control responses and such can be sent.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800147 onFaceCreated(face);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800148}
149
150void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100151TcpChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100152 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800153{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400154 m_acceptor.async_accept(m_socket, [=] (const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800155}
156
157void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100158TcpChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100159 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100160 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800161{
162 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400163 if (error != boost::asio::error::operation_aborted) {
164 NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
165 if (onAcceptFailed)
166 onAcceptFailed(500, "Accept failed: " + error.message());
167 }
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800168 return;
169 }
170
Davide Pesavento77911cc2017-04-08 22:12:30 -0400171 NFD_LOG_CHAN_TRACE("Incoming connection from " << m_socket.remote_endpoint());
Davide Pesavento15b55052018-01-27 19:09:28 -0500172
173 FaceParams params;
174 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
175 createFace(std::move(m_socket), params, onFaceCreated);
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800176
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100177 // prepare accepting the next connection
178 accept(onFaceCreated, onAcceptFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800179}
180
181void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100182TcpChannel::handleConnect(const boost::system::error_code& error,
Davide Pesavento77911cc2017-04-08 22:12:30 -0400183 const tcp::Endpoint& remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100184 const shared_ptr<ip::tcp::socket>& socket,
Davide Pesavento15b55052018-01-27 19:09:28 -0500185 const FaceParams& params,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100186 const scheduler::EventId& connectTimeoutEvent,
187 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100188 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800189{
Davide Pesavento3dade002019-03-19 11:29:56 -0600190 connectTimeoutEvent.cancel();
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800191
192 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400193 if (error != boost::asio::error::operation_aborted) {
194 NFD_LOG_CHAN_DEBUG("Connection to " << remoteEndpoint << " failed: " << error.message());
195 if (onConnectFailed)
196 onConnectFailed(504, "Connection failed: " + error.message());
197 }
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800198 return;
199 }
200
Davide Pesavento77911cc2017-04-08 22:12:30 -0400201 NFD_LOG_CHAN_TRACE("Connected to " << socket->remote_endpoint());
Davide Pesavento15b55052018-01-27 19:09:28 -0500202 createFace(std::move(*socket), params, onFaceCreated);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800203}
204
205void
Davide Pesavento77911cc2017-04-08 22:12:30 -0400206TcpChannel::handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
207 const shared_ptr<ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100208 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800209{
Davide Pesavento77911cc2017-04-08 22:12:30 -0400210 NFD_LOG_CHAN_DEBUG("Connection to " << remoteEndpoint << " timed out");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100211
212 // abort the connection attempt
213 boost::system::error_code error;
214 socket->close(error);
215
216 if (onConnectFailed)
Davide Pesavento77911cc2017-04-08 22:12:30 -0400217 onConnectFailed(504, "Connection timed out");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800218}
219
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400220} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800221} // namespace nfd