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" |
| 27 | #include "core/global-io.hpp" |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 28 | #include "core/network-interface.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 29 | |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 30 | #if defined(__linux__) |
| 31 | #include <sys/socket.h> |
| 32 | #endif |
| 33 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 34 | namespace nfd { |
| 35 | |
| 36 | using namespace boost::asio; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 37 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 38 | NFD_LOG_INIT("UdpFactory"); |
| 39 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 40 | static const boost::asio::ip::address_v4 ALL_V4_ENDPOINT( |
| 41 | boost::asio::ip::address_v4::from_string("0.0.0.0")); |
| 42 | |
| 43 | static const boost::asio::ip::address_v6 ALL_V6_ENDPOINT( |
| 44 | boost::asio::ip::address_v6::from_string("::")); |
| 45 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 46 | UdpFactory::UdpFactory(const std::string& defaultPort/* = "6363"*/) |
| 47 | : m_defaultPort(defaultPort) |
| 48 | { |
| 49 | } |
| 50 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 51 | void |
| 52 | UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint) |
| 53 | { |
| 54 | using namespace boost::asio::ip; |
| 55 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 56 | const address& address = endpoint.address(); |
| 57 | |
| 58 | if (address.is_v4() && address == ALL_V4_ENDPOINT) |
| 59 | { |
| 60 | prohibitAllIpv4Endpoints(endpoint.port()); |
| 61 | } |
| 62 | else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT) |
| 63 | { |
| 64 | prohibitAllIpv6Endpoints(endpoint.port()); |
| 65 | } |
| 66 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 67 | NFD_LOG_TRACE("prohibiting UDP " << endpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 68 | |
| 69 | m_prohibitedEndpoints.insert(endpoint); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | UdpFactory::prohibitAllIpv4Endpoints(const uint16_t port) |
| 74 | { |
| 75 | using namespace boost::asio::ip; |
| 76 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 77 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
| 78 | for (const address_v4& addr : nic.ipv4Addresses) { |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 79 | if (addr != ALL_V4_ENDPOINT) { |
| 80 | prohibitEndpoint(udp::Endpoint(addr, port)); |
| 81 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 82 | } |
| 83 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 84 | if (nic.isBroadcastCapable() && nic.broadcastAddress != ALL_V4_ENDPOINT) |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 85 | { |
| 86 | NFD_LOG_TRACE("prohibiting broadcast address: " << nic.broadcastAddress.to_string()); |
| 87 | prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port)); |
| 88 | } |
| 89 | } |
| 90 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 91 | prohibitEndpoint(udp::Endpoint(address::from_string("255.255.255.255"), port)); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | UdpFactory::prohibitAllIpv6Endpoints(const uint16_t port) |
| 96 | { |
| 97 | using namespace boost::asio::ip; |
| 98 | |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 99 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
| 100 | for (const address_v6& addr : nic.ipv6Addresses) { |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 101 | if (addr != ALL_V6_ENDPOINT) { |
| 102 | prohibitEndpoint(udp::Endpoint(addr, port)); |
| 103 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 104 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 105 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 106 | } |
| 107 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 108 | shared_ptr<UdpChannel> |
| 109 | UdpFactory::createChannel(const udp::Endpoint& endpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 110 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 111 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 112 | NFD_LOG_DEBUG("Creating unicast channel " << endpoint); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 113 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 114 | shared_ptr<UdpChannel> channel = findChannel(endpoint); |
| 115 | if (static_cast<bool>(channel)) |
| 116 | return channel; |
| 117 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 118 | //checking if the endpoint is already in use for multicast face |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 119 | shared_ptr<MulticastUdpFace> multicast = findMulticastFace(endpoint); |
| 120 | if (static_cast<bool>(multicast)) |
| 121 | throw Error("Cannot create the requested UDP unicast channel, local " |
| 122 | "endpoint is already allocated for a UDP multicast face"); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 123 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 124 | if (endpoint.address().is_multicast()) { |
| 125 | throw Error("This method is only for unicast channel. The provided " |
| 126 | "endpoint is multicast. Use createMulticastFace to " |
| 127 | "create a multicast face"); |
| 128 | } |
| 129 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 130 | channel = make_shared<UdpChannel>(endpoint, timeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 131 | m_channels[endpoint] = channel; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 132 | prohibitEndpoint(endpoint); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 133 | |
| 134 | return channel; |
| 135 | } |
| 136 | |
| 137 | shared_ptr<UdpChannel> |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 138 | UdpFactory::createChannel(const std::string& localIp, |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 139 | const std::string& localPort, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 140 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 141 | { |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 142 | using namespace boost::asio::ip; |
| 143 | udp::Endpoint endpoint(address::from_string(localIp), boost::lexical_cast<uint16_t>(localPort)); |
| 144 | return createChannel(endpoint, timeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | shared_ptr<MulticastUdpFace> |
| 148 | UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 149 | const udp::Endpoint& multicastEndpoint, |
| 150 | const std::string& networkInterfaceName /* "" */) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 151 | { |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 152 | //checking if the local and multicast endpoint are already in use for a multicast face |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 153 | shared_ptr<MulticastUdpFace> multicastFace = findMulticastFace(localEndpoint); |
| 154 | if (static_cast<bool>(multicastFace)) { |
| 155 | if (multicastFace->getMulticastGroup() == multicastEndpoint) |
| 156 | return multicastFace; |
| 157 | else |
| 158 | throw Error("Cannot create the requested UDP multicast face, local " |
| 159 | "endpoint is already allocated for a UDP multicast face " |
| 160 | "on a different multicast group"); |
| 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 | //checking if the local endpoint is already in use for an unicast channel |
| 164 | shared_ptr<UdpChannel> unicast = findChannel(localEndpoint); |
| 165 | if (static_cast<bool>(unicast)) { |
| 166 | throw Error("Cannot create the requested UDP multicast face, local " |
| 167 | "endpoint is already allocated for a UDP unicast channel"); |
| 168 | } |
| 169 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 170 | if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) { |
| 171 | throw Error("Cannot create the requested UDP multicast face, " |
| 172 | "remote endpoint is owned by this NFD instance"); |
| 173 | } |
| 174 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 175 | if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) { |
| 176 | throw Error("IPv6 multicast is not supported yet. Please provide an IPv4 address"); |
| 177 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 178 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 179 | if (localEndpoint.port() != multicastEndpoint.port()) { |
| 180 | throw Error("Cannot create the requested UDP multicast face, " |
| 181 | "both endpoints should have the same port number. "); |
| 182 | } |
| 183 | |
| 184 | if (!multicastEndpoint.address().is_multicast()) { |
| 185 | throw Error("Cannot create the requested UDP multicast face, " |
| 186 | "the multicast group given as input is not a multicast address"); |
| 187 | } |
| 188 | |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 189 | shared_ptr<ip::udp::socket> receiveSocket = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 190 | make_shared<ip::udp::socket>(ref(getGlobalIoService())); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 191 | |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 192 | shared_ptr<ip::udp::socket> sendSocket = |
| 193 | make_shared<ip::udp::socket>(ref(getGlobalIoService())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 194 | |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 195 | receiveSocket->open(multicastEndpoint.protocol()); |
| 196 | receiveSocket->set_option(ip::udp::socket::reuse_address(true)); |
| 197 | |
| 198 | sendSocket->open(multicastEndpoint.protocol()); |
| 199 | sendSocket->set_option(ip::udp::socket::reuse_address(true)); |
| 200 | sendSocket->set_option(ip::multicast::enable_loopback(false)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 201 | |
| 202 | try { |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 203 | sendSocket->bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port())); |
| 204 | receiveSocket->bind(multicastEndpoint); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 205 | |
| 206 | if (localEndpoint.address() != ip::address::from_string("0.0.0.0")) { |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 207 | sendSocket->set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 208 | } |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 209 | sendSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(), |
| 210 | localEndpoint.address().to_v4())); |
| 211 | |
| 212 | receiveSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(), |
| 213 | localEndpoint.address().to_v4())); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 214 | } |
| 215 | catch (boost::system::system_error& e) { |
| 216 | std::stringstream msg; |
| 217 | msg << "Failed to properly configure the socket, check the address (" << e.what() << ")"; |
| 218 | throw Error(msg.str()); |
| 219 | } |
| 220 | |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 221 | #if defined(__linux__) |
| 222 | //On linux system, if there are more than one MulticastUdpFace for the same multicast group but |
| 223 | //bound on different network interfaces, the socket has to be bound with the specific interface |
| 224 | //using SO_BINDTODEVICE, otherwise the face will receive packets also from other interfaces. |
| 225 | //Without SO_BINDTODEVICE every MulticastUdpFace that have joined the same multicast group |
| 226 | //on different interfaces will receive the same packet. |
| 227 | //This applies only on linux, for OS X the ip::multicast::join_group is enough to get |
| 228 | //the desired behaviour |
| 229 | if (!networkInterfaceName.empty()) { |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 230 | if (::setsockopt(receiveSocket->native_handle(), SOL_SOCKET, SO_BINDTODEVICE, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 231 | networkInterfaceName.c_str(), networkInterfaceName.size()+1) == -1){ |
| 232 | throw Error("Cannot bind multicast face to " + networkInterfaceName |
| 233 | + " make sure you have CAP_NET_RAW capability" ); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | #endif |
| 238 | |
Alexander Afanasyev | dc01700 | 2014-06-18 16:37:54 -0700 | [diff] [blame] | 239 | multicastFace = make_shared<MulticastUdpFace>(receiveSocket, sendSocket, |
| 240 | localEndpoint, multicastEndpoint); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 241 | |
| 242 | multicastFace->onFail.connectSingleShot(bind(&UdpFactory::afterFaceFailed, this, localEndpoint)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 243 | |
| 244 | m_multicastFaces[localEndpoint] = multicastFace; |
| 245 | |
| 246 | return multicastFace; |
| 247 | } |
| 248 | |
| 249 | shared_ptr<MulticastUdpFace> |
| 250 | UdpFactory::createMulticastFace(const std::string& localIp, |
| 251 | const std::string& multicastIp, |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 252 | const std::string& multicastPort, |
| 253 | const std::string& networkInterfaceName /* "" */) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 254 | { |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 255 | using namespace boost::asio::ip; |
| 256 | udp::Endpoint localEndpoint(address::from_string(localIp), |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 257 | boost::lexical_cast<uint16_t>(multicastPort)); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 258 | |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 259 | udp::Endpoint multicastEndpoint(address::from_string(multicastIp), |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 260 | boost::lexical_cast<uint16_t>(multicastPort)); |
| 261 | |
| 262 | return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void |
| 266 | UdpFactory::createFace(const FaceUri& uri, |
| 267 | const FaceCreatedCallback& onCreated, |
| 268 | const FaceConnectFailedCallback& onConnectFailed) |
| 269 | { |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 270 | 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 Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 273 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 274 | if (endpoint.address().is_multicast()) { |
| 275 | onConnectFailed("The provided address is multicast. Please use createMulticastFace method"); |
| 276 | return; |
| 277 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 278 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 279 | 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 Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 286 | // very simple logic for now |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 287 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 288 | 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 DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 295 | channel->second->connect(endpoint, onCreated, onConnectFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | } |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 299 | onConnectFailed("No channels available to connect to " + |
| 300 | boost::lexical_cast<std::string>(endpoint)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | shared_ptr<UdpChannel> |
| 304 | UdpFactory::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 | |
| 313 | shared_ptr<MulticastUdpFace> |
| 314 | UdpFactory::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 | |
| 323 | void |
| 324 | UdpFactory::afterFaceFailed(udp::Endpoint& endpoint) |
| 325 | { |
| 326 | NFD_LOG_DEBUG("afterFaceFailed: " << endpoint); |
| 327 | m_multicastFaces.erase(endpoint); |
| 328 | } |
| 329 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 330 | std::list<shared_ptr<const Channel> > |
| 331 | UdpFactory::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 Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 343 | |
| 344 | } // namespace nfd |