blob: dc7e36948f1fa26f9fd91a86e4d57ccc104da65b [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Chengyu Fan4381fb62015-01-14 11:37:04 -07003 * 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 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"
Chengyu Fan4381fb62015-01-14 11:37:04 -070029#include "core/global-io.hpp"
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080030
31NFD_LOG_INIT("TcpFactory");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080034
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080035static const boost::asio::ip::address_v4 ALL_V4_ENDPOINT(
36 boost::asio::ip::address_v4::from_string("0.0.0.0"));
37
38static const boost::asio::ip::address_v6 ALL_V6_ENDPOINT(
39 boost::asio::ip::address_v6::from_string("::"));
40
Alexander Afanasyevd6655302014-02-28 08:41:28 -080041TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/)
42 : m_defaultPort(defaultPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080043{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060044}
45
46void
47TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint)
48{
49 using namespace boost::asio::ip;
50
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060051 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 Afanasyev70aaf8a2014-12-13 00:44:22 -080062 NFD_LOG_TRACE("prohibiting TCP " << endpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060063
64 m_prohibitedEndpoints.insert(endpoint);
65}
66
67void
68TcpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
69{
70 using namespace boost::asio::ip;
71
Davide Pesaventob499a602014-11-18 22:36:56 +010072 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
73 for (const address_v4& addr : nic.ipv4Addresses) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080074 if (addr != ALL_V4_ENDPOINT) {
75 prohibitEndpoint(tcp::Endpoint(addr, port));
76 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060077 }
Davide Pesaventob499a602014-11-18 22:36:56 +010078 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060079}
80
81void
82TcpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
83{
84 using namespace boost::asio::ip;
85
Davide Pesaventob499a602014-11-18 22:36:56 +010086 for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
87 for (const address_v6& addr : nic.ipv6Addresses) {
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -080088 if (addr != ALL_V6_ENDPOINT) {
89 prohibitEndpoint(tcp::Endpoint(addr, port));
90 }
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060091 }
Davide Pesaventob499a602014-11-18 22:36:56 +010092 }
Alexander Afanasyevd6655302014-02-28 08:41:28 -080093}
94
95shared_ptr<TcpChannel>
96TcpFactory::createChannel(const tcp::Endpoint& endpoint)
97{
98 shared_ptr<TcpChannel> channel = findChannel(endpoint);
Davide Pesaventob499a602014-11-18 22:36:56 +010099 if (static_cast<bool>(channel))
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800100 return channel;
101
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700102 channel = make_shared<TcpChannel>(endpoint);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800103 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600104 prohibitEndpoint(endpoint);
105
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800106 NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800107 return channel;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800108}
109
110shared_ptr<TcpChannel>
Alexander Afanasyev0e156df2015-01-26 22:33:43 -0800111TcpFactory::createChannel(const std::string& localIp, const std::string& localPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800112{
Alexander Afanasyev0e156df2015-01-26 22:33:43 -0800113 using namespace boost::asio::ip;
114 tcp::Endpoint endpoint(address::from_string(localIp), boost::lexical_cast<uint16_t>(localPort));
Chengyu Fan4381fb62015-01-14 11:37:04 -0700115 return createChannel(endpoint);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800116}
117
118shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800119TcpFactory::findChannel(const tcp::Endpoint& localEndpoint)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800120{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800121 ChannelMap::iterator i = m_channels.find(localEndpoint);
122 if (i != m_channels.end())
123 return i->second;
124 else
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800125 return shared_ptr<TcpChannel>();
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800126}
127
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800128void
129TcpFactory::createFace(const FaceUri& uri,
130 const FaceCreatedCallback& onCreated,
131 const FaceConnectFailedCallback& onConnectFailed)
132{
Chengyu Fan4381fb62015-01-14 11:37:04 -0700133 BOOST_ASSERT(uri.isCanonical());
134 boost::asio::ip::address ipAddress = boost::asio::ip::address::from_string(uri.getHost());
135 tcp::Endpoint endpoint(ipAddress, boost::lexical_cast<uint16_t>(uri.getPort()));
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800136
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600137 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
138 {
139 onConnectFailed("Requested endpoint is prohibited "
140 "(reserved by this NFD or disallowed by face management protocol)");
141 return;
142 }
143
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800144 // very simple logic for now
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800145 for (ChannelMap::iterator channel = m_channels.begin();
146 channel != m_channels.end();
147 ++channel)
148 {
149 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
150 (channel->first.address().is_v6() && endpoint.address().is_v6()))
151 {
152 channel->second->connect(endpoint, onCreated, onConnectFailed);
153 return;
154 }
155 }
156 onConnectFailed("No channels available to connect to "
157 + boost::lexical_cast<std::string>(endpoint));
158}
159
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600160std::list<shared_ptr<const Channel> >
161TcpFactory::getChannels() const
162{
163 std::list<shared_ptr<const Channel> > channels;
164 for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
165 {
166 channels.push_back(i->second);
167 }
168
169 return channels;
170}
171
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800172} // namespace nfd