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