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