blob: 3ab9e1f3f23766af3f6e7b3f5c6586edac94e66a [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060051 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010052 const FaceCreationFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010053{
Junxiao Shic5401492015-12-23 16:33:21 -070054 shared_ptr<Face> face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010055 try {
Yukai Tu375dcb02015-08-01 13:04:43 +080056 face = createFace(remoteEndpoint, persistency).second;
Giulio Grassi624f6c62014-02-18 19:42:14 +010057 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010058 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040059 NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
Davide Pesavento292e5e12015-03-13 02:08:33 +010060 if (onConnectFailed)
Davide Pesavento77911cc2017-04-08 22:12:30 -040061 onConnectFailed(504, std::string("Face creation failed: ") + e.what());
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060062 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010063 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010064
Davide Pesavento292e5e12015-03-13 02:08:33 +010065 // Need to invoke the callback regardless of whether or not we had already
66 // created the face so that control responses and such can be sent
67 onFaceCreated(face);
Giulio Grassi624f6c62014-02-18 19:42:14 +010068}
69
Junxiao Shic5401492015-12-23 16:33:21 -070070void
71UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento77911cc2017-04-08 22:12:30 -040072 const FaceCreationFailedCallback& onFaceCreationFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010073{
Junxiao Shic5401492015-12-23 16:33:21 -070074 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040075 NFD_LOG_CHAN_WARN("Already listening");
Junxiao Shic5401492015-12-23 16:33:21 -070076 return;
77 }
78
79 m_socket.open(m_localEndpoint.protocol());
80 m_socket.set_option(ip::udp::socket::reuse_address(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040081 if (m_localEndpoint.address().is_v6()) {
Junxiao Shic5401492015-12-23 16:33:21 -070082 m_socket.set_option(ip::v6_only(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040083 }
Junxiao Shic5401492015-12-23 16:33:21 -070084 m_socket.bind(m_localEndpoint);
Davide Pesavento77911cc2017-04-08 22:12:30 -040085
86 waitForNewPeer(onFaceCreated, onFaceCreationFailed);
87 NFD_LOG_CHAN_DEBUG("Started listening");
Junxiao Shic5401492015-12-23 16:33:21 -070088}
89
90void
91UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
92 const FaceCreationFailedCallback& onReceiveFailed)
93{
Davide Pesaventoacca18a2017-04-09 13:23:16 -040094 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
Junxiao Shic5401492015-12-23 16:33:21 -070095 bind(&UdpChannel::handleNewPeer, this,
96 boost::asio::placeholders::error,
97 boost::asio::placeholders::bytes_transferred,
98 onFaceCreated, onReceiveFailed));
99}
100
101void
102UdpChannel::handleNewPeer(const boost::system::error_code& error,
103 size_t nBytesReceived,
104 const FaceCreatedCallback& onFaceCreated,
105 const FaceCreationFailedCallback& onReceiveFailed)
106{
107 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400108 if (error != boost::asio::error::operation_aborted) {
109 NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
110 if (onReceiveFailed)
111 onReceiveFailed(500, "Receive failed: " + error.message());
112 }
Junxiao Shic5401492015-12-23 16:33:21 -0700113 return;
114 }
115
Davide Pesavento77911cc2017-04-08 22:12:30 -0400116 NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint);
Junxiao Shic5401492015-12-23 16:33:21 -0700117
118 bool isCreated = false;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700119 shared_ptr<Face> face;
Junxiao Shic5401492015-12-23 16:33:21 -0700120 try {
121 std::tie(isCreated, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
122 }
123 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400124 NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700125 if (onReceiveFailed)
Davide Pesavento77911cc2017-04-08 22:12:30 -0400126 onReceiveFailed(504, std::string("Face creation failed: ") + e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700127 return;
128 }
129
130 if (isCreated)
131 onFaceCreated(face);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400132 else
133 NFD_LOG_CHAN_DEBUG("Received datagram for existing face");
Junxiao Shic5401492015-12-23 16:33:21 -0700134
135 // dispatch the datagram to the face for processing
Davide Pesavento77911cc2017-04-08 22:12:30 -0400136 auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
Davide Pesaventoacca18a2017-04-09 13:23:16 -0400137 transport->receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
Junxiao Shic5401492015-12-23 16:33:21 -0700138
Davide Pesavento77911cc2017-04-08 22:12:30 -0400139 waitForNewPeer(onFaceCreated, onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100140}
141
Junxiao Shicde37ad2015-12-24 01:02:05 -0700142std::pair<bool, shared_ptr<Face>>
Davide Pesavento77911cc2017-04-08 22:12:30 -0400143UdpChannel::createFace(const udp::Endpoint& remoteEndpoint,
144 ndn::nfd::FacePersistency persistency)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100145{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100146 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100147 if (it != m_channelFaces.end()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700148 // we already have a face for this endpoint, so reuse it
Davide Pesavento77911cc2017-04-08 22:12:30 -0400149 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000150 return {false, it->second};
Davide Pesavento292e5e12015-03-13 02:08:33 +0100151 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700152
Davide Pesavento292e5e12015-03-13 02:08:33 +0100153 // else, create a new face
154 ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
155 socket.set_option(ip::udp::socket::reuse_address(true));
156 socket.bind(m_localEndpoint);
157 socket.connect(remoteEndpoint);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600158
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400159 auto linkService = make_unique<GenericLinkService>();
160 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700161 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600162
Yukai Tu0a49d342015-09-13 12:54:22 +0800163 m_channelFaces[remoteEndpoint] = face;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400164 connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
Junxiao Shic5401492015-12-23 16:33:21 -0700165
Davide Pesavento292e5e12015-03-13 02:08:33 +0100166 return {true, face};
Giulio Grassi624f6c62014-02-18 19:42:14 +0100167}
168
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400169} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100170} // namespace nfd