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