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