blob: d0589449e5bbededdb2db68a8128accc08800ebc [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080024
25#include "tcp-channel.hpp"
Junxiao Shi61e3cc52014-03-03 20:40:28 -070026#include "core/global-io.hpp"
27#include "core/face-uri.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080028
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080030
Alexander Afanasyev3958b012014-01-31 15:06:13 -080031NFD_LOG_INIT("TcpChannel");
32
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080033using namespace boost::asio;
34
Junxiao Shi61e3cc52014-03-03 20:40:28 -070035TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint)
36 : m_localEndpoint(localEndpoint)
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -070037 , m_isListening(false)
Junxiao Shi61e3cc52014-03-03 20:40:28 -070038{
39 this->setUri(FaceUri(localEndpoint));
40}
41
42TcpChannel::~TcpChannel()
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080043{
44}
45
46void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080047TcpChannel::listen(const FaceCreatedCallback& onFaceCreated,
48 const ConnectFailedCallback& onAcceptFailed,
49 int backlog/* = tcp::acceptor::max_connections*/)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080050{
Alexander Afanasyevf6980282014-05-13 18:28:40 -070051 m_acceptor = make_shared<ip::tcp::acceptor>(ref(getGlobalIoService()));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080052 m_acceptor->open(m_localEndpoint.protocol());
53 m_acceptor->set_option(ip::tcp::acceptor::reuse_address(true));
Alexander Afanasyeveee71fd2014-03-13 17:40:33 -070054 if (m_localEndpoint.address().is_v6())
55 {
56 m_acceptor->set_option(ip::v6_only(true));
57 }
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080058 m_acceptor->bind(m_localEndpoint);
59 m_acceptor->listen(backlog);
60
61 shared_ptr<ip::tcp::socket> clientSocket =
Alexander Afanasyevf6980282014-05-13 18:28:40 -070062 make_shared<ip::tcp::socket>(ref(getGlobalIoService()));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080063 m_acceptor->async_accept(*clientSocket,
Alexander Afanasyeva16abd22014-01-31 18:12:29 -080064 bind(&TcpChannel::handleSuccessfulAccept, this, _1,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080065 clientSocket,
Alexander Afanasyeva16abd22014-01-31 18:12:29 -080066 onFaceCreated, onAcceptFailed));
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -070067
68 m_isListening = true;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080069}
70
71void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080072TcpChannel::connect(const tcp::Endpoint& remoteEndpoint,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080073 const TcpChannel::FaceCreatedCallback& onFaceCreated,
74 const TcpChannel::ConnectFailedCallback& onConnectFailed,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070075 const time::seconds& timeout/* = time::seconds(4)*/)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080076{
Alexander Afanasyev334b7142014-01-28 12:59:39 -080077 ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint);
78 if (i != m_channelFaces.end()) {
79 onFaceCreated(i->second);
80 return;
81 }
82
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080083 shared_ptr<ip::tcp::socket> clientSocket =
Alexander Afanasyevf6980282014-05-13 18:28:40 -070084 make_shared<ip::tcp::socket>(ref(getGlobalIoService()));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080085
Alexander Afanasyev18861802014-06-13 17:49:03 -070086 EventId connectTimeoutEvent = scheduler::schedule(timeout,
87 bind(&TcpChannel::handleFailedConnect, this,
88 clientSocket, onConnectFailed));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080089
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080090 clientSocket->async_connect(remoteEndpoint,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080091 bind(&TcpChannel::handleSuccessfulConnect, this, _1,
Alexander Afanasyev18861802014-06-13 17:49:03 -070092 clientSocket, connectTimeoutEvent,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080093 onFaceCreated, onConnectFailed));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080094}
95
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080096void
97TcpChannel::connect(const std::string& remoteHost, const std::string& remotePort,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080098 const TcpChannel::FaceCreatedCallback& onFaceCreated,
99 const TcpChannel::ConnectFailedCallback& onConnectFailed,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700100 const time::seconds& timeout/* = time::seconds(4)*/)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800101{
102 shared_ptr<ip::tcp::socket> clientSocket =
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700103 make_shared<ip::tcp::socket>(ref(getGlobalIoService()));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800104
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800105 ip::tcp::resolver::query query(remoteHost, remotePort);
106 shared_ptr<ip::tcp::resolver> resolver =
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700107 make_shared<ip::tcp::resolver>(ref(getGlobalIoService()));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800108
Alexander Afanasyev18861802014-06-13 17:49:03 -0700109 EventId connectTimeoutEvent = scheduler::schedule(timeout,
110 bind(&TcpChannel::handleFailedConnect, this,
111 clientSocket, onConnectFailed));
112
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800113 resolver->async_resolve(query,
Davide Pesavento855bf292014-02-27 03:58:01 +0100114 bind(&TcpChannel::handleEndpointResolution, this, _1, _2,
Alexander Afanasyev18861802014-06-13 17:49:03 -0700115 clientSocket, connectTimeoutEvent,
Alexander Afanasyev3958b012014-01-31 15:06:13 -0800116 onFaceCreated, onConnectFailed,
117 resolver));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800118}
119
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800120size_t
121TcpChannel::size() const
122{
123 return m_channelFaces.size();
124}
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800125
126void
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800127TcpChannel::createFace(const shared_ptr<ip::tcp::socket>& socket,
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700128 const FaceCreatedCallback& onFaceCreated,
129 bool isOnDemand)
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800130{
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800131 tcp::Endpoint remoteEndpoint = socket->remote_endpoint();
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800132
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800133 shared_ptr<Face> face;
Alexander Afanasyevbfd31de2014-02-26 13:20:08 -0800134 if (socket->local_endpoint().address().is_loopback())
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700135 face = make_shared<TcpLocalFace>(socket, isOnDemand);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800136 else
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700137 face = make_shared<TcpFace>(socket, isOnDemand);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700138
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800139 face->onFail += bind(&TcpChannel::afterFaceFailed, this, remoteEndpoint);
140
141 onFaceCreated(face);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800142 m_channelFaces[remoteEndpoint] = face;
143}
144
145void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800146TcpChannel::afterFaceFailed(tcp::Endpoint &remoteEndpoint)
147{
148 NFD_LOG_DEBUG("afterFaceFailed: " << remoteEndpoint);
149 m_channelFaces.erase(remoteEndpoint);
150}
151
152void
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800153TcpChannel::handleSuccessfulAccept(const boost::system::error_code& error,
154 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
155 const FaceCreatedCallback& onFaceCreated,
156 const ConnectFailedCallback& onAcceptFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800157{
158 if (error) {
159 if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
160 return;
161
Alexander Afanasyev3958b012014-01-31 15:06:13 -0800162 NFD_LOG_DEBUG("Connect to remote endpoint failed: "
163 << error.category().message(error.value()));
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800164
165 if (static_cast<bool>(onAcceptFailed))
166 onAcceptFailed("Connect to remote endpoint failed: " +
167 error.category().message(error.value()));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800168 return;
169 }
170
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800171 // prepare accepting the next connection
172 shared_ptr<ip::tcp::socket> clientSocket =
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700173 make_shared<ip::tcp::socket>(ref(getGlobalIoService()));
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800174 m_acceptor->async_accept(*clientSocket,
175 bind(&TcpChannel::handleSuccessfulAccept, this, _1,
176 clientSocket,
177 onFaceCreated, onAcceptFailed));
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800178
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800179 NFD_LOG_DEBUG("[" << m_localEndpoint << "] "
180 "<< Connection from " << socket->remote_endpoint());
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700181 createFace(socket, onFaceCreated, true);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800182}
183
184void
185TcpChannel::handleSuccessfulConnect(const boost::system::error_code& error,
186 const shared_ptr<ip::tcp::socket>& socket,
Alexander Afanasyev18861802014-06-13 17:49:03 -0700187 const EventId& connectTimeoutEvent,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -0800188 const FaceCreatedCallback& onFaceCreated,
189 const ConnectFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800190{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700191 scheduler::cancel(connectTimeoutEvent);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800192
193 if (error) {
194 if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
195 return;
196
197 socket->close();
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700198
Alexander Afanasyev3958b012014-01-31 15:06:13 -0800199 NFD_LOG_DEBUG("Connect to remote endpoint failed: "
200 << error.category().message(error.value()));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700201
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800202 onConnectFailed("Connect to remote endpoint failed: " +
203 error.category().message(error.value()));
204 return;
205 }
206
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800207 NFD_LOG_DEBUG("[" << m_localEndpoint << "] "
208 ">> Connection to " << socket->remote_endpoint());
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700209
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700210 createFace(socket, onFaceCreated, false);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800211}
212
213void
Alexander Afanasyev18861802014-06-13 17:49:03 -0700214TcpChannel::handleFailedConnect(const shared_ptr<ip::tcp::socket>& socket,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -0800215 const ConnectFailedCallback& onConnectFailed)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800216{
Alexander Afanasyev18861802014-06-13 17:49:03 -0700217 NFD_LOG_DEBUG("Connect to remote endpoint timed out");
218 onConnectFailed("Connect to remote endpoint timed out");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800219 socket->close(); // abort the connection
220}
221
222void
Davide Pesavento855bf292014-02-27 03:58:01 +0100223TcpChannel::handleEndpointResolution(const boost::system::error_code& error,
224 ip::tcp::resolver::iterator remoteEndpoint,
225 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Alexander Afanasyev18861802014-06-13 17:49:03 -0700226 const EventId& connectTimeoutEvent,
Davide Pesavento855bf292014-02-27 03:58:01 +0100227 const FaceCreatedCallback& onFaceCreated,
228 const ConnectFailedCallback& onConnectFailed,
229 const shared_ptr<ip::tcp::resolver>& resolver)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800230{
231 if (error ||
232 remoteEndpoint == ip::tcp::resolver::iterator())
233 {
234 if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
235 return;
236
237 socket->close();
Alexander Afanasyev18861802014-06-13 17:49:03 -0700238 scheduler::cancel(connectTimeoutEvent);
Alexander Afanasyev3958b012014-01-31 15:06:13 -0800239
240 NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: "
241 << error.category().message(error.value()));
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700242
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800243 onConnectFailed("Remote endpoint hostname or port cannot be resolved: " +
244 error.category().message(error.value()));
245 return;
246 }
247
248 // got endpoint, now trying to connect (only try the first resolution option)
249 socket->async_connect(*remoteEndpoint,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -0800250 bind(&TcpChannel::handleSuccessfulConnect, this, _1,
Alexander Afanasyev18861802014-06-13 17:49:03 -0700251 socket, connectTimeoutEvent,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -0800252 onFaceCreated, onConnectFailed));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800253}
254
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800255} // namespace nfd