Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 24 | */ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 25 | |
| 26 | #include "udp-channel.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 27 | #include "generic-link-service.hpp" |
| 28 | #include "unicast-udp-transport.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 29 | #include "core/global-io.hpp" |
| 30 | |
| 31 | namespace nfd { |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 32 | namespace face { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 33 | |
| 34 | NFD_LOG_INIT("UdpChannel"); |
| 35 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 36 | namespace ip = boost::asio::ip; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 37 | |
| 38 | UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 39 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 40 | : m_localEndpoint(localEndpoint) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 41 | , m_socket(getGlobalIoService()) |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 42 | , m_idleFaceTimeout(timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 43 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 44 | setUri(FaceUri(m_localEndpoint)); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 45 | NFD_LOG_CHAN_INFO("Creating channel"); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 48 | void |
| 49 | UdpChannel::connect(const udp::Endpoint& remoteEndpoint, |
Yukai Tu | 375dcb0 | 2015-08-01 13:04:43 +0800 | [diff] [blame] | 50 | ndn::nfd::FacePersistency persistency, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 51 | const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 52 | const FaceCreationFailedCallback& onConnectFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 53 | { |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 54 | shared_ptr<Face> face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 55 | try { |
Yukai Tu | 375dcb0 | 2015-08-01 13:04:43 +0800 | [diff] [blame] | 56 | face = createFace(remoteEndpoint, persistency).second; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 57 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 58 | catch (const boost::system::system_error& e) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 59 | NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what()); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 60 | if (onConnectFailed) |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 61 | onConnectFailed(504, std::string("Face creation failed: ") + e.what()); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 62 | return; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 63 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 64 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 65 | // 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 Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 70 | void |
| 71 | UdpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 72 | const FaceCreationFailedCallback& onFaceCreationFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 73 | { |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 74 | if (isListening()) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 75 | NFD_LOG_CHAN_WARN("Already listening"); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
| 79 | m_socket.open(m_localEndpoint.protocol()); |
| 80 | m_socket.set_option(ip::udp::socket::reuse_address(true)); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 81 | if (m_localEndpoint.address().is_v6()) { |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 82 | m_socket.set_option(ip::v6_only(true)); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 83 | } |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 84 | m_socket.bind(m_localEndpoint); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 85 | |
| 86 | waitForNewPeer(onFaceCreated, onFaceCreationFailed); |
| 87 | NFD_LOG_CHAN_DEBUG("Started listening"); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
| 91 | UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated, |
| 92 | const FaceCreationFailedCallback& onReceiveFailed) |
| 93 | { |
Davide Pesavento | acca18a | 2017-04-09 13:23:16 -0400 | [diff] [blame] | 94 | m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint, |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 95 | bind(&UdpChannel::handleNewPeer, this, |
| 96 | boost::asio::placeholders::error, |
| 97 | boost::asio::placeholders::bytes_transferred, |
| 98 | onFaceCreated, onReceiveFailed)); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | UdpChannel::handleNewPeer(const boost::system::error_code& error, |
| 103 | size_t nBytesReceived, |
| 104 | const FaceCreatedCallback& onFaceCreated, |
| 105 | const FaceCreationFailedCallback& onReceiveFailed) |
| 106 | { |
| 107 | if (error) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 108 | 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 Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 116 | NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 117 | |
| 118 | bool isCreated = false; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 119 | shared_ptr<Face> face; |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 120 | 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 Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 124 | NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what()); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 125 | if (onReceiveFailed) |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 126 | onReceiveFailed(504, std::string("Face creation failed: ") + e.what()); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
| 130 | if (isCreated) |
| 131 | onFaceCreated(face); |
| 132 | |
| 133 | // dispatch the datagram to the face for processing |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 134 | auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport()); |
Davide Pesavento | acca18a | 2017-04-09 13:23:16 -0400 | [diff] [blame] | 135 | transport->receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 136 | |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 137 | waitForNewPeer(onFaceCreated, onReceiveFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 140 | std::pair<bool, shared_ptr<Face>> |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 141 | UdpChannel::createFace(const udp::Endpoint& remoteEndpoint, |
| 142 | ndn::nfd::FacePersistency persistency) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 143 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 144 | auto it = m_channelFaces.find(remoteEndpoint); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 145 | if (it != m_channelFaces.end()) { |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 146 | // we already have a face for this endpoint, so reuse it |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 147 | NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 148 | return {false, it->second}; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 149 | } |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 150 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 151 | // else, create a new face |
| 152 | ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol()); |
| 153 | socket.set_option(ip::udp::socket::reuse_address(true)); |
| 154 | socket.bind(m_localEndpoint); |
| 155 | socket.connect(remoteEndpoint); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 156 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 157 | auto linkService = make_unique<GenericLinkService>(); |
| 158 | auto transport = make_unique<UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 159 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 160 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 161 | m_channelFaces[remoteEndpoint] = face; |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 162 | connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); }); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 163 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 164 | return {true, face}; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 167 | } // namespace face |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 168 | } // namespace nfd |