Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 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 | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 28 | #include "core/network-interface.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 30 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 32 | namespace ip = boost::asio::ip; |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 33 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 34 | NFD_LOG_INIT("TcpFactory"); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 35 | |
| 36 | void |
| 37 | TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint) |
| 38 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 39 | if (endpoint.address().is_v4() && |
| 40 | endpoint.address() == ip::address_v4::any()) { |
| 41 | prohibitAllIpv4Endpoints(endpoint.port()); |
| 42 | } |
| 43 | else if (endpoint.address().is_v6() && |
| 44 | endpoint.address() == ip::address_v6::any()) { |
| 45 | prohibitAllIpv6Endpoints(endpoint.port()); |
| 46 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 47 | |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 48 | NFD_LOG_TRACE("prohibiting TCP " << endpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 49 | m_prohibitedEndpoints.insert(endpoint); |
| 50 | } |
| 51 | |
| 52 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 53 | TcpFactory::prohibitAllIpv4Endpoints(uint16_t port) |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 54 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 55 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 56 | for (const auto& addr : nic.ipv4Addresses) { |
| 57 | if (addr != ip::address_v4::any()) { |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 58 | prohibitEndpoint(tcp::Endpoint(addr, port)); |
| 59 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 60 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 61 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 65 | TcpFactory::prohibitAllIpv6Endpoints(uint16_t port) |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 66 | { |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 67 | for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 68 | for (const auto& addr : nic.ipv6Addresses) { |
| 69 | if (addr != ip::address_v6::any()) { |
Alexander Afanasyev | 70aaf8a | 2014-12-13 00:44:22 -0800 | [diff] [blame] | 70 | prohibitEndpoint(tcp::Endpoint(addr, port)); |
| 71 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 72 | } |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 73 | } |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | shared_ptr<TcpChannel> |
| 77 | TcpFactory::createChannel(const tcp::Endpoint& endpoint) |
| 78 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 79 | auto channel = findChannel(endpoint); |
| 80 | if (channel) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 81 | return channel; |
| 82 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 83 | channel = make_shared<TcpChannel>(endpoint); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 84 | m_channels[endpoint] = channel; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 85 | prohibitEndpoint(endpoint); |
| 86 | |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 87 | NFD_LOG_DEBUG("Channel [" << endpoint << "] created"); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 88 | return channel; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | shared_ptr<TcpChannel> |
Alexander Afanasyev | 0e156df | 2015-01-26 22:33:43 -0800 | [diff] [blame] | 92 | TcpFactory::createChannel(const std::string& localIp, const std::string& localPort) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 93 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 94 | tcp::Endpoint endpoint(ip::address::from_string(localIp), |
| 95 | boost::lexical_cast<uint16_t>(localPort)); |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 96 | return createChannel(endpoint); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 99 | void |
| 100 | TcpFactory::createFace(const FaceUri& uri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 101 | ndn::nfd::FacePersistency persistency, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 102 | const FaceCreatedCallback& onCreated, |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 103 | const FaceCreationFailedCallback& onConnectFailed) |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 104 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 105 | BOOST_ASSERT(uri.isCanonical()); |
| 106 | |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 107 | if (persistency != ndn::nfd::FACE_PERSISTENCY_PERSISTENT) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame^] | 108 | NFD_LOG_TRACE("createFace only supports FACE_PERSISTENCY_PERSISTENT"); |
| 109 | onConnectFailed(406, "Outgoing TCP faces only support persistent persistency"); |
| 110 | return; |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 113 | tcp::Endpoint endpoint(ip::address::from_string(uri.getHost()), |
| 114 | boost::lexical_cast<uint16_t>(uri.getPort())); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 115 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame^] | 116 | if (endpoint.address().is_multicast()) { |
| 117 | NFD_LOG_TRACE("createFace cannot create multicast faces"); |
| 118 | onConnectFailed(406, "Cannot create multicast TCP faces"); |
| 119 | return; |
| 120 | } |
| 121 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 122 | if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame^] | 123 | NFD_LOG_TRACE("Requested endpoint is prohibited " |
| 124 | "(reserved by NFD or disallowed by face management protocol)"); |
| 125 | onConnectFailed(406, "Requested endpoint is prohibited"); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 126 | return; |
| 127 | } |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 128 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 129 | // very simple logic for now |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 130 | for (const auto& i : m_channels) { |
| 131 | if ((i.first.address().is_v4() && endpoint.address().is_v4()) || |
| 132 | (i.first.address().is_v6() && endpoint.address().is_v6())) { |
| 133 | i.second->connect(endpoint, onCreated, onConnectFailed); |
| 134 | return; |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 135 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame^] | 138 | NFD_LOG_TRACE("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint)); |
| 139 | onConnectFailed(504, "No channels available to connect"); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 142 | std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 143 | TcpFactory::getChannels() const |
| 144 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 145 | std::vector<shared_ptr<const Channel>> channels; |
| 146 | channels.reserve(m_channels.size()); |
| 147 | |
| 148 | for (const auto& i : m_channels) |
| 149 | channels.push_back(i.second); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 150 | |
| 151 | return channels; |
| 152 | } |
| 153 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 154 | shared_ptr<TcpChannel> |
| 155 | TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) const |
| 156 | { |
| 157 | auto i = m_channels.find(localEndpoint); |
| 158 | if (i != m_channels.end()) |
| 159 | return i->second; |
| 160 | else |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 164 | } // namespace nfd |