blob: 5f0f94067c54e040e1c997aa88203f88e17e56db [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,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070073 const time::seconds& timeout/* = time::seconds(4)*/)
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 Afanasyeva0a10fb2014-02-13 19:56:15 -080093size_t
94TcpChannel::size() const
95{
96 return m_channelFaces.size();
97}
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080098
99void
Yukai Tu16aabbc2015-10-06 05:08:42 -0700100TcpChannel::createFace(ip::tcp::socket&& socket,
Eric Newberryf40551a2016-09-05 15:41:16 -0700101 bool isOnDemand,
102 bool wantLocalFieldsEnabled,
103 const FaceCreatedCallback& onFaceCreated)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800104{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700105 shared_ptr<Face> face;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100106 tcp::Endpoint remoteEndpoint = socket.remote_endpoint();
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600107
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100108 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100109 if (it == m_channelFaces.end()) {
Yukai Tu16aabbc2015-10-06 05:08:42 -0700110 auto persistency = isOnDemand ? ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
111 : ndn::nfd::FACE_PERSISTENCY_PERSISTENT;
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400112 auto linkService = make_unique<GenericLinkService>();
Eric Newberryf40551a2016-09-05 15:41:16 -0700113 auto options = linkService->getOptions();
114 options.allowLocalFields = wantLocalFieldsEnabled;
115 linkService->setOptions(options);
116
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400117 auto transport = make_unique<TcpTransport>(std::move(socket), persistency);
Eric Newberryf40551a2016-09-05 15:41:16 -0700118
Junxiao Shicde37ad2015-12-24 01:02:05 -0700119 face = make_shared<Face>(std::move(linkService), std::move(transport));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600120
Davide Pesavento292e5e12015-03-13 02:08:33 +0100121 m_channelFaces[remoteEndpoint] = face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700122 connectFaceClosedSignal(*face,
123 [this, remoteEndpoint] {
124 NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
125 m_channelFaces.erase(remoteEndpoint);
126 });
Davide Pesavento292e5e12015-03-13 02:08:33 +0100127 }
128 else {
129 // we already have a face for this endpoint, just reuse it
130 face = it->second;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700131
Davide Pesavento292e5e12015-03-13 02:08:33 +0100132 boost::system::error_code error;
133 socket.shutdown(ip::tcp::socket::shutdown_both, error);
134 socket.close(error);
135 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800136
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600137 // Need to invoke the callback regardless of whether or not we have already created
138 // the face so that control responses and such can be sent.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800139 onFaceCreated(face);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800140}
141
142void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100143TcpChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100144 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800145{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100146 m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept, this,
147 boost::asio::placeholders::error,
148 onFaceCreated, onAcceptFailed));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800149}
150
151void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100152TcpChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100153 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100154 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800155{
156 if (error) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100157 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800158 return;
159
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100160 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Accept failed: " << error.message());
161 if (onAcceptFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700162 onAcceptFailed(500, "Accept failed: " + error.message());
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800163 return;
164 }
165
Davide Pesavento292e5e12015-03-13 02:08:33 +0100166 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connection from " << m_acceptSocket.remote_endpoint());
167
Eric Newberryf40551a2016-09-05 15:41:16 -0700168 createFace(std::move(m_acceptSocket), true, false, onFaceCreated);
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800169
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100170 // prepare accepting the next connection
171 accept(onFaceCreated, onAcceptFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800172}
173
174void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100175TcpChannel::handleConnect(const boost::system::error_code& error,
176 const shared_ptr<ip::tcp::socket>& socket,
Eric Newberryf40551a2016-09-05 15:41:16 -0700177 bool wantLocalFieldsEnabled,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100178 const scheduler::EventId& connectTimeoutEvent,
179 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100180 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800181{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700182 scheduler::cancel(connectTimeoutEvent);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800183
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700184#if (BOOST_VERSION == 105400)
185 // To handle regression in Boost 1.54
186 // https://svn.boost.org/trac/boost/ticket/8795
187 boost::system::error_code anotherErrorCode;
188 socket->remote_endpoint(anotherErrorCode);
189 if (error || anotherErrorCode) {
190#else
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800191 if (error) {
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700192#endif
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100193 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800194 return;
195
Davide Pesavento292e5e12015-03-13 02:08:33 +0100196 NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << error.message());
197
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800198 socket->close();
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700199
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100200 if (onConnectFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700201 onConnectFailed(504, "Connect failed: " + error.message());
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800202 return;
203 }
204
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100205 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connected to " << socket->remote_endpoint());
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700206
Eric Newberryf40551a2016-09-05 15:41:16 -0700207 createFace(std::move(*socket), false, wantLocalFieldsEnabled, onFaceCreated);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800208}
209
210void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100211TcpChannel::handleConnectTimeout(const shared_ptr<ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100212 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800213{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700214 NFD_LOG_DEBUG("Connect to remote endpoint timed out");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100215
216 // abort the connection attempt
217 boost::system::error_code error;
218 socket->close(error);
219
220 if (onConnectFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700221 onConnectFailed(504, "Connect to remote endpoint timed out");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800222}
223
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400224} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800225} // namespace nfd