Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 3 | * 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 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 { |
| 32 | |
| 33 | NFD_LOG_INIT("UdpChannel"); |
| 34 | |
| 35 | using namespace boost::asio; |
| 36 | |
| 37 | UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 38 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 39 | : m_localEndpoint(localEndpoint) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 40 | , m_socket(getGlobalIoService()) |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 41 | , m_idleFaceTimeout(timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 42 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 43 | setUri(FaceUri(m_localEndpoint)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame^] | 46 | size_t |
| 47 | UdpChannel::size() const |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 48 | { |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame^] | 49 | return m_channelFaces.size(); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 52 | void |
| 53 | UdpChannel::connect(const udp::Endpoint& remoteEndpoint, |
Yukai Tu | 375dcb0 | 2015-08-01 13:04:43 +0800 | [diff] [blame] | 54 | ndn::nfd::FacePersistency persistency, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 55 | const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 56 | const FaceCreationFailedCallback& onConnectFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 57 | { |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame^] | 58 | shared_ptr<Face> face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 59 | try { |
Yukai Tu | 375dcb0 | 2015-08-01 13:04:43 +0800 | [diff] [blame] | 60 | face = createFace(remoteEndpoint, persistency).second; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 61 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 62 | catch (const boost::system::system_error& e) { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 63 | NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << e.what()); |
| 64 | if (onConnectFailed) |
| 65 | onConnectFailed(e.what()); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 66 | return; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 67 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 68 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 69 | // Need to invoke the callback regardless of whether or not we had already |
| 70 | // created the face so that control responses and such can be sent |
| 71 | onFaceCreated(face); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame^] | 74 | void |
| 75 | UdpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 76 | const FaceCreationFailedCallback& onReceiveFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 77 | { |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame^] | 78 | if (isListening()) { |
| 79 | NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening"); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | m_socket.open(m_localEndpoint.protocol()); |
| 84 | m_socket.set_option(ip::udp::socket::reuse_address(true)); |
| 85 | if (m_localEndpoint.address().is_v6()) |
| 86 | m_socket.set_option(ip::v6_only(true)); |
| 87 | |
| 88 | m_socket.bind(m_localEndpoint); |
| 89 | this->waitForNewPeer(onFaceCreated, onReceiveFailed); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated, |
| 94 | const FaceCreationFailedCallback& onReceiveFailed) |
| 95 | { |
| 96 | m_socket.async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE), |
| 97 | m_remoteEndpoint, |
| 98 | bind(&UdpChannel::handleNewPeer, this, |
| 99 | boost::asio::placeholders::error, |
| 100 | boost::asio::placeholders::bytes_transferred, |
| 101 | onFaceCreated, onReceiveFailed)); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | UdpChannel::handleNewPeer(const boost::system::error_code& error, |
| 106 | size_t nBytesReceived, |
| 107 | const FaceCreatedCallback& onFaceCreated, |
| 108 | const FaceCreationFailedCallback& onReceiveFailed) |
| 109 | { |
| 110 | if (error) { |
| 111 | if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone |
| 112 | return; |
| 113 | |
| 114 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] Receive failed: " << error.message()); |
| 115 | if (onReceiveFailed) |
| 116 | onReceiveFailed(error.message()); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] New peer " << m_remoteEndpoint); |
| 121 | |
| 122 | bool isCreated = false; |
| 123 | shared_ptr<face::LpFaceWrapper> face; |
| 124 | try { |
| 125 | std::tie(isCreated, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 126 | } |
| 127 | catch (const boost::system::system_error& e) { |
| 128 | NFD_LOG_WARN("[" << m_localEndpoint << "] Failed to create face for peer " |
| 129 | << m_remoteEndpoint << ": " << e.what()); |
| 130 | if (onReceiveFailed) |
| 131 | onReceiveFailed(e.what()); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (isCreated) |
| 136 | onFaceCreated(face); |
| 137 | |
| 138 | // dispatch the datagram to the face for processing |
| 139 | static_cast<face::UnicastUdpTransport*>(face->getLpFace()->getTransport())->receiveDatagram(m_inputBuffer, nBytesReceived, error); |
| 140 | |
| 141 | this->waitForNewPeer(onFaceCreated, onReceiveFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 144 | std::pair<bool, shared_ptr<face::LpFaceWrapper>> |
Yukai Tu | 375dcb0 | 2015-08-01 13:04:43 +0800 | [diff] [blame] | 145 | UdpChannel::createFace(const udp::Endpoint& remoteEndpoint, ndn::nfd::FacePersistency persistency) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 146 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 147 | auto it = m_channelFaces.find(remoteEndpoint); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 148 | if (it != m_channelFaces.end()) { |
| 149 | // we already have a face for this endpoint, just reuse it |
Yukai Tu | 375dcb0 | 2015-08-01 13:04:43 +0800 | [diff] [blame] | 150 | auto face = it->second; |
| 151 | // only on-demand -> persistent -> permanent transition is allowed |
| 152 | bool isTransitionAllowed = persistency != face->getPersistency() && |
| 153 | (face->getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND || |
| 154 | persistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 155 | if (isTransitionAllowed) { |
| 156 | face->setPersistency(persistency); |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 157 | } |
Yukai Tu | 375dcb0 | 2015-08-01 13:04:43 +0800 | [diff] [blame] | 158 | return {false, face}; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 159 | } |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 160 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 161 | // else, create a new face |
| 162 | ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol()); |
| 163 | socket.set_option(ip::udp::socket::reuse_address(true)); |
| 164 | socket.bind(m_localEndpoint); |
| 165 | socket.connect(remoteEndpoint); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 166 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 167 | auto linkService = make_unique<face::GenericLinkService>(); |
| 168 | auto transport = make_unique<face::UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame^] | 169 | auto face = make_shared<face::LpFaceWrapper>(make_unique<face::LpFace>( |
| 170 | std::move(linkService), std::move(transport))); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 171 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 172 | face->setPersistency(persistency); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 173 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 174 | m_channelFaces[remoteEndpoint] = face; |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame^] | 175 | face->onFail.connectSingleShot([this, remoteEndpoint] (const std::string&) { |
| 176 | NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map"); |
| 177 | m_channelFaces.erase(remoteEndpoint); |
| 178 | }); |
| 179 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 180 | return {true, face}; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 183 | } // namespace nfd |