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) |
| 39 | , m_isListening(false) |
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 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 44 | m_socket = make_shared<ip::udp::socket>(ref(getGlobalIoService())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 45 | m_socket->open(m_localEndpoint.protocol()); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 46 | m_socket->set_option(ip::udp::socket::reuse_address(true)); |
Alexander Afanasyev | eee71fd | 2014-03-13 17:40:33 -0700 | [diff] [blame] | 47 | if (m_localEndpoint.address().is_v6()) |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 48 | m_socket->set_option(ip::v6_only(true)); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 49 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 50 | try { |
| 51 | m_socket->bind(m_localEndpoint); |
| 52 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 53 | catch (const boost::system::system_error& e) { |
| 54 | // bind failed, so the socket is useless now |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 55 | m_socket->close(); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 56 | throw Error("bind failed: " + std::string(e.what())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 57 | } |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
| 61 | UdpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 62 | const ConnectFailedCallback& onReceiveFailed) |
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 | if (isListening()) { |
| 65 | NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening"); |
| 66 | return; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 67 | } |
| 68 | m_isListening = true; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 69 | |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 70 | m_socket->async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE), |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 71 | m_newRemoteEndpoint, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 72 | bind(&UdpChannel::handleNewPeer, this, |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 73 | boost::asio::placeholders::error, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 74 | boost::asio::placeholders::bytes_transferred, |
| 75 | onFaceCreated, onReceiveFailed)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 78 | void |
| 79 | UdpChannel::connect(const udp::Endpoint& remoteEndpoint, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 80 | const FaceCreatedCallback& onFaceCreated, |
| 81 | const ConnectFailedCallback& onConnectFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 82 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 83 | auto it = m_channelFaces.find(remoteEndpoint); |
| 84 | if (it != m_channelFaces.end()) { |
| 85 | it->second->setOnDemand(false); |
| 86 | onFaceCreated(it->second); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 90 | // creating a new socket for the face that will be created soon |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 91 | shared_ptr<ip::udp::socket> clientSocket = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 92 | make_shared<ip::udp::socket>(ref(getGlobalIoService())); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 93 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 94 | clientSocket->open(m_localEndpoint.protocol()); |
| 95 | clientSocket->set_option(ip::udp::socket::reuse_address(true)); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 96 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 97 | try { |
| 98 | clientSocket->bind(m_localEndpoint); |
| 99 | clientSocket->connect(remoteEndpoint); //@todo connect or async_connect |
| 100 | //(since there is no handshake the connect shouldn't block). If we go for |
| 101 | //async_connect, make sure that if in the meantime we receive a UDP pkt from |
| 102 | //that endpoint nothing bad happen (it's difficult, but it could happen) |
| 103 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 104 | catch (const boost::system::system_error& e) { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 105 | clientSocket->close(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 106 | onConnectFailed("Failed to configure socket (" + std::string(e.what()) + ")"); |
| 107 | return; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 108 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 109 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 110 | createFace(clientSocket, onFaceCreated, false); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 113 | size_t |
| 114 | UdpChannel::size() const |
| 115 | { |
| 116 | return m_channelFaces.size(); |
| 117 | } |
| 118 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 119 | shared_ptr<UdpFace> |
| 120 | UdpChannel::createFace(const shared_ptr<ip::udp::socket>& socket, |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 121 | const FaceCreatedCallback& onFaceCreated, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 122 | bool isOnDemand) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 123 | { |
| 124 | udp::Endpoint remoteEndpoint = socket->remote_endpoint(); |
| 125 | |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 126 | shared_ptr<UdpFace> face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 127 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 128 | auto it = m_channelFaces.find(remoteEndpoint); |
| 129 | if (it == m_channelFaces.end()) |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 130 | { |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 131 | face = make_shared<UdpFace>(socket, isOnDemand, m_idleFaceTimeout); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 132 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 133 | face->onFail.connectSingleShot([this, remoteEndpoint] (const std::string&) { |
| 134 | NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map"); |
| 135 | m_channelFaces.erase(remoteEndpoint); |
| 136 | }); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 137 | |
| 138 | m_channelFaces[remoteEndpoint] = face; |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | // we've already created a a face for this endpoint, just reuse it |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 143 | face = it->second; |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 144 | |
| 145 | boost::system::error_code error; |
| 146 | socket->shutdown(ip::udp::socket::shutdown_both, error); |
| 147 | socket->close(error); |
| 148 | } |
| 149 | |
| 150 | // Need to invoke the callback regardless of whether or not we have already created |
| 151 | // the face so that control responses and such can be sent. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 152 | onFaceCreated(face); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 153 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 154 | return face; |
| 155 | } |
| 156 | |
| 157 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 158 | UdpChannel::handleNewPeer(const boost::system::error_code& error, |
| 159 | size_t nBytesReceived, |
| 160 | const FaceCreatedCallback& onFaceCreated, |
| 161 | const ConnectFailedCallback& onReceiveFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 162 | { |
Alexander Afanasyev | 90c20fa | 2015-02-12 16:46:45 -0800 | [diff] [blame] | 163 | if (error) { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 164 | 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] | 165 | return; |
| 166 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 167 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] Receive failed: " << error.message()); |
| 168 | if (onReceiveFailed) |
| 169 | onReceiveFailed(error.message()); |
Alexander Afanasyev | 90c20fa | 2015-02-12 16:46:45 -0800 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 173 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] New peer " << m_newRemoteEndpoint); |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 174 | |
| 175 | shared_ptr<UdpFace> face; |
| 176 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 177 | auto it = m_channelFaces.find(m_newRemoteEndpoint); |
| 178 | if (it != m_channelFaces.end()) { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 179 | //The face already exists. |
| 180 | //Usually this shouldn't happen, because the channel creates a Udpface |
| 181 | //as soon as it receives a pkt from a new endpoint and then the |
| 182 | //traffic is dispatched by the kernel directly to the face. |
| 183 | //However, if the node receives multiple packets from the same endpoint |
| 184 | //"at the same time", while the channel is creating the face the kernel |
| 185 | //could dispatch the other pkts to the channel because the face is not yet |
| 186 | //ready. In this case, the channel has to pass the pkt to the face |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 187 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 188 | NFD_LOG_DEBUG("The creation of the face for the remote endpoint " |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 189 | << m_newRemoteEndpoint << " is already in progress"); |
| 190 | face = it->second; |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 191 | } |
| 192 | else { |
| 193 | shared_ptr<ip::udp::socket> clientSocket = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 194 | make_shared<ip::udp::socket>(ref(getGlobalIoService())); |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 195 | clientSocket->open(m_localEndpoint.protocol()); |
| 196 | clientSocket->set_option(ip::udp::socket::reuse_address(true)); |
| 197 | clientSocket->bind(m_localEndpoint); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 198 | |
Alexander Afanasyev | c5173de | 2014-12-18 10:51:35 -0800 | [diff] [blame] | 199 | boost::system::error_code ec; |
| 200 | clientSocket->connect(m_newRemoteEndpoint, ec); |
| 201 | if (ec) { |
| 202 | NFD_LOG_WARN("Error while creating on-demand UDP face from " << m_newRemoteEndpoint << ": " |
| 203 | << boost::system::system_error(ec).what()); |
| 204 | return; |
| 205 | } |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 206 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 207 | face = createFace(clientSocket, onFaceCreated, true); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Davide Pesavento | a753058 | 2014-06-30 20:45:52 +0200 | [diff] [blame] | 210 | // dispatch the datagram to the face for processing |
| 211 | face->receiveDatagram(m_inputBuffer, nBytesReceived, error); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 212 | |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 213 | m_socket->async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE), |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 214 | m_newRemoteEndpoint, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 215 | bind(&UdpChannel::handleNewPeer, this, |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 216 | boost::asio::placeholders::error, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 217 | boost::asio::placeholders::bytes_transferred, |
| 218 | onFaceCreated, onReceiveFailed)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 221 | } // namespace nfd |