blob: e85cdffb09d3b5615526d8a0f352bf14cc048e86 [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shidda0b462014-06-30 19:42:29 -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 * 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/>.
Junxiao Shidda0b462014-06-30 19:42:29 -070024 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070025
Alexander Afanasyev4a771362014-04-24 21:29:33 -070026#include <ndn-cxx/face.hpp>
27#include <ndn-cxx/name.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070028
Alexander Afanasyev4a771362014-04-24 21:29:33 -070029#include <ndn-cxx/management/nfd-controller.hpp>
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070030#include <ndn-cxx/management/nfd-face-monitor.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070031
32#include <boost/program_options/options_description.hpp>
33#include <boost/program_options/variables_map.hpp>
34#include <boost/program_options/parsers.hpp>
35
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070036#include "version.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070037#include "core/face-uri.hpp"
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070038#include "network.hpp"
39
40namespace po = boost::program_options;
41
42namespace nfd {
43
44using namespace ndn::nfd;
45using ndn::Face;
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070046using ndn::nfd::FaceEventNotification;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070047
Junxiao Shidda0b462014-06-30 19:42:29 -070048class AutoregServer : boost::noncopyable
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070049{
50public:
51 AutoregServer()
52 : m_controller(m_face)
Junxiao Shi15b12e72014-08-09 19:56:24 -070053 , m_faceMonitor(m_face)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070054 , m_cost(255)
55 {
56 }
57
58 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070059 onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070060 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070061 std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070062 }
63
64 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070065 onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
Junxiao Shidda0b462014-06-30 19:42:29 -070066 uint32_t code, const std::string& reason)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070067 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070068 std::cerr << "FAILED: register " << prefix << " on face " << faceId
Alexander Afanasyevf4e89b42014-05-31 15:54:18 +030069 << " (code: " << code << ", reason: " << reason << ")" << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070070 }
71
Junxiao Shidda0b462014-06-30 19:42:29 -070072 /**
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070073 * \return true if uri has schema allowed to do auto-registrations
Junxiao Shidda0b462014-06-30 19:42:29 -070074 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070075 bool
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070076 hasAllowedSchema(const FaceUri& uri)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070077 {
78 const std::string& scheme = uri.getScheme();
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070079 return (scheme == "udp4" || scheme == "tcp4" ||
80 scheme == "udp6" || scheme == "tcp6");
81 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070082
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070083 /**
84 * \return true if uri is blacklisted
85 */
86 bool
87 isBlacklisted(const FaceUri& uri)
88 {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070089 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost());
90
91 for (std::vector<Network>::const_iterator network = m_blackList.begin();
92 network != m_blackList.end();
93 ++network)
94 {
95 if (network->doesContain(address))
96 return true;
97 }
98
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070099 return false;
100 }
101
102 /**
103 * \return true if uri is whitelisted
104 */
105 bool
106 isWhitelisted(const FaceUri& uri)
107 {
108 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost());
109
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700110 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
111 network != m_whiteList.end();
112 ++network)
113 {
114 if (network->doesContain(address))
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700115 return true;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700116 }
117
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700118 return false;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700119 }
120
121 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700122 registerPrefixesForFace(uint64_t faceId,
123 const std::vector<ndn::Name>& prefixes)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700124 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700125 for (std::vector<ndn::Name>::const_iterator prefix = prefixes.begin();
126 prefix != prefixes.end();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700127 ++prefix)
128 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700129 m_controller.start<RibRegisterCommand>(
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700130 ControlParameters()
131 .setName(*prefix)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700132 .setFaceId(faceId)
Junxiao Shidda0b462014-06-30 19:42:29 -0700133 .setOrigin(ROUTE_ORIGIN_AUTOREG)
134 .setCost(m_cost)
135 .setExpirationPeriod(time::milliseconds::max()),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700136 bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, *prefix),
137 bind(&AutoregServer::onRegisterCommandFailure, this, faceId, *prefix, _1, _2));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700138 }
139 }
140
141 void
Junxiao Shi15b12e72014-08-09 19:56:24 -0700142 onNotification(const FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700143 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700144 if (notification.getKind() == FACE_EVENT_CREATED &&
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700145 !notification.isLocal())
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700146 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700147 std::cerr << "PROCESSING: " << notification << std::endl;
148
149 FaceUri uri(notification.getRemoteUri());
150
151 if (hasAllowedSchema(uri)) {
152 // register all-face prefixes
153 registerPrefixesForFace(notification.getFaceId(), m_allFacesPrefixes);
154
155 // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
156 if (notification.isOnDemand() && !isBlacklisted(uri) && isWhitelisted(uri)) {
157 registerPrefixesForFace(notification.getFaceId(), m_autoregPrefixes);
158 }
159 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700160 }
161 else
162 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700163 std::cerr << "IGNORED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700164 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700165 }
166
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700167
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700168 void
169 signalHandler()
170 {
171 m_face.shutdown();
172 }
173
174
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700175 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700176 usage(std::ostream& os,
177 const po::options_description& optionDesciption,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700178 const char* programName)
179 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700180 os << "Usage:\n"
181 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
182 << "\n";
183 os << optionDesciption;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700184 }
185
186 void
187 startProcessing()
188 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700189 std::cerr << "AUTOREG prefixes: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700190 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
191 prefix != m_autoregPrefixes.end();
192 ++prefix)
193 {
194 std::cout << " " << *prefix << std::endl;
195 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700196 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
197 for (std::vector<ndn::Name>::const_iterator prefix = m_allFacesPrefixes.begin();
198 prefix != m_allFacesPrefixes.end();
199 ++prefix)
200 {
201 std::cout << " " << *prefix << std::endl;
202 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700203
204 if (!m_blackList.empty())
205 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700206 std::cerr << "Blacklisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700207 for (std::vector<Network>::const_iterator network = m_blackList.begin();
208 network != m_blackList.end();
209 ++network)
210 {
211 std::cout << " " << *network << std::endl;
212 }
213 }
214
Junxiao Shidda0b462014-06-30 19:42:29 -0700215 std::cerr << "Whitelisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700216 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
217 network != m_whiteList.end();
218 ++network)
219 {
220 std::cout << " " << *network << std::endl;
221 }
222
Junxiao Shi15b12e72014-08-09 19:56:24 -0700223 m_faceMonitor.onNotification += bind(&AutoregServer::onNotification, this, _1);
224 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700225
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700226 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700227 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700228
229 m_face.processEvents();
230 }
231
232 int
233 main(int argc, char* argv[])
234 {
235 po::options_description optionDesciption;
236 optionDesciption.add_options()
237 ("help,h", "produce help message")
238 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700239 "prefix that should be automatically registered when new a remote non-local face is "
240 "established")
241 ("all-faces-prefix,a", po::value<std::vector<ndn::Name> >(&m_allFacesPrefixes)->composing(),
242 "prefix that should be automatically registered for all TCP and UDP non-local faces "
243 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700244 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
245 "FIB cost which should be assigned to autoreg nexthops")
246 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
247 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
248 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
249 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700250 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700251 ;
252
253 po::variables_map options;
254 try
255 {
256 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
257 po::notify(options);
258 }
259 catch (std::exception& e)
260 {
261 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700262 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700263 return 1;
264 }
265
266 if (options.count("help"))
267 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700268 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700269 return 0;
270 }
271
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700272 if (options.count("version"))
273 {
274 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
275 return 0;
276 }
277
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700278 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700279 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700280 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
281 << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700282 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700283 return 2;
284 }
285
286 if (m_whiteList.empty())
287 {
288 // Allow everything
289 m_whiteList.push_back(Network::getMaxRangeV4());
290 m_whiteList.push_back(Network::getMaxRangeV6());
291 }
292
293 try
294 {
295 startProcessing();
296 }
297 catch (std::exception& e)
298 {
299 std::cerr << "ERROR: " << e.what() << std::endl;
300 return 2;
301 }
302
303 return 0;
304 }
305
306private:
307 Face m_face;
308 Controller m_controller;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700309 FaceMonitor m_faceMonitor;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700310 std::vector<ndn::Name> m_autoregPrefixes;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700311 std::vector<ndn::Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700312 uint64_t m_cost;
313 std::vector<Network> m_whiteList;
314 std::vector<Network> m_blackList;
315};
316
317} // namespace nfd
318
319int
320main(int argc, char* argv[])
321{
322 nfd::AutoregServer server;
323 return server.main(argc, argv);
324}