blob: 656b671f5bc0f8e7dbd9a60c07f37cdf218bf79f [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberry42602412016-08-27 09:33:18 -07003 * Copyright (c) 2014-2016, 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"
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029#include "core/global-io.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080030
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080031namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080032
Alexander Afanasyev3958b012014-01-31 15:06:13 -080033NFD_LOG_INIT("TcpChannel");
34
Yukai Tu16aabbc2015-10-06 05:08:42 -070035namespace ip = boost::asio::ip;
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080036
Junxiao Shi61e3cc52014-03-03 20:40:28 -070037TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010038 : m_localEndpoint(localEndpoint)
39 , m_acceptor(getGlobalIoService())
40 , m_acceptSocket(getGlobalIoService())
Junxiao Shi61e3cc52014-03-03 20:40:28 -070041{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010042 setUri(FaceUri(m_localEndpoint));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080043}
44
45void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080046TcpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010047 const FaceCreationFailedCallback& onAcceptFailed,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080048 int backlog/* = tcp::acceptor::max_connections*/)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080049{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010050 if (isListening()) {
51 NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
52 return;
53 }
54
55 m_acceptor.open(m_localEndpoint.protocol());
56 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(true));
Alexander Afanasyeveee71fd2014-03-13 17:40:33 -070057 if (m_localEndpoint.address().is_v6())
Davide Pesavento6ad890a2015-03-09 03:43:17 +010058 m_acceptor.set_option(ip::v6_only(true));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080059
Davide Pesavento6ad890a2015-03-09 03:43:17 +010060 m_acceptor.bind(m_localEndpoint);
61 m_acceptor.listen(backlog);
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -070062
Davide Pesavento6ad890a2015-03-09 03:43:17 +010063 // start accepting connections
64 accept(onFaceCreated, onAcceptFailed);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080065}
66
67void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080068TcpChannel::connect(const tcp::Endpoint& remoteEndpoint,
Eric Newberryf40551a2016-09-05 15:41:16 -070069 bool wantLocalFieldsEnabled,
Davide Pesavento47ab0292015-11-02 18:45:39 +010070 const FaceCreatedCallback& onFaceCreated,
71 const FaceCreationFailedCallback& onConnectFailed,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070072 const time::seconds& timeout/* = time::seconds(4)*/)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080073{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010074 auto it = m_channelFaces.find(remoteEndpoint);
75 if (it != m_channelFaces.end()) {
76 onFaceCreated(it->second);
Alexander Afanasyev334b7142014-01-28 12:59:39 -080077 return;
78 }
79
Davide Pesavento292e5e12015-03-13 02:08:33 +010080 auto clientSocket = make_shared<ip::tcp::socket>(ref(getGlobalIoService()));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080081
Junxiao Shi1e46be32015-01-08 20:18:05 -070082 scheduler::EventId connectTimeoutEvent = scheduler::schedule(timeout,
Davide Pesavento292e5e12015-03-13 02:08:33 +010083 bind(&TcpChannel::handleConnectTimeout, this, clientSocket, onConnectFailed));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080084
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080085 clientSocket->async_connect(remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +010086 bind(&TcpChannel::handleConnect, this,
Eric Newberryf40551a2016-09-05 15:41:16 -070087 boost::asio::placeholders::error, clientSocket,
88 wantLocalFieldsEnabled, connectTimeoutEvent,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080089 onFaceCreated, onConnectFailed));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080090}
91
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080092size_t
93TcpChannel::size() const
94{
95 return m_channelFaces.size();
96}
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080097
98void
Yukai Tu16aabbc2015-10-06 05:08:42 -070099TcpChannel::createFace(ip::tcp::socket&& socket,
Eric Newberryf40551a2016-09-05 15:41:16 -0700100 bool isOnDemand,
101 bool wantLocalFieldsEnabled,
102 const FaceCreatedCallback& onFaceCreated)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800103{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700104 shared_ptr<Face> face;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100105 tcp::Endpoint remoteEndpoint = socket.remote_endpoint();
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600106
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100107 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100108 if (it == m_channelFaces.end()) {
Yukai Tu16aabbc2015-10-06 05:08:42 -0700109 auto persistency = isOnDemand ? ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
110 : ndn::nfd::FACE_PERSISTENCY_PERSISTENT;
111 auto linkService = make_unique<face::GenericLinkService>();
Eric Newberryf40551a2016-09-05 15:41:16 -0700112 auto options = linkService->getOptions();
113 options.allowLocalFields = wantLocalFieldsEnabled;
114 linkService->setOptions(options);
115
Yukai Tu16aabbc2015-10-06 05:08:42 -0700116 auto transport = make_unique<face::TcpTransport>(std::move(socket), persistency);
Eric Newberryf40551a2016-09-05 15:41:16 -0700117
Junxiao Shicde37ad2015-12-24 01:02:05 -0700118 face = make_shared<Face>(std::move(linkService), std::move(transport));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600119
Davide Pesavento292e5e12015-03-13 02:08:33 +0100120 m_channelFaces[remoteEndpoint] = face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700121 connectFaceClosedSignal(*face,
122 [this, remoteEndpoint] {
123 NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
124 m_channelFaces.erase(remoteEndpoint);
125 });
Davide Pesavento292e5e12015-03-13 02:08:33 +0100126 }
127 else {
128 // we already have a face for this endpoint, just reuse it
129 face = it->second;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700130
Davide Pesavento292e5e12015-03-13 02:08:33 +0100131 boost::system::error_code error;
132 socket.shutdown(ip::tcp::socket::shutdown_both, error);
133 socket.close(error);
134 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800135
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600136 // Need to invoke the callback regardless of whether or not we have already created
137 // the face so that control responses and such can be sent.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800138 onFaceCreated(face);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800139}
140
141void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100142TcpChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100143 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800144{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100145 m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept, this,
146 boost::asio::placeholders::error,
147 onFaceCreated, onAcceptFailed));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800148}
149
150void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100151TcpChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100152 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100153 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800154{
155 if (error) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100156 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800157 return;
158
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100159 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Accept failed: " << error.message());
160 if (onAcceptFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700161 onAcceptFailed(500, "Accept failed: " + error.message());
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800162 return;
163 }
164
Davide Pesavento292e5e12015-03-13 02:08:33 +0100165 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connection from " << m_acceptSocket.remote_endpoint());
166
Eric Newberryf40551a2016-09-05 15:41:16 -0700167 createFace(std::move(m_acceptSocket), true, false, onFaceCreated);
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800168
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100169 // prepare accepting the next connection
170 accept(onFaceCreated, onAcceptFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800171}
172
173void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100174TcpChannel::handleConnect(const boost::system::error_code& error,
175 const shared_ptr<ip::tcp::socket>& socket,
Eric Newberryf40551a2016-09-05 15:41:16 -0700176 bool wantLocalFieldsEnabled,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100177 const scheduler::EventId& connectTimeoutEvent,
178 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100179 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800180{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700181 scheduler::cancel(connectTimeoutEvent);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800182
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700183#if (BOOST_VERSION == 105400)
184 // To handle regression in Boost 1.54
185 // https://svn.boost.org/trac/boost/ticket/8795
186 boost::system::error_code anotherErrorCode;
187 socket->remote_endpoint(anotherErrorCode);
188 if (error || anotherErrorCode) {
189#else
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800190 if (error) {
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700191#endif
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100192 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800193 return;
194
Davide Pesavento292e5e12015-03-13 02:08:33 +0100195 NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << error.message());
196
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800197 socket->close();
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700198
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100199 if (onConnectFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700200 onConnectFailed(504, "Connect failed: " + error.message());
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800201 return;
202 }
203
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100204 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connected to " << socket->remote_endpoint());
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700205
Eric Newberryf40551a2016-09-05 15:41:16 -0700206 createFace(std::move(*socket), false, wantLocalFieldsEnabled, onFaceCreated);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800207}
208
209void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100210TcpChannel::handleConnectTimeout(const shared_ptr<ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100211 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800212{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700213 NFD_LOG_DEBUG("Connect to remote endpoint timed out");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100214
215 // abort the connection attempt
216 boost::system::error_code error;
217 socket->close(error);
218
219 if (onConnectFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700220 onConnectFailed(504, "Connect to remote endpoint timed out");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800221}
222
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800223} // namespace nfd