blob: 49c22b783f32e8fb33a481e72350e8771ccc3f2f [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- 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 Shi61e3cc52014-03-03 20:40:28 -07009#include "core/face-uri.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010010
11namespace nfd {
12
13NFD_LOG_INIT("UdpChannel");
14
15using namespace boost::asio;
16
17UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070018 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010019 : m_localEndpoint(localEndpoint)
20 , m_isListening(false)
Giulio Grassi69871f02014-03-09 16:14:44 +010021 , m_idleFaceTimeout(timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010022{
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 Afanasyeveee71fd2014-03-13 17:40:33 -070030 if (m_localEndpoint.address().is_v6())
31 {
32 m_socket->set_option(ip::v6_only(true));
33 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060034
Giulio Grassi624f6c62014-02-18 19:42:14 +010035 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 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060044
Junxiao Shi61e3cc52014-03-03 20:40:28 -070045 this->setUri(FaceUri(localEndpoint));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060046
Giulio Grassi69871f02014-03-09 16:14:44 +010047 //setting the timeout to close the idle faces
48 m_closeIdleFaceEvent = scheduler::schedule(m_idleFaceTimeout,
49 bind(&UdpChannel::closeIdleFaces, this));
Junxiao Shi61e3cc52014-03-03 20:40:28 -070050}
51
52UdpChannel::~UdpChannel()
53{
Giulio Grassi69871f02014-03-09 16:14:44 +010054 scheduler::cancel(m_closeIdleFaceEvent);
Giulio Grassi624f6c62014-02-18 19:42:14 +010055}
56
57void
58UdpChannel::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;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060065
Giulio Grassi624f6c62014-02-18 19:42:14 +010066 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
77void
78UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060079 const FaceCreatedCallback& onFaceCreated,
80 const ConnectFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010081{
82 ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint);
83 if (i != m_channelFaces.end()) {
Alexander Afanasyev355c0662014-03-20 18:08:17 -070084 i->second->setOnDemand(false);
Giulio Grassi624f6c62014-02-18 19:42:14 +010085 onFaceCreated(i->second);
86 return;
87 }
88
89 //creating a new socket for the face that will be created soon
90 shared_ptr<ip::udp::socket> clientSocket =
91 make_shared<ip::udp::socket>(boost::ref(getGlobalIoService()));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060092
Giulio Grassi624f6c62014-02-18 19:42:14 +010093 clientSocket->open(m_localEndpoint.protocol());
94 clientSocket->set_option(ip::udp::socket::reuse_address(true));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060095
Giulio Grassi624f6c62014-02-18 19:42:14 +010096 try {
97 clientSocket->bind(m_localEndpoint);
98 clientSocket->connect(remoteEndpoint); //@todo connect or async_connect
99 //(since there is no handshake the connect shouldn't block). If we go for
100 //async_connect, make sure that if in the meantime we receive a UDP pkt from
101 //that endpoint nothing bad happen (it's difficult, but it could happen)
102 }
103 catch (boost::system::system_error& e) {
104 clientSocket->close();
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600105 onConnectFailed("Failed to configure socket (" + std::string(e.what()) + ")");
106 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100107 }
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700108 createFace(clientSocket, onFaceCreated, false);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100109}
110
111void
112UdpChannel::connect(const std::string& remoteHost,
113 const std::string& remotePort,
114 const FaceCreatedCallback& onFaceCreated,
115 const ConnectFailedCallback& onConnectFailed)
116{
117 ip::udp::resolver::query query(remoteHost, remotePort);
118 shared_ptr<ip::udp::resolver> resolver =
119 make_shared<ip::udp::resolver>(boost::ref(getGlobalIoService()));
120
121 resolver->async_resolve(query,
122 bind(&UdpChannel::handleEndpointResolution, this, _1, _2,
123 onFaceCreated, onConnectFailed,
124 resolver));
125}
126
127void
128UdpChannel::handleEndpointResolution(const boost::system::error_code& error,
129 ip::udp::resolver::iterator remoteEndpoint,
130 const FaceCreatedCallback& onFaceCreated,
131 const ConnectFailedCallback& onConnectFailed,
132 const shared_ptr<ip::udp::resolver>& resolver)
133{
134 if (error != 0 ||
135 remoteEndpoint == ip::udp::resolver::iterator())
136 {
137 if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
138 return;
139
140 NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: "
141 << error.category().message(error.value()));
142
143 onConnectFailed("Remote endpoint hostname or port cannot be resolved: " +
144 error.category().message(error.value()));
145 return;
146 }
147
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600148 connect(*remoteEndpoint, onFaceCreated, onConnectFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100149}
150
151size_t
152UdpChannel::size() const
153{
154 return m_channelFaces.size();
155}
156
157
158shared_ptr<UdpFace>
159UdpChannel::createFace(const shared_ptr<ip::udp::socket>& socket,
Giulio Grassi69871f02014-03-09 16:14:44 +0100160 const FaceCreatedCallback& onFaceCreated,
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700161 bool isOnDemand)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100162{
163 udp::Endpoint remoteEndpoint = socket->remote_endpoint();
164
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700165 shared_ptr<UdpFace> face = make_shared<UdpFace>(boost::cref(socket), isOnDemand);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100166 face->onFail += bind(&UdpChannel::afterFaceFailed, this, remoteEndpoint);
167
168 onFaceCreated(face);
169 m_channelFaces[remoteEndpoint] = face;
170 return face;
171}
172
173void
174UdpChannel::newPeer(const boost::system::error_code& error,
175 std::size_t nBytesReceived)
176{
177 NFD_LOG_DEBUG("UdpChannel::newPeer from " << m_newRemoteEndpoint);
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000178
179 shared_ptr<UdpFace> face;
180
Giulio Grassi624f6c62014-02-18 19:42:14 +0100181 ChannelFaceMap::iterator i = m_channelFaces.find(m_newRemoteEndpoint);
182 if (i != m_channelFaces.end()) {
183 //The face already exists.
184 //Usually this shouldn't happen, because the channel creates a Udpface
185 //as soon as it receives a pkt from a new endpoint and then the
186 //traffic is dispatched by the kernel directly to the face.
187 //However, if the node receives multiple packets from the same endpoint
188 //"at the same time", while the channel is creating the face the kernel
189 //could dispatch the other pkts to the channel because the face is not yet
190 //ready. In this case, the channel has to pass the pkt to the face
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000191
Giulio Grassi624f6c62014-02-18 19:42:14 +0100192 NFD_LOG_DEBUG("The creation of the face for the remote endpoint "
193 << m_newRemoteEndpoint
194 << " is in progress");
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000195
196 face = i->second;
197 }
198 else {
199 shared_ptr<ip::udp::socket> clientSocket =
200 make_shared<ip::udp::socket>(boost::ref(getGlobalIoService()));
201 clientSocket->open(m_localEndpoint.protocol());
202 clientSocket->set_option(ip::udp::socket::reuse_address(true));
203 clientSocket->bind(m_localEndpoint);
204 clientSocket->connect(m_newRemoteEndpoint);
205
Giulio Grassi69871f02014-03-09 16:14:44 +0100206 face = createFace(clientSocket,
207 onFaceCreatedNewPeerCallback,
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700208 true);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100209 }
210
Giulio Grassi624f6c62014-02-18 19:42:14 +0100211 //Passing the message to the correspondent face
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000212 face->handleFirstReceive(m_inputBuffer, nBytesReceived, error);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100213
214 m_socket->async_receive_from(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE),
215 m_newRemoteEndpoint,
216 bind(&UdpChannel::newPeer, this,
217 boost::asio::placeholders::error,
218 boost::asio::placeholders::bytes_transferred));
219}
220
221
Giulio Grassi69871f02014-03-09 16:14:44 +0100222void
223UdpChannel::afterFaceFailed(udp::Endpoint &endpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100224{
225 NFD_LOG_DEBUG("afterFaceFailed: " << endpoint);
226 m_channelFaces.erase(endpoint);
227}
228
Giulio Grassi69871f02014-03-09 16:14:44 +0100229void
230UdpChannel::closeIdleFaces()
231{
232 ChannelFaceMap::iterator next = m_channelFaces.begin();
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600233
Giulio Grassi69871f02014-03-09 16:14:44 +0100234 while (next != m_channelFaces.end()) {
235 ChannelFaceMap::iterator it = next;
236 next++;
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700237 if (it->second->isOnDemand() &&
Giulio Grassi69871f02014-03-09 16:14:44 +0100238 !it->second->hasBeenUsedRecently()) {
239 //face has been idle since the last time closeIdleFaces
240 //has been called. Going to close it
241 NFD_LOG_DEBUG("Found idle face id: " << it->second->getId());
242 it->second->close();
243 } else {
244 it->second->resetRecentUsage();
245 }
246 }
247 m_closeIdleFaceEvent = scheduler::schedule(m_idleFaceTimeout,
248 bind(&UdpChannel::closeIdleFaces, this));
249}
250
Giulio Grassi624f6c62014-02-18 19:42:14 +0100251} // namespace nfd