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