blob: dcb819d35d8a487fbaf1bb1c0b156183d841c917 [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 -060046void
47UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
48{
49 using namespace boost::asio::ip;
50
51 static const address_v4 ALL_V4_ENDPOINT(address_v4::from_string("0.0.0.0"));
52 static const address_v6 ALL_V6_ENDPOINT(address_v6::from_string("::"));
53
54 const address& address = endpoint.address();
55
56 if (address.is_v4() && address == ALL_V4_ENDPOINT)
57 {
58 prohibitAllIpv4Endpoints(endpoint.port());
59 }
60 else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
61 {
62 prohibitAllIpv6Endpoints(endpoint.port());
63 }
64
65 NFD_LOG_TRACE("prohibiting UDP " <<
66 endpoint.address().to_string() << ":" << endpoint.port());
67
68 m_prohibitedEndpoints.insert(endpoint);
69}
70
71void
72UdpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
73{
74 using namespace boost::asio::ip;
75
76 static const address_v4 INVALID_BROADCAST(address_v4::from_string("0.0.0.0"));
77
Davide Pesaventob499a602014-11-18 22:36:56 +010078 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
79 for (const address_v4& addr : nic.ipv4Addresses) {
80 prohibitEndpoint(udp::Endpoint(addr, port));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060081 }
82
Davide Pesaventob499a602014-11-18 22:36:56 +010083 if (nic.isBroadcastCapable() && nic.broadcastAddress != INVALID_BROADCAST)
84 {
85 NFD_LOG_TRACE("prohibiting broadcast address: " << nic.broadcastAddress.to_string());
86 prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port));
87 }
88 }
89
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060090 prohibitEndpoint(udp::Endpoint(address::from_string("255.255.255.255"), port));
91}
92
93void
94UdpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
95{
96 using namespace boost::asio::ip;
97
Davide Pesaventob499a602014-11-18 22:36:56 +010098 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
99 for (const address_v6& addr : nic.ipv6Addresses) {
100 prohibitEndpoint(udp::Endpoint(addr, port));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600101 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100102 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600103}
104
Giulio Grassi624f6c62014-02-18 19:42:14 +0100105shared_ptr<UdpChannel>
106UdpFactory::createChannel(const udp::Endpoint& endpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700107 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100108{
Davide Pesaventob499a602014-11-18 22:36:56 +0100109 NFD_LOG_DEBUG("Creating unicast channel " << endpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700110
Giulio Grassi624f6c62014-02-18 19:42:14 +0100111 shared_ptr<UdpChannel> channel = findChannel(endpoint);
112 if (static_cast<bool>(channel))
113 return channel;
114
Junxiao Shi79494162014-04-02 18:25:11 -0700115 //checking if the endpoint is already in use for multicast face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100116 shared_ptr<MulticastUdpFace> multicast = findMulticastFace(endpoint);
117 if (static_cast<bool>(multicast))
118 throw Error("Cannot create the requested UDP unicast channel, local "
119 "endpoint is already allocated for a UDP multicast face");
Junxiao Shi79494162014-04-02 18:25:11 -0700120
Giulio Grassi624f6c62014-02-18 19:42:14 +0100121 if (endpoint.address().is_multicast()) {
122 throw Error("This method is only for unicast channel. The provided "
123 "endpoint is multicast. Use createMulticastFace to "
124 "create a multicast face");
125 }
126
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700127 channel = make_shared<UdpChannel>(endpoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100128 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600129 prohibitEndpoint(endpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100130
131 return channel;
132}
133
134shared_ptr<UdpChannel>
135UdpFactory::createChannel(const std::string& localHost,
136 const std::string& localPort,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700137 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100138{
Davide Pesaventob499a602014-11-18 22:36:56 +0100139 return createChannel(UdpResolver::syncResolve(localHost, localPort), timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100140}
141
142shared_ptr<MulticastUdpFace>
143UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200144 const udp::Endpoint& multicastEndpoint,
145 const std::string& networkInterfaceName /* "" */)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100146{
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200147 //checking if the local and multicast endpoint are already in use for a multicast face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100148 shared_ptr<MulticastUdpFace> multicastFace = findMulticastFace(localEndpoint);
149 if (static_cast<bool>(multicastFace)) {
150 if (multicastFace->getMulticastGroup() == multicastEndpoint)
151 return multicastFace;
152 else
153 throw Error("Cannot create the requested UDP multicast face, local "
154 "endpoint is already allocated for a UDP multicast face "
155 "on a different multicast group");
156 }
Junxiao Shi79494162014-04-02 18:25:11 -0700157
Giulio Grassi624f6c62014-02-18 19:42:14 +0100158 //checking if the local endpoint is already in use for an unicast channel
159 shared_ptr<UdpChannel> unicast = findChannel(localEndpoint);
160 if (static_cast<bool>(unicast)) {
161 throw Error("Cannot create the requested UDP multicast face, local "
162 "endpoint is already allocated for a UDP unicast channel");
163 }
164
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600165 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
166 throw Error("Cannot create the requested UDP multicast face, "
167 "remote endpoint is owned by this NFD instance");
168 }
169
Giulio Grassi624f6c62014-02-18 19:42:14 +0100170 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
171 throw Error("IPv6 multicast is not supported yet. Please provide an IPv4 address");
172 }
Junxiao Shi79494162014-04-02 18:25:11 -0700173
Giulio Grassi624f6c62014-02-18 19:42:14 +0100174 if (localEndpoint.port() != multicastEndpoint.port()) {
175 throw Error("Cannot create the requested UDP multicast face, "
176 "both endpoints should have the same port number. ");
177 }
178
179 if (!multicastEndpoint.address().is_multicast()) {
180 throw Error("Cannot create the requested UDP multicast face, "
181 "the multicast group given as input is not a multicast address");
182 }
183
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700184 shared_ptr<ip::udp::socket> receiveSocket =
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700185 make_shared<ip::udp::socket>(ref(getGlobalIoService()));
Junxiao Shi79494162014-04-02 18:25:11 -0700186
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700187 shared_ptr<ip::udp::socket> sendSocket =
188 make_shared<ip::udp::socket>(ref(getGlobalIoService()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100189
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700190 receiveSocket->open(multicastEndpoint.protocol());
191 receiveSocket->set_option(ip::udp::socket::reuse_address(true));
192
193 sendSocket->open(multicastEndpoint.protocol());
194 sendSocket->set_option(ip::udp::socket::reuse_address(true));
195 sendSocket->set_option(ip::multicast::enable_loopback(false));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100196
197 try {
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700198 sendSocket->bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
199 receiveSocket->bind(multicastEndpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100200
201 if (localEndpoint.address() != ip::address::from_string("0.0.0.0")) {
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700202 sendSocket->set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100203 }
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700204 sendSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
205 localEndpoint.address().to_v4()));
206
207 receiveSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
208 localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100209 }
210 catch (boost::system::system_error& e) {
211 std::stringstream msg;
212 msg << "Failed to properly configure the socket, check the address (" << e.what() << ")";
213 throw Error(msg.str());
214 }
215
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200216#if defined(__linux__)
217 //On linux system, if there are more than one MulticastUdpFace for the same multicast group but
218 //bound on different network interfaces, the socket has to be bound with the specific interface
219 //using SO_BINDTODEVICE, otherwise the face will receive packets also from other interfaces.
220 //Without SO_BINDTODEVICE every MulticastUdpFace that have joined the same multicast group
221 //on different interfaces will receive the same packet.
222 //This applies only on linux, for OS X the ip::multicast::join_group is enough to get
223 //the desired behaviour
224 if (!networkInterfaceName.empty()) {
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700225 if (::setsockopt(receiveSocket->native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200226 networkInterfaceName.c_str(), networkInterfaceName.size()+1) == -1){
227 throw Error("Cannot bind multicast face to " + networkInterfaceName
228 + " make sure you have CAP_NET_RAW capability" );
229 }
230 }
231
232#endif
233
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700234 multicastFace = make_shared<MulticastUdpFace>(receiveSocket, sendSocket,
235 localEndpoint, multicastEndpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100236 multicastFace->onFail += bind(&UdpFactory::afterFaceFailed, this, localEndpoint);
237
238 m_multicastFaces[localEndpoint] = multicastFace;
239
240 return multicastFace;
241}
242
243shared_ptr<MulticastUdpFace>
244UdpFactory::createMulticastFace(const std::string& localIp,
245 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200246 const std::string& multicastPort,
247 const std::string& networkInterfaceName /* "" */)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100248{
Junxiao Shi79494162014-04-02 18:25:11 -0700249
Giulio Grassi624f6c62014-02-18 19:42:14 +0100250 return createMulticastFace(UdpResolver::syncResolve(localIp,
251 multicastPort),
252 UdpResolver::syncResolve(multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200253 multicastPort),
254 networkInterfaceName);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100255}
256
257void
258UdpFactory::createFace(const FaceUri& uri,
259 const FaceCreatedCallback& onCreated,
260 const FaceConnectFailedCallback& onConnectFailed)
261{
262 resolver::AddressSelector addressSelector = resolver::AnyAddress();
263 if (uri.getScheme() == "udp4")
264 addressSelector = resolver::Ipv4Address();
265 else if (uri.getScheme() == "udp6")
266 addressSelector = resolver::Ipv6Address();
Junxiao Shi79494162014-04-02 18:25:11 -0700267
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700268 if (!uri.getPath().empty() && uri.getPath() != "/")
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600269 {
270 onConnectFailed("Invalid URI");
271 }
272
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700273 UdpResolver::asyncResolve(uri.getHost(),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100274 uri.getPort().empty() ? m_defaultPort : uri.getPort(),
275 bind(&UdpFactory::continueCreateFaceAfterResolve, this, _1,
276 onCreated, onConnectFailed),
277 onConnectFailed,
278 addressSelector);
279
280}
281
282void
283UdpFactory::continueCreateFaceAfterResolve(const udp::Endpoint& endpoint,
284 const FaceCreatedCallback& onCreated,
285 const FaceConnectFailedCallback& onConnectFailed)
286{
287 if (endpoint.address().is_multicast()) {
288 onConnectFailed("The provided address is multicast. Please use createMulticastFace method");
289 return;
290 }
Junxiao Shi79494162014-04-02 18:25:11 -0700291
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600292 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
293 {
294 onConnectFailed("Requested endpoint is prohibited "
295 "(reserved by this NFD or disallowed by face management protocol)");
296 return;
297 }
298
Giulio Grassi624f6c62014-02-18 19:42:14 +0100299 // very simple logic for now
Junxiao Shi79494162014-04-02 18:25:11 -0700300
Giulio Grassi624f6c62014-02-18 19:42:14 +0100301 for (ChannelMap::iterator channel = m_channels.begin();
302 channel != m_channels.end();
303 ++channel)
304 {
305 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
306 (channel->first.address().is_v6() && endpoint.address().is_v6()))
307 {
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600308 channel->second->connect(endpoint, onCreated, onConnectFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100309 return;
310 }
311 }
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700312 onConnectFailed("No channels available to connect to " +
313 boost::lexical_cast<std::string>(endpoint));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100314}
315
316shared_ptr<UdpChannel>
317UdpFactory::findChannel(const udp::Endpoint& localEndpoint)
318{
319 ChannelMap::iterator i = m_channels.find(localEndpoint);
320 if (i != m_channels.end())
321 return i->second;
322 else
323 return shared_ptr<UdpChannel>();
324}
325
326shared_ptr<MulticastUdpFace>
327UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint)
328{
329 MulticastFaceMap::iterator i = m_multicastFaces.find(localEndpoint);
330 if (i != m_multicastFaces.end())
331 return i->second;
332 else
333 return shared_ptr<MulticastUdpFace>();
334}
335
336void
337UdpFactory::afterFaceFailed(udp::Endpoint& endpoint)
338{
339 NFD_LOG_DEBUG("afterFaceFailed: " << endpoint);
340 m_multicastFaces.erase(endpoint);
341}
342
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600343std::list<shared_ptr<const Channel> >
344UdpFactory::getChannels() const
345{
346 std::list<shared_ptr<const Channel> > channels;
347 for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
348 {
349 channels.push_back(i->second);
350 }
351
352 return channels;
353}
354
355
Giulio Grassi624f6c62014-02-18 19:42:14 +0100356
357} // namespace nfd