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