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