blob: 8ae23373d43dd12dc8510180f2e25cff2adb136f [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberry42602412016-08-27 09:33:18 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Chengyu Fan4381fb62015-01-14 11:37:04 -07004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080025
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080026#include "tcp-factory.hpp"
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080027#include "core/logger.hpp"
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060028#include "core/network-interface.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080029
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080030namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080031
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020032namespace ip = boost::asio::ip;
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080033
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020034NFD_LOG_INIT("TcpFactory");
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060035
36void
37TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint)
38{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020039 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 DiBenedettoca53ac62014-03-27 19:58:40 -060047
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080048 NFD_LOG_TRACE("prohibiting TCP " << endpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060049 m_prohibitedEndpoints.insert(endpoint);
50}
51
52void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020053TcpFactory::prohibitAllIpv4Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060054{
Davide Pesaventob499a602014-11-18 22:36:56 +010055 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020056 for (const auto& addr : nic.ipv4Addresses) {
57 if (addr != ip::address_v4::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080058 prohibitEndpoint(tcp::Endpoint(addr, port));
59 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060060 }
Davide Pesaventob499a602014-11-18 22:36:56 +010061 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060062}
63
64void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020065TcpFactory::prohibitAllIpv6Endpoints(uint16_t port)
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060066{
Davide Pesaventob499a602014-11-18 22:36:56 +010067 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020068 for (const auto& addr : nic.ipv6Addresses) {
69 if (addr != ip::address_v6::any()) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080070 prohibitEndpoint(tcp::Endpoint(addr, port));
71 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060072 }
Davide Pesaventob499a602014-11-18 22:36:56 +010073 }
Alexander Afanasyevd6655302014-02-28 08:41:28 -080074}
75
76shared_ptr<TcpChannel>
77TcpFactory::createChannel(const tcp::Endpoint& endpoint)
78{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020079 auto channel = findChannel(endpoint);
80 if (channel)
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080081 return channel;
82
Alexander Afanasyevf6980282014-05-13 18:28:40 -070083 channel = make_shared<TcpChannel>(endpoint);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080084 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060085 prohibitEndpoint(endpoint);
86
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080087 NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080088 return channel;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080089}
90
91shared_ptr<TcpChannel>
Alexander Afanasyev0e156df2015-01-26 22:33:43 -080092TcpFactory::createChannel(const std::string& localIp, const std::string& localPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080093{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020094 tcp::Endpoint endpoint(ip::address::from_string(localIp),
95 boost::lexical_cast<uint16_t>(localPort));
Chengyu Fan4381fb62015-01-14 11:37:04 -070096 return createChannel(endpoint);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080097}
98
Alexander Afanasyevd6655302014-02-28 08:41:28 -080099void
100TcpFactory::createFace(const FaceUri& uri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800101 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700102 bool wantLocalFieldsEnabled,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800103 const FaceCreatedCallback& onCreated,
Eric Newberryf40551a2016-09-05 15:41:16 -0700104 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800105{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200106 BOOST_ASSERT(uri.isCanonical());
107
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800108 if (persistency != ndn::nfd::FACE_PERSISTENCY_PERSISTENT) {
Eric Newberry42602412016-08-27 09:33:18 -0700109 NFD_LOG_TRACE("createFace only supports FACE_PERSISTENCY_PERSISTENT");
Eric Newberryf40551a2016-09-05 15:41:16 -0700110 onFailure(406, "Outgoing TCP faces only support persistent persistency");
Eric Newberry42602412016-08-27 09:33:18 -0700111 return;
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800112 }
113
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200114 tcp::Endpoint endpoint(ip::address::from_string(uri.getHost()),
115 boost::lexical_cast<uint16_t>(uri.getPort()));
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800116
Eric Newberry42602412016-08-27 09:33:18 -0700117 if (endpoint.address().is_multicast()) {
118 NFD_LOG_TRACE("createFace cannot create multicast faces");
Eric Newberryf40551a2016-09-05 15:41:16 -0700119 onFailure(406, "Cannot create multicast TCP faces");
Eric Newberry42602412016-08-27 09:33:18 -0700120 return;
121 }
122
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200123 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
Eric Newberry42602412016-08-27 09:33:18 -0700124 NFD_LOG_TRACE("Requested endpoint is prohibited "
125 "(reserved by NFD or disallowed by face management protocol)");
Eric Newberryf40551a2016-09-05 15:41:16 -0700126 onFailure(406, "Requested endpoint is prohibited");
127 return;
128 }
129
130 if (wantLocalFieldsEnabled && !endpoint.address().is_loopback()) {
131 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
132 onFailure(406, "Local fields can only be enabled on faces with local scope");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200133 return;
134 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600135
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800136 // very simple logic for now
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200137 for (const auto& i : m_channels) {
138 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
139 (i.first.address().is_v6() && endpoint.address().is_v6())) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700140 i.second->connect(endpoint, wantLocalFieldsEnabled, onCreated, onFailure);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200141 return;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800142 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200143 }
144
Eric Newberry42602412016-08-27 09:33:18 -0700145 NFD_LOG_TRACE("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
Eric Newberryf40551a2016-09-05 15:41:16 -0700146 onFailure(504, "No channels available to connect");
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800147}
148
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200149std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600150TcpFactory::getChannels() const
151{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200152 std::vector<shared_ptr<const Channel>> channels;
153 channels.reserve(m_channels.size());
154
155 for (const auto& i : m_channels)
156 channels.push_back(i.second);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600157
158 return channels;
159}
160
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200161shared_ptr<TcpChannel>
162TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) const
163{
164 auto i = m_channels.find(localEndpoint);
165 if (i != m_channels.end())
166 return i->second;
167 else
168 return nullptr;
169}
170
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800171} // namespace nfd