Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 3 | * 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 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" |
| 28 | #include "lp-face-wrapper.hpp" |
| 29 | #include "multicast-udp-transport.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 30 | #include "core/global-io.hpp" |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 31 | #include "core/network-interface.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 32 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 33 | #ifdef __linux__ |
| 34 | #include <cerrno> // for errno |
| 35 | #include <cstring> // for std::strerror() |
| 36 | #include <sys/socket.h> // for setsockopt() |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 37 | #endif |
| 38 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 39 | namespace nfd { |
| 40 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 41 | namespace ip = boost::asio::ip; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 42 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 43 | NFD_LOG_INIT("UdpFactory"); |
| 44 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 45 | void |
| 46 | UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint) |
| 47 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 48 | 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 DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 56 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 57 | NFD_LOG_TRACE("prohibiting UDP " << endpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 58 | m_prohibitedEndpoints.insert(endpoint); |
| 59 | } |
| 60 | |
| 61 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 62 | UdpFactory::prohibitAllIpv4Endpoints(uint16_t port) |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 63 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 64 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 65 | for (const auto& addr : nic.ipv4Addresses) { |
| 66 | if (addr != ip::address_v4::any()) { |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 67 | prohibitEndpoint(udp::Endpoint(addr, port)); |
| 68 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 69 | } |
| 70 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 71 | if (nic.isBroadcastCapable() && |
| 72 | nic.broadcastAddress != ip::address_v4::any()) { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 73 | prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port)); |
| 74 | } |
| 75 | } |
| 76 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 77 | prohibitEndpoint(udp::Endpoint(ip::address_v4::broadcast(), port)); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 81 | UdpFactory::prohibitAllIpv6Endpoints(uint16_t port) |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 82 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 83 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 84 | for (const auto& addr : nic.ipv6Addresses) { |
| 85 | if (addr != ip::address_v6::any()) { |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 86 | prohibitEndpoint(udp::Endpoint(addr, port)); |
| 87 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 88 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 89 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 90 | } |
| 91 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 92 | shared_ptr<UdpChannel> |
| 93 | UdpFactory::createChannel(const udp::Endpoint& endpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 94 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 95 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 96 | NFD_LOG_DEBUG("Creating unicast channel " << endpoint); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 97 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 98 | auto channel = findChannel(endpoint); |
| 99 | if (channel) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 100 | return channel; |
| 101 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 102 | 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 Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 107 | // check if the endpoint is already used by a multicast face |
| 108 | auto face = findMulticastFace(endpoint); |
| 109 | if (face) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 110 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP unicast channel, local " |
| 111 | "endpoint is already allocated for a UDP multicast face")); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 112 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 113 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 114 | channel = make_shared<UdpChannel>(endpoint, timeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 115 | m_channels[endpoint] = channel; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 116 | prohibitEndpoint(endpoint); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 117 | |
| 118 | return channel; |
| 119 | } |
| 120 | |
| 121 | shared_ptr<UdpChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 122 | UdpFactory::createChannel(const std::string& localIp, const std::string& localPort, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 123 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 124 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 125 | udp::Endpoint endpoint(ip::address::from_string(localIp), |
| 126 | boost::lexical_cast<uint16_t>(localPort)); |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 127 | return createChannel(endpoint, timeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 130 | shared_ptr<face::LpFaceWrapper> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 131 | UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 132 | const udp::Endpoint& multicastEndpoint, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 133 | const std::string& networkInterfaceName/* = ""*/) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 134 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 135 | // 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] | 136 | auto face = findMulticastFace(localEndpoint); |
| 137 | if (face) { |
| 138 | if (face->getRemoteUri() == FaceUri(multicastEndpoint)) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 139 | return face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 140 | else |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 141 | 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 Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 144 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 145 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 146 | // 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] | 147 | auto unicastCh = findChannel(localEndpoint); |
| 148 | if (unicastCh) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 149 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local " |
| 150 | "endpoint is already allocated for a UDP unicast channel")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 153 | if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 154 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, " |
| 155 | "remote endpoint is owned by this NFD instance")); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 156 | } |
| 157 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 158 | if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 159 | BOOST_THROW_EXCEPTION(Error("IPv6 multicast is not supported yet. Please provide an IPv4 " |
| 160 | "address")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 161 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 162 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 163 | if (localEndpoint.port() != multicastEndpoint.port()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 164 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, " |
| 165 | "both endpoints should have the same port number. ")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if (!multicastEndpoint.address().is_multicast()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 169 | BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, " |
| 170 | "the multicast group given as input is not a multicast address")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 173 | 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 Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 177 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 178 | 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 Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 183 | if (localEndpoint.address() != ip::address_v4::any()) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 184 | sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 185 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 186 | 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 Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 189 | localEndpoint.address().to_v4())); |
| 190 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 191 | #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 Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 200 | if (!networkInterfaceName.empty()) { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 201 | if (::setsockopt(receiveSocket.native_handle(), SOL_SOCKET, SO_BINDTODEVICE, |
| 202 | networkInterfaceName.c_str(), networkInterfaceName.size() + 1) < 0) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 203 | BOOST_THROW_EXCEPTION(Error("Cannot bind multicast face to " + networkInterfaceName + |
| 204 | ": " + std::strerror(errno))); |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 205 | } |
| 206 | } |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 207 | #endif |
| 208 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 209 | 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 Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 215 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 216 | face->onFail.connectSingleShot([this, localEndpoint] (const std::string& reason) { |
| 217 | m_multicastFaces.erase(localEndpoint); |
| 218 | }); |
| 219 | m_multicastFaces[localEndpoint] = face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 220 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 221 | return face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 224 | shared_ptr<face::LpFaceWrapper> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 225 | UdpFactory::createMulticastFace(const std::string& localIp, |
| 226 | const std::string& multicastIp, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 227 | const std::string& multicastPort, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 228 | const std::string& networkInterfaceName/* = ""*/) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 229 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 230 | udp::Endpoint localEndpoint(ip::address::from_string(localIp), |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 231 | boost::lexical_cast<uint16_t>(multicastPort)); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 232 | udp::Endpoint multicastEndpoint(ip::address::from_string(multicastIp), |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 233 | boost::lexical_cast<uint16_t>(multicastPort)); |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 234 | return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void |
| 238 | UdpFactory::createFace(const FaceUri& uri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 239 | ndn::nfd::FacePersistency persistency, |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 240 | const FaceCreatedCallback& onCreated, |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 241 | const FaceCreationFailedCallback& onConnectFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 242 | { |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 243 | BOOST_ASSERT(uri.isCanonical()); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 244 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 245 | 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 Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 251 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 252 | if (endpoint.address().is_multicast()) { |
| 253 | onConnectFailed("The provided address is multicast. Please use createMulticastFace method"); |
| 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()) { |
| 258 | onConnectFailed("Requested endpoint is prohibited " |
| 259 | "(reserved by this NFD or disallowed by face management protocol)"); |
| 260 | return; |
| 261 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 262 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 263 | // very simple logic for now |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 264 | 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 Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 268 | return; |
| 269 | } |
| 270 | } |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 271 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 272 | onConnectFailed("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint)); |
| 273 | } |
| 274 | |
| 275 | std::vector<shared_ptr<const Channel>> |
| 276 | UdpFactory::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 Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | shared_ptr<UdpChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 288 | UdpFactory::findChannel(const udp::Endpoint& localEndpoint) const |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 289 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 290 | auto i = m_channels.find(localEndpoint); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 291 | if (i != m_channels.end()) |
| 292 | return i->second; |
| 293 | else |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 294 | return nullptr; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 297 | shared_ptr<face::LpFaceWrapper> |
| 298 | UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint) const |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 299 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 300 | auto i = m_multicastFaces.find(localEndpoint); |
| 301 | if (i != m_multicastFaces.end()) |
| 302 | return i->second; |
| 303 | else |
| 304 | return nullptr; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 305 | } |
| 306 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 307 | } // namespace nfd |