Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 25 | |
| 26 | #include "udp-factory.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 27 | #include "generic-link-service.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 28 | #include "multicast-udp-transport.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 29 | #include "core/global-io.hpp" |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 30 | #include "core/network-interface.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 31 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 32 | #ifdef __linux__ |
| 33 | #include <cerrno> // for errno |
| 34 | #include <cstring> // for std::strerror() |
| 35 | #include <sys/socket.h> // for setsockopt() |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 36 | #endif |
| 37 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 38 | namespace nfd { |
| 39 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 40 | namespace ip = boost::asio::ip; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 41 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 42 | NFD_LOG_INIT("UdpFactory"); |
| 43 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 44 | void |
| 45 | UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint) |
| 46 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 47 | if (endpoint.address().is_v4() && |
| 48 | endpoint.address() == ip::address_v4::any()) { |
| 49 | prohibitAllIpv4Endpoints(endpoint.port()); |
| 50 | } |
| 51 | else if (endpoint.address().is_v6() && |
| 52 | endpoint.address() == ip::address_v6::any()) { |
| 53 | prohibitAllIpv6Endpoints(endpoint.port()); |
| 54 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 55 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 56 | NFD_LOG_TRACE("prohibiting UDP " << endpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 57 | m_prohibitedEndpoints.insert(endpoint); |
| 58 | } |
| 59 | |
| 60 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 61 | UdpFactory::prohibitAllIpv4Endpoints(uint16_t port) |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 62 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 63 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 64 | for (const auto& addr : nic.ipv4Addresses) { |
| 65 | if (addr != ip::address_v4::any()) { |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 66 | prohibitEndpoint(udp::Endpoint(addr, port)); |
| 67 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 68 | } |
| 69 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 70 | if (nic.isBroadcastCapable() && |
| 71 | nic.broadcastAddress != ip::address_v4::any()) { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 72 | prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port)); |
| 73 | } |
| 74 | } |
| 75 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 76 | prohibitEndpoint(udp::Endpoint(ip::address_v4::broadcast(), port)); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 80 | UdpFactory::prohibitAllIpv6Endpoints(uint16_t port) |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 81 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 82 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 83 | for (const auto& addr : nic.ipv6Addresses) { |
| 84 | if (addr != ip::address_v6::any()) { |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 85 | prohibitEndpoint(udp::Endpoint(addr, port)); |
| 86 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 87 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 88 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 89 | } |
| 90 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 91 | shared_ptr<UdpChannel> |
| 92 | UdpFactory::createChannel(const udp::Endpoint& endpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 93 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 94 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 95 | NFD_LOG_DEBUG("Creating unicast channel " << endpoint); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 96 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 97 | auto channel = findChannel(endpoint); |
| 98 | if (channel) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 99 | return channel; |
| 100 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 101 | if (endpoint.address().is_multicast()) { |
| 102 | BOOST_THROW_EXCEPTION(Error("createChannel is only for unicast channels. The provided endpoint " |
| 103 | "is multicast. Use createMulticastFace to create a multicast face")); |
| 104 | } |
| 105 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 106 | // check if the endpoint is already used by a multicast face |
| 107 | auto face = findMulticastFace(endpoint); |
| 108 | if (face) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 109 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP unicast channel, local " |
| 110 | "endpoint is already allocated for a UDP multicast face")); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 111 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 112 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 113 | channel = std::make_shared<UdpChannel>(endpoint, timeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 114 | m_channels[endpoint] = channel; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 115 | prohibitEndpoint(endpoint); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 116 | |
| 117 | return channel; |
| 118 | } |
| 119 | |
| 120 | shared_ptr<UdpChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 121 | UdpFactory::createChannel(const std::string& localIp, const std::string& localPort, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 122 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 123 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 124 | udp::Endpoint endpoint(ip::address::from_string(localIp), |
| 125 | boost::lexical_cast<uint16_t>(localPort)); |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 126 | return createChannel(endpoint, timeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 129 | shared_ptr<Face> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 130 | UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 131 | const udp::Endpoint& multicastEndpoint, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 132 | const std::string& networkInterfaceName/* = ""*/) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 133 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 134 | // checking if the local and multicast endpoints are already in use for a multicast face |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 135 | auto face = findMulticastFace(localEndpoint); |
| 136 | if (face) { |
| 137 | if (face->getRemoteUri() == FaceUri(multicastEndpoint)) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 138 | return face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 139 | else |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 140 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local " |
| 141 | "endpoint is already allocated for a UDP multicast face " |
| 142 | "on a different multicast group")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 143 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 144 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 145 | // checking if the local endpoint is already in use for a unicast channel |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 146 | auto unicastCh = findChannel(localEndpoint); |
| 147 | if (unicastCh) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 148 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local " |
| 149 | "endpoint is already allocated for a UDP unicast channel")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 152 | if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 153 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, " |
| 154 | "remote endpoint is owned by this NFD instance")); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 157 | if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 158 | BOOST_THROW_EXCEPTION(Error("IPv6 multicast is not supported yet. Please provide an IPv4 " |
| 159 | "address")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 160 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 161 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 162 | if (localEndpoint.port() != multicastEndpoint.port()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 163 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, " |
| 164 | "both endpoints should have the same port number. ")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (!multicastEndpoint.address().is_multicast()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 168 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, " |
| 169 | "the multicast group given as input is not a multicast address")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 172 | ip::udp::socket receiveSocket(getGlobalIoService()); |
| 173 | receiveSocket.open(multicastEndpoint.protocol()); |
| 174 | receiveSocket.set_option(ip::udp::socket::reuse_address(true)); |
| 175 | receiveSocket.bind(multicastEndpoint); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 176 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 177 | ip::udp::socket sendSocket(getGlobalIoService()); |
| 178 | sendSocket.open(multicastEndpoint.protocol()); |
| 179 | sendSocket.set_option(ip::udp::socket::reuse_address(true)); |
| 180 | sendSocket.set_option(ip::multicast::enable_loopback(false)); |
| 181 | sendSocket.bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port())); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 182 | if (localEndpoint.address() != ip::address_v4::any()) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 183 | sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 184 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 185 | sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(), |
| 186 | localEndpoint.address().to_v4())); |
| 187 | receiveSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(), |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 188 | localEndpoint.address().to_v4())); |
| 189 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 190 | #ifdef __linux__ |
| 191 | /* |
| 192 | * On Linux, if there is more than one MulticastUdpFace for the same multicast |
| 193 | * group but they are bound to different network interfaces, the socket needs |
| 194 | * to be bound to the specific interface using SO_BINDTODEVICE, otherwise the |
| 195 | * face will receive all packets sent to the other interfaces as well. |
| 196 | * This happens only on Linux. On OS X, the ip::multicast::join_group option |
| 197 | * is enough to get the desired behaviour. |
| 198 | */ |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 199 | if (!networkInterfaceName.empty()) { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 200 | if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE, |
| 201 | networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 202 | BOOST_THROW_EXCEPTION(Error("Cannot bind multicast face to " + networkInterfaceName + |
| 203 | ": " + std::strerror(errno))); |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 204 | } |
| 205 | } |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 206 | #endif |
| 207 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 208 | auto linkService = make_unique<face::GenericLinkService>(); |
| 209 | auto transport = make_unique<face::MulticastUdpTransport>(localEndpoint, multicastEndpoint, |
| 210 | std::move(receiveSocket), |
| 211 | std::move(sendSocket)); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 212 | face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 213 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 214 | m_multicastFaces[localEndpoint] = face; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 215 | connectFaceClosedSignal(*face, [this, localEndpoint] { m_multicastFaces.erase(localEndpoint); }); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 216 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 217 | return face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 220 | shared_ptr<Face> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 221 | UdpFactory::createMulticastFace(const std::string& localIp, |
| 222 | const std::string& multicastIp, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 223 | const std::string& multicastPort, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 224 | const std::string& networkInterfaceName/* = ""*/) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 225 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 226 | udp::Endpoint localEndpoint(ip::address::from_string(localIp), |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 227 | boost::lexical_cast<uint16_t>(multicastPort)); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 228 | udp::Endpoint multicastEndpoint(ip::address::from_string(multicastIp), |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 229 | boost::lexical_cast<uint16_t>(multicastPort)); |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 230 | return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void |
| 234 | UdpFactory::createFace(const FaceUri& uri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 235 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 236 | bool wantLocalFieldsEnabled, |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 237 | const FaceCreatedCallback& onCreated, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 238 | const FaceCreationFailedCallback& onFailure) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 239 | { |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 240 | BOOST_ASSERT(uri.isCanonical()); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 241 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 242 | if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 243 | NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND"); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 244 | onFailure(406, "Outgoing unicast UDP faces do not support on-demand persistency"); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 245 | return; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | udp::Endpoint endpoint(ip::address::from_string(uri.getHost()), |
| 249 | boost::lexical_cast<uint16_t>(uri.getPort())); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 250 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 251 | if (endpoint.address().is_multicast()) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 252 | NFD_LOG_TRACE("createFace does not support multicast faces"); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 253 | onFailure(406, "Cannot create multicast UDP faces"); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 254 | return; |
| 255 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 256 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 257 | if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 258 | NFD_LOG_TRACE("Requested endpoint is prohibited " |
| 259 | "(reserved by this NFD or disallowed by face management protocol)"); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 260 | onFailure(406, "Requested endpoint is prohibited"); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | if (wantLocalFieldsEnabled) { |
| 265 | // UDP faces are never local |
| 266 | NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled"); |
| 267 | onFailure(406, "Local fields can only be enabled on faces with local scope"); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 268 | return; |
| 269 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 270 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 271 | // very simple logic for now |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 272 | for (const auto& i : m_channels) { |
| 273 | if ((i.first.address().is_v4() && endpoint.address().is_v4()) || |
| 274 | (i.first.address().is_v6() && endpoint.address().is_v6())) { |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 275 | i.second->connect(endpoint, persistency, onCreated, onFailure); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | } |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 279 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 280 | NFD_LOG_TRACE("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint)); |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 281 | onFailure(504, "No channels available to connect"); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | std::vector<shared_ptr<const Channel>> |
| 285 | UdpFactory::getChannels() const |
| 286 | { |
| 287 | std::vector<shared_ptr<const Channel>> channels; |
| 288 | channels.reserve(m_channels.size()); |
| 289 | |
| 290 | for (const auto& i : m_channels) |
| 291 | channels.push_back(i.second); |
| 292 | |
| 293 | return channels; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | shared_ptr<UdpChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 297 | UdpFactory::findChannel(const udp::Endpoint& localEndpoint) const |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 298 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 299 | auto i = m_channels.find(localEndpoint); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 300 | if (i != m_channels.end()) |
| 301 | return i->second; |
| 302 | else |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 303 | return nullptr; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 306 | shared_ptr<Face> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 307 | UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint) const |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 308 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 309 | auto i = m_multicastFaces.find(localEndpoint); |
| 310 | if (i != m_multicastFaces.end()) |
| 311 | return i->second; |
| 312 | else |
| 313 | return nullptr; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 314 | } |
| 315 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 316 | } // namespace nfd |