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 | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 24 | |
| 25 | #include "udp-channel.hpp" |
| 26 | #include "core/global-io.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 27 | #include "core/face-uri.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | |
| 31 | NFD_LOG_INIT("UdpChannel"); |
| 32 | |
| 33 | using namespace boost::asio; |
| 34 | |
| 35 | UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 36 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 37 | : m_localEndpoint(localEndpoint) |
| 38 | , m_isListening(false) |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 39 | , m_idleFaceTimeout(timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 40 | { |
| 41 | /// \todo the reuse_address works as we want in Linux, but in other system could be different. |
| 42 | /// We need to check this |
| 43 | /// (SO_REUSEADDR doesn't behave uniformly in different OS) |
| 44 | |
| 45 | m_socket = make_shared<ip::udp::socket>(boost::ref(getGlobalIoService())); |
| 46 | m_socket->open(m_localEndpoint.protocol()); |
| 47 | m_socket->set_option(boost::asio::ip::udp::socket::reuse_address(true)); |
Alexander Afanasyev | eee71fd | 2014-03-13 17:40:33 -0700 | [diff] [blame] | 48 | if (m_localEndpoint.address().is_v6()) |
| 49 | { |
| 50 | m_socket->set_option(ip::v6_only(true)); |
| 51 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 52 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 53 | try { |
| 54 | m_socket->bind(m_localEndpoint); |
| 55 | } |
| 56 | catch (boost::system::system_error& e) { |
| 57 | //The bind failed, so the socket is useless now |
| 58 | m_socket->close(); |
| 59 | throw Error("Failed to properly configure the socket. " |
| 60 | "UdpChannel creation aborted, check the address (" + std::string(e.what()) + ")"); |
| 61 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 62 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 63 | this->setUri(FaceUri(localEndpoint)); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 64 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 65 | //setting the timeout to close the idle faces |
| 66 | m_closeIdleFaceEvent = scheduler::schedule(m_idleFaceTimeout, |
| 67 | bind(&UdpChannel::closeIdleFaces, this)); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | UdpChannel::~UdpChannel() |
| 71 | { |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 72 | scheduler::cancel(m_closeIdleFaceEvent); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
| 76 | UdpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 77 | const ConnectFailedCallback& onListenFailed) |
| 78 | { |
| 79 | if (m_isListening) { |
| 80 | throw Error("Listen already called on this channel"); |
| 81 | } |
| 82 | m_isListening = true; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 83 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 84 | onFaceCreatedNewPeerCallback = onFaceCreated; |
| 85 | onConnectFailedNewPeerCallback = onListenFailed; |
| 86 | |
| 87 | m_socket->async_receive_from(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), |
| 88 | m_newRemoteEndpoint, |
| 89 | bind(&UdpChannel::newPeer, this, |
| 90 | boost::asio::placeholders::error, |
| 91 | boost::asio::placeholders::bytes_transferred)); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void |
| 96 | UdpChannel::connect(const udp::Endpoint& remoteEndpoint, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 97 | const FaceCreatedCallback& onFaceCreated, |
| 98 | const ConnectFailedCallback& onConnectFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 99 | { |
| 100 | ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint); |
| 101 | if (i != m_channelFaces.end()) { |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 102 | i->second->setOnDemand(false); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 103 | onFaceCreated(i->second); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | //creating a new socket for the face that will be created soon |
| 108 | shared_ptr<ip::udp::socket> clientSocket = |
| 109 | make_shared<ip::udp::socket>(boost::ref(getGlobalIoService())); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 110 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 111 | clientSocket->open(m_localEndpoint.protocol()); |
| 112 | clientSocket->set_option(ip::udp::socket::reuse_address(true)); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 113 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 114 | try { |
| 115 | clientSocket->bind(m_localEndpoint); |
| 116 | clientSocket->connect(remoteEndpoint); //@todo connect or async_connect |
| 117 | //(since there is no handshake the connect shouldn't block). If we go for |
| 118 | //async_connect, make sure that if in the meantime we receive a UDP pkt from |
| 119 | //that endpoint nothing bad happen (it's difficult, but it could happen) |
| 120 | } |
| 121 | catch (boost::system::system_error& e) { |
| 122 | clientSocket->close(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 123 | onConnectFailed("Failed to configure socket (" + std::string(e.what()) + ")"); |
| 124 | return; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 125 | } |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 126 | createFace(clientSocket, onFaceCreated, false); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
| 130 | UdpChannel::connect(const std::string& remoteHost, |
| 131 | const std::string& remotePort, |
| 132 | const FaceCreatedCallback& onFaceCreated, |
| 133 | const ConnectFailedCallback& onConnectFailed) |
| 134 | { |
| 135 | ip::udp::resolver::query query(remoteHost, remotePort); |
| 136 | shared_ptr<ip::udp::resolver> resolver = |
| 137 | make_shared<ip::udp::resolver>(boost::ref(getGlobalIoService())); |
| 138 | |
| 139 | resolver->async_resolve(query, |
| 140 | bind(&UdpChannel::handleEndpointResolution, this, _1, _2, |
| 141 | onFaceCreated, onConnectFailed, |
| 142 | resolver)); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | UdpChannel::handleEndpointResolution(const boost::system::error_code& error, |
| 147 | ip::udp::resolver::iterator remoteEndpoint, |
| 148 | const FaceCreatedCallback& onFaceCreated, |
| 149 | const ConnectFailedCallback& onConnectFailed, |
| 150 | const shared_ptr<ip::udp::resolver>& resolver) |
| 151 | { |
| 152 | if (error != 0 || |
| 153 | remoteEndpoint == ip::udp::resolver::iterator()) |
| 154 | { |
| 155 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 156 | return; |
| 157 | |
| 158 | NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: " |
| 159 | << error.category().message(error.value())); |
| 160 | |
| 161 | onConnectFailed("Remote endpoint hostname or port cannot be resolved: " + |
| 162 | error.category().message(error.value())); |
| 163 | return; |
| 164 | } |
| 165 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 166 | connect(*remoteEndpoint, onFaceCreated, onConnectFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | size_t |
| 170 | UdpChannel::size() const |
| 171 | { |
| 172 | return m_channelFaces.size(); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | shared_ptr<UdpFace> |
| 177 | UdpChannel::createFace(const shared_ptr<ip::udp::socket>& socket, |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 178 | const FaceCreatedCallback& onFaceCreated, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 179 | bool isOnDemand) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 180 | { |
| 181 | udp::Endpoint remoteEndpoint = socket->remote_endpoint(); |
| 182 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 183 | shared_ptr<UdpFace> face = make_shared<UdpFace>(boost::cref(socket), isOnDemand); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 184 | face->onFail += bind(&UdpChannel::afterFaceFailed, this, remoteEndpoint); |
| 185 | |
| 186 | onFaceCreated(face); |
| 187 | m_channelFaces[remoteEndpoint] = face; |
| 188 | return face; |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | UdpChannel::newPeer(const boost::system::error_code& error, |
| 193 | std::size_t nBytesReceived) |
| 194 | { |
| 195 | NFD_LOG_DEBUG("UdpChannel::newPeer from " << m_newRemoteEndpoint); |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 196 | |
| 197 | shared_ptr<UdpFace> face; |
| 198 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 199 | ChannelFaceMap::iterator i = m_channelFaces.find(m_newRemoteEndpoint); |
| 200 | if (i != m_channelFaces.end()) { |
| 201 | //The face already exists. |
| 202 | //Usually this shouldn't happen, because the channel creates a Udpface |
| 203 | //as soon as it receives a pkt from a new endpoint and then the |
| 204 | //traffic is dispatched by the kernel directly to the face. |
| 205 | //However, if the node receives multiple packets from the same endpoint |
| 206 | //"at the same time", while the channel is creating the face the kernel |
| 207 | //could dispatch the other pkts to the channel because the face is not yet |
| 208 | //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] | 209 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 210 | NFD_LOG_DEBUG("The creation of the face for the remote endpoint " |
| 211 | << m_newRemoteEndpoint |
| 212 | << " is in progress"); |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 213 | |
| 214 | face = i->second; |
| 215 | } |
| 216 | else { |
| 217 | shared_ptr<ip::udp::socket> clientSocket = |
| 218 | make_shared<ip::udp::socket>(boost::ref(getGlobalIoService())); |
| 219 | clientSocket->open(m_localEndpoint.protocol()); |
| 220 | clientSocket->set_option(ip::udp::socket::reuse_address(true)); |
| 221 | clientSocket->bind(m_localEndpoint); |
| 222 | clientSocket->connect(m_newRemoteEndpoint); |
| 223 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 224 | face = createFace(clientSocket, |
| 225 | onFaceCreatedNewPeerCallback, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 226 | true); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 229 | //Passing the message to the correspondent face |
Alexander Afanasyev | 6f5ff63 | 2014-03-07 16:40:10 +0000 | [diff] [blame] | 230 | face->handleFirstReceive(m_inputBuffer, nBytesReceived, error); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 231 | |
| 232 | m_socket->async_receive_from(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), |
| 233 | m_newRemoteEndpoint, |
| 234 | bind(&UdpChannel::newPeer, this, |
| 235 | boost::asio::placeholders::error, |
| 236 | boost::asio::placeholders::bytes_transferred)); |
| 237 | } |
| 238 | |
| 239 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 240 | void |
| 241 | UdpChannel::afterFaceFailed(udp::Endpoint &endpoint) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 242 | { |
| 243 | NFD_LOG_DEBUG("afterFaceFailed: " << endpoint); |
| 244 | m_channelFaces.erase(endpoint); |
| 245 | } |
| 246 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 247 | void |
| 248 | UdpChannel::closeIdleFaces() |
| 249 | { |
| 250 | ChannelFaceMap::iterator next = m_channelFaces.begin(); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 251 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 252 | while (next != m_channelFaces.end()) { |
| 253 | ChannelFaceMap::iterator it = next; |
| 254 | next++; |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 255 | if (it->second->isOnDemand() && |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 256 | !it->second->hasBeenUsedRecently()) { |
| 257 | //face has been idle since the last time closeIdleFaces |
| 258 | //has been called. Going to close it |
| 259 | NFD_LOG_DEBUG("Found idle face id: " << it->second->getId()); |
| 260 | it->second->close(); |
| 261 | } else { |
| 262 | it->second->resetRecentUsage(); |
| 263 | } |
| 264 | } |
| 265 | m_closeIdleFaceEvent = scheduler::schedule(m_idleFaceTimeout, |
| 266 | bind(&UdpChannel::closeIdleFaces, this)); |
| 267 | } |
| 268 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 269 | } // namespace nfd |