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