blob: f9873c2671e08deaf847aca9ff273ca204027b0a [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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,
Davide Pesavento47ab0292015-11-02 18:45:39 +010069 const FaceCreatedCallback& onFaceCreated,
70 const FaceCreationFailedCallback& onConnectFailed,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070071 const time::seconds& timeout/* = time::seconds(4)*/)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080072{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010073 auto it = m_channelFaces.find(remoteEndpoint);
74 if (it != m_channelFaces.end()) {
75 onFaceCreated(it->second);
Alexander Afanasyev334b7142014-01-28 12:59:39 -080076 return;
77 }
78
Davide Pesavento292e5e12015-03-13 02:08:33 +010079 auto clientSocket = make_shared<ip::tcp::socket>(ref(getGlobalIoService()));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080080
Junxiao Shi1e46be32015-01-08 20:18:05 -070081 scheduler::EventId connectTimeoutEvent = scheduler::schedule(timeout,
Davide Pesavento292e5e12015-03-13 02:08:33 +010082 bind(&TcpChannel::handleConnectTimeout, this, clientSocket, onConnectFailed));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080083
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080084 clientSocket->async_connect(remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +010085 bind(&TcpChannel::handleConnect, this,
86 boost::asio::placeholders::error,
Alexander Afanasyev18861802014-06-13 17:49:03 -070087 clientSocket, connectTimeoutEvent,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080088 onFaceCreated, onConnectFailed));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080089}
90
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080091size_t
92TcpChannel::size() const
93{
94 return m_channelFaces.size();
95}
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080096
97void
Yukai Tu16aabbc2015-10-06 05:08:42 -070098TcpChannel::createFace(ip::tcp::socket&& socket,
Alexander Afanasyev355c0662014-03-20 18:08:17 -070099 const FaceCreatedCallback& onFaceCreated,
100 bool isOnDemand)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800101{
Yukai Tu16aabbc2015-10-06 05:08:42 -0700102 shared_ptr<face::LpFaceWrapper> 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()) {
Yukai Tu16aabbc2015-10-06 05:08:42 -0700107 auto persistency = isOnDemand ? ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
108 : ndn::nfd::FACE_PERSISTENCY_PERSISTENT;
109 auto linkService = make_unique<face::GenericLinkService>();
110 auto transport = make_unique<face::TcpTransport>(std::move(socket), persistency);
111 auto lpFace = make_unique<face::LpFace>(std::move(linkService), std::move(transport));
112 face = make_shared<face::LpFaceWrapper>(std::move(lpFace));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600113
Davide Pesavento292e5e12015-03-13 02:08:33 +0100114 face->onFail.connectSingleShot([this, remoteEndpoint] (const std::string&) {
115 NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
116 m_channelFaces.erase(remoteEndpoint);
117 });
118 m_channelFaces[remoteEndpoint] = face;
119 }
120 else {
121 // we already have a face for this endpoint, just reuse it
122 face = it->second;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700123
Davide Pesavento292e5e12015-03-13 02:08:33 +0100124 boost::system::error_code error;
125 socket.shutdown(ip::tcp::socket::shutdown_both, error);
126 socket.close(error);
127 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800128
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600129 // Need to invoke the callback regardless of whether or not we have already created
130 // the face so that control responses and such can be sent.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800131 onFaceCreated(face);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800132}
133
134void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100135TcpChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100136 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800137{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100138 m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept, this,
139 boost::asio::placeholders::error,
140 onFaceCreated, onAcceptFailed));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800141}
142
143void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100144TcpChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100145 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100146 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800147{
148 if (error) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100149 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800150 return;
151
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100152 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Accept failed: " << error.message());
153 if (onAcceptFailed)
154 onAcceptFailed(error.message());
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800155 return;
156 }
157
Davide Pesavento292e5e12015-03-13 02:08:33 +0100158 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connection from " << m_acceptSocket.remote_endpoint());
159
160 createFace(std::move(m_acceptSocket), onFaceCreated, true);
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800161
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100162 // prepare accepting the next connection
163 accept(onFaceCreated, onAcceptFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800164}
165
166void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100167TcpChannel::handleConnect(const boost::system::error_code& error,
168 const shared_ptr<ip::tcp::socket>& socket,
169 const scheduler::EventId& connectTimeoutEvent,
170 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100171 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800172{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700173 scheduler::cancel(connectTimeoutEvent);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800174
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700175#if (BOOST_VERSION == 105400)
176 // To handle regression in Boost 1.54
177 // https://svn.boost.org/trac/boost/ticket/8795
178 boost::system::error_code anotherErrorCode;
179 socket->remote_endpoint(anotherErrorCode);
180 if (error || anotherErrorCode) {
181#else
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800182 if (error) {
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700183#endif
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100184 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800185 return;
186
Davide Pesavento292e5e12015-03-13 02:08:33 +0100187 NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << error.message());
188
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800189 socket->close();
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700190
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100191 if (onConnectFailed)
192 onConnectFailed(error.message());
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800193 return;
194 }
195
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100196 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connected to " << socket->remote_endpoint());
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700197
Davide Pesavento292e5e12015-03-13 02:08:33 +0100198 createFace(std::move(*socket), onFaceCreated, false);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800199}
200
201void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100202TcpChannel::handleConnectTimeout(const shared_ptr<ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100203 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800204{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700205 NFD_LOG_DEBUG("Connect to remote endpoint timed out");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100206
207 // abort the connection attempt
208 boost::system::error_code error;
209 socket->close(error);
210
211 if (onConnectFailed)
212 onConnectFailed("Connect to remote endpoint timed out");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800213}
214
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800215} // namespace nfd