blob: 1dee52141455427dd25d9df853fab7828cfd42ac [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/*
Eric Newberry0c841642018-01-17 15:01:00 -07003 * Copyright (c) 2014-2018, 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
Eric Newberry0c841642018-01-17 15:01:00 -070038TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint, bool wantCongestionMarking)
Davide Pesavento292e5e12015-03-13 02:08:33 +010039 : m_localEndpoint(localEndpoint)
40 , m_acceptor(getGlobalIoService())
Davide Pesavento77911cc2017-04-08 22:12:30 -040041 , m_socket(getGlobalIoService())
Eric Newberry0c841642018-01-17 15:01:00 -070042 , m_wantCongestionMarking(wantCongestionMarking)
Junxiao Shi61e3cc52014-03-03 20:40:28 -070043{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010044 setUri(FaceUri(m_localEndpoint));
Davide Pesavento77911cc2017-04-08 22:12:30 -040045 NFD_LOG_CHAN_INFO("Creating channel");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080046}
47
48void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080049TcpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010050 const FaceCreationFailedCallback& onAcceptFailed,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080051 int backlog/* = tcp::acceptor::max_connections*/)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080052{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010053 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040054 NFD_LOG_CHAN_WARN("Already listening");
Davide Pesavento6ad890a2015-03-09 03:43:17 +010055 return;
56 }
57
58 m_acceptor.open(m_localEndpoint.protocol());
59 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040060 if (m_localEndpoint.address().is_v6()) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +010061 m_acceptor.set_option(ip::v6_only(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040062 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010063 m_acceptor.bind(m_localEndpoint);
64 m_acceptor.listen(backlog);
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -070065
Davide Pesavento6ad890a2015-03-09 03:43:17 +010066 accept(onFaceCreated, onAcceptFailed);
Davide Pesavento77911cc2017-04-08 22:12:30 -040067 NFD_LOG_CHAN_DEBUG("Started listening");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080068}
69
70void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080071TcpChannel::connect(const tcp::Endpoint& remoteEndpoint,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -040072 ndn::nfd::FacePersistency persistency,
Eric Newberry2642cd22017-07-13 21:34:53 -040073 bool wantLocalFields,
74 bool wantLpReliability,
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 Pesavento292e5e12015-03-13 02:08:33 +010086 auto clientSocket = make_shared<ip::tcp::socket>(ref(getGlobalIoService()));
Davide Pesavento77911cc2017-04-08 22:12:30 -040087 auto timeoutEvent = scheduler::schedule(timeout, bind(&TcpChannel::handleConnectTimeout, this,
88 remoteEndpoint, clientSocket, onConnectFailed));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080089
Davide Pesavento77911cc2017-04-08 22:12:30 -040090 NFD_LOG_CHAN_TRACE("Connecting to " << remoteEndpoint);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080091 clientSocket->async_connect(remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +010092 bind(&TcpChannel::handleConnect, this,
Eric Newberry2642cd22017-07-13 21:34:53 -040093 boost::asio::placeholders::error, remoteEndpoint, clientSocket,
94 // Creating a parameters struct works around limit on number of
95 // parameters to bind
96 ConnectParams{persistency, wantLocalFields, wantLpReliability},
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -040097 timeoutEvent, onFaceCreated, onConnectFailed));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080098}
99
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800100void
Yukai Tu16aabbc2015-10-06 05:08:42 -0700101TcpChannel::createFace(ip::tcp::socket&& socket,
Davide Pesavento77911cc2017-04-08 22:12:30 -0400102 ndn::nfd::FacePersistency persistency,
Eric Newberry2642cd22017-07-13 21:34:53 -0400103 bool wantLocalFields,
104 bool wantLpReliability,
Eric Newberryf40551a2016-09-05 15:41:16 -0700105 const FaceCreatedCallback& onFaceCreated)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800106{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700107 shared_ptr<Face> face;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100108 tcp::Endpoint remoteEndpoint = socket.remote_endpoint();
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600109
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100110 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100111 if (it == m_channelFaces.end()) {
Eric Newberry2642cd22017-07-13 21:34:53 -0400112 GenericLinkService::Options options;
113 options.allowLocalFields = wantLocalFields;
114 options.reliabilityOptions.isEnabled = wantLpReliability;
Eric Newberry0c841642018-01-17 15:01:00 -0700115 options.allowCongestionMarking = m_wantCongestionMarking;
Eric Newberry2642cd22017-07-13 21:34:53 -0400116 auto linkService = make_unique<GenericLinkService>(options);
Eric Newberryf40551a2016-09-05 15:41:16 -0700117
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400118 auto transport = make_unique<TcpTransport>(std::move(socket), persistency);
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;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400122 connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
Davide Pesavento292e5e12015-03-13 02:08:33 +0100123 }
124 else {
125 // we already have a face for this endpoint, just reuse it
126 face = it->second;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400127 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700128
Davide Pesavento292e5e12015-03-13 02:08:33 +0100129 boost::system::error_code error;
130 socket.shutdown(ip::tcp::socket::shutdown_both, error);
131 socket.close(error);
132 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800133
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600134 // Need to invoke the callback regardless of whether or not we have already created
135 // the face so that control responses and such can be sent.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800136 onFaceCreated(face);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800137}
138
139void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100140TcpChannel::accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100141 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800142{
Davide Pesavento77911cc2017-04-08 22:12:30 -0400143 m_acceptor.async_accept(m_socket, bind(&TcpChannel::handleAccept, this,
144 boost::asio::placeholders::error,
145 onFaceCreated, onAcceptFailed));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800146}
147
148void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100149TcpChannel::handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100150 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100151 const FaceCreationFailedCallback& onAcceptFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800152{
153 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400154 if (error != boost::asio::error::operation_aborted) {
155 NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
156 if (onAcceptFailed)
157 onAcceptFailed(500, "Accept failed: " + error.message());
158 }
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800159 return;
160 }
161
Davide Pesavento77911cc2017-04-08 22:12:30 -0400162 NFD_LOG_CHAN_TRACE("Incoming connection from " << m_socket.remote_endpoint());
Eric Newberry2642cd22017-07-13 21:34:53 -0400163 createFace(std::move(m_socket), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, false, false, onFaceCreated);
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800164
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100165 // prepare accepting the next connection
166 accept(onFaceCreated, onAcceptFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800167}
168
169void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100170TcpChannel::handleConnect(const boost::system::error_code& error,
Davide Pesavento77911cc2017-04-08 22:12:30 -0400171 const tcp::Endpoint& remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100172 const shared_ptr<ip::tcp::socket>& socket,
Eric Newberry2642cd22017-07-13 21:34:53 -0400173 TcpChannel::ConnectParams params,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100174 const scheduler::EventId& connectTimeoutEvent,
175 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100176 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800177{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700178 scheduler::cancel(connectTimeoutEvent);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800179
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700180#if (BOOST_VERSION == 105400)
181 // To handle regression in Boost 1.54
182 // https://svn.boost.org/trac/boost/ticket/8795
183 boost::system::error_code anotherErrorCode;
184 socket->remote_endpoint(anotherErrorCode);
185 if (error || anotherErrorCode) {
186#else
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800187 if (error) {
Alexander Afanasyev7465d5f2014-07-07 10:19:42 -0700188#endif
Davide Pesavento77911cc2017-04-08 22:12:30 -0400189 if (error != boost::asio::error::operation_aborted) {
190 NFD_LOG_CHAN_DEBUG("Connection to " << remoteEndpoint << " failed: " << error.message());
191 if (onConnectFailed)
192 onConnectFailed(504, "Connection failed: " + error.message());
193 }
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800194 return;
195 }
196
Davide Pesavento77911cc2017-04-08 22:12:30 -0400197 NFD_LOG_CHAN_TRACE("Connected to " << socket->remote_endpoint());
Eric Newberry2642cd22017-07-13 21:34:53 -0400198 createFace(std::move(*socket), params.persistency, params.wantLocalFields,
199 params.wantLpReliability, onFaceCreated);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800200}
201
202void
Davide Pesavento77911cc2017-04-08 22:12:30 -0400203TcpChannel::handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
204 const shared_ptr<ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100205 const FaceCreationFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800206{
Davide Pesavento77911cc2017-04-08 22:12:30 -0400207 NFD_LOG_CHAN_DEBUG("Connection to " << remoteEndpoint << " timed out");
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100208
209 // abort the connection attempt
210 boost::system::error_code error;
211 socket->close(error);
212
213 if (onConnectFailed)
Davide Pesavento77911cc2017-04-08 22:12:30 -0400214 onConnectFailed(504, "Connection timed out");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800215}
216
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400217} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800218} // namespace nfd