blob: 6ecaeaf9116c240c304224667b7991036f57a85f [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry2642cd22017-07-13 21:34:53 -04002/*
Yanbiao Li58ba3f92017-02-15 14:27:18 +00003 * Copyright (c) 2014-2017, 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 {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040032namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010033
34NFD_LOG_INIT("UdpChannel");
35
Davide Pesavento8fd15e62017-04-06 19:58:54 -040036namespace ip = boost::asio::ip;
Giulio Grassi624f6c62014-02-18 19:42:14 +010037
38UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040039 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010040 : m_localEndpoint(localEndpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010041 , m_socket(getGlobalIoService())
Davide Pesavento43ff2a92017-05-18 19:50:57 -040042 , m_idleFaceTimeout(idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010043{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010044 setUri(FaceUri(m_localEndpoint));
Davide Pesavento77911cc2017-04-08 22:12:30 -040045 NFD_LOG_CHAN_INFO("Creating channel");
Giulio Grassi624f6c62014-02-18 19:42:14 +010046}
47
Giulio Grassi624f6c62014-02-18 19:42:14 +010048void
49UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
Yukai Tu375dcb02015-08-01 13:04:43 +080050 ndn::nfd::FacePersistency persistency,
Eric Newberry2642cd22017-07-13 21:34:53 -040051 bool wantLpReliability,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060052 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010053 const FaceCreationFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010054{
Junxiao Shic5401492015-12-23 16:33:21 -070055 shared_ptr<Face> face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010056 try {
Eric Newberry2642cd22017-07-13 21:34:53 -040057 face = createFace(remoteEndpoint, persistency, wantLpReliability).second;
Giulio Grassi624f6c62014-02-18 19:42:14 +010058 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010059 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040060 NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
Davide Pesavento292e5e12015-03-13 02:08:33 +010061 if (onConnectFailed)
Davide Pesavento77911cc2017-04-08 22:12:30 -040062 onConnectFailed(504, std::string("Face creation failed: ") + e.what());
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060063 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010064 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010065
Davide Pesavento292e5e12015-03-13 02:08:33 +010066 // Need to invoke the callback regardless of whether or not we had already
67 // created the face so that control responses and such can be sent
68 onFaceCreated(face);
Giulio Grassi624f6c62014-02-18 19:42:14 +010069}
70
Junxiao Shic5401492015-12-23 16:33:21 -070071void
72UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento77911cc2017-04-08 22:12:30 -040073 const FaceCreationFailedCallback& onFaceCreationFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010074{
Junxiao Shic5401492015-12-23 16:33:21 -070075 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040076 NFD_LOG_CHAN_WARN("Already listening");
Junxiao Shic5401492015-12-23 16:33:21 -070077 return;
78 }
79
80 m_socket.open(m_localEndpoint.protocol());
81 m_socket.set_option(ip::udp::socket::reuse_address(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040082 if (m_localEndpoint.address().is_v6()) {
Junxiao Shic5401492015-12-23 16:33:21 -070083 m_socket.set_option(ip::v6_only(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040084 }
Junxiao Shic5401492015-12-23 16:33:21 -070085 m_socket.bind(m_localEndpoint);
Davide Pesavento77911cc2017-04-08 22:12:30 -040086
87 waitForNewPeer(onFaceCreated, onFaceCreationFailed);
88 NFD_LOG_CHAN_DEBUG("Started listening");
Junxiao Shic5401492015-12-23 16:33:21 -070089}
90
91void
92UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
93 const FaceCreationFailedCallback& onReceiveFailed)
94{
Davide Pesaventoacca18a2017-04-09 13:23:16 -040095 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
Junxiao Shic5401492015-12-23 16:33:21 -070096 bind(&UdpChannel::handleNewPeer, this,
97 boost::asio::placeholders::error,
98 boost::asio::placeholders::bytes_transferred,
99 onFaceCreated, onReceiveFailed));
100}
101
102void
103UdpChannel::handleNewPeer(const boost::system::error_code& error,
104 size_t nBytesReceived,
105 const FaceCreatedCallback& onFaceCreated,
106 const FaceCreationFailedCallback& onReceiveFailed)
107{
108 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400109 if (error != boost::asio::error::operation_aborted) {
110 NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
111 if (onReceiveFailed)
112 onReceiveFailed(500, "Receive failed: " + error.message());
113 }
Junxiao Shic5401492015-12-23 16:33:21 -0700114 return;
115 }
116
Davide Pesavento77911cc2017-04-08 22:12:30 -0400117 NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint);
Junxiao Shic5401492015-12-23 16:33:21 -0700118
119 bool isCreated = false;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700120 shared_ptr<Face> face;
Junxiao Shic5401492015-12-23 16:33:21 -0700121 try {
Eric Newberry2642cd22017-07-13 21:34:53 -0400122 std::tie(isCreated, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
123 false);
Junxiao Shic5401492015-12-23 16:33:21 -0700124 }
125 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400126 NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700127 if (onReceiveFailed)
Davide Pesavento77911cc2017-04-08 22:12:30 -0400128 onReceiveFailed(504, std::string("Face creation failed: ") + e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700129 return;
130 }
131
132 if (isCreated)
133 onFaceCreated(face);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400134 else
135 NFD_LOG_CHAN_DEBUG("Received datagram for existing face");
Junxiao Shic5401492015-12-23 16:33:21 -0700136
137 // dispatch the datagram to the face for processing
Davide Pesavento77911cc2017-04-08 22:12:30 -0400138 auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
Davide Pesaventoacca18a2017-04-09 13:23:16 -0400139 transport->receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
Junxiao Shic5401492015-12-23 16:33:21 -0700140
Davide Pesavento77911cc2017-04-08 22:12:30 -0400141 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>>
Davide Pesavento77911cc2017-04-08 22:12:30 -0400145UdpChannel::createFace(const udp::Endpoint& remoteEndpoint,
Eric Newberry2642cd22017-07-13 21:34:53 -0400146 ndn::nfd::FacePersistency persistency,
147 bool wantLpReliability)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100148{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100149 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100150 if (it != m_channelFaces.end()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700151 // we already have a face for this endpoint, so reuse it
Davide Pesavento77911cc2017-04-08 22:12:30 -0400152 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000153 return {false, it->second};
Davide Pesavento292e5e12015-03-13 02:08:33 +0100154 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700155
Davide Pesavento292e5e12015-03-13 02:08:33 +0100156 // else, create a new face
157 ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
158 socket.set_option(ip::udp::socket::reuse_address(true));
159 socket.bind(m_localEndpoint);
160 socket.connect(remoteEndpoint);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600161
Eric Newberry2642cd22017-07-13 21:34:53 -0400162 GenericLinkService::Options options;
163 options.reliabilityOptions.isEnabled = wantLpReliability;
164 auto linkService = make_unique<GenericLinkService>(options);
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400165 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700166 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600167
Yukai Tu0a49d342015-09-13 12:54:22 +0800168 m_channelFaces[remoteEndpoint] = face;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400169 connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
Junxiao Shic5401492015-12-23 16:33:21 -0700170
Davide Pesavento292e5e12015-03-13 02:08:33 +0100171 return {true, face};
Giulio Grassi624f6c62014-02-18 19:42:14 +0100172}
173
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400174} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100175} // namespace nfd