blob: 491ad82ace6cbbaad1678d221b98109babb9a262 [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,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070039 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010040 : m_localEndpoint(localEndpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010041 , m_socket(getGlobalIoService())
Giulio Grassi69871f02014-03-09 16:14:44 +010042 , m_idleFaceTimeout(timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010043{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010044 setUri(FaceUri(m_localEndpoint));
Giulio Grassi624f6c62014-02-18 19:42:14 +010045}
46
Junxiao Shic5401492015-12-23 16:33:21 -070047size_t
48UdpChannel::size() const
Giulio Grassi624f6c62014-02-18 19:42:14 +010049{
Junxiao Shic5401492015-12-23 16:33:21 -070050 return m_channelFaces.size();
Giulio Grassi624f6c62014-02-18 19:42:14 +010051}
52
Giulio Grassi624f6c62014-02-18 19:42:14 +010053void
54UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
Yukai Tu375dcb02015-08-01 13:04:43 +080055 ndn::nfd::FacePersistency persistency,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060056 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010057 const FaceCreationFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010058{
Junxiao Shic5401492015-12-23 16:33:21 -070059 shared_ptr<Face> face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010060 try {
Yukai Tu375dcb02015-08-01 13:04:43 +080061 face = createFace(remoteEndpoint, persistency).second;
Giulio Grassi624f6c62014-02-18 19:42:14 +010062 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010063 catch (const boost::system::system_error& e) {
Davide Pesavento292e5e12015-03-13 02:08:33 +010064 NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << e.what());
65 if (onConnectFailed)
Eric Newberry42602412016-08-27 09:33:18 -070066 onConnectFailed(504, std::string("Connect failed: ") + e.what());
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060067 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010068 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010069
Davide Pesavento292e5e12015-03-13 02:08:33 +010070 // Need to invoke the callback regardless of whether or not we had already
71 // created the face so that control responses and such can be sent
72 onFaceCreated(face);
Giulio Grassi624f6c62014-02-18 19:42:14 +010073}
74
Junxiao Shic5401492015-12-23 16:33:21 -070075void
76UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
77 const FaceCreationFailedCallback& onReceiveFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010078{
Junxiao Shic5401492015-12-23 16:33:21 -070079 if (isListening()) {
80 NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
81 return;
82 }
83
84 m_socket.open(m_localEndpoint.protocol());
85 m_socket.set_option(ip::udp::socket::reuse_address(true));
86 if (m_localEndpoint.address().is_v6())
87 m_socket.set_option(ip::v6_only(true));
88
89 m_socket.bind(m_localEndpoint);
90 this->waitForNewPeer(onFaceCreated, onReceiveFailed);
91}
92
93void
94UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
95 const FaceCreationFailedCallback& onReceiveFailed)
96{
97 m_socket.async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE),
98 m_remoteEndpoint,
99 bind(&UdpChannel::handleNewPeer, this,
100 boost::asio::placeholders::error,
101 boost::asio::placeholders::bytes_transferred,
102 onFaceCreated, onReceiveFailed));
103}
104
105void
106UdpChannel::handleNewPeer(const boost::system::error_code& error,
107 size_t nBytesReceived,
108 const FaceCreatedCallback& onFaceCreated,
109 const FaceCreationFailedCallback& onReceiveFailed)
110{
111 if (error) {
112 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
113 return;
114
115 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Receive failed: " << error.message());
116 if (onReceiveFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700117 onReceiveFailed(500, "Receive failed: " + error.message());
Junxiao Shic5401492015-12-23 16:33:21 -0700118 return;
119 }
120
121 NFD_LOG_DEBUG("[" << m_localEndpoint << "] New peer " << m_remoteEndpoint);
122
123 bool isCreated = false;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700124 shared_ptr<Face> face;
Junxiao Shic5401492015-12-23 16:33:21 -0700125 try {
126 std::tie(isCreated, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
127 }
128 catch (const boost::system::system_error& e) {
129 NFD_LOG_WARN("[" << m_localEndpoint << "] Failed to create face for peer "
130 << m_remoteEndpoint << ": " << e.what());
131 if (onReceiveFailed)
Eric Newberry42602412016-08-27 09:33:18 -0700132 onReceiveFailed(504, "Failed to create face for peer");
Junxiao Shic5401492015-12-23 16:33:21 -0700133 return;
134 }
135
136 if (isCreated)
137 onFaceCreated(face);
138
139 // dispatch the datagram to the face for processing
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400140 static_cast<UnicastUdpTransport*>(face->getTransport())->receiveDatagram(m_inputBuffer, nBytesReceived, error);
Junxiao Shic5401492015-12-23 16:33:21 -0700141
142 this->waitForNewPeer(onFaceCreated, onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100143}
144
Junxiao Shicde37ad2015-12-24 01:02:05 -0700145std::pair<bool, shared_ptr<Face>>
Yukai Tu375dcb02015-08-01 13:04:43 +0800146UdpChannel::createFace(const udp::Endpoint& remoteEndpoint, ndn::nfd::FacePersistency persistency)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100147{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100148 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100149 if (it != m_channelFaces.end()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700150 // we already have a face for this endpoint, so reuse it
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000151 return {false, it->second};
Davide Pesavento292e5e12015-03-13 02:08:33 +0100152 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700153
Davide Pesavento292e5e12015-03-13 02:08:33 +0100154 // else, create a new face
155 ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
156 socket.set_option(ip::udp::socket::reuse_address(true));
157 socket.bind(m_localEndpoint);
158 socket.connect(remoteEndpoint);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600159
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400160 auto linkService = make_unique<GenericLinkService>();
161 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700162 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600163
Yukai Tu0a49d342015-09-13 12:54:22 +0800164 m_channelFaces[remoteEndpoint] = face;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700165 connectFaceClosedSignal(*face,
166 [this, remoteEndpoint] {
Junxiao Shic5401492015-12-23 16:33:21 -0700167 NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
168 m_channelFaces.erase(remoteEndpoint);
169 });
170
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