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