Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "udp-factory.hpp" |
| 8 | #include "core/global-io.hpp" |
| 9 | #include "core/resolver.hpp" |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame^] | 10 | #include "core/network-interface.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | using namespace boost::asio; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 15 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 16 | NFD_LOG_INIT("UdpFactory"); |
| 17 | |
| 18 | UdpFactory::UdpFactory(const std::string& defaultPort/* = "6363"*/) |
| 19 | : m_defaultPort(defaultPort) |
| 20 | { |
| 21 | } |
| 22 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame^] | 23 | |
| 24 | |
| 25 | void |
| 26 | UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint) |
| 27 | { |
| 28 | using namespace boost::asio::ip; |
| 29 | |
| 30 | static const address_v4 ALL_V4_ENDPOINT(address_v4::from_string("0.0.0.0")); |
| 31 | static const address_v6 ALL_V6_ENDPOINT(address_v6::from_string("::")); |
| 32 | |
| 33 | const address& address = endpoint.address(); |
| 34 | |
| 35 | if (address.is_v4() && address == ALL_V4_ENDPOINT) |
| 36 | { |
| 37 | prohibitAllIpv4Endpoints(endpoint.port()); |
| 38 | } |
| 39 | else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT) |
| 40 | { |
| 41 | prohibitAllIpv6Endpoints(endpoint.port()); |
| 42 | } |
| 43 | |
| 44 | NFD_LOG_TRACE("prohibiting UDP " << |
| 45 | endpoint.address().to_string() << ":" << endpoint.port()); |
| 46 | |
| 47 | m_prohibitedEndpoints.insert(endpoint); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | UdpFactory::prohibitAllIpv4Endpoints(const uint16_t port) |
| 52 | { |
| 53 | using namespace boost::asio::ip; |
| 54 | |
| 55 | static const address_v4 INVALID_BROADCAST(address_v4::from_string("0.0.0.0")); |
| 56 | |
| 57 | const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces()); |
| 58 | |
| 59 | for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin(); |
| 60 | i != nicList.end(); |
| 61 | ++i) |
| 62 | { |
| 63 | const shared_ptr<NetworkInterfaceInfo>& nic = *i; |
| 64 | const std::vector<address_v4>& ipv4Addresses = nic->ipv4Addresses; |
| 65 | |
| 66 | for (std::vector<address_v4>::const_iterator j = ipv4Addresses.begin(); |
| 67 | j != ipv4Addresses.end(); |
| 68 | ++j) |
| 69 | { |
| 70 | prohibitEndpoint(udp::Endpoint(*j, port)); |
| 71 | } |
| 72 | |
| 73 | if (nic->isBroadcastCapable() && nic->broadcastAddress != INVALID_BROADCAST) |
| 74 | { |
| 75 | NFD_LOG_TRACE("prohibiting broadcast address: " << nic->broadcastAddress.to_string()); |
| 76 | prohibitEndpoint(udp::Endpoint(nic->broadcastAddress, port)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | prohibitEndpoint(udp::Endpoint(address::from_string("255.255.255.255"), port)); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | UdpFactory::prohibitAllIpv6Endpoints(const uint16_t port) |
| 85 | { |
| 86 | using namespace boost::asio::ip; |
| 87 | |
| 88 | const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces()); |
| 89 | |
| 90 | for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin(); |
| 91 | i != nicList.end(); |
| 92 | ++i) |
| 93 | { |
| 94 | const shared_ptr<NetworkInterfaceInfo>& nic = *i; |
| 95 | const std::vector<address_v6>& ipv6Addresses = nic->ipv6Addresses; |
| 96 | |
| 97 | for (std::vector<address_v6>::const_iterator j = ipv6Addresses.begin(); |
| 98 | j != ipv6Addresses.end(); |
| 99 | ++j) |
| 100 | { |
| 101 | prohibitEndpoint(udp::Endpoint(*j, port)); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 106 | shared_ptr<UdpChannel> |
| 107 | UdpFactory::createChannel(const udp::Endpoint& endpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 108 | const time::seconds& timeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 109 | { |
| 110 | NFD_LOG_DEBUG("Creating unicast " << endpoint); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 111 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 112 | shared_ptr<UdpChannel> channel = findChannel(endpoint); |
| 113 | if (static_cast<bool>(channel)) |
| 114 | return channel; |
| 115 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 116 | |
| 117 | //checking if the endpoint is already in use for multicast face |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 118 | shared_ptr<MulticastUdpFace> multicast = findMulticastFace(endpoint); |
| 119 | if (static_cast<bool>(multicast)) |
| 120 | throw Error("Cannot create the requested UDP unicast channel, local " |
| 121 | "endpoint is already allocated for a UDP multicast face"); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 122 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 123 | if (endpoint.address().is_multicast()) { |
| 124 | throw Error("This method is only for unicast channel. The provided " |
| 125 | "endpoint is multicast. Use createMulticastFace to " |
| 126 | "create a multicast face"); |
| 127 | } |
| 128 | |
| 129 | channel = make_shared<UdpChannel>(boost::cref(endpoint), |
| 130 | timeout); |
| 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> |
| 138 | UdpFactory::createChannel(const std::string& localHost, |
| 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 | { |
| 142 | return createChannel(UdpResolver::syncResolve(localHost, localPort), |
| 143 | timeout); |
| 144 | } |
| 145 | |
| 146 | shared_ptr<MulticastUdpFace> |
| 147 | UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 148 | const udp::Endpoint& multicastEndpoint) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 149 | { |
| 150 | //checking if the local and musticast endpoint are already in use for a multicast face |
| 151 | shared_ptr<MulticastUdpFace> multicastFace = findMulticastFace(localEndpoint); |
| 152 | if (static_cast<bool>(multicastFace)) { |
| 153 | if (multicastFace->getMulticastGroup() == multicastEndpoint) |
| 154 | return multicastFace; |
| 155 | else |
| 156 | throw Error("Cannot create the requested UDP multicast face, local " |
| 157 | "endpoint is already allocated for a UDP multicast face " |
| 158 | "on a different multicast group"); |
| 159 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 160 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 161 | //checking if the local endpoint is already in use for an unicast channel |
| 162 | shared_ptr<UdpChannel> unicast = findChannel(localEndpoint); |
| 163 | if (static_cast<bool>(unicast)) { |
| 164 | throw Error("Cannot create the requested UDP multicast face, local " |
| 165 | "endpoint is already allocated for a UDP unicast channel"); |
| 166 | } |
| 167 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame^] | 168 | if (m_prohibitedEndpoints.find(multicastEndpoint) != m_prohibitedEndpoints.end()) { |
| 169 | throw Error("Cannot create the requested UDP multicast face, " |
| 170 | "remote endpoint is owned by this NFD instance"); |
| 171 | } |
| 172 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 173 | if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) { |
| 174 | throw Error("IPv6 multicast is not supported yet. Please provide an IPv4 address"); |
| 175 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 176 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 177 | if (localEndpoint.port() != multicastEndpoint.port()) { |
| 178 | throw Error("Cannot create the requested UDP multicast face, " |
| 179 | "both endpoints should have the same port number. "); |
| 180 | } |
| 181 | |
| 182 | if (!multicastEndpoint.address().is_multicast()) { |
| 183 | throw Error("Cannot create the requested UDP multicast face, " |
| 184 | "the multicast group given as input is not a multicast address"); |
| 185 | } |
| 186 | |
| 187 | shared_ptr<ip::udp::socket> clientSocket = |
| 188 | make_shared<ip::udp::socket>(boost::ref(getGlobalIoService())); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 189 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 190 | clientSocket->open(multicastEndpoint.protocol()); |
| 191 | |
| 192 | clientSocket->set_option(ip::udp::socket::reuse_address(true)); |
| 193 | |
| 194 | try { |
| 195 | clientSocket->bind(multicastEndpoint); |
| 196 | |
| 197 | if (localEndpoint.address() != ip::address::from_string("0.0.0.0")) { |
| 198 | clientSocket->set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4())); |
| 199 | } |
| 200 | clientSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(), |
| 201 | localEndpoint.address().to_v4())); |
| 202 | } |
| 203 | catch (boost::system::system_error& e) { |
| 204 | std::stringstream msg; |
| 205 | msg << "Failed to properly configure the socket, check the address (" << e.what() << ")"; |
| 206 | throw Error(msg.str()); |
| 207 | } |
| 208 | |
| 209 | clientSocket->set_option(ip::multicast::enable_loopback(false)); |
| 210 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 211 | multicastFace = make_shared<MulticastUdpFace>(boost::cref(clientSocket), localEndpoint); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 212 | multicastFace->onFail += bind(&UdpFactory::afterFaceFailed, this, localEndpoint); |
| 213 | |
| 214 | m_multicastFaces[localEndpoint] = multicastFace; |
| 215 | |
| 216 | return multicastFace; |
| 217 | } |
| 218 | |
| 219 | shared_ptr<MulticastUdpFace> |
| 220 | UdpFactory::createMulticastFace(const std::string& localIp, |
| 221 | const std::string& multicastIp, |
| 222 | const std::string& multicastPort) |
| 223 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 224 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 225 | return createMulticastFace(UdpResolver::syncResolve(localIp, |
| 226 | multicastPort), |
| 227 | UdpResolver::syncResolve(multicastIp, |
| 228 | multicastPort)); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | UdpFactory::createFace(const FaceUri& uri, |
| 233 | const FaceCreatedCallback& onCreated, |
| 234 | const FaceConnectFailedCallback& onConnectFailed) |
| 235 | { |
| 236 | resolver::AddressSelector addressSelector = resolver::AnyAddress(); |
| 237 | if (uri.getScheme() == "udp4") |
| 238 | addressSelector = resolver::Ipv4Address(); |
| 239 | else if (uri.getScheme() == "udp6") |
| 240 | addressSelector = resolver::Ipv6Address(); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 241 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame^] | 242 | if (!uri.getPath().empty()) |
| 243 | { |
| 244 | onConnectFailed("Invalid URI"); |
| 245 | } |
| 246 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 247 | UdpResolver::asyncResolve(uri.getHost(), |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 248 | uri.getPort().empty() ? m_defaultPort : uri.getPort(), |
| 249 | bind(&UdpFactory::continueCreateFaceAfterResolve, this, _1, |
| 250 | onCreated, onConnectFailed), |
| 251 | onConnectFailed, |
| 252 | addressSelector); |
| 253 | |
| 254 | } |
| 255 | |
| 256 | void |
| 257 | UdpFactory::continueCreateFaceAfterResolve(const udp::Endpoint& endpoint, |
| 258 | const FaceCreatedCallback& onCreated, |
| 259 | const FaceConnectFailedCallback& onConnectFailed) |
| 260 | { |
| 261 | if (endpoint.address().is_multicast()) { |
| 262 | onConnectFailed("The provided address is multicast. Please use createMulticastFace method"); |
| 263 | return; |
| 264 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 265 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame^] | 266 | if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) |
| 267 | { |
| 268 | onConnectFailed("Requested endpoint is prohibited " |
| 269 | "(reserved by this NFD or disallowed by face management protocol)"); |
| 270 | return; |
| 271 | } |
| 272 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 273 | // very simple logic for now |
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 | for (ChannelMap::iterator channel = m_channels.begin(); |
| 276 | channel != m_channels.end(); |
| 277 | ++channel) |
| 278 | { |
| 279 | if ((channel->first.address().is_v4() && endpoint.address().is_v4()) || |
| 280 | (channel->first.address().is_v6() && endpoint.address().is_v6())) |
| 281 | { |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame^] | 282 | channel->second->connect(endpoint, onCreated, onConnectFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | } |
| 286 | onConnectFailed("No channels available to connect to " |
| 287 | + boost::lexical_cast<std::string>(endpoint)); |
| 288 | } |
| 289 | |
| 290 | shared_ptr<UdpChannel> |
| 291 | UdpFactory::findChannel(const udp::Endpoint& localEndpoint) |
| 292 | { |
| 293 | ChannelMap::iterator i = m_channels.find(localEndpoint); |
| 294 | if (i != m_channels.end()) |
| 295 | return i->second; |
| 296 | else |
| 297 | return shared_ptr<UdpChannel>(); |
| 298 | } |
| 299 | |
| 300 | shared_ptr<MulticastUdpFace> |
| 301 | UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint) |
| 302 | { |
| 303 | MulticastFaceMap::iterator i = m_multicastFaces.find(localEndpoint); |
| 304 | if (i != m_multicastFaces.end()) |
| 305 | return i->second; |
| 306 | else |
| 307 | return shared_ptr<MulticastUdpFace>(); |
| 308 | } |
| 309 | |
| 310 | void |
| 311 | UdpFactory::afterFaceFailed(udp::Endpoint& endpoint) |
| 312 | { |
| 313 | NFD_LOG_DEBUG("afterFaceFailed: " << endpoint); |
| 314 | m_multicastFaces.erase(endpoint); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | } // namespace nfd |