blob: 978f540967806909e1ddddb08f9b437a62d5ddf8 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento8fd15e62017-04-06 19:58:54 -04003 * Copyright (c) 2014-2017, 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 {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040032namespace face {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080033
Alexander Afanasyev3958b012014-01-31 15:06:13 -080034NFD_LOG_INIT("TcpChannel");
35
Yukai Tu16aabbc2015-10-06 05:08:42 -070036namespace ip = boost::asio::ip;
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080037
Junxiao Shi61e3cc52014-03-03 20:40:28 -070038TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010039 : m_localEndpoint(localEndpoint)
40 , m_acceptor(getGlobalIoService())
41 , m_acceptSocket(getGlobalIoService())
Junxiao Shi61e3cc52014-03-03 20:40:28 -070042{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010043 setUri(FaceUri(m_localEndpoint));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080044}
45
46void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080047TcpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010048 const FaceCreationFailedCallback& onAcceptFailed,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080049 int backlog/* = tcp::acceptor::max_connections*/)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080050{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010051 if (isListening()) {
52 NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
53 return;
54 }
55
56 m_acceptor.open(m_localEndpoint.protocol());
57 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(true));
Alexander Afanasyeveee71fd2014-03-13 17:40:33 -070058 if (m_localEndpoint.address().is_v6())
Davide Pesavento6ad890a2015-03-09 03:43:17 +010059 m_acceptor.set_option(ip::v6_only(true));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080060
Davide Pesavento6ad890a2015-03-09 03:43:17 +010061 m_acceptor.bind(m_localEndpoint);
62 m_acceptor.listen(backlog);
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -070063
Davide Pesavento6ad890a2015-03-09 03:43:17 +010064 // start accepting connections
65 accept(onFaceCreated, onAcceptFailed);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080066}
67
68void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080069TcpChannel::connect(const tcp::Endpoint& remoteEndpoint,
Eric Newberryf40551a2016-09-05 15:41:16 -070070 bool wantLocalFieldsEnabled,
Davide Pesavento47ab0292015-11-02 18:45:39 +010071 const FaceCreatedCallback& onFaceCreated,
72 const FaceCreationFailedCallback& onConnectFailed,
Davide Pesaventoc19408d2017-04-08 00:42:55 -040073 time::nanoseconds timeout)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080074{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010075 auto it = m_channelFaces.find(remoteEndpoint);
76 if (it != m_channelFaces.end()) {
77 onFaceCreated(it->second);
Alexander Afanasyev334b7142014-01-28 12:59:39 -080078 return;
79 }
80
Davide Pesavento292e5e12015-03-13 02:08:33 +010081 auto clientSocket = make_shared<ip::tcp::socket>(ref(getGlobalIoService()));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080082
Junxiao Shi1e46be32015-01-08 20:18:05 -070083 scheduler::EventId connectTimeoutEvent = scheduler::schedule(timeout,
Davide Pesavento292e5e12015-03-13 02:08:33 +010084 bind(&TcpChannel::handleConnectTimeout, this, clientSocket, onConnectFailed));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080085
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080086 clientSocket->async_connect(remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +010087 bind(&TcpChannel::handleConnect, this,
Eric Newberryf40551a2016-09-05 15:41:16 -070088 boost::asio::placeholders::error, clientSocket,
89 wantLocalFieldsEnabled, connectTimeoutEvent,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080090 onFaceCreated, onConnectFailed));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080091}
92
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080093void
Yukai Tu16aabbc2015-10-06 05:08:42 -070094TcpChannel::createFace(ip::tcp::socket&& socket,
Eric Newberryf40551a2016-09-05 15:41:16 -070095 bool isOnDemand,
96 bool wantLocalFieldsEnabled,
97 const FaceCreatedCallback& onFaceCreated)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -080098{
Junxiao Shicde37ad2015-12-24 01:02:05 -070099 shared_ptr<Face> face;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100100 tcp::Endpoint remoteEndpoint = socket.remote_endpoint();
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600101
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100102 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100103 if (it == m_channelFaces.end()) {
Yukai Tu16aabbc2015-10-06 05:08:42 -0700104 auto persistency = isOnDemand ? ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
105 : ndn::nfd::FACE_PERSISTENCY_PERSISTENT;
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400106 auto linkService = make_unique<GenericLinkService>();
Eric Newberryf40551a2016-09-05 15:41:16 -0700107 auto options = linkService->getOptions();
108 options.allowLocalFields = wantLocalFieldsEnabled;
109 linkService->setOptions(options);
110
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400111 auto transport = make_unique<TcpTransport>(std::move(socket), persistency);
Eric Newberryf40551a2016-09-05 15:41:16 -0700112
Junxiao Shicde37ad2015-12-24 01:02:05 -0700113 face = make_shared<Face>(std::move(linkService), std::move(transport));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600114
Davide Pesavento292e5e12015-03-13 02:08:33 +0100115 m_channelFaces[remoteEndpoint] = face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700116 connectFaceClosedSignal(*face,
117 [this, remoteEndpoint] {
118 NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
119 m_channelFaces.erase(remoteEndpoint);
120 });
Davide Pesavento292e5e12015-03-13 02:08:33 +0100121 }
122 else {
123 // we already have a face for this endpoint, just reuse it
124 face = it->second;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700125
Davide Pesavento292e5e12015-03-13 02:08:33 +0100126 boost::system::error_code error;
127 socket.shutdown(ip::tcp::socket::shutdown_both, error);
128 socket.close(error);
129 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800130
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600131 // Need to invoke the callback regardless of whether or not we have already created
132 // the face so that control responses and such can be sent.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800133 onFaceCreated(face);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800134}
135
136void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100137TcpChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100138 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800139{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100140 m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept, this,
141 boost::asio::placeholders::error,
142 onFaceCreated, onAcceptFailed));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800143}
144
145void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100146TcpChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100147 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100148 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800149{
150 if (error) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100151 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800152 return;
153
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100154 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Accept failed: " << error.message());
155 if (onAcceptFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700156 onAcceptFailed(500, "Accept failed: " + error.message());
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800157 return;
158 }
159
Davide Pesavento292e5e12015-03-13 02:08:33 +0100160 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connection from " << m_acceptSocket.remote_endpoint());
161
Eric Newberryf40551a2016-09-05 15:41:16 -0700162 createFace(std::move(m_acceptSocket), true, false, onFaceCreated);
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800163
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100164 // prepare accepting the next connection
165 accept(onFaceCreated, onAcceptFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800166}
167
168void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100169TcpChannel::handleConnect(const boost::system::error_code& error,
170 const shared_ptr<ip::tcp::socket>& socket,
Eric Newberryf40551a2016-09-05 15:41:16 -0700171 bool wantLocalFieldsEnabled,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100172 const scheduler::EventId& connectTimeoutEvent,
173 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100174 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800175{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700176 scheduler::cancel(connectTimeoutEvent);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800177
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700178#if (BOOST_VERSION == 105400)
179 // To handle regression in Boost 1.54
180 // https://svn.boost.org/trac/boost/ticket/8795
181 boost::system::error_code anotherErrorCode;
182 socket->remote_endpoint(anotherErrorCode);
183 if (error || anotherErrorCode) {
184#else
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800185 if (error) {
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700186#endif
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100187 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800188 return;
189
Davide Pesavento292e5e12015-03-13 02:08:33 +0100190 NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << error.message());
191
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800192 socket->close();
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700193
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100194 if (onConnectFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700195 onConnectFailed(504, "Connect failed: " + error.message());
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800196 return;
197 }
198
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100199 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connected to " << socket->remote_endpoint());
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700200
Eric Newberryf40551a2016-09-05 15:41:16 -0700201 createFace(std::move(*socket), false, wantLocalFieldsEnabled, onFaceCreated);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800202}
203
204void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100205TcpChannel::handleConnectTimeout(const shared_ptr<ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100206 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800207{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700208 NFD_LOG_DEBUG("Connect to remote endpoint timed out");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100209
210 // abort the connection attempt
211 boost::system::error_code error;
212 socket->close(error);
213
214 if (onConnectFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700215 onConnectFailed(504, "Connect to remote endpoint timed out");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800216}
217
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400218} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800219} // namespace nfd