blob: 1a4ca01047307307e8fdf8761e7c4bb47a2ba74c [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedettoef04f272014-06-04 14:28:31 -06003 * 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 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
26#include "udp-factory.hpp"
27#include "core/global-io.hpp"
28#include "core/resolver.hpp"
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060029#include "core/network-interface.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010030
Giulio Grassi6d7176d2014-04-16 16:08:48 +020031#if defined(__linux__)
32#include <sys/socket.h>
33#endif
34
Giulio Grassi624f6c62014-02-18 19:42:14 +010035namespace nfd {
36
37using namespace boost::asio;
Junxiao Shi79494162014-04-02 18:25:11 -070038
Giulio Grassi624f6c62014-02-18 19:42:14 +010039NFD_LOG_INIT("UdpFactory");
40
41UdpFactory::UdpFactory(const std::string& defaultPort/* = "6363"*/)
42 : m_defaultPort(defaultPort)
43{
44}
45
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060046
47
48void
49UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
50{
51 using namespace boost::asio::ip;
52
53 static const address_v4 ALL_V4_ENDPOINT(address_v4::from_string("0.0.0.0"));
54 static const address_v6 ALL_V6_ENDPOINT(address_v6::from_string("::"));
55
56 const address& address = endpoint.address();
57
58 if (address.is_v4() && address == ALL_V4_ENDPOINT)
59 {
60 prohibitAllIpv4Endpoints(endpoint.port());
61 }
62 else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
63 {
64 prohibitAllIpv6Endpoints(endpoint.port());
65 }
66
67 NFD_LOG_TRACE("prohibiting UDP " <<
68 endpoint.address().to_string() << ":" << endpoint.port());
69
70 m_prohibitedEndpoints.insert(endpoint);
71}
72
73void
74UdpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
75{
76 using namespace boost::asio::ip;
77
78 static const address_v4 INVALID_BROADCAST(address_v4::from_string("0.0.0.0"));
79
80 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
81
82 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
83 i != nicList.end();
84 ++i)
85 {
86 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
87 const std::vector<address_v4>& ipv4Addresses = nic->ipv4Addresses;
88
89 for (std::vector<address_v4>::const_iterator j = ipv4Addresses.begin();
90 j != ipv4Addresses.end();
91 ++j)
92 {
93 prohibitEndpoint(udp::Endpoint(*j, port));
94 }
95
96 if (nic->isBroadcastCapable() && nic->broadcastAddress != INVALID_BROADCAST)
97 {
98 NFD_LOG_TRACE("prohibiting broadcast address: " << nic->broadcastAddress.to_string());
99 prohibitEndpoint(udp::Endpoint(nic->broadcastAddress, port));
100 }
101 }
102
103 prohibitEndpoint(udp::Endpoint(address::from_string("255.255.255.255"), port));
104}
105
106void
107UdpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
108{
109 using namespace boost::asio::ip;
110
111 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
112
113 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
114 i != nicList.end();
115 ++i)
116 {
117 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
118 const std::vector<address_v6>& ipv6Addresses = nic->ipv6Addresses;
119
120 for (std::vector<address_v6>::const_iterator j = ipv6Addresses.begin();
121 j != ipv6Addresses.end();
122 ++j)
123 {
124 prohibitEndpoint(udp::Endpoint(*j, port));
125 }
126 }
127}
128
Giulio Grassi624f6c62014-02-18 19:42:14 +0100129shared_ptr<UdpChannel>
130UdpFactory::createChannel(const udp::Endpoint& endpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700131 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100132{
133 NFD_LOG_DEBUG("Creating unicast " << endpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700134
Giulio Grassi624f6c62014-02-18 19:42:14 +0100135 shared_ptr<UdpChannel> channel = findChannel(endpoint);
136 if (static_cast<bool>(channel))
137 return channel;
138
Junxiao Shi79494162014-04-02 18:25:11 -0700139
140 //checking if the endpoint is already in use for multicast face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100141 shared_ptr<MulticastUdpFace> multicast = findMulticastFace(endpoint);
142 if (static_cast<bool>(multicast))
143 throw Error("Cannot create the requested UDP unicast channel, local "
144 "endpoint is already allocated for a UDP multicast face");
Junxiao Shi79494162014-04-02 18:25:11 -0700145
Giulio Grassi624f6c62014-02-18 19:42:14 +0100146 if (endpoint.address().is_multicast()) {
147 throw Error("This method is only for unicast channel. The provided "
148 "endpoint is multicast. Use createMulticastFace to "
149 "create a multicast face");
150 }
151
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700152 channel = make_shared<UdpChannel>(endpoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100153 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600154 prohibitEndpoint(endpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100155
156 return channel;
157}
158
159shared_ptr<UdpChannel>
160UdpFactory::createChannel(const std::string& localHost,
161 const std::string& localPort,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700162 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100163{
164 return createChannel(UdpResolver::syncResolve(localHost, localPort),
165 timeout);
166}
167
168shared_ptr<MulticastUdpFace>
169UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200170 const udp::Endpoint& multicastEndpoint,
171 const std::string& networkInterfaceName /* "" */)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100172{
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200173 //checking if the local and multicast endpoint are already in use for a multicast face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100174 shared_ptr<MulticastUdpFace> multicastFace = findMulticastFace(localEndpoint);
175 if (static_cast<bool>(multicastFace)) {
176 if (multicastFace->getMulticastGroup() == multicastEndpoint)
177 return multicastFace;
178 else
179 throw Error("Cannot create the requested UDP multicast face, local "
180 "endpoint is already allocated for a UDP multicast face "
181 "on a different multicast group");
182 }
Junxiao Shi79494162014-04-02 18:25:11 -0700183
Giulio Grassi624f6c62014-02-18 19:42:14 +0100184 //checking if the local endpoint is already in use for an unicast channel
185 shared_ptr<UdpChannel> unicast = findChannel(localEndpoint);
186 if (static_cast<bool>(unicast)) {
187 throw Error("Cannot create the requested UDP multicast face, local "
188 "endpoint is already allocated for a UDP unicast channel");
189 }
190
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600191 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
192 throw Error("Cannot create the requested UDP multicast face, "
193 "remote endpoint is owned by this NFD instance");
194 }
195
Giulio Grassi624f6c62014-02-18 19:42:14 +0100196 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
197 throw Error("IPv6 multicast is not supported yet. Please provide an IPv4 address");
198 }
Junxiao Shi79494162014-04-02 18:25:11 -0700199
Giulio Grassi624f6c62014-02-18 19:42:14 +0100200 if (localEndpoint.port() != multicastEndpoint.port()) {
201 throw Error("Cannot create the requested UDP multicast face, "
202 "both endpoints should have the same port number. ");
203 }
204
205 if (!multicastEndpoint.address().is_multicast()) {
206 throw Error("Cannot create the requested UDP multicast face, "
207 "the multicast group given as input is not a multicast address");
208 }
209
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700210 shared_ptr<ip::udp::socket> receiveSocket =
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700211 make_shared<ip::udp::socket>(ref(getGlobalIoService()));
Junxiao Shi79494162014-04-02 18:25:11 -0700212
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700213 shared_ptr<ip::udp::socket> sendSocket =
214 make_shared<ip::udp::socket>(ref(getGlobalIoService()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100215
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700216 receiveSocket->open(multicastEndpoint.protocol());
217 receiveSocket->set_option(ip::udp::socket::reuse_address(true));
218
219 sendSocket->open(multicastEndpoint.protocol());
220 sendSocket->set_option(ip::udp::socket::reuse_address(true));
221 sendSocket->set_option(ip::multicast::enable_loopback(false));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100222
223 try {
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700224 sendSocket->bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
225 receiveSocket->bind(multicastEndpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100226
227 if (localEndpoint.address() != ip::address::from_string("0.0.0.0")) {
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700228 sendSocket->set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100229 }
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700230 sendSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
231 localEndpoint.address().to_v4()));
232
233 receiveSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
234 localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100235 }
236 catch (boost::system::system_error& e) {
237 std::stringstream msg;
238 msg << "Failed to properly configure the socket, check the address (" << e.what() << ")";
239 throw Error(msg.str());
240 }
241
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200242#if defined(__linux__)
243 //On linux system, if there are more than one MulticastUdpFace for the same multicast group but
244 //bound on different network interfaces, the socket has to be bound with the specific interface
245 //using SO_BINDTODEVICE, otherwise the face will receive packets also from other interfaces.
246 //Without SO_BINDTODEVICE every MulticastUdpFace that have joined the same multicast group
247 //on different interfaces will receive the same packet.
248 //This applies only on linux, for OS X the ip::multicast::join_group is enough to get
249 //the desired behaviour
250 if (!networkInterfaceName.empty()) {
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700251 if (::setsockopt(receiveSocket->native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200252 networkInterfaceName.c_str(), networkInterfaceName.size()+1) == -1){
253 throw Error("Cannot bind multicast face to " + networkInterfaceName
254 + " make sure you have CAP_NET_RAW capability" );
255 }
256 }
257
258#endif
259
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700260 multicastFace = make_shared<MulticastUdpFace>(receiveSocket, sendSocket,
261 localEndpoint, multicastEndpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100262 multicastFace->onFail += bind(&UdpFactory::afterFaceFailed, this, localEndpoint);
263
264 m_multicastFaces[localEndpoint] = multicastFace;
265
266 return multicastFace;
267}
268
269shared_ptr<MulticastUdpFace>
270UdpFactory::createMulticastFace(const std::string& localIp,
271 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200272 const std::string& multicastPort,
273 const std::string& networkInterfaceName /* "" */)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100274{
Junxiao Shi79494162014-04-02 18:25:11 -0700275
Giulio Grassi624f6c62014-02-18 19:42:14 +0100276 return createMulticastFace(UdpResolver::syncResolve(localIp,
277 multicastPort),
278 UdpResolver::syncResolve(multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200279 multicastPort),
280 networkInterfaceName);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100281}
282
283void
284UdpFactory::createFace(const FaceUri& uri,
285 const FaceCreatedCallback& onCreated,
286 const FaceConnectFailedCallback& onConnectFailed)
287{
288 resolver::AddressSelector addressSelector = resolver::AnyAddress();
289 if (uri.getScheme() == "udp4")
290 addressSelector = resolver::Ipv4Address();
291 else if (uri.getScheme() == "udp6")
292 addressSelector = resolver::Ipv6Address();
Junxiao Shi79494162014-04-02 18:25:11 -0700293
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600294 if (!uri.getPath().empty())
295 {
296 onConnectFailed("Invalid URI");
297 }
298
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700299 UdpResolver::asyncResolve(uri.getHost(),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100300 uri.getPort().empty() ? m_defaultPort : uri.getPort(),
301 bind(&UdpFactory::continueCreateFaceAfterResolve, this, _1,
302 onCreated, onConnectFailed),
303 onConnectFailed,
304 addressSelector);
305
306}
307
308void
309UdpFactory::continueCreateFaceAfterResolve(const udp::Endpoint& endpoint,
310 const FaceCreatedCallback& onCreated,
311 const FaceConnectFailedCallback& onConnectFailed)
312{
313 if (endpoint.address().is_multicast()) {
314 onConnectFailed("The provided address is multicast. Please use createMulticastFace method");
315 return;
316 }
Junxiao Shi79494162014-04-02 18:25:11 -0700317
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600318 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
319 {
320 onConnectFailed("Requested endpoint is prohibited "
321 "(reserved by this NFD or disallowed by face management protocol)");
322 return;
323 }
324
Giulio Grassi624f6c62014-02-18 19:42:14 +0100325 // very simple logic for now
Junxiao Shi79494162014-04-02 18:25:11 -0700326
Giulio Grassi624f6c62014-02-18 19:42:14 +0100327 for (ChannelMap::iterator channel = m_channels.begin();
328 channel != m_channels.end();
329 ++channel)
330 {
331 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
332 (channel->first.address().is_v6() && endpoint.address().is_v6()))
333 {
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600334 channel->second->connect(endpoint, onCreated, onConnectFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100335 return;
336 }
337 }
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700338 onConnectFailed("No channels available to connect to " +
339 boost::lexical_cast<std::string>(endpoint));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100340}
341
342shared_ptr<UdpChannel>
343UdpFactory::findChannel(const udp::Endpoint& localEndpoint)
344{
345 ChannelMap::iterator i = m_channels.find(localEndpoint);
346 if (i != m_channels.end())
347 return i->second;
348 else
349 return shared_ptr<UdpChannel>();
350}
351
352shared_ptr<MulticastUdpFace>
353UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint)
354{
355 MulticastFaceMap::iterator i = m_multicastFaces.find(localEndpoint);
356 if (i != m_multicastFaces.end())
357 return i->second;
358 else
359 return shared_ptr<MulticastUdpFace>();
360}
361
362void
363UdpFactory::afterFaceFailed(udp::Endpoint& endpoint)
364{
365 NFD_LOG_DEBUG("afterFaceFailed: " << endpoint);
366 m_multicastFaces.erase(endpoint);
367}
368
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600369std::list<shared_ptr<const Channel> >
370UdpFactory::getChannels() const
371{
372 std::list<shared_ptr<const Channel> > channels;
373 for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
374 {
375 channels.push_back(i->second);
376 }
377
378 return channels;
379}
380
381
Giulio Grassi624f6c62014-02-18 19:42:14 +0100382
383} // namespace nfd