blob: bb08313317831fd994c70b763b279c3db957c83f [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080024
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080025#include "tcp-factory.hpp"
Alexander Afanasyevd6655302014-02-28 08:41:28 -080026#include "core/resolver.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 Afanasyev5f1ec252014-02-28 10:59:17 -080029
30NFD_LOG_INIT("TcpFactory");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080033
Alexander Afanasyevd6655302014-02-28 08:41:28 -080034TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/)
35 : m_defaultPort(defaultPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080036{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060037
38}
39
40void
41TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint)
42{
43 using namespace boost::asio::ip;
44
45 static const address_v4 ALL_V4_ENDPOINT(address_v4::from_string("0.0.0.0"));
46 static const address_v6 ALL_V6_ENDPOINT(address_v6::from_string("::"));
47
48 const address& address = endpoint.address();
49
50 if (address.is_v4() && address == ALL_V4_ENDPOINT)
51 {
52 prohibitAllIpv4Endpoints(endpoint.port());
53 }
54 else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
55 {
56 prohibitAllIpv6Endpoints(endpoint.port());
57 }
58
59 NFD_LOG_TRACE("prohibiting TCP " <<
60 endpoint.address().to_string() << ":" << endpoint.port());
61
62 m_prohibitedEndpoints.insert(endpoint);
63}
64
65void
66TcpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
67{
68 using namespace boost::asio::ip;
69
70 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
71
72 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
73 i != nicList.end();
74 ++i)
75 {
76 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
77 const std::vector<address_v4>& ipv4Addresses = nic->ipv4Addresses;
78
79 for (std::vector<address_v4>::const_iterator j = ipv4Addresses.begin();
80 j != ipv4Addresses.end();
81 ++j)
82 {
83 prohibitEndpoint(tcp::Endpoint(*j, port));
84 }
85 }
86}
87
88void
89TcpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
90{
91 using namespace boost::asio::ip;
92
93 const std::list<shared_ptr<NetworkInterfaceInfo> > nicList(listNetworkInterfaces());
94
95 for (std::list<shared_ptr<NetworkInterfaceInfo> >::const_iterator i = nicList.begin();
96 i != nicList.end();
97 ++i)
98 {
99 const shared_ptr<NetworkInterfaceInfo>& nic = *i;
100 const std::vector<address_v6>& ipv6Addresses = nic->ipv6Addresses;
101
102 for (std::vector<address_v6>::const_iterator j = ipv6Addresses.begin();
103 j != ipv6Addresses.end();
104 ++j)
105 {
106 prohibitEndpoint(tcp::Endpoint(*j, port));
107 }
108 }
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800109}
110
111shared_ptr<TcpChannel>
112TcpFactory::createChannel(const tcp::Endpoint& endpoint)
113{
114 shared_ptr<TcpChannel> channel = findChannel(endpoint);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800115 if(static_cast<bool>(channel))
116 return channel;
117
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700118 channel = make_shared<TcpChannel>(endpoint);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800119 m_channels[endpoint] = channel;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600120 prohibitEndpoint(endpoint);
121
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800122 NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800123 return channel;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800124}
125
126shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800127TcpFactory::createChannel(const std::string& localHost, const std::string& localPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800128{
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800129 return createChannel(TcpResolver::syncResolve(localHost, localPort));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800130}
131
132shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800133TcpFactory::findChannel(const tcp::Endpoint& localEndpoint)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800134{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800135 ChannelMap::iterator i = m_channels.find(localEndpoint);
136 if (i != m_channels.end())
137 return i->second;
138 else
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800139 return shared_ptr<TcpChannel>();
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800140}
141
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800142void
143TcpFactory::createFace(const FaceUri& uri,
144 const FaceCreatedCallback& onCreated,
145 const FaceConnectFailedCallback& onConnectFailed)
146{
147 resolver::AddressSelector addressSelector = resolver::AnyAddress();
148 if (uri.getScheme() == "tcp4")
149 addressSelector = resolver::Ipv4Address();
150 else if (uri.getScheme() == "tcp6")
151 addressSelector = resolver::Ipv6Address();
152
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600153 if (!uri.getPath().empty())
154 {
155 onConnectFailed("Invalid URI");
156 }
157
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700158 TcpResolver::asyncResolve(uri.getHost(),
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -0800159 uri.getPort().empty() ? m_defaultPort : uri.getPort(),
160 bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1,
161 onCreated, onConnectFailed),
162 onConnectFailed,
163 addressSelector);
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800164}
165
166void
167TcpFactory::continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint,
168 const FaceCreatedCallback& onCreated,
169 const FaceConnectFailedCallback& onConnectFailed)
170{
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600171 if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
172 {
173 onConnectFailed("Requested endpoint is prohibited "
174 "(reserved by this NFD or disallowed by face management protocol)");
175 return;
176 }
177
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800178 // very simple logic for now
179
180 for (ChannelMap::iterator channel = m_channels.begin();
181 channel != m_channels.end();
182 ++channel)
183 {
184 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
185 (channel->first.address().is_v6() && endpoint.address().is_v6()))
186 {
187 channel->second->connect(endpoint, onCreated, onConnectFailed);
188 return;
189 }
190 }
191 onConnectFailed("No channels available to connect to "
192 + boost::lexical_cast<std::string>(endpoint));
193}
194
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800195} // namespace nfd