blob: 2a39e36b8e0fcc874d150d32718eb36cfd95992c [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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"
Yukai Tu0a49d342015-09-13 12:54:22 +080027#include "generic-link-service.hpp"
28#include "lp-face-wrapper.hpp"
29#include "multicast-udp-transport.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010030#include "core/global-io.hpp"
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060031#include "core/network-interface.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010032
Davide Pesavento292e5e12015-03-13 02:08:33 +010033#ifdef __linux__
34#include <cerrno> // for errno
35#include <cstring> // for std::strerror()
36#include <sys/socket.h> // for setsockopt()
Giulio Grassi6d7176d2014-04-16 16:08:48 +020037#endif
38
Giulio Grassi624f6c62014-02-18 19:42:14 +010039namespace nfd {
40
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020041namespace ip = boost::asio::ip;
Junxiao Shi79494162014-04-02 18:25:11 -070042
Giulio Grassi624f6c62014-02-18 19:42:14 +010043NFD_LOG_INIT("UdpFactory");
44
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060045void
46UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
47{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020048 if (endpoint.address().is_v4() &&
49 endpoint.address() == ip::address_v4::any()) {
50 prohibitAllIpv4Endpoints(endpoint.port());
51 }
52 else if (endpoint.address().is_v6() &&
53 endpoint.address() == ip::address_v6::any()) {
54 prohibitAllIpv6Endpoints(endpoint.port());
55 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060056
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080057 NFD_LOG_TRACE("prohibiting UDP " << endpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060058 m_prohibitedEndpoints.insert(endpoint);
59}
60
61void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020062UdpFactory::prohibitAllIpv4Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060063{
Davide Pesaventob499a602014-11-18 22:36:56 +010064 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020065 for (const auto& addr : nic.ipv4Addresses) {
66 if (addr != ip::address_v4::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080067 prohibitEndpoint(udp::Endpoint(addr, port));
68 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060069 }
70
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020071 if (nic.isBroadcastCapable() &&
72 nic.broadcastAddress != ip::address_v4::any()) {
Davide Pesaventob499a602014-11-18 22:36:56 +010073 prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port));
74 }
75 }
76
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020077 prohibitEndpoint(udp::Endpoint(ip::address_v4::broadcast(), port));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060078}
79
80void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020081UdpFactory::prohibitAllIpv6Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060082{
Davide Pesaventob499a602014-11-18 22:36:56 +010083 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020084 for (const auto& addr : nic.ipv6Addresses) {
85 if (addr != ip::address_v6::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080086 prohibitEndpoint(udp::Endpoint(addr, port));
87 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060088 }
Davide Pesaventob499a602014-11-18 22:36:56 +010089 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060090}
91
Giulio Grassi624f6c62014-02-18 19:42:14 +010092shared_ptr<UdpChannel>
93UdpFactory::createChannel(const udp::Endpoint& endpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070094 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010095{
Davide Pesaventob499a602014-11-18 22:36:56 +010096 NFD_LOG_DEBUG("Creating unicast channel " << endpoint);
Junxiao Shi79494162014-04-02 18:25:11 -070097
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020098 auto channel = findChannel(endpoint);
99 if (channel)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100100 return channel;
101
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200102 if (endpoint.address().is_multicast()) {
103 BOOST_THROW_EXCEPTION(Error("createChannel is only for unicast channels. The provided endpoint "
104 "is multicast. Use createMulticastFace to create a multicast face"));
105 }
106
Yukai Tu0a49d342015-09-13 12:54:22 +0800107 // check if the endpoint is already used by a multicast face
108 auto face = findMulticastFace(endpoint);
109 if (face) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700110 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP unicast channel, local "
111 "endpoint is already allocated for a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800112 }
Junxiao Shi79494162014-04-02 18:25:11 -0700113
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700114 channel = make_shared<UdpChannel>(endpoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100115 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600116 prohibitEndpoint(endpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100117
118 return channel;
119}
120
121shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200122UdpFactory::createChannel(const std::string& localIp, const std::string& localPort,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700123 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100124{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200125 udp::Endpoint endpoint(ip::address::from_string(localIp),
126 boost::lexical_cast<uint16_t>(localPort));
Alexander Afanasyev0e156df2015-01-26 22:33:43 -0800127 return createChannel(endpoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100128}
129
Yukai Tu0a49d342015-09-13 12:54:22 +0800130shared_ptr<face::LpFaceWrapper>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100131UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200132 const udp::Endpoint& multicastEndpoint,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100133 const std::string& networkInterfaceName/* = ""*/)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100134{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100135 // checking if the local and multicast endpoints are already in use for a multicast face
Yukai Tu0a49d342015-09-13 12:54:22 +0800136 auto face = findMulticastFace(localEndpoint);
137 if (face) {
138 if (face->getRemoteUri() == FaceUri(multicastEndpoint))
Davide Pesavento292e5e12015-03-13 02:08:33 +0100139 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100140 else
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700141 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
142 "endpoint is already allocated for a UDP multicast face "
143 "on a different multicast group"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100144 }
Junxiao Shi79494162014-04-02 18:25:11 -0700145
Davide Pesavento292e5e12015-03-13 02:08:33 +0100146 // checking if the local endpoint is already in use for a unicast channel
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200147 auto unicastCh = findChannel(localEndpoint);
148 if (unicastCh) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700149 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
150 "endpoint is already allocated for a UDP unicast channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100151 }
152
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600153 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700154 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
155 "remote endpoint is owned by this NFD instance"));
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600156 }
157
Giulio Grassi624f6c62014-02-18 19:42:14 +0100158 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700159 BOOST_THROW_EXCEPTION(Error("IPv6 multicast is not supported yet. Please provide an IPv4 "
160 "address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100161 }
Junxiao Shi79494162014-04-02 18:25:11 -0700162
Giulio Grassi624f6c62014-02-18 19:42:14 +0100163 if (localEndpoint.port() != multicastEndpoint.port()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700164 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
165 "both endpoints should have the same port number. "));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100166 }
167
168 if (!multicastEndpoint.address().is_multicast()) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700169 BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, "
170 "the multicast group given as input is not a multicast address"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100171 }
172
Davide Pesavento292e5e12015-03-13 02:08:33 +0100173 ip::udp::socket receiveSocket(getGlobalIoService());
174 receiveSocket.open(multicastEndpoint.protocol());
175 receiveSocket.set_option(ip::udp::socket::reuse_address(true));
176 receiveSocket.bind(multicastEndpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700177
Davide Pesavento292e5e12015-03-13 02:08:33 +0100178 ip::udp::socket sendSocket(getGlobalIoService());
179 sendSocket.open(multicastEndpoint.protocol());
180 sendSocket.set_option(ip::udp::socket::reuse_address(true));
181 sendSocket.set_option(ip::multicast::enable_loopback(false));
182 sendSocket.bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200183 if (localEndpoint.address() != ip::address_v4::any())
Davide Pesavento292e5e12015-03-13 02:08:33 +0100184 sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100185
Davide Pesavento292e5e12015-03-13 02:08:33 +0100186 sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
187 localEndpoint.address().to_v4()));
188 receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700189 localEndpoint.address().to_v4()));
190
Davide Pesavento292e5e12015-03-13 02:08:33 +0100191#ifdef __linux__
192 /*
193 * On Linux, if there is more than one MulticastUdpFace for the same multicast
194 * group but they are bound to different network interfaces, the socket needs
195 * to be bound to the specific interface using SO_BINDTODEVICE, otherwise the
196 * face will receive all packets sent to the other interfaces as well.
197 * This happens only on Linux. On OS X, the ip::multicast::join_group option
198 * is enough to get the desired behaviour.
199 */
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200200 if (!networkInterfaceName.empty()) {
Davide Pesavento292e5e12015-03-13 02:08:33 +0100201 if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
202 networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700203 BOOST_THROW_EXCEPTION(Error("Cannot bind multicast face to " + networkInterfaceName +
204 ": " + std::strerror(errno)));
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200205 }
206 }
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200207#endif
208
Yukai Tu0a49d342015-09-13 12:54:22 +0800209 auto linkService = make_unique<face::GenericLinkService>();
210 auto transport = make_unique<face::MulticastUdpTransport>(localEndpoint, multicastEndpoint,
211 std::move(receiveSocket),
212 std::move(sendSocket));
213 auto lpFace = make_unique<face::LpFace>(std::move(linkService), std::move(transport));
214 face = make_shared<face::LpFaceWrapper>(std::move(lpFace));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700215
Davide Pesavento292e5e12015-03-13 02:08:33 +0100216 face->onFail.connectSingleShot([this, localEndpoint] (const std::string& reason) {
217 m_multicastFaces.erase(localEndpoint);
218 });
219 m_multicastFaces[localEndpoint] = face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100220
Davide Pesavento292e5e12015-03-13 02:08:33 +0100221 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100222}
223
Yukai Tu0a49d342015-09-13 12:54:22 +0800224shared_ptr<face::LpFaceWrapper>
Giulio Grassi624f6c62014-02-18 19:42:14 +0100225UdpFactory::createMulticastFace(const std::string& localIp,
226 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200227 const std::string& multicastPort,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100228 const std::string& networkInterfaceName/* = ""*/)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100229{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100230 udp::Endpoint localEndpoint(ip::address::from_string(localIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700231 boost::lexical_cast<uint16_t>(multicastPort));
Davide Pesavento292e5e12015-03-13 02:08:33 +0100232 udp::Endpoint multicastEndpoint(ip::address::from_string(multicastIp),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700233 boost::lexical_cast<uint16_t>(multicastPort));
Chengyu Fan4381fb62015-01-14 11:37:04 -0700234 return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100235}
236
237void
238UdpFactory::createFace(const FaceUri& uri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800239 ndn::nfd::FacePersistency persistency,
Giulio Grassi624f6c62014-02-18 19:42:14 +0100240 const FaceCreatedCallback& onCreated,
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200241 const FaceCreationFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100242{
Chengyu Fan4381fb62015-01-14 11:37:04 -0700243 BOOST_ASSERT(uri.isCanonical());
Davide Pesavento292e5e12015-03-13 02:08:33 +0100244
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200245 if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
246 BOOST_THROW_EXCEPTION(Error("UdpFactory::createFace does not support FACE_PERSISTENCY_ON_DEMAND"));
247 }
248
249 udp::Endpoint endpoint(ip::address::from_string(uri.getHost()),
250 boost::lexical_cast<uint16_t>(uri.getPort()));
Junxiao Shi79494162014-04-02 18:25:11 -0700251
Giulio Grassi624f6c62014-02-18 19:42:14 +0100252 if (endpoint.address().is_multicast()) {
253 onConnectFailed("The provided address is multicast. Please use createMulticastFace method");
254 return;
255 }
Junxiao Shi79494162014-04-02 18:25:11 -0700256
Davide Pesavento292e5e12015-03-13 02:08:33 +0100257 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
258 onConnectFailed("Requested endpoint is prohibited "
259 "(reserved by this NFD or disallowed by face management protocol)");
260 return;
261 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600262
Giulio Grassi624f6c62014-02-18 19:42:14 +0100263 // very simple logic for now
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200264 for (const auto& i : m_channels) {
265 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
266 (i.first.address().is_v6() && endpoint.address().is_v6())) {
267 i.second->connect(endpoint, persistency, onCreated, onConnectFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100268 return;
269 }
270 }
Davide Pesavento292e5e12015-03-13 02:08:33 +0100271
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200272 onConnectFailed("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
273}
274
275std::vector<shared_ptr<const Channel>>
276UdpFactory::getChannels() const
277{
278 std::vector<shared_ptr<const Channel>> channels;
279 channels.reserve(m_channels.size());
280
281 for (const auto& i : m_channels)
282 channels.push_back(i.second);
283
284 return channels;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100285}
286
287shared_ptr<UdpChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200288UdpFactory::findChannel(const udp::Endpoint& localEndpoint) const
Giulio Grassi624f6c62014-02-18 19:42:14 +0100289{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200290 auto i = m_channels.find(localEndpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100291 if (i != m_channels.end())
292 return i->second;
293 else
Yukai Tu0a49d342015-09-13 12:54:22 +0800294 return nullptr;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100295}
296
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200297shared_ptr<face::LpFaceWrapper>
298UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint) const
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600299{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200300 auto i = m_multicastFaces.find(localEndpoint);
301 if (i != m_multicastFaces.end())
302 return i->second;
303 else
304 return nullptr;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600305}
306
Giulio Grassi624f6c62014-02-18 19:42:14 +0100307} // namespace nfd