blob: 64907be8f98cd20da60601c2f7c2e38291463fb8 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberry42602412016-08-27 09:33:18 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi5dd26c32014-07-20 23:15:14 -070024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
26#include "udp-channel.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080027#include "generic-link-service.hpp"
28#include "unicast-udp-transport.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010029#include "core/global-io.hpp"
30
31namespace nfd {
32
33NFD_LOG_INIT("UdpChannel");
34
35using namespace boost::asio;
36
37UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070038 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010039 : m_localEndpoint(localEndpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010040 , m_socket(getGlobalIoService())
Giulio Grassi69871f02014-03-09 16:14:44 +010041 , m_idleFaceTimeout(timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010042{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010043 setUri(FaceUri(m_localEndpoint));
Giulio Grassi624f6c62014-02-18 19:42:14 +010044}
45
Junxiao Shic5401492015-12-23 16:33:21 -070046size_t
47UdpChannel::size() const
Giulio Grassi624f6c62014-02-18 19:42:14 +010048{
Junxiao Shic5401492015-12-23 16:33:21 -070049 return m_channelFaces.size();
Giulio Grassi624f6c62014-02-18 19:42:14 +010050}
51
Giulio Grassi624f6c62014-02-18 19:42:14 +010052void
53UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
Yukai Tu375dcb02015-08-01 13:04:43 +080054 ndn::nfd::FacePersistency persistency,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060055 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010056 const FaceCreationFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010057{
Junxiao Shic5401492015-12-23 16:33:21 -070058 shared_ptr<Face> face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010059 try {
Yukai Tu375dcb02015-08-01 13:04:43 +080060 face = createFace(remoteEndpoint, persistency).second;
Giulio Grassi624f6c62014-02-18 19:42:14 +010061 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010062 catch (const boost::system::system_error& e) {
Davide Pesavento292e5e12015-03-13 02:08:33 +010063 NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << e.what());
64 if (onConnectFailed)
Eric Newberry42602412016-08-27 09:33:18 -070065 onConnectFailed(504, std::string("Connect failed: ") + e.what());
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060066 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010067 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010068
Davide Pesavento292e5e12015-03-13 02:08:33 +010069 // Need to invoke the callback regardless of whether or not we had already
70 // created the face so that control responses and such can be sent
71 onFaceCreated(face);
Giulio Grassi624f6c62014-02-18 19:42:14 +010072}
73
Junxiao Shic5401492015-12-23 16:33:21 -070074void
75UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
76 const FaceCreationFailedCallback& onReceiveFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010077{
Junxiao Shic5401492015-12-23 16:33:21 -070078 if (isListening()) {
79 NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
80 return;
81 }
82
83 m_socket.open(m_localEndpoint.protocol());
84 m_socket.set_option(ip::udp::socket::reuse_address(true));
85 if (m_localEndpoint.address().is_v6())
86 m_socket.set_option(ip::v6_only(true));
87
88 m_socket.bind(m_localEndpoint);
89 this->waitForNewPeer(onFaceCreated, onReceiveFailed);
90}
91
92void
93UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
94 const FaceCreationFailedCallback& onReceiveFailed)
95{
96 m_socket.async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE),
97 m_remoteEndpoint,
98 bind(&UdpChannel::handleNewPeer, this,
99 boost::asio::placeholders::error,
100 boost::asio::placeholders::bytes_transferred,
101 onFaceCreated, onReceiveFailed));
102}
103
104void
105UdpChannel::handleNewPeer(const boost::system::error_code& error,
106 size_t nBytesReceived,
107 const FaceCreatedCallback& onFaceCreated,
108 const FaceCreationFailedCallback& onReceiveFailed)
109{
110 if (error) {
111 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
112 return;
113
114 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Receive failed: " << error.message());
115 if (onReceiveFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700116 onReceiveFailed(500, "Receive failed: " + error.message());
Junxiao Shic5401492015-12-23 16:33:21 -0700117 return;
118 }
119
120 NFD_LOG_DEBUG("[" << m_localEndpoint << "] New peer " << m_remoteEndpoint);
121
122 bool isCreated = false;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700123 shared_ptr<Face> face;
Junxiao Shic5401492015-12-23 16:33:21 -0700124 try {
125 std::tie(isCreated, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
126 }
127 catch (const boost::system::system_error& e) {
128 NFD_LOG_WARN("[" << m_localEndpoint << "] Failed to create face for peer "
129 << m_remoteEndpoint << ": " << e.what());
130 if (onReceiveFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700131 onReceiveFailed(504, "Failed to create face for peer");
Junxiao Shic5401492015-12-23 16:33:21 -0700132 return;
133 }
134
135 if (isCreated)
136 onFaceCreated(face);
137
138 // dispatch the datagram to the face for processing
Junxiao Shicde37ad2015-12-24 01:02:05 -0700139 static_cast<face::UnicastUdpTransport*>(face->getTransport())->receiveDatagram(m_inputBuffer, nBytesReceived, error);
Junxiao Shic5401492015-12-23 16:33:21 -0700140
141 this->waitForNewPeer(onFaceCreated, onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100142}
143
Junxiao Shicde37ad2015-12-24 01:02:05 -0700144std::pair<bool, shared_ptr<Face>>
Yukai Tu375dcb02015-08-01 13:04:43 +0800145UdpChannel::createFace(const udp::Endpoint& remoteEndpoint, ndn::nfd::FacePersistency persistency)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100146{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100147 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100148 if (it != m_channelFaces.end()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700149 // we already have a face for this endpoint, so reuse it
Yukai Tu375dcb02015-08-01 13:04:43 +0800150 auto face = it->second;
Eric Newberryf40551a2016-09-05 15:41:16 -0700151
152 // TODO #3232: Remove persistency transitions from faces/create
Yukai Tu375dcb02015-08-01 13:04:43 +0800153 // only on-demand -> persistent -> permanent transition is allowed
154 bool isTransitionAllowed = persistency != face->getPersistency() &&
155 (face->getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND ||
156 persistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT);
157 if (isTransitionAllowed) {
158 face->setPersistency(persistency);
Yukai Tu731f0d72015-07-04 11:14:44 +0800159 }
Yukai Tu375dcb02015-08-01 13:04:43 +0800160 return {false, face};
Davide Pesavento292e5e12015-03-13 02:08:33 +0100161 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700162
Davide Pesavento292e5e12015-03-13 02:08:33 +0100163 // else, create a new face
164 ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
165 socket.set_option(ip::udp::socket::reuse_address(true));
166 socket.bind(m_localEndpoint);
167 socket.connect(remoteEndpoint);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600168
Yukai Tu0a49d342015-09-13 12:54:22 +0800169 auto linkService = make_unique<face::GenericLinkService>();
170 auto transport = make_unique<face::UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700171 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600172
Yukai Tu0a49d342015-09-13 12:54:22 +0800173 face->setPersistency(persistency);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600174
Yukai Tu0a49d342015-09-13 12:54:22 +0800175 m_channelFaces[remoteEndpoint] = face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700176 connectFaceClosedSignal(*face,
177 [this, remoteEndpoint] {
Junxiao Shic5401492015-12-23 16:33:21 -0700178 NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
179 m_channelFaces.erase(remoteEndpoint);
180 });
181
Davide Pesavento292e5e12015-03-13 02:08:33 +0100182 return {true, face};
Giulio Grassi624f6c62014-02-18 19:42:14 +0100183}
184
Giulio Grassi624f6c62014-02-18 19:42:14 +0100185} // namespace nfd