Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 25 | |
| 26 | #include "tcp-channel.hpp" |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 27 | #include "generic-link-service.hpp" |
| 28 | #include "tcp-transport.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 29 | #include "core/global-io.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 31 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 33 | NFD_LOG_INIT("TcpChannel"); |
| 34 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 35 | namespace ip = boost::asio::ip; |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 36 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 37 | TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 38 | : m_localEndpoint(localEndpoint) |
| 39 | , m_acceptor(getGlobalIoService()) |
| 40 | , m_acceptSocket(getGlobalIoService()) |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 41 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 42 | setUri(FaceUri(m_localEndpoint)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 46 | TcpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 47 | const FaceCreationFailedCallback& onAcceptFailed, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 48 | int backlog/* = tcp::acceptor::max_connections*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 49 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 50 | 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 Afanasyev | eee71fd | 2014-03-13 17:40:33 -0700 | [diff] [blame] | 57 | if (m_localEndpoint.address().is_v6()) |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 58 | m_acceptor.set_option(ip::v6_only(true)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 59 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 60 | m_acceptor.bind(m_localEndpoint); |
| 61 | m_acceptor.listen(backlog); |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 62 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 63 | // start accepting connections |
| 64 | accept(onFaceCreated, onAcceptFailed); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 68 | TcpChannel::connect(const tcp::Endpoint& remoteEndpoint, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 69 | bool wantLocalFieldsEnabled, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 70 | const FaceCreatedCallback& onFaceCreated, |
| 71 | const FaceCreationFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 72 | const time::seconds& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 73 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 74 | auto it = m_channelFaces.find(remoteEndpoint); |
| 75 | if (it != m_channelFaces.end()) { |
| 76 | onFaceCreated(it->second); |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 80 | auto clientSocket = make_shared<ip::tcp::socket>(ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 81 | |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 82 | scheduler::EventId connectTimeoutEvent = scheduler::schedule(timeout, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 83 | bind(&TcpChannel::handleConnectTimeout, this, clientSocket, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 85 | clientSocket->async_connect(remoteEndpoint, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 86 | bind(&TcpChannel::handleConnect, this, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 87 | boost::asio::placeholders::error, clientSocket, |
| 88 | wantLocalFieldsEnabled, connectTimeoutEvent, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 89 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 92 | size_t |
| 93 | TcpChannel::size() const |
| 94 | { |
| 95 | return m_channelFaces.size(); |
| 96 | } |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 97 | |
| 98 | void |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 99 | TcpChannel::createFace(ip::tcp::socket&& socket, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 100 | bool isOnDemand, |
| 101 | bool wantLocalFieldsEnabled, |
| 102 | const FaceCreatedCallback& onFaceCreated) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 103 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 104 | shared_ptr<Face> face; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 105 | tcp::Endpoint remoteEndpoint = socket.remote_endpoint(); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 106 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 107 | auto it = m_channelFaces.find(remoteEndpoint); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 108 | if (it == m_channelFaces.end()) { |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 109 | auto persistency = isOnDemand ? ndn::nfd::FACE_PERSISTENCY_ON_DEMAND |
| 110 | : ndn::nfd::FACE_PERSISTENCY_PERSISTENT; |
| 111 | auto linkService = make_unique<face::GenericLinkService>(); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 112 | auto options = linkService->getOptions(); |
| 113 | options.allowLocalFields = wantLocalFieldsEnabled; |
| 114 | linkService->setOptions(options); |
| 115 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 116 | auto transport = make_unique<face::TcpTransport>(std::move(socket), persistency); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 117 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 118 | face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 119 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 120 | m_channelFaces[remoteEndpoint] = face; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 121 | connectFaceClosedSignal(*face, |
| 122 | [this, remoteEndpoint] { |
| 123 | NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map"); |
| 124 | m_channelFaces.erase(remoteEndpoint); |
| 125 | }); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 126 | } |
| 127 | else { |
| 128 | // we already have a face for this endpoint, just reuse it |
| 129 | face = it->second; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 130 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 131 | boost::system::error_code error; |
| 132 | socket.shutdown(ip::tcp::socket::shutdown_both, error); |
| 133 | socket.close(error); |
| 134 | } |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 135 | |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 136 | // 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 Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 138 | onFaceCreated(face); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 142 | TcpChannel::accept(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 143 | const FaceCreationFailedCallback& onAcceptFailed) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 144 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 145 | m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept, this, |
| 146 | boost::asio::placeholders::error, |
| 147 | onFaceCreated, onAcceptFailed)); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 151 | TcpChannel::handleAccept(const boost::system::error_code& error, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 152 | const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 153 | const FaceCreationFailedCallback& onAcceptFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 154 | { |
| 155 | if (error) { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 156 | if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 157 | return; |
| 158 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 159 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] Accept failed: " << error.message()); |
| 160 | if (onAcceptFailed) |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 161 | onAcceptFailed(500, "Accept failed: " + error.message()); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 165 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connection from " << m_acceptSocket.remote_endpoint()); |
| 166 | |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 167 | createFace(std::move(m_acceptSocket), true, false, onFaceCreated); |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 168 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 169 | // prepare accepting the next connection |
| 170 | accept(onFaceCreated, onAcceptFailed); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 174 | TcpChannel::handleConnect(const boost::system::error_code& error, |
| 175 | const shared_ptr<ip::tcp::socket>& socket, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 176 | bool wantLocalFieldsEnabled, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 177 | const scheduler::EventId& connectTimeoutEvent, |
| 178 | const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 179 | const FaceCreationFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 180 | { |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 181 | scheduler::cancel(connectTimeoutEvent); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 182 | |
Alexander Afanasyev | 7465d5f | 2014-07-07 10:19:42 -0700 | [diff] [blame] | 183 | #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 Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 190 | if (error) { |
Alexander Afanasyev | 7465d5f | 2014-07-07 10:19:42 -0700 | [diff] [blame] | 191 | #endif |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 192 | if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 193 | return; |
| 194 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 195 | NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << error.message()); |
| 196 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 197 | socket->close(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 198 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 199 | if (onConnectFailed) |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 200 | onConnectFailed(504, "Connect failed: " + error.message()); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 204 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connected to " << socket->remote_endpoint()); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 205 | |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 206 | createFace(std::move(*socket), false, wantLocalFieldsEnabled, onFaceCreated); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 210 | TcpChannel::handleConnectTimeout(const shared_ptr<ip::tcp::socket>& socket, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 211 | const FaceCreationFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 212 | { |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 213 | NFD_LOG_DEBUG("Connect to remote endpoint timed out"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 214 | |
| 215 | // abort the connection attempt |
| 216 | boost::system::error_code error; |
| 217 | socket->close(error); |
| 218 | |
| 219 | if (onConnectFailed) |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 220 | onConnectFailed(504, "Connect to remote endpoint timed out"); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 223 | } // namespace nfd |