blob: 4ef4e17eaf25eda6fab79bd983cc03d7cb5e6011 [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"
27#include "core/global-io.hpp"
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060028#include "core/network-interface.hpp"
Chengyu Fan4381fb62015-01-14 11:37:04 -070029#include <ndn-cxx/util/dns.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
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080041static const boost::asio::ip::address_v4 ALL_V4_ENDPOINT(
42 boost::asio::ip::address_v4::from_string("0.0.0.0"));
43
44static const boost::asio::ip::address_v6 ALL_V6_ENDPOINT(
45 boost::asio::ip::address_v6::from_string("::"));
46
Giulio Grassi624f6c62014-02-18 19:42:14 +010047UdpFactory::UdpFactory(const std::string& defaultPort/* = "6363"*/)
48 : m_defaultPort(defaultPort)
49{
50}
51
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060052void
53UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
54{
55 using namespace boost::asio::ip;
56
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060057 const address& address = endpoint.address();
58
59 if (address.is_v4() && address == ALL_V4_ENDPOINT)
60 {
61 prohibitAllIpv4Endpoints(endpoint.port());
62 }
63 else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
64 {
65 prohibitAllIpv6Endpoints(endpoint.port());
66 }
67
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080068 NFD_LOG_TRACE("prohibiting UDP " << endpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060069
70 m_prohibitedEndpoints.insert(endpoint);
71}
72
73void
74UdpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
75{
76 using namespace boost::asio::ip;
77
Davide Pesaventob499a602014-11-18 22:36:56 +010078 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
79 for (const address_v4& addr : nic.ipv4Addresses) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080080 if (addr != ALL_V4_ENDPOINT) {
81 prohibitEndpoint(udp::Endpoint(addr, port));
82 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060083 }
84
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080085 if (nic.isBroadcastCapable() && nic.broadcastAddress != ALL_V4_ENDPOINT)
Davide Pesaventob499a602014-11-18 22:36:56 +010086 {
87 NFD_LOG_TRACE("prohibiting broadcast address: " << nic.broadcastAddress.to_string());
88 prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port));
89 }
90 }
91
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060092 prohibitEndpoint(udp::Endpoint(address::from_string("255.255.255.255"), port));
93}
94
95void
96UdpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
97{
98 using namespace boost::asio::ip;
99
Davide Pesaventob499a602014-11-18 22:36:56 +0100100 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
101 for (const address_v6& addr : nic.ipv6Addresses) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800102 if (addr != ALL_V6_ENDPOINT) {
103 prohibitEndpoint(udp::Endpoint(addr, port));
104 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600105 }
Davide Pesaventob499a602014-11-18 22:36:56 +0100106 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600107}
108
Giulio Grassi624f6c62014-02-18 19:42:14 +0100109shared_ptr<UdpChannel>
110UdpFactory::createChannel(const udp::Endpoint& endpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700111 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100112{
Davide Pesaventob499a602014-11-18 22:36:56 +0100113 NFD_LOG_DEBUG("Creating unicast channel " << endpoint);
Junxiao Shi79494162014-04-02 18:25:11 -0700114
Giulio Grassi624f6c62014-02-18 19:42:14 +0100115 shared_ptr<UdpChannel> channel = findChannel(endpoint);
116 if (static_cast<bool>(channel))
117 return channel;
118
Junxiao Shi79494162014-04-02 18:25:11 -0700119 //checking if the endpoint is already in use for multicast face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100120 shared_ptr<MulticastUdpFace> multicast = findMulticastFace(endpoint);
121 if (static_cast<bool>(multicast))
122 throw Error("Cannot create the requested UDP unicast channel, local "
123 "endpoint is already allocated for a UDP multicast face");
Junxiao Shi79494162014-04-02 18:25:11 -0700124
Giulio Grassi624f6c62014-02-18 19:42:14 +0100125 if (endpoint.address().is_multicast()) {
126 throw Error("This method is only for unicast channel. The provided "
127 "endpoint is multicast. Use createMulticastFace to "
128 "create a multicast face");
129 }
130
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700131 channel = make_shared<UdpChannel>(endpoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100132 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600133 prohibitEndpoint(endpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100134
135 return channel;
136}
137
138shared_ptr<UdpChannel>
139UdpFactory::createChannel(const std::string& localHost,
140 const std::string& localPort,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700141 const time::seconds& timeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100142{
Chengyu Fan4381fb62015-01-14 11:37:04 -0700143 udp::Endpoint endPoint(ndn::dns::syncResolve(localHost, getGlobalIoService()),
144 boost::lexical_cast<uint16_t>(localPort));
145 return createChannel(endPoint, timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100146}
147
148shared_ptr<MulticastUdpFace>
149UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200150 const udp::Endpoint& multicastEndpoint,
151 const std::string& networkInterfaceName /* "" */)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100152{
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200153 //checking if the local and multicast endpoint are already in use for a multicast face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100154 shared_ptr<MulticastUdpFace> multicastFace = findMulticastFace(localEndpoint);
155 if (static_cast<bool>(multicastFace)) {
156 if (multicastFace->getMulticastGroup() == multicastEndpoint)
157 return multicastFace;
158 else
159 throw Error("Cannot create the requested UDP multicast face, local "
160 "endpoint is already allocated for a UDP multicast face "
161 "on a different multicast group");
162 }
Junxiao Shi79494162014-04-02 18:25:11 -0700163
Giulio Grassi624f6c62014-02-18 19:42:14 +0100164 //checking if the local endpoint is already in use for an unicast channel
165 shared_ptr<UdpChannel> unicast = findChannel(localEndpoint);
166 if (static_cast<bool>(unicast)) {
167 throw Error("Cannot create the requested UDP multicast face, local "
168 "endpoint is already allocated for a UDP unicast channel");
169 }
170
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600171 if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) {
172 throw Error("Cannot create the requested UDP multicast face, "
173 "remote endpoint is owned by this NFD instance");
174 }
175
Giulio Grassi624f6c62014-02-18 19:42:14 +0100176 if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) {
177 throw Error("IPv6 multicast is not supported yet. Please provide an IPv4 address");
178 }
Junxiao Shi79494162014-04-02 18:25:11 -0700179
Giulio Grassi624f6c62014-02-18 19:42:14 +0100180 if (localEndpoint.port() != multicastEndpoint.port()) {
181 throw Error("Cannot create the requested UDP multicast face, "
182 "both endpoints should have the same port number. ");
183 }
184
185 if (!multicastEndpoint.address().is_multicast()) {
186 throw Error("Cannot create the requested UDP multicast face, "
187 "the multicast group given as input is not a multicast address");
188 }
189
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700190 shared_ptr<ip::udp::socket> receiveSocket =
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700191 make_shared<ip::udp::socket>(ref(getGlobalIoService()));
Junxiao Shi79494162014-04-02 18:25:11 -0700192
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700193 shared_ptr<ip::udp::socket> sendSocket =
194 make_shared<ip::udp::socket>(ref(getGlobalIoService()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100195
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700196 receiveSocket->open(multicastEndpoint.protocol());
197 receiveSocket->set_option(ip::udp::socket::reuse_address(true));
198
199 sendSocket->open(multicastEndpoint.protocol());
200 sendSocket->set_option(ip::udp::socket::reuse_address(true));
201 sendSocket->set_option(ip::multicast::enable_loopback(false));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100202
203 try {
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700204 sendSocket->bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
205 receiveSocket->bind(multicastEndpoint);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100206
207 if (localEndpoint.address() != ip::address::from_string("0.0.0.0")) {
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700208 sendSocket->set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100209 }
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700210 sendSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
211 localEndpoint.address().to_v4()));
212
213 receiveSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
214 localEndpoint.address().to_v4()));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100215 }
216 catch (boost::system::system_error& e) {
217 std::stringstream msg;
218 msg << "Failed to properly configure the socket, check the address (" << e.what() << ")";
219 throw Error(msg.str());
220 }
221
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200222#if defined(__linux__)
223 //On linux system, if there are more than one MulticastUdpFace for the same multicast group but
224 //bound on different network interfaces, the socket has to be bound with the specific interface
225 //using SO_BINDTODEVICE, otherwise the face will receive packets also from other interfaces.
226 //Without SO_BINDTODEVICE every MulticastUdpFace that have joined the same multicast group
227 //on different interfaces will receive the same packet.
228 //This applies only on linux, for OS X the ip::multicast::join_group is enough to get
229 //the desired behaviour
230 if (!networkInterfaceName.empty()) {
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700231 if (::setsockopt(receiveSocket->native_handle(), SOL_SOCKET, SO_BINDTODEVICE,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200232 networkInterfaceName.c_str(), networkInterfaceName.size()+1) == -1){
233 throw Error("Cannot bind multicast face to " + networkInterfaceName
234 + " make sure you have CAP_NET_RAW capability" );
235 }
236 }
237
238#endif
239
Alexander Afanasyevdc017002014-06-18 16:37:54 -0700240 multicastFace = make_shared<MulticastUdpFace>(receiveSocket, sendSocket,
241 localEndpoint, multicastEndpoint);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700242
243 multicastFace->onFail.connectSingleShot(bind(&UdpFactory::afterFaceFailed, this, localEndpoint));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100244
245 m_multicastFaces[localEndpoint] = multicastFace;
246
247 return multicastFace;
248}
249
250shared_ptr<MulticastUdpFace>
251UdpFactory::createMulticastFace(const std::string& localIp,
252 const std::string& multicastIp,
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200253 const std::string& multicastPort,
254 const std::string& networkInterfaceName /* "" */)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100255{
Chengyu Fan4381fb62015-01-14 11:37:04 -0700256 udp::Endpoint localEndpoint(ndn::dns::syncResolve(localIp, getGlobalIoService()),
257 boost::lexical_cast<uint16_t>(multicastPort));
Junxiao Shi79494162014-04-02 18:25:11 -0700258
Chengyu Fan4381fb62015-01-14 11:37:04 -0700259 udp::Endpoint multicastEndpoint(ndn::dns::syncResolve(multicastIp, getGlobalIoService()),
260 boost::lexical_cast<uint16_t>(multicastPort));
261
262 return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100263}
264
265void
266UdpFactory::createFace(const FaceUri& uri,
267 const FaceCreatedCallback& onCreated,
268 const FaceConnectFailedCallback& onConnectFailed)
269{
Chengyu Fan4381fb62015-01-14 11:37:04 -0700270 BOOST_ASSERT(uri.isCanonical());
271 boost::asio::ip::address ipAddress = boost::asio::ip::address::from_string(uri.getHost());
272 udp::Endpoint endpoint(ipAddress, boost::lexical_cast<uint16_t>(uri.getPort()));
Junxiao Shi79494162014-04-02 18:25:11 -0700273
Giulio Grassi624f6c62014-02-18 19:42:14 +0100274 if (endpoint.address().is_multicast()) {
275 onConnectFailed("The provided address is multicast. Please use createMulticastFace method");
276 return;
277 }
Junxiao Shi79494162014-04-02 18:25:11 -0700278
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600279 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
280 {
281 onConnectFailed("Requested endpoint is prohibited "
282 "(reserved by this NFD or disallowed by face management protocol)");
283 return;
284 }
285
Giulio Grassi624f6c62014-02-18 19:42:14 +0100286 // very simple logic for now
Junxiao Shi79494162014-04-02 18:25:11 -0700287
Giulio Grassi624f6c62014-02-18 19:42:14 +0100288 for (ChannelMap::iterator channel = m_channels.begin();
289 channel != m_channels.end();
290 ++channel)
291 {
292 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
293 (channel->first.address().is_v6() && endpoint.address().is_v6()))
294 {
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600295 channel->second->connect(endpoint, onCreated, onConnectFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100296 return;
297 }
298 }
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700299 onConnectFailed("No channels available to connect to " +
300 boost::lexical_cast<std::string>(endpoint));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100301}
302
303shared_ptr<UdpChannel>
304UdpFactory::findChannel(const udp::Endpoint& localEndpoint)
305{
306 ChannelMap::iterator i = m_channels.find(localEndpoint);
307 if (i != m_channels.end())
308 return i->second;
309 else
310 return shared_ptr<UdpChannel>();
311}
312
313shared_ptr<MulticastUdpFace>
314UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint)
315{
316 MulticastFaceMap::iterator i = m_multicastFaces.find(localEndpoint);
317 if (i != m_multicastFaces.end())
318 return i->second;
319 else
320 return shared_ptr<MulticastUdpFace>();
321}
322
323void
324UdpFactory::afterFaceFailed(udp::Endpoint& endpoint)
325{
326 NFD_LOG_DEBUG("afterFaceFailed: " << endpoint);
327 m_multicastFaces.erase(endpoint);
328}
329
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600330std::list<shared_ptr<const Channel> >
331UdpFactory::getChannels() const
332{
333 std::list<shared_ptr<const Channel> > channels;
334 for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
335 {
336 channels.push_back(i->second);
337 }
338
339 return channels;
340}
341
342
Giulio Grassi624f6c62014-02-18 19:42:14 +0100343
344} // namespace nfd