blob: d3791e4b1b6bfa72c98ea0e5c6294c06dd366481 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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 * 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
46void
47UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010048 const FaceCreationFailedCallback& onReceiveFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010049{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010050 if (isListening()) {
51 NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
52 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010053 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060054
Davide Pesavento292e5e12015-03-13 02:08:33 +010055 m_socket.open(m_localEndpoint.protocol());
56 m_socket.set_option(ip::udp::socket::reuse_address(true));
57 if (m_localEndpoint.address().is_v6())
58 m_socket.set_option(ip::v6_only(true));
59
60 m_socket.bind(m_localEndpoint);
61 m_socket.async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE),
62 m_remoteEndpoint,
63 bind(&UdpChannel::handleNewPeer, this,
64 boost::asio::placeholders::error,
65 boost::asio::placeholders::bytes_transferred,
66 onFaceCreated, onReceiveFailed));
Giulio Grassi624f6c62014-02-18 19:42:14 +010067}
68
Giulio Grassi624f6c62014-02-18 19:42:14 +010069void
70UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
Yukai Tu375dcb02015-08-01 13:04:43 +080071 ndn::nfd::FacePersistency persistency,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060072 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010073 const FaceCreationFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010074{
Yukai Tu0a49d342015-09-13 12:54:22 +080075 shared_ptr<face::LpFaceWrapper> face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010076 try {
Yukai Tu375dcb02015-08-01 13:04:43 +080077 face = createFace(remoteEndpoint, persistency).second;
Giulio Grassi624f6c62014-02-18 19:42:14 +010078 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010079 catch (const boost::system::system_error& e) {
Davide Pesavento292e5e12015-03-13 02:08:33 +010080 NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << e.what());
81 if (onConnectFailed)
82 onConnectFailed(e.what());
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060083 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010084 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010085
Davide Pesavento292e5e12015-03-13 02:08:33 +010086 // Need to invoke the callback regardless of whether or not we had already
87 // created the face so that control responses and such can be sent
88 onFaceCreated(face);
Giulio Grassi624f6c62014-02-18 19:42:14 +010089}
90
Giulio Grassi624f6c62014-02-18 19:42:14 +010091size_t
92UdpChannel::size() const
93{
94 return m_channelFaces.size();
95}
96
Yukai Tu0a49d342015-09-13 12:54:22 +080097std::pair<bool, shared_ptr<face::LpFaceWrapper>>
Yukai Tu375dcb02015-08-01 13:04:43 +080098UdpChannel::createFace(const udp::Endpoint& remoteEndpoint, ndn::nfd::FacePersistency persistency)
Giulio Grassi624f6c62014-02-18 19:42:14 +010099{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100100 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100101 if (it != m_channelFaces.end()) {
102 // we already have a face for this endpoint, just reuse it
Yukai Tu375dcb02015-08-01 13:04:43 +0800103 auto face = it->second;
104 // only on-demand -> persistent -> permanent transition is allowed
105 bool isTransitionAllowed = persistency != face->getPersistency() &&
106 (face->getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND ||
107 persistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT);
108 if (isTransitionAllowed) {
109 face->setPersistency(persistency);
Yukai Tu731f0d72015-07-04 11:14:44 +0800110 }
Yukai Tu375dcb02015-08-01 13:04:43 +0800111 return {false, face};
Davide Pesavento292e5e12015-03-13 02:08:33 +0100112 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700113
Davide Pesavento292e5e12015-03-13 02:08:33 +0100114 // else, create a new face
115 ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
116 socket.set_option(ip::udp::socket::reuse_address(true));
117 socket.bind(m_localEndpoint);
118 socket.connect(remoteEndpoint);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600119
Yukai Tu0a49d342015-09-13 12:54:22 +0800120 auto linkService = make_unique<face::GenericLinkService>();
121 auto transport = make_unique<face::UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout);
122 auto lpFace = make_unique<face::LpFace>(std::move(linkService), std::move(transport));
123 auto face = make_shared<face::LpFaceWrapper>(std::move(lpFace));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600124
Yukai Tu0a49d342015-09-13 12:54:22 +0800125 face->setPersistency(persistency);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100126 face->onFail.connectSingleShot([this, remoteEndpoint] (const std::string&) {
127 NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
128 m_channelFaces.erase(remoteEndpoint);
129 });
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600130
Yukai Tu0a49d342015-09-13 12:54:22 +0800131 m_channelFaces[remoteEndpoint] = face;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100132 return {true, face};
Giulio Grassi624f6c62014-02-18 19:42:14 +0100133}
134
135void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100136UdpChannel::handleNewPeer(const boost::system::error_code& error,
137 size_t nBytesReceived,
138 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100139 const FaceCreationFailedCallback& onReceiveFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100140{
Alexander Afanasyev90c20fa2015-02-12 16:46:45 -0800141 if (error) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100142 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev90c20fa2015-02-12 16:46:45 -0800143 return;
144
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100145 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Receive failed: " << error.message());
146 if (onReceiveFailed)
147 onReceiveFailed(error.message());
Alexander Afanasyev90c20fa2015-02-12 16:46:45 -0800148 return;
149 }
150
Davide Pesavento292e5e12015-03-13 02:08:33 +0100151 NFD_LOG_DEBUG("[" << m_localEndpoint << "] New peer " << m_remoteEndpoint);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000152
Davide Pesavento292e5e12015-03-13 02:08:33 +0100153 bool created;
Yukai Tu0a49d342015-09-13 12:54:22 +0800154 shared_ptr<face::LpFaceWrapper> face;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100155 try {
Yukai Tu375dcb02015-08-01 13:04:43 +0800156 std::tie(created, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000157 }
Davide Pesavento292e5e12015-03-13 02:08:33 +0100158 catch (const boost::system::system_error& e) {
159 NFD_LOG_WARN("[" << m_localEndpoint << "] Failed to create face for peer "
160 << m_remoteEndpoint << ": " << e.what());
161 if (onReceiveFailed)
162 onReceiveFailed(e.what());
163 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100164 }
165
Davide Pesavento292e5e12015-03-13 02:08:33 +0100166 if (created)
167 onFaceCreated(face);
168
Davide Pesaventoa7530582014-06-30 20:45:52 +0200169 // dispatch the datagram to the face for processing
Yukai Tu0a49d342015-09-13 12:54:22 +0800170 static_cast<face::UnicastUdpTransport*>(face->getLpFace()->getTransport())->receiveDatagram(m_inputBuffer, nBytesReceived, error);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100171
Davide Pesavento292e5e12015-03-13 02:08:33 +0100172 m_socket.async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE),
173 m_remoteEndpoint,
174 bind(&UdpChannel::handleNewPeer, this,
175 boost::asio::placeholders::error,
176 boost::asio::placeholders::bytes_transferred,
177 onFaceCreated, onReceiveFailed));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100178}
179
Giulio Grassi624f6c62014-02-18 19:42:14 +0100180} // namespace nfd