Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [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 | * 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 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 26 | #include "tcp-factory.hpp" |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 27 | #include "core/resolver.hpp" |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 29 | #include "core/network-interface.hpp" |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 30 | |
| 31 | NFD_LOG_INIT("TcpFactory"); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 35 | TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/) |
| 36 | : m_defaultPort(defaultPort) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 37 | { |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 38 | |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint) |
| 43 | { |
| 44 | using namespace boost::asio::ip; |
| 45 | |
| 46 | static const address_v4 ALL_V4_ENDPOINT(address_v4::from_string("0.0.0.0")); |
| 47 | static const address_v6 ALL_V6_ENDPOINT(address_v6::from_string("::")); |
| 48 | |
| 49 | const address& address = endpoint.address(); |
| 50 | |
| 51 | if (address.is_v4() && address == ALL_V4_ENDPOINT) |
| 52 | { |
| 53 | prohibitAllIpv4Endpoints(endpoint.port()); |
| 54 | } |
| 55 | else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT) |
| 56 | { |
| 57 | prohibitAllIpv6Endpoints(endpoint.port()); |
| 58 | } |
| 59 | |
| 60 | NFD_LOG_TRACE("prohibiting TCP " << |
| 61 | endpoint.address().to_string() << ":" << endpoint.port()); |
| 62 | |
| 63 | m_prohibitedEndpoints.insert(endpoint); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | TcpFactory::prohibitAllIpv4Endpoints(const uint16_t port) |
| 68 | { |
| 69 | using namespace boost::asio::ip; |
| 70 | |
| 71 | const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces()); |
| 72 | |
| 73 | for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin(); |
| 74 | i != nicList.end(); |
| 75 | ++i) |
| 76 | { |
| 77 | const shared_ptr<NetworkInterfaceInfo>& nic = *i; |
| 78 | const std::vector<address_v4>& ipv4Addresses = nic->ipv4Addresses; |
| 79 | |
| 80 | for (std::vector<address_v4>::const_iterator j = ipv4Addresses.begin(); |
| 81 | j != ipv4Addresses.end(); |
| 82 | ++j) |
| 83 | { |
| 84 | prohibitEndpoint(tcp::Endpoint(*j, port)); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | TcpFactory::prohibitAllIpv6Endpoints(const uint16_t port) |
| 91 | { |
| 92 | using namespace boost::asio::ip; |
| 93 | |
| 94 | const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces()); |
| 95 | |
| 96 | for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin(); |
| 97 | i != nicList.end(); |
| 98 | ++i) |
| 99 | { |
| 100 | const shared_ptr<NetworkInterfaceInfo>& nic = *i; |
| 101 | const std::vector<address_v6>& ipv6Addresses = nic->ipv6Addresses; |
| 102 | |
| 103 | for (std::vector<address_v6>::const_iterator j = ipv6Addresses.begin(); |
| 104 | j != ipv6Addresses.end(); |
| 105 | ++j) |
| 106 | { |
| 107 | prohibitEndpoint(tcp::Endpoint(*j, port)); |
| 108 | } |
| 109 | } |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | shared_ptr<TcpChannel> |
| 113 | TcpFactory::createChannel(const tcp::Endpoint& endpoint) |
| 114 | { |
| 115 | shared_ptr<TcpChannel> channel = findChannel(endpoint); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 116 | if(static_cast<bool>(channel)) |
| 117 | return channel; |
| 118 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 119 | channel = make_shared<TcpChannel>(endpoint); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 120 | m_channels[endpoint] = channel; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 121 | prohibitEndpoint(endpoint); |
| 122 | |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 123 | NFD_LOG_DEBUG("Channel [" << endpoint << "] created"); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 124 | return channel; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | shared_ptr<TcpChannel> |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 128 | TcpFactory::createChannel(const std::string& localHost, const std::string& localPort) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 129 | { |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 130 | return createChannel(TcpResolver::syncResolve(localHost, localPort)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | shared_ptr<TcpChannel> |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 134 | TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 135 | { |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 136 | ChannelMap::iterator i = m_channels.find(localEndpoint); |
| 137 | if (i != m_channels.end()) |
| 138 | return i->second; |
| 139 | else |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 140 | return shared_ptr<TcpChannel>(); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 143 | void |
| 144 | TcpFactory::createFace(const FaceUri& uri, |
| 145 | const FaceCreatedCallback& onCreated, |
| 146 | const FaceConnectFailedCallback& onConnectFailed) |
| 147 | { |
| 148 | resolver::AddressSelector addressSelector = resolver::AnyAddress(); |
| 149 | if (uri.getScheme() == "tcp4") |
| 150 | addressSelector = resolver::Ipv4Address(); |
| 151 | else if (uri.getScheme() == "tcp6") |
| 152 | addressSelector = resolver::Ipv6Address(); |
| 153 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 154 | if (!uri.getPath().empty()) |
| 155 | { |
| 156 | onConnectFailed("Invalid URI"); |
| 157 | } |
| 158 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 159 | TcpResolver::asyncResolve(uri.getHost(), |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 160 | uri.getPort().empty() ? m_defaultPort : uri.getPort(), |
| 161 | bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1, |
| 162 | onCreated, onConnectFailed), |
| 163 | onConnectFailed, |
| 164 | addressSelector); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
| 168 | TcpFactory::continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint, |
| 169 | const FaceCreatedCallback& onCreated, |
| 170 | const FaceConnectFailedCallback& onConnectFailed) |
| 171 | { |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 172 | if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) |
| 173 | { |
| 174 | onConnectFailed("Requested endpoint is prohibited " |
| 175 | "(reserved by this NFD or disallowed by face management protocol)"); |
| 176 | return; |
| 177 | } |
| 178 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 179 | // very simple logic for now |
| 180 | |
| 181 | for (ChannelMap::iterator channel = m_channels.begin(); |
| 182 | channel != m_channels.end(); |
| 183 | ++channel) |
| 184 | { |
| 185 | if ((channel->first.address().is_v4() && endpoint.address().is_v4()) || |
| 186 | (channel->first.address().is_v6() && endpoint.address().is_v6())) |
| 187 | { |
| 188 | channel->second->connect(endpoint, onCreated, onConnectFailed); |
| 189 | return; |
| 190 | } |
| 191 | } |
| 192 | onConnectFailed("No channels available to connect to " |
| 193 | + boost::lexical_cast<std::string>(endpoint)); |
| 194 | } |
| 195 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame^] | 196 | std::list<shared_ptr<const Channel> > |
| 197 | TcpFactory::getChannels() const |
| 198 | { |
| 199 | std::list<shared_ptr<const Channel> > channels; |
| 200 | for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i) |
| 201 | { |
| 202 | channels.push_back(i->second); |
| 203 | } |
| 204 | |
| 205 | return channels; |
| 206 | } |
| 207 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 208 | } // namespace nfd |