blob: 30a230956d110db1713ffed27ac3cd478a4bd213 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedettoef04f272014-06-04 14:28:31 -06003 * 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 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 Afanasyevd6655302014-02-28 08:41:28 -080027#include "core/resolver.hpp"
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080028#include "core/logger.hpp"
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060029#include "core/network-interface.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 Afanasyevd6655302014-02-28 08:41:28 -080035TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/)
36 : m_defaultPort(defaultPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080037{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060038
39}
40
41void
42TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint)
43{
44 using namespace boost::asio::ip;
45
46 static const address_v4 ALL_V4_ENDPOINT(address_v4::from_string("0.0.0.0"));
47 static const address_v6 ALL_V6_ENDPOINT(address_v6::from_string("::"));
48
49 const address& address = endpoint.address();
50
51 if (address.is_v4() && address == ALL_V4_ENDPOINT)
52 {
53 prohibitAllIpv4Endpoints(endpoint.port());
54 }
55 else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
56 {
57 prohibitAllIpv6Endpoints(endpoint.port());
58 }
59
60 NFD_LOG_TRACE("prohibiting TCP " <<
61 endpoint.address().to_string() << ":" << endpoint.port());
62
63 m_prohibitedEndpoints.insert(endpoint);
64}
65
66void
67TcpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
68{
69 using namespace boost::asio::ip;
70
71 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
72
73 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
74 i != nicList.end();
75 ++i)
76 {
77 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
78 const std::vector<address_v4>& ipv4Addresses = nic->ipv4Addresses;
79
80 for (std::vector<address_v4>::const_iterator j = ipv4Addresses.begin();
81 j != ipv4Addresses.end();
82 ++j)
83 {
84 prohibitEndpoint(tcp::Endpoint(*j, port));
85 }
86 }
87}
88
89void
90TcpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
91{
92 using namespace boost::asio::ip;
93
94 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
95
96 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
97 i != nicList.end();
98 ++i)
99 {
100 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
101 const std::vector<address_v6>& ipv6Addresses = nic->ipv6Addresses;
102
103 for (std::vector<address_v6>::const_iterator j = ipv6Addresses.begin();
104 j != ipv6Addresses.end();
105 ++j)
106 {
107 prohibitEndpoint(tcp::Endpoint(*j, port));
108 }
109 }
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800110}
111
112shared_ptr<TcpChannel>
113TcpFactory::createChannel(const tcp::Endpoint& endpoint)
114{
115 shared_ptr<TcpChannel> channel = findChannel(endpoint);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800116 if(static_cast<bool>(channel))
117 return channel;
118
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700119 channel = make_shared<TcpChannel>(endpoint);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800120 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600121 prohibitEndpoint(endpoint);
122
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800123 NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800124 return channel;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800125}
126
127shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800128TcpFactory::createChannel(const std::string& localHost, const std::string& localPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800129{
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800130 return createChannel(TcpResolver::syncResolve(localHost, localPort));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800131}
132
133shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800134TcpFactory::findChannel(const tcp::Endpoint& localEndpoint)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800135{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800136 ChannelMap::iterator i = m_channels.find(localEndpoint);
137 if (i != m_channels.end())
138 return i->second;
139 else
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800140 return shared_ptr<TcpChannel>();
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800141}
142
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800143void
144TcpFactory::createFace(const FaceUri& uri,
145 const FaceCreatedCallback& onCreated,
146 const FaceConnectFailedCallback& onConnectFailed)
147{
148 resolver::AddressSelector addressSelector = resolver::AnyAddress();
149 if (uri.getScheme() == "tcp4")
150 addressSelector = resolver::Ipv4Address();
151 else if (uri.getScheme() == "tcp6")
152 addressSelector = resolver::Ipv6Address();
153
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600154 if (!uri.getPath().empty())
155 {
156 onConnectFailed("Invalid URI");
157 }
158
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700159 TcpResolver::asyncResolve(uri.getHost(),
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800160 uri.getPort().empty() ? m_defaultPort : uri.getPort(),
161 bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1,
162 onCreated, onConnectFailed),
163 onConnectFailed,
164 addressSelector);
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800165}
166
167void
168TcpFactory::continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint,
169 const FaceCreatedCallback& onCreated,
170 const FaceConnectFailedCallback& onConnectFailed)
171{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600172 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
173 {
174 onConnectFailed("Requested endpoint is prohibited "
175 "(reserved by this NFD or disallowed by face management protocol)");
176 return;
177 }
178
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800179 // very simple logic for now
180
181 for (ChannelMap::iterator channel = m_channels.begin();
182 channel != m_channels.end();
183 ++channel)
184 {
185 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
186 (channel->first.address().is_v6() && endpoint.address().is_v6()))
187 {
188 channel->second->connect(endpoint, onCreated, onConnectFailed);
189 return;
190 }
191 }
192 onConnectFailed("No channels available to connect to "
193 + boost::lexical_cast<std::string>(endpoint));
194}
195
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600196std::list<shared_ptr<const Channel> >
197TcpFactory::getChannels() const
198{
199 std::list<shared_ptr<const Channel> > channels;
200 for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
201 {
202 channels.push_back(i->second);
203 }
204
205 return channels;
206}
207
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800208} // namespace nfd