blob: cac94a2a5a741de46b364a117e281dc88e0e38bc [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology
9 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Giulio Grassi624f6c62014-02-18 19:42:14 +010024
25#include "udp-factory.hpp"
26#include "core/global-io.hpp"
27#include "core/resolver.hpp"
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060028#include "core/network-interface.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010029
30namespace nfd {
31
32using namespace boost::asio;
Junxiao Shi79494162014-04-02 18:25:11 -070033
Giulio Grassi624f6c62014-02-18 19:42:14 +010034NFD_LOG_INIT("UdpFactory");
35
36UdpFactory::UdpFactory(const std::string& defaultPort/* = "6363"*/)
37 : m_defaultPort(defaultPort)
38{
39}
40
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060041
42
43void
44UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
45{
46 using namespace boost::asio::ip;
47
48 static const address_v4 ALL_V4_ENDPOINT(address_v4::from_string("0.0.0.0"));
49 static const address_v6 ALL_V6_ENDPOINT(address_v6::from_string("::"));
50
51 const address& address = endpoint.address();
52
53 if (address.is_v4() && address == ALL_V4_ENDPOINT)
54 {
55 prohibitAllIpv4Endpoints(endpoint.port());
56 }
57 else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
58 {
59 prohibitAllIpv6Endpoints(endpoint.port());
60 }
61
62 NFD_LOG_TRACE("prohibiting UDP " <<
63 endpoint.address().to_string() << ":" << endpoint.port());
64
65 m_prohibitedEndpoints.insert(endpoint);
66}
67
68void
69UdpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
70{
71 using namespace boost::asio::ip;
72
73 static const address_v4 INVALID_BROADCAST(address_v4::from_string("0.0.0.0"));
74
75 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
76
77 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
78 i != nicList.end();
79 ++i)
80 {
81 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
82 const std::vector<address_v4>& ipv4Addresses = nic->ipv4Addresses;
83
84 for (std::vector<address_v4>::const_iterator j = ipv4Addresses.begin();
85 j != ipv4Addresses.end();
86 ++j)
87 {
88 prohibitEndpoint(udp::Endpoint(*j, port));
89 }
90
91 if (nic->isBroadcastCapable() && nic->broadcastAddress != INVALID_BROADCAST)
92 {
93 NFD_LOG_TRACE("prohibiting broadcast address: " << nic->broadcastAddress.to_string());
94 prohibitEndpoint(udp::Endpoint(nic->broadcastAddress, port));
95 }
96 }
97
98 prohibitEndpoint(udp::Endpoint(address::from_string("255.255.255.255"), port));
99}
100
101void
102UdpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
103{
104 using namespace boost::asio::ip;
105
106 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
107
108 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
109 i != nicList.end();
110 ++i)
111 {
112 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
113 const std::vector<address_v6>& ipv6Addresses = nic->ipv6Addresses;
114
115 for (std::vector<address_v6>::const_iterator j = ipv6Addresses.begin();
116 j != ipv6Addresses.end();
117 ++j)
118 {
119 prohibitEndpoint(udp::Endpoint(*j, port));
120 }
121 }
122}
123
Giulio Grassi624f6c62014-02-18 19:42:14 +0100124shared_ptr<UdpChannel>
125UdpFactory::createChannel(const udp::Endpoint& endpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700126 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100127{
128 NFD_LOG_DEBUG("Creating unicast " << endpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700129
Giulio Grassi624f6c62014-02-18 19:42:14 +0100130 shared_ptr<UdpChannel> channel = findChannel(endpoint);
131 if (static_cast<bool>(channel))
132 return channel;
133
Junxiao Shi79494162014-04-02 18:25:11 -0700134
135 //checking if the endpoint is already in use for multicast face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100136 shared_ptr<MulticastUdpFace> multicast = findMulticastFace(endpoint);
137 if (static_cast<bool>(multicast))
138 throw Error("Cannot create the requested UDP unicast channel, local "
139 "endpoint is already allocated for a UDP multicast face");
Junxiao Shi79494162014-04-02 18:25:11 -0700140
Giulio Grassi624f6c62014-02-18 19:42:14 +0100141 if (endpoint.address().is_multicast()) {
142 throw Error("This method is only for unicast channel. The provided "
143 "endpoint is multicast. Use createMulticastFace to "
144 "create a multicast face");
145 }
146
147 channel = make_shared<UdpChannel>(boost::cref(endpoint),
148 timeout);
149 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600150 prohibitEndpoint(endpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100151
152 return channel;
153}
154
155shared_ptr<UdpChannel>
156UdpFactory::createChannel(const std::string& localHost,
157 const std::string& localPort,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700158 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100159{
160 return createChannel(UdpResolver::syncResolve(localHost, localPort),
161 timeout);
162}
163
164shared_ptr<MulticastUdpFace>
165UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700166 const udp::Endpoint& multicastEndpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100167{
168 //checking if the local and musticast endpoint are already in use for a multicast face
169 shared_ptr<MulticastUdpFace> multicastFace = findMulticastFace(localEndpoint);
170 if (static_cast<bool>(multicastFace)) {
171 if (multicastFace->getMulticastGroup() == multicastEndpoint)
172 return multicastFace;
173 else
174 throw Error("Cannot create the requested UDP multicast face, local "
175 "endpoint is already allocated for a UDP multicast face "
176 "on a different multicast group");
177 }
Junxiao Shi79494162014-04-02 18:25:11 -0700178
Giulio Grassi624f6c62014-02-18 19:42:14 +0100179 //checking if the local endpoint is already in use for an unicast channel
180 shared_ptr<UdpChannel> unicast = findChannel(localEndpoint);
181 if (static_cast<bool>(unicast)) {
182 throw Error("Cannot create the requested UDP multicast face, local "
183 "endpoint is already allocated for a UDP unicast channel");
184 }
185
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600186 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
187 throw Error("Cannot create the requested UDP multicast face, "
188 "remote endpoint is owned by this NFD instance");
189 }
190
Giulio Grassi624f6c62014-02-18 19:42:14 +0100191 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
192 throw Error("IPv6 multicast is not supported yet. Please provide an IPv4 address");
193 }
Junxiao Shi79494162014-04-02 18:25:11 -0700194
Giulio Grassi624f6c62014-02-18 19:42:14 +0100195 if (localEndpoint.port() != multicastEndpoint.port()) {
196 throw Error("Cannot create the requested UDP multicast face, "
197 "both endpoints should have the same port number. ");
198 }
199
200 if (!multicastEndpoint.address().is_multicast()) {
201 throw Error("Cannot create the requested UDP multicast face, "
202 "the multicast group given as input is not a multicast address");
203 }
204
205 shared_ptr<ip::udp::socket> clientSocket =
206 make_shared<ip::udp::socket>(boost::ref(getGlobalIoService()));
Junxiao Shi79494162014-04-02 18:25:11 -0700207
Giulio Grassi624f6c62014-02-18 19:42:14 +0100208 clientSocket->open(multicastEndpoint.protocol());
209
210 clientSocket->set_option(ip::udp::socket::reuse_address(true));
211
212 try {
213 clientSocket->bind(multicastEndpoint);
214
215 if (localEndpoint.address() != ip::address::from_string("0.0.0.0")) {
216 clientSocket->set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
217 }
218 clientSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
219 localEndpoint.address().to_v4()));
220 }
221 catch (boost::system::system_error& e) {
222 std::stringstream msg;
223 msg << "Failed to properly configure the socket, check the address (" << e.what() << ")";
224 throw Error(msg.str());
225 }
226
227 clientSocket->set_option(ip::multicast::enable_loopback(false));
228
Junxiao Shi79494162014-04-02 18:25:11 -0700229 multicastFace = make_shared<MulticastUdpFace>(boost::cref(clientSocket), localEndpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100230 multicastFace->onFail += bind(&UdpFactory::afterFaceFailed, this, localEndpoint);
231
232 m_multicastFaces[localEndpoint] = multicastFace;
233
234 return multicastFace;
235}
236
237shared_ptr<MulticastUdpFace>
238UdpFactory::createMulticastFace(const std::string& localIp,
239 const std::string& multicastIp,
240 const std::string& multicastPort)
241{
Junxiao Shi79494162014-04-02 18:25:11 -0700242
Giulio Grassi624f6c62014-02-18 19:42:14 +0100243 return createMulticastFace(UdpResolver::syncResolve(localIp,
244 multicastPort),
245 UdpResolver::syncResolve(multicastIp,
246 multicastPort));
247}
248
249void
250UdpFactory::createFace(const FaceUri& uri,
251 const FaceCreatedCallback& onCreated,
252 const FaceConnectFailedCallback& onConnectFailed)
253{
254 resolver::AddressSelector addressSelector = resolver::AnyAddress();
255 if (uri.getScheme() == "udp4")
256 addressSelector = resolver::Ipv4Address();
257 else if (uri.getScheme() == "udp6")
258 addressSelector = resolver::Ipv6Address();
Junxiao Shi79494162014-04-02 18:25:11 -0700259
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600260 if (!uri.getPath().empty())
261 {
262 onConnectFailed("Invalid URI");
263 }
264
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700265 UdpResolver::asyncResolve(uri.getHost(),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100266 uri.getPort().empty() ? m_defaultPort : uri.getPort(),
267 bind(&UdpFactory::continueCreateFaceAfterResolve, this, _1,
268 onCreated, onConnectFailed),
269 onConnectFailed,
270 addressSelector);
271
272}
273
274void
275UdpFactory::continueCreateFaceAfterResolve(const udp::Endpoint& endpoint,
276 const FaceCreatedCallback& onCreated,
277 const FaceConnectFailedCallback& onConnectFailed)
278{
279 if (endpoint.address().is_multicast()) {
280 onConnectFailed("The provided address is multicast. Please use createMulticastFace method");
281 return;
282 }
Junxiao Shi79494162014-04-02 18:25:11 -0700283
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600284 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
285 {
286 onConnectFailed("Requested endpoint is prohibited "
287 "(reserved by this NFD or disallowed by face management protocol)");
288 return;
289 }
290
Giulio Grassi624f6c62014-02-18 19:42:14 +0100291 // very simple logic for now
Junxiao Shi79494162014-04-02 18:25:11 -0700292
Giulio Grassi624f6c62014-02-18 19:42:14 +0100293 for (ChannelMap::iterator channel = m_channels.begin();
294 channel != m_channels.end();
295 ++channel)
296 {
297 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
298 (channel->first.address().is_v6() && endpoint.address().is_v6()))
299 {
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600300 channel->second->connect(endpoint, onCreated, onConnectFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100301 return;
302 }
303 }
304 onConnectFailed("No channels available to connect to "
305 + boost::lexical_cast<std::string>(endpoint));
306}
307
308shared_ptr<UdpChannel>
309UdpFactory::findChannel(const udp::Endpoint& localEndpoint)
310{
311 ChannelMap::iterator i = m_channels.find(localEndpoint);
312 if (i != m_channels.end())
313 return i->second;
314 else
315 return shared_ptr<UdpChannel>();
316}
317
318shared_ptr<MulticastUdpFace>
319UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint)
320{
321 MulticastFaceMap::iterator i = m_multicastFaces.find(localEndpoint);
322 if (i != m_multicastFaces.end())
323 return i->second;
324 else
325 return shared_ptr<MulticastUdpFace>();
326}
327
328void
329UdpFactory::afterFaceFailed(udp::Endpoint& endpoint)
330{
331 NFD_LOG_DEBUG("afterFaceFailed: " << endpoint);
332 m_multicastFaces.erase(endpoint);
333}
334
335
336} // namespace nfd