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