blob: cb4faa7828b8f4b587bc589b81f2905059cf00ce [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,
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 Shi61e3cc52014-03-03 20:40:28 -070039
40 this->setUri(FaceUri(localEndpoint));
41}
42
43UdpChannel::~UdpChannel()
44{
Giulio Grassi624f6c62014-02-18 19:42:14 +010045}
46
47void
48UdpChannel::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
67void
68UdpChannel::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
99void
100UdpChannel::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
115void
116UdpChannel::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
139size_t
140UdpChannel::size() const
141{
142 return m_channelFaces.size();
143}
144
145
146shared_ptr<UdpFace>
147UdpChannel::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
160void
161UdpChannel::newPeer(const boost::system::error_code& error,
162 std::size_t nBytesReceived)
163{
164 NFD_LOG_DEBUG("UdpChannel::newPeer from " << m_newRemoteEndpoint);
165 ChannelFaceMap::iterator i = m_channelFaces.find(m_newRemoteEndpoint);
166 if (i != m_channelFaces.end()) {
167 //The face already exists.
168 //Usually this shouldn't happen, because the channel creates a Udpface
169 //as soon as it receives a pkt from a new endpoint and then the
170 //traffic is dispatched by the kernel directly to the face.
171 //However, if the node receives multiple packets from the same endpoint
172 //"at the same time", while the channel is creating the face the kernel
173 //could dispatch the other pkts to the channel because the face is not yet
174 //ready. In this case, the channel has to pass the pkt to the face
175 NFD_LOG_DEBUG("The creation of the face for the remote endpoint "
176 << m_newRemoteEndpoint
177 << " is in progress");
178 //Passing the message to the correspondent face
179 i->second->handleFirstReceive(m_inputBuffer, nBytesReceived, error);
180 return;
181 }
182
183 shared_ptr<ip::udp::socket> clientSocket =
184 make_shared<ip::udp::socket>(boost::ref(getGlobalIoService()));
185 clientSocket->open(m_localEndpoint.protocol());
186 clientSocket->set_option(ip::udp::socket::reuse_address(true));
187 clientSocket->bind(m_localEndpoint);
188 clientSocket->connect(m_newRemoteEndpoint);
189
190 shared_ptr<UdpFace> newFace = createFace(clientSocket, onFaceCreatedNewPeerCallback);
191
192 //Passing the message to the correspondent face
193 newFace->handleFirstReceive(m_inputBuffer, nBytesReceived, error);
194
195 m_socket->async_receive_from(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE),
196 m_newRemoteEndpoint,
197 bind(&UdpChannel::newPeer, this,
198 boost::asio::placeholders::error,
199 boost::asio::placeholders::bytes_transferred));
200}
201
202
203void UdpChannel::afterFaceFailed(udp::Endpoint &endpoint)
204{
205 NFD_LOG_DEBUG("afterFaceFailed: " << endpoint);
206 m_channelFaces.erase(endpoint);
207}
208
209} // namespace nfd