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