Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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" |
| 27 | #include "core/global-io.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 28 | #include "core/face-uri.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 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 | { |
| 42 | /// \todo the reuse_address works as we want in Linux, but in other system could be different. |
| 43 | /// We need to check this |
| 44 | /// (SO_REUSEADDR doesn't behave uniformly in different OS) |
| 45 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 46 | m_socket = make_shared<ip::udp::socket>(ref(getGlobalIoService())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 47 | m_socket->open(m_localEndpoint.protocol()); |
| 48 | m_socket->set_option(boost::asio::ip::udp::socket::reuse_address(true)); |
Alexander Afanasyev | eee71fd | 2014-03-13 17:40:33 -0700 | [diff] [blame] | 49 | if (m_localEndpoint.address().is_v6()) |
| 50 | { |
| 51 | m_socket->set_option(ip::v6_only(true)); |
| 52 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 53 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 54 | try { |
| 55 | m_socket->bind(m_localEndpoint); |
| 56 | } |
| 57 | catch (boost::system::system_error& e) { |
| 58 | //The bind failed, so the socket is useless now |
| 59 | m_socket->close(); |
| 60 | throw Error("Failed to properly configure the socket. " |
| 61 | "UdpChannel creation aborted, check the address (" + std::string(e.what()) + ")"); |
| 62 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 63 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 64 | this->setUri(FaceUri(localEndpoint)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
| 68 | UdpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 69 | const ConnectFailedCallback& onListenFailed) |
| 70 | { |
| 71 | if (m_isListening) { |
| 72 | throw Error("Listen already called on this channel"); |
| 73 | } |
| 74 | m_isListening = true; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 75 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 76 | onFaceCreatedNewPeerCallback = onFaceCreated; |
| 77 | onConnectFailedNewPeerCallback = onListenFailed; |
| 78 | |
| 79 | m_socket->async_receive_from(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), |
| 80 | m_newRemoteEndpoint, |
| 81 | bind(&UdpChannel::newPeer, this, |
| 82 | boost::asio::placeholders::error, |
| 83 | boost::asio::placeholders::bytes_transferred)); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void |
| 88 | UdpChannel::connect(const udp::Endpoint& remoteEndpoint, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 89 | const FaceCreatedCallback& onFaceCreated, |
| 90 | const ConnectFailedCallback& onConnectFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 91 | { |
| 92 | ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint); |
| 93 | if (i != m_channelFaces.end()) { |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 94 | i->second->setOnDemand(false); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 95 | onFaceCreated(i->second); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | //creating a new socket for the face that will be created soon |
| 100 | shared_ptr<ip::udp::socket> clientSocket = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 101 | make_shared<ip::udp::socket>(ref(getGlobalIoService())); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 102 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 103 | clientSocket->open(m_localEndpoint.protocol()); |
| 104 | clientSocket->set_option(ip::udp::socket::reuse_address(true)); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 105 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 106 | try { |
| 107 | clientSocket->bind(m_localEndpoint); |
| 108 | clientSocket->connect(remoteEndpoint); //@todo connect or async_connect |
| 109 | //(since there is no handshake the connect shouldn't block). If we go for |
| 110 | //async_connect, make sure that if in the meantime we receive a UDP pkt from |
| 111 | //that endpoint nothing bad happen (it's difficult, but it could happen) |
| 112 | } |
| 113 | catch (boost::system::system_error& e) { |
| 114 | clientSocket->close(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 115 | onConnectFailed("Failed to configure socket (" + std::string(e.what()) + ")"); |
| 116 | return; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 117 | } |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 118 | createFace(clientSocket, onFaceCreated, false); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
| 122 | UdpChannel::connect(const std::string& remoteHost, |
| 123 | const std::string& remotePort, |
| 124 | const FaceCreatedCallback& onFaceCreated, |
| 125 | const ConnectFailedCallback& onConnectFailed) |
| 126 | { |
| 127 | ip::udp::resolver::query query(remoteHost, remotePort); |
| 128 | shared_ptr<ip::udp::resolver> resolver = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 129 | make_shared<ip::udp::resolver>(ref(getGlobalIoService())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 130 | |
| 131 | resolver->async_resolve(query, |
| 132 | bind(&UdpChannel::handleEndpointResolution, this, _1, _2, |
| 133 | onFaceCreated, onConnectFailed, |
| 134 | resolver)); |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | UdpChannel::handleEndpointResolution(const boost::system::error_code& error, |
| 139 | ip::udp::resolver::iterator remoteEndpoint, |
| 140 | const FaceCreatedCallback& onFaceCreated, |
| 141 | const ConnectFailedCallback& onConnectFailed, |
| 142 | const shared_ptr<ip::udp::resolver>& resolver) |
| 143 | { |
| 144 | if (error != 0 || |
| 145 | remoteEndpoint == ip::udp::resolver::iterator()) |
| 146 | { |
| 147 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 148 | return; |
| 149 | |
| 150 | NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: " |
| 151 | << error.category().message(error.value())); |
| 152 | |
| 153 | onConnectFailed("Remote endpoint hostname or port cannot be resolved: " + |
| 154 | error.category().message(error.value())); |
| 155 | return; |
| 156 | } |
| 157 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 158 | connect(*remoteEndpoint, onFaceCreated, onConnectFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | size_t |
| 162 | UdpChannel::size() const |
| 163 | { |
| 164 | return m_channelFaces.size(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | shared_ptr<UdpFace> |
| 169 | UdpChannel::createFace(const shared_ptr<ip::udp::socket>& socket, |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 170 | const FaceCreatedCallback& onFaceCreated, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 171 | bool isOnDemand) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 172 | { |
| 173 | udp::Endpoint remoteEndpoint = socket->remote_endpoint(); |
| 174 | |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 175 | shared_ptr<UdpFace> face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 176 | |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 177 | ChannelFaceMap::iterator faceMapPos = m_channelFaces.find(remoteEndpoint); |
| 178 | if (faceMapPos == m_channelFaces.end()) |
| 179 | { |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 180 | face = make_shared<UdpFace>(socket, isOnDemand, m_idleFaceTimeout); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 181 | face->onFail += bind(&UdpChannel::afterFaceFailed, this, remoteEndpoint); |
| 182 | |
| 183 | m_channelFaces[remoteEndpoint] = face; |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | // we've already created a a face for this endpoint, just reuse it |
| 188 | face = faceMapPos->second; |
| 189 | |
| 190 | boost::system::error_code error; |
| 191 | socket->shutdown(ip::udp::socket::shutdown_both, error); |
| 192 | socket->close(error); |
| 193 | } |
| 194 | |
| 195 | // Need to invoke the callback regardless of whether or not we have already created |
| 196 | // the face so that control responses and such can be sent. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 197 | onFaceCreated(face); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 198 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 199 | return face; |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | UdpChannel::newPeer(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 204 | size_t nBytesReceived) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 205 | { |
| 206 | NFD_LOG_DEBUG("UdpChannel::newPeer from " << m_newRemoteEndpoint); |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 207 | |
| 208 | shared_ptr<UdpFace> face; |
| 209 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 210 | ChannelFaceMap::iterator i = m_channelFaces.find(m_newRemoteEndpoint); |
| 211 | if (i != m_channelFaces.end()) { |
| 212 | //The face already exists. |
| 213 | //Usually this shouldn't happen, because the channel creates a Udpface |
| 214 | //as soon as it receives a pkt from a new endpoint and then the |
| 215 | //traffic is dispatched by the kernel directly to the face. |
| 216 | //However, if the node receives multiple packets from the same endpoint |
| 217 | //"at the same time", while the channel is creating the face the kernel |
| 218 | //could dispatch the other pkts to the channel because the face is not yet |
| 219 | //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] | 220 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 221 | NFD_LOG_DEBUG("The creation of the face for the remote endpoint " |
| 222 | << m_newRemoteEndpoint |
| 223 | << " is in progress"); |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 224 | |
| 225 | face = i->second; |
| 226 | } |
| 227 | else { |
| 228 | shared_ptr<ip::udp::socket> clientSocket = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 229 | make_shared<ip::udp::socket>(ref(getGlobalIoService())); |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 230 | clientSocket->open(m_localEndpoint.protocol()); |
| 231 | clientSocket->set_option(ip::udp::socket::reuse_address(true)); |
| 232 | clientSocket->bind(m_localEndpoint); |
| 233 | clientSocket->connect(m_newRemoteEndpoint); |
| 234 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 235 | face = createFace(clientSocket, |
| 236 | onFaceCreatedNewPeerCallback, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 237 | true); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Davide Pesavento | a753058 | 2014-06-30 20:45:52 +0200 | [diff] [blame] | 240 | // dispatch the datagram to the face for processing |
| 241 | face->receiveDatagram(m_inputBuffer, nBytesReceived, error); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 242 | |
| 243 | m_socket->async_receive_from(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), |
| 244 | m_newRemoteEndpoint, |
| 245 | bind(&UdpChannel::newPeer, this, |
| 246 | boost::asio::placeholders::error, |
| 247 | boost::asio::placeholders::bytes_transferred)); |
| 248 | } |
| 249 | |
| 250 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 251 | void |
| 252 | UdpChannel::afterFaceFailed(udp::Endpoint &endpoint) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 253 | { |
| 254 | NFD_LOG_DEBUG("afterFaceFailed: " << endpoint); |
| 255 | m_channelFaces.erase(endpoint); |
| 256 | } |
| 257 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 258 | } // namespace nfd |