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