Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "tcp-channel.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 8 | #include "core/global-io.hpp" |
| 9 | #include "core/face-uri.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 11 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 12 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 13 | NFD_LOG_INIT("TcpChannel"); |
| 14 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 15 | using namespace boost::asio; |
| 16 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 17 | TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint) |
| 18 | : m_localEndpoint(localEndpoint) |
| 19 | { |
| 20 | this->setUri(FaceUri(localEndpoint)); |
| 21 | } |
| 22 | |
| 23 | TcpChannel::~TcpChannel() |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 24 | { |
| 25 | } |
| 26 | |
| 27 | void |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 28 | TcpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 29 | const ConnectFailedCallback& onAcceptFailed, |
| 30 | int backlog/* = tcp::acceptor::max_connections*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 31 | { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 32 | m_acceptor = make_shared<ip::tcp::acceptor>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 33 | m_acceptor->open(m_localEndpoint.protocol()); |
| 34 | m_acceptor->set_option(ip::tcp::acceptor::reuse_address(true)); |
Alexander Afanasyev | eee71fd | 2014-03-13 17:40:33 -0700 | [diff] [blame] | 35 | if (m_localEndpoint.address().is_v6()) |
| 36 | { |
| 37 | m_acceptor->set_option(ip::v6_only(true)); |
| 38 | } |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 39 | m_acceptor->bind(m_localEndpoint); |
| 40 | m_acceptor->listen(backlog); |
| 41 | |
| 42 | shared_ptr<ip::tcp::socket> clientSocket = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 43 | make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 44 | m_acceptor->async_accept(*clientSocket, |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 45 | bind(&TcpChannel::handleSuccessfulAccept, this, _1, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 46 | clientSocket, |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 47 | onFaceCreated, onAcceptFailed)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 51 | TcpChannel::connect(const tcp::Endpoint& remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 52 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 53 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 54 | const time::seconds& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 55 | { |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 56 | ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint); |
| 57 | if (i != m_channelFaces.end()) { |
| 58 | onFaceCreated(i->second); |
| 59 | return; |
| 60 | } |
| 61 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 62 | shared_ptr<ip::tcp::socket> clientSocket = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 63 | make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 65 | shared_ptr<ndn::monotonic_deadline_timer> connectTimeoutTimer = |
| 66 | make_shared<ndn::monotonic_deadline_timer>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 68 | clientSocket->async_connect(remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 69 | bind(&TcpChannel::handleSuccessfulConnect, this, _1, |
| 70 | clientSocket, connectTimeoutTimer, |
| 71 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 72 | |
| 73 | connectTimeoutTimer->expires_from_now(timeout); |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 74 | connectTimeoutTimer->async_wait(bind(&TcpChannel::handleFailedConnect, this, _1, |
| 75 | clientSocket, connectTimeoutTimer, |
| 76 | onConnectFailed)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 79 | void |
| 80 | TcpChannel::connect(const std::string& remoteHost, const std::string& remotePort, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 81 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 82 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 83 | const time::seconds& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 84 | { |
| 85 | shared_ptr<ip::tcp::socket> clientSocket = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 86 | make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 88 | shared_ptr<ndn::monotonic_deadline_timer> connectTimeoutTimer = |
| 89 | make_shared<ndn::monotonic_deadline_timer>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 91 | ip::tcp::resolver::query query(remoteHost, remotePort); |
| 92 | shared_ptr<ip::tcp::resolver> resolver = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 93 | make_shared<ip::tcp::resolver>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 94 | |
| 95 | resolver->async_resolve(query, |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 96 | bind(&TcpChannel::handleEndpointResolution, this, _1, _2, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 97 | clientSocket, connectTimeoutTimer, |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 98 | onFaceCreated, onConnectFailed, |
| 99 | resolver)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 100 | |
| 101 | connectTimeoutTimer->expires_from_now(timeout); |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 102 | connectTimeoutTimer->async_wait(bind(&TcpChannel::handleFailedConnect, this, _1, |
| 103 | clientSocket, connectTimeoutTimer, |
| 104 | onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 107 | size_t |
| 108 | TcpChannel::size() const |
| 109 | { |
| 110 | return m_channelFaces.size(); |
| 111 | } |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 112 | |
| 113 | void |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 114 | TcpChannel::createFace(const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame^] | 115 | const FaceCreatedCallback& onFaceCreated, |
| 116 | bool isOnDemand) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 117 | { |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 118 | tcp::Endpoint remoteEndpoint = socket->remote_endpoint(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 120 | shared_ptr<Face> face; |
Alexander Afanasyev | bfd31de | 2014-02-26 13:20:08 -0800 | [diff] [blame] | 121 | if (socket->local_endpoint().address().is_loopback()) |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame^] | 122 | face = make_shared<TcpLocalFace>(boost::cref(socket), isOnDemand); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 123 | else |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame^] | 124 | face = make_shared<TcpFace>(boost::cref(socket), isOnDemand); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 126 | face->onFail += bind(&TcpChannel::afterFaceFailed, this, remoteEndpoint); |
| 127 | |
| 128 | onFaceCreated(face); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 129 | m_channelFaces[remoteEndpoint] = face; |
| 130 | } |
| 131 | |
| 132 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 133 | TcpChannel::afterFaceFailed(tcp::Endpoint &remoteEndpoint) |
| 134 | { |
| 135 | NFD_LOG_DEBUG("afterFaceFailed: " << remoteEndpoint); |
| 136 | m_channelFaces.erase(remoteEndpoint); |
| 137 | } |
| 138 | |
| 139 | void |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 140 | TcpChannel::handleSuccessfulAccept(const boost::system::error_code& error, |
| 141 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
| 142 | const FaceCreatedCallback& onFaceCreated, |
| 143 | const ConnectFailedCallback& onAcceptFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 144 | { |
| 145 | if (error) { |
| 146 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 147 | return; |
| 148 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 149 | NFD_LOG_DEBUG("Connect to remote endpoint failed: " |
| 150 | << error.category().message(error.value())); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 151 | |
| 152 | if (static_cast<bool>(onAcceptFailed)) |
| 153 | onAcceptFailed("Connect to remote endpoint failed: " + |
| 154 | error.category().message(error.value())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 158 | // prepare accepting the next connection |
| 159 | shared_ptr<ip::tcp::socket> clientSocket = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 160 | make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 161 | m_acceptor->async_accept(*clientSocket, |
| 162 | bind(&TcpChannel::handleSuccessfulAccept, this, _1, |
| 163 | clientSocket, |
| 164 | onFaceCreated, onAcceptFailed)); |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 166 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] " |
| 167 | "<< Connection from " << socket->remote_endpoint()); |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame^] | 168 | createFace(socket, onFaceCreated, true); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void |
| 172 | TcpChannel::handleSuccessfulConnect(const boost::system::error_code& error, |
| 173 | const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 174 | const shared_ptr<ndn::monotonic_deadline_timer>& timer, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 175 | const FaceCreatedCallback& onFaceCreated, |
| 176 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 177 | { |
| 178 | timer->cancel(); |
| 179 | |
| 180 | if (error) { |
| 181 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 182 | return; |
| 183 | |
| 184 | socket->close(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 185 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 186 | NFD_LOG_DEBUG("Connect to remote endpoint failed: " |
| 187 | << error.category().message(error.value())); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 188 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 189 | onConnectFailed("Connect to remote endpoint failed: " + |
| 190 | error.category().message(error.value())); |
| 191 | return; |
| 192 | } |
| 193 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 194 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] " |
| 195 | ">> Connection to " << socket->remote_endpoint()); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 196 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame^] | 197 | createFace(socket, onFaceCreated, false); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void |
| 201 | TcpChannel::handleFailedConnect(const boost::system::error_code& error, |
| 202 | const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 203 | const shared_ptr<ndn::monotonic_deadline_timer>& timer, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 204 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 205 | { |
| 206 | if (error) { // e.g., cancelled |
| 207 | return; |
| 208 | } |
| 209 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 210 | NFD_LOG_DEBUG("Connect to remote endpoint timed out: " |
| 211 | << error.category().message(error.value())); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 212 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 213 | onConnectFailed("Connect to remote endpoint timed out: " + |
| 214 | error.category().message(error.value())); |
| 215 | socket->close(); // abort the connection |
| 216 | } |
| 217 | |
| 218 | void |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 219 | TcpChannel::handleEndpointResolution(const boost::system::error_code& error, |
| 220 | ip::tcp::resolver::iterator remoteEndpoint, |
| 221 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 222 | const shared_ptr<ndn::monotonic_deadline_timer>& timer, |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 223 | const FaceCreatedCallback& onFaceCreated, |
| 224 | const ConnectFailedCallback& onConnectFailed, |
| 225 | const shared_ptr<ip::tcp::resolver>& resolver) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 226 | { |
| 227 | if (error || |
| 228 | remoteEndpoint == ip::tcp::resolver::iterator()) |
| 229 | { |
| 230 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 231 | return; |
| 232 | |
| 233 | socket->close(); |
| 234 | timer->cancel(); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 235 | |
| 236 | NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: " |
| 237 | << error.category().message(error.value())); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 238 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 239 | onConnectFailed("Remote endpoint hostname or port cannot be resolved: " + |
| 240 | error.category().message(error.value())); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | // got endpoint, now trying to connect (only try the first resolution option) |
| 245 | socket->async_connect(*remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 246 | bind(&TcpChannel::handleSuccessfulConnect, this, _1, |
| 247 | socket, timer, |
| 248 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 251 | } // namespace nfd |