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" |
| 10 | |
| 11 | namespace nfd { |
| 12 | |
| 13 | using namespace boost::asio; |
| 14 | |
| 15 | NFD_LOG_INIT("UdpFactory"); |
| 16 | |
| 17 | UdpFactory::UdpFactory(const std::string& defaultPort/* = "6363"*/) |
| 18 | : m_defaultPort(defaultPort) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | shared_ptr<UdpChannel> |
| 23 | UdpFactory::createChannel(const udp::Endpoint& endpoint, |
| 24 | const time::Duration& timeout) |
| 25 | { |
| 26 | NFD_LOG_DEBUG("Creating unicast " << endpoint); |
| 27 | |
| 28 | shared_ptr<UdpChannel> channel = findChannel(endpoint); |
| 29 | if (static_cast<bool>(channel)) |
| 30 | return channel; |
| 31 | |
| 32 | |
| 33 | //checking if the endpoint is already in use for multicast face |
| 34 | shared_ptr<MulticastUdpFace> multicast = findMulticastFace(endpoint); |
| 35 | if (static_cast<bool>(multicast)) |
| 36 | throw Error("Cannot create the requested UDP unicast channel, local " |
| 37 | "endpoint is already allocated for a UDP multicast face"); |
| 38 | |
| 39 | if (endpoint.address().is_multicast()) { |
| 40 | throw Error("This method is only for unicast channel. The provided " |
| 41 | "endpoint is multicast. Use createMulticastFace to " |
| 42 | "create a multicast face"); |
| 43 | } |
| 44 | |
| 45 | channel = make_shared<UdpChannel>(boost::cref(endpoint), |
| 46 | timeout); |
| 47 | m_channels[endpoint] = channel; |
| 48 | |
| 49 | return channel; |
| 50 | } |
| 51 | |
| 52 | shared_ptr<UdpChannel> |
| 53 | UdpFactory::createChannel(const std::string& localHost, |
| 54 | const std::string& localPort, |
| 55 | const time::Duration& timeout) |
| 56 | { |
| 57 | return createChannel(UdpResolver::syncResolve(localHost, localPort), |
| 58 | timeout); |
| 59 | } |
| 60 | |
| 61 | shared_ptr<MulticastUdpFace> |
| 62 | UdpFactory::createMulticastFace(const udp::Endpoint& localEndpoint, |
| 63 | const udp::Endpoint& multicastEndpoint) |
| 64 | { |
| 65 | //checking if the local and musticast endpoint are already in use for a multicast face |
| 66 | shared_ptr<MulticastUdpFace> multicastFace = findMulticastFace(localEndpoint); |
| 67 | if (static_cast<bool>(multicastFace)) { |
| 68 | if (multicastFace->getMulticastGroup() == multicastEndpoint) |
| 69 | return multicastFace; |
| 70 | else |
| 71 | throw Error("Cannot create the requested UDP multicast face, local " |
| 72 | "endpoint is already allocated for a UDP multicast face " |
| 73 | "on a different multicast group"); |
| 74 | } |
| 75 | |
| 76 | //checking if the local endpoint is already in use for an unicast channel |
| 77 | shared_ptr<UdpChannel> unicast = findChannel(localEndpoint); |
| 78 | if (static_cast<bool>(unicast)) { |
| 79 | throw Error("Cannot create the requested UDP multicast face, local " |
| 80 | "endpoint is already allocated for a UDP unicast channel"); |
| 81 | } |
| 82 | |
| 83 | if (localEndpoint.address().is_v6() || multicastEndpoint.address().is_v6()) { |
| 84 | throw Error("IPv6 multicast is not supported yet. Please provide an IPv4 address"); |
| 85 | } |
| 86 | |
| 87 | if (localEndpoint.port() != multicastEndpoint.port()) { |
| 88 | throw Error("Cannot create the requested UDP multicast face, " |
| 89 | "both endpoints should have the same port number. "); |
| 90 | } |
| 91 | |
| 92 | if (!multicastEndpoint.address().is_multicast()) { |
| 93 | throw Error("Cannot create the requested UDP multicast face, " |
| 94 | "the multicast group given as input is not a multicast address"); |
| 95 | } |
| 96 | |
| 97 | shared_ptr<ip::udp::socket> clientSocket = |
| 98 | make_shared<ip::udp::socket>(boost::ref(getGlobalIoService())); |
| 99 | |
| 100 | clientSocket->open(multicastEndpoint.protocol()); |
| 101 | |
| 102 | clientSocket->set_option(ip::udp::socket::reuse_address(true)); |
| 103 | |
| 104 | try { |
| 105 | clientSocket->bind(multicastEndpoint); |
| 106 | |
| 107 | if (localEndpoint.address() != ip::address::from_string("0.0.0.0")) { |
| 108 | clientSocket->set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4())); |
| 109 | } |
| 110 | clientSocket->set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(), |
| 111 | localEndpoint.address().to_v4())); |
| 112 | } |
| 113 | catch (boost::system::system_error& e) { |
| 114 | std::stringstream msg; |
| 115 | msg << "Failed to properly configure the socket, check the address (" << e.what() << ")"; |
| 116 | throw Error(msg.str()); |
| 117 | } |
| 118 | |
| 119 | clientSocket->set_option(ip::multicast::enable_loopback(false)); |
| 120 | |
| 121 | multicastFace = make_shared<MulticastUdpFace>(boost::cref(clientSocket)); |
| 122 | multicastFace->onFail += bind(&UdpFactory::afterFaceFailed, this, localEndpoint); |
| 123 | |
| 124 | m_multicastFaces[localEndpoint] = multicastFace; |
| 125 | |
| 126 | return multicastFace; |
| 127 | } |
| 128 | |
| 129 | shared_ptr<MulticastUdpFace> |
| 130 | UdpFactory::createMulticastFace(const std::string& localIp, |
| 131 | const std::string& multicastIp, |
| 132 | const std::string& multicastPort) |
| 133 | { |
| 134 | |
| 135 | return createMulticastFace(UdpResolver::syncResolve(localIp, |
| 136 | multicastPort), |
| 137 | UdpResolver::syncResolve(multicastIp, |
| 138 | multicastPort)); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | UdpFactory::createFace(const FaceUri& uri, |
| 143 | const FaceCreatedCallback& onCreated, |
| 144 | const FaceConnectFailedCallback& onConnectFailed) |
| 145 | { |
| 146 | resolver::AddressSelector addressSelector = resolver::AnyAddress(); |
| 147 | if (uri.getScheme() == "udp4") |
| 148 | addressSelector = resolver::Ipv4Address(); |
| 149 | else if (uri.getScheme() == "udp6") |
| 150 | addressSelector = resolver::Ipv6Address(); |
| 151 | |
| 152 | UdpResolver::asyncResolve(uri.getDomain(), |
| 153 | uri.getPort().empty() ? m_defaultPort : uri.getPort(), |
| 154 | bind(&UdpFactory::continueCreateFaceAfterResolve, this, _1, |
| 155 | onCreated, onConnectFailed), |
| 156 | onConnectFailed, |
| 157 | addressSelector); |
| 158 | |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | UdpFactory::continueCreateFaceAfterResolve(const udp::Endpoint& endpoint, |
| 163 | const FaceCreatedCallback& onCreated, |
| 164 | const FaceConnectFailedCallback& onConnectFailed) |
| 165 | { |
| 166 | if (endpoint.address().is_multicast()) { |
| 167 | onConnectFailed("The provided address is multicast. Please use createMulticastFace method"); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | // very simple logic for now |
| 172 | |
| 173 | for (ChannelMap::iterator channel = m_channels.begin(); |
| 174 | channel != m_channels.end(); |
| 175 | ++channel) |
| 176 | { |
| 177 | if ((channel->first.address().is_v4() && endpoint.address().is_v4()) || |
| 178 | (channel->first.address().is_v6() && endpoint.address().is_v6())) |
| 179 | { |
| 180 | channel->second->connect(endpoint, onCreated); |
| 181 | return; |
| 182 | } |
| 183 | } |
| 184 | onConnectFailed("No channels available to connect to " |
| 185 | + boost::lexical_cast<std::string>(endpoint)); |
| 186 | } |
| 187 | |
| 188 | shared_ptr<UdpChannel> |
| 189 | UdpFactory::findChannel(const udp::Endpoint& localEndpoint) |
| 190 | { |
| 191 | ChannelMap::iterator i = m_channels.find(localEndpoint); |
| 192 | if (i != m_channels.end()) |
| 193 | return i->second; |
| 194 | else |
| 195 | return shared_ptr<UdpChannel>(); |
| 196 | } |
| 197 | |
| 198 | shared_ptr<MulticastUdpFace> |
| 199 | UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint) |
| 200 | { |
| 201 | MulticastFaceMap::iterator i = m_multicastFaces.find(localEndpoint); |
| 202 | if (i != m_multicastFaces.end()) |
| 203 | return i->second; |
| 204 | else |
| 205 | return shared_ptr<MulticastUdpFace>(); |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | UdpFactory::afterFaceFailed(udp::Endpoint& endpoint) |
| 210 | { |
| 211 | NFD_LOG_DEBUG("afterFaceFailed: " << endpoint); |
| 212 | m_multicastFaces.erase(endpoint); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | } // namespace nfd |