Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 3 | * 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 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" |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 27 | #include "tcp-face.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 28 | #include "core/global-io.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 30 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 32 | NFD_LOG_INIT("TcpChannel"); |
| 33 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 34 | using namespace boost::asio; |
| 35 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 36 | TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 37 | : m_localEndpoint(localEndpoint) |
| 38 | , m_acceptor(getGlobalIoService()) |
| 39 | , m_acceptSocket(getGlobalIoService()) |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 41 | setUri(FaceUri(m_localEndpoint)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 45 | TcpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 46 | const ConnectFailedCallback& onAcceptFailed, |
| 47 | int backlog/* = tcp::acceptor::max_connections*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 48 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 49 | if (isListening()) { |
| 50 | NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening"); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | m_acceptor.open(m_localEndpoint.protocol()); |
| 55 | m_acceptor.set_option(ip::tcp::acceptor::reuse_address(true)); |
Alexander Afanasyev | eee71fd | 2014-03-13 17:40:33 -0700 | [diff] [blame] | 56 | if (m_localEndpoint.address().is_v6()) |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 57 | m_acceptor.set_option(ip::v6_only(true)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 58 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 59 | m_acceptor.bind(m_localEndpoint); |
| 60 | m_acceptor.listen(backlog); |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 61 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 62 | // start accepting connections |
| 63 | accept(onFaceCreated, onAcceptFailed); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 67 | TcpChannel::connect(const tcp::Endpoint& remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 68 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 69 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 70 | const time::seconds& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 71 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 72 | auto it = m_channelFaces.find(remoteEndpoint); |
| 73 | if (it != m_channelFaces.end()) { |
| 74 | onFaceCreated(it->second); |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 78 | auto clientSocket = make_shared<ip::tcp::socket>(ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 79 | |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 80 | scheduler::EventId connectTimeoutEvent = scheduler::schedule(timeout, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 81 | bind(&TcpChannel::handleConnectTimeout, this, clientSocket, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 83 | clientSocket->async_connect(remoteEndpoint, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 84 | bind(&TcpChannel::handleConnect, this, |
| 85 | boost::asio::placeholders::error, |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 86 | clientSocket, connectTimeoutEvent, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 87 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 90 | size_t |
| 91 | TcpChannel::size() const |
| 92 | { |
| 93 | return m_channelFaces.size(); |
| 94 | } |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 95 | |
| 96 | void |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 97 | TcpChannel::createFace(ip::tcp::socket socket, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 98 | const FaceCreatedCallback& onFaceCreated, |
| 99 | bool isOnDemand) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 100 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 101 | shared_ptr<Face> face; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 102 | tcp::Endpoint remoteEndpoint = socket.remote_endpoint(); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 103 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 104 | auto it = m_channelFaces.find(remoteEndpoint); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 105 | if (it == m_channelFaces.end()) { |
| 106 | tcp::Endpoint localEndpoint = socket.local_endpoint(); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 107 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 108 | if (localEndpoint.address().is_loopback() && |
| 109 | remoteEndpoint.address().is_loopback()) |
| 110 | face = make_shared<TcpLocalFace>(FaceUri(remoteEndpoint), FaceUri(localEndpoint), |
| 111 | std::move(socket), isOnDemand); |
| 112 | else |
| 113 | face = make_shared<TcpFace>(FaceUri(remoteEndpoint), FaceUri(localEndpoint), |
| 114 | std::move(socket), isOnDemand); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 115 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 116 | face->onFail.connectSingleShot([this, remoteEndpoint] (const std::string&) { |
| 117 | NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map"); |
| 118 | m_channelFaces.erase(remoteEndpoint); |
| 119 | }); |
| 120 | m_channelFaces[remoteEndpoint] = face; |
| 121 | } |
| 122 | else { |
| 123 | // we already have a face for this endpoint, just reuse it |
| 124 | face = it->second; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 125 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 126 | boost::system::error_code error; |
| 127 | socket.shutdown(ip::tcp::socket::shutdown_both, error); |
| 128 | socket.close(error); |
| 129 | } |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 130 | |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 131 | // 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 Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 133 | onFaceCreated(face); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 137 | TcpChannel::accept(const FaceCreatedCallback& onFaceCreated, |
| 138 | const ConnectFailedCallback& onAcceptFailed) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 139 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 140 | m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept, this, |
| 141 | boost::asio::placeholders::error, |
| 142 | onFaceCreated, onAcceptFailed)); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 146 | TcpChannel::handleAccept(const boost::system::error_code& error, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 147 | const FaceCreatedCallback& onFaceCreated, |
| 148 | const ConnectFailedCallback& onAcceptFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 149 | { |
| 150 | if (error) { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 151 | 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] | 152 | return; |
| 153 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 154 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] Accept failed: " << error.message()); |
| 155 | if (onAcceptFailed) |
| 156 | onAcceptFailed(error.message()); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 160 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connection from " << m_acceptSocket.remote_endpoint()); |
| 161 | |
| 162 | createFace(std::move(m_acceptSocket), onFaceCreated, true); |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 163 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 164 | // prepare accepting the next connection |
| 165 | accept(onFaceCreated, onAcceptFailed); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 169 | TcpChannel::handleConnect(const boost::system::error_code& error, |
| 170 | const shared_ptr<ip::tcp::socket>& socket, |
| 171 | const scheduler::EventId& connectTimeoutEvent, |
| 172 | const FaceCreatedCallback& onFaceCreated, |
| 173 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 174 | { |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 175 | scheduler::cancel(connectTimeoutEvent); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | 7465d5f | 2014-07-07 10:19:42 -0700 | [diff] [blame] | 177 | #if (BOOST_VERSION == 105400) |
| 178 | // To handle regression in Boost 1.54 |
| 179 | // https://svn.boost.org/trac/boost/ticket/8795 |
| 180 | boost::system::error_code anotherErrorCode; |
| 181 | socket->remote_endpoint(anotherErrorCode); |
| 182 | if (error || anotherErrorCode) { |
| 183 | #else |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 184 | if (error) { |
Alexander Afanasyev | 7465d5f | 2014-07-07 10:19:42 -0700 | [diff] [blame] | 185 | #endif |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 186 | 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] | 187 | return; |
| 188 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 189 | NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << error.message()); |
| 190 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 191 | socket->close(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 192 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 193 | if (onConnectFailed) |
| 194 | onConnectFailed(error.message()); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 198 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connected to " << socket->remote_endpoint()); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 199 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 200 | createFace(std::move(*socket), onFaceCreated, false); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 204 | TcpChannel::handleConnectTimeout(const shared_ptr<ip::tcp::socket>& socket, |
| 205 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 206 | { |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 207 | NFD_LOG_DEBUG("Connect to remote endpoint timed out"); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 208 | |
| 209 | // abort the connection attempt |
| 210 | boost::system::error_code error; |
| 211 | socket->close(error); |
| 212 | |
| 213 | if (onConnectFailed) |
| 214 | onConnectFailed("Connect to remote endpoint timed out"); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 217 | } // namespace nfd |