blob: 81cf4017c0bc46c4ae2a7b0e74d6918099350698 [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"
Davide Pesavento6ad890a2015-03-09 03:43:17 +010027#include "udp-face.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010028#include "core/global-io.hpp"
29
30namespace nfd {
31
32NFD_LOG_INIT("UdpChannel");
33
34using namespace boost::asio;
35
36UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070037 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010038 : m_localEndpoint(localEndpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010039 , m_socket(getGlobalIoService())
Giulio Grassi69871f02014-03-09 16:14:44 +010040 , m_idleFaceTimeout(timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010041{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010042 setUri(FaceUri(m_localEndpoint));
Giulio Grassi624f6c62014-02-18 19:42:14 +010043}
44
45void
46UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento6ad890a2015-03-09 03:43:17 +010047 const ConnectFailedCallback& onReceiveFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010048{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010049 if (isListening()) {
50 NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
51 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010052 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060053
Davide Pesavento292e5e12015-03-13 02:08:33 +010054 m_socket.open(m_localEndpoint.protocol());
55 m_socket.set_option(ip::udp::socket::reuse_address(true));
56 if (m_localEndpoint.address().is_v6())
57 m_socket.set_option(ip::v6_only(true));
58
59 m_socket.bind(m_localEndpoint);
60 m_socket.async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE),
61 m_remoteEndpoint,
62 bind(&UdpChannel::handleNewPeer, this,
63 boost::asio::placeholders::error,
64 boost::asio::placeholders::bytes_transferred,
65 onFaceCreated, onReceiveFailed));
Giulio Grassi624f6c62014-02-18 19:42:14 +010066}
67
Giulio Grassi624f6c62014-02-18 19:42:14 +010068void
69UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060070 const FaceCreatedCallback& onFaceCreated,
71 const ConnectFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010072{
Davide Pesavento292e5e12015-03-13 02:08:33 +010073 shared_ptr<UdpFace> face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010074 try {
Davide Pesavento292e5e12015-03-13 02:08:33 +010075 face = createFace(remoteEndpoint, false).second;
Giulio Grassi624f6c62014-02-18 19:42:14 +010076 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010077 catch (const boost::system::system_error& e) {
Davide Pesavento292e5e12015-03-13 02:08:33 +010078 NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << e.what());
79 if (onConnectFailed)
80 onConnectFailed(e.what());
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060081 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010082 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010083
Davide Pesavento292e5e12015-03-13 02:08:33 +010084 // Need to invoke the callback regardless of whether or not we had already
85 // created the face so that control responses and such can be sent
86 onFaceCreated(face);
Giulio Grassi624f6c62014-02-18 19:42:14 +010087}
88
Giulio Grassi624f6c62014-02-18 19:42:14 +010089size_t
90UdpChannel::size() const
91{
92 return m_channelFaces.size();
93}
94
Davide Pesavento292e5e12015-03-13 02:08:33 +010095std::pair<bool, shared_ptr<UdpFace>>
96UdpChannel::createFace(const udp::Endpoint& remoteEndpoint, bool isOnDemand)
Giulio Grassi624f6c62014-02-18 19:42:14 +010097{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010098 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +010099 if (it != m_channelFaces.end()) {
100 // we already have a face for this endpoint, just reuse it
Yukai Tu731f0d72015-07-04 11:14:44 +0800101 if (!isOnDemand) {
102 // only on-demand -> persistent transition is allowed
103 it->second->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
104 }
Davide Pesavento292e5e12015-03-13 02:08:33 +0100105 return {false, it->second};
106 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700107
Davide Pesavento292e5e12015-03-13 02:08:33 +0100108 // else, create a new face
109 ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
110 socket.set_option(ip::udp::socket::reuse_address(true));
111 socket.bind(m_localEndpoint);
112 socket.connect(remoteEndpoint);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600113
Davide Pesavento292e5e12015-03-13 02:08:33 +0100114 auto face = make_shared<UdpFace>(FaceUri(remoteEndpoint), FaceUri(m_localEndpoint),
115 std::move(socket), isOnDemand, m_idleFaceTimeout);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600116
Davide Pesavento292e5e12015-03-13 02:08:33 +0100117 face->onFail.connectSingleShot([this, remoteEndpoint] (const std::string&) {
118 NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
119 m_channelFaces.erase(remoteEndpoint);
120 });
121 m_channelFaces[remoteEndpoint] = face;
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600122
Davide Pesavento292e5e12015-03-13 02:08:33 +0100123 return {true, face};
Giulio Grassi624f6c62014-02-18 19:42:14 +0100124}
125
126void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100127UdpChannel::handleNewPeer(const boost::system::error_code& error,
128 size_t nBytesReceived,
129 const FaceCreatedCallback& onFaceCreated,
130 const ConnectFailedCallback& onReceiveFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100131{
Alexander Afanasyev90c20fa2015-02-12 16:46:45 -0800132 if (error) {
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100133 if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
Alexander Afanasyev90c20fa2015-02-12 16:46:45 -0800134 return;
135
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100136 NFD_LOG_DEBUG("[" << m_localEndpoint << "] Receive failed: " << error.message());
137 if (onReceiveFailed)
138 onReceiveFailed(error.message());
Alexander Afanasyev90c20fa2015-02-12 16:46:45 -0800139 return;
140 }
141
Davide Pesavento292e5e12015-03-13 02:08:33 +0100142 NFD_LOG_DEBUG("[" << m_localEndpoint << "] New peer " << m_remoteEndpoint);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000143
Davide Pesavento292e5e12015-03-13 02:08:33 +0100144 bool created;
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000145 shared_ptr<UdpFace> face;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100146 try {
147 std::tie(created, face) = createFace(m_remoteEndpoint, true);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000148 }
Davide Pesavento292e5e12015-03-13 02:08:33 +0100149 catch (const boost::system::system_error& e) {
150 NFD_LOG_WARN("[" << m_localEndpoint << "] Failed to create face for peer "
151 << m_remoteEndpoint << ": " << e.what());
152 if (onReceiveFailed)
153 onReceiveFailed(e.what());
154 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100155 }
156
Davide Pesavento292e5e12015-03-13 02:08:33 +0100157 if (created)
158 onFaceCreated(face);
159
Davide Pesaventoa7530582014-06-30 20:45:52 +0200160 // dispatch the datagram to the face for processing
161 face->receiveDatagram(m_inputBuffer, nBytesReceived, error);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100162
Davide Pesavento292e5e12015-03-13 02:08:33 +0100163 m_socket.async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE),
164 m_remoteEndpoint,
165 bind(&UdpChannel::handleNewPeer, this,
166 boost::asio::placeholders::error,
167 boost::asio::placeholders::bytes_transferred,
168 onFaceCreated, onReceiveFailed));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100169}
170
Giulio Grassi624f6c62014-02-18 19:42:14 +0100171} // namespace nfd