blob: a262220f1f5317a5922e9a424618ee139db12e93 [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
Junxiao Shidda0b462014-06-30 19:42:29 -070059 onRegisterCommandSuccess(const FaceEventNotification& notification, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070060 {
Junxiao Shidda0b462014-06-30 19:42:29 -070061 std::cerr << "SUCCEED: register " << prefix << " on face "
62 << notification.getFaceId() << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070063 }
64
65 void
Junxiao Shidda0b462014-06-30 19:42:29 -070066 onRegisterCommandFailure(const FaceEventNotification& notification, const Name& prefix,
67 uint32_t code, const std::string& reason)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070068 {
Junxiao Shidda0b462014-06-30 19:42:29 -070069 std::cerr << "FAILED: register " << prefix << " on face " << notification.getFaceId()
Alexander Afanasyevf4e89b42014-05-31 15:54:18 +030070 << " (code: " << code << ", reason: " << reason << ")" << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070071 }
72
Junxiao Shidda0b462014-06-30 19:42:29 -070073 /**
74 * \return true if auto-register should not be performed for uri
75 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070076 bool
77 isFiltered(const FaceUri& uri)
78 {
79 const std::string& scheme = uri.getScheme();
80 if (!(scheme == "udp4" || scheme == "tcp4" ||
81 scheme == "udp6" || scheme == "tcp6"))
82 return true;
83
84 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost());
85
86 for (std::vector<Network>::const_iterator network = m_blackList.begin();
87 network != m_blackList.end();
88 ++network)
89 {
90 if (network->doesContain(address))
91 return true;
92 }
93
94 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
95 network != m_whiteList.end();
96 ++network)
97 {
98 if (network->doesContain(address))
99 return false;
100 }
101
102 return true;
103 }
104
105 void
106 processCreateFace(const FaceEventNotification& notification)
107 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700108 FaceUri uri(notification.getRemoteUri());
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700109
110 if (isFiltered(uri))
111 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700112 std::cerr << "FILTERED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700113 return;
114 }
115
Junxiao Shidda0b462014-06-30 19:42:29 -0700116 std::cerr << "PROCESSING: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700117 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
118 prefix != m_autoregPrefixes.end();
119 ++prefix)
120 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700121 m_controller.start<RibRegisterCommand>(
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700122 ControlParameters()
123 .setName(*prefix)
124 .setFaceId(notification.getFaceId())
Junxiao Shidda0b462014-06-30 19:42:29 -0700125 .setOrigin(ROUTE_ORIGIN_AUTOREG)
126 .setCost(m_cost)
127 .setExpirationPeriod(time::milliseconds::max()),
128 bind(&AutoregServer::onRegisterCommandSuccess, this, notification, *prefix),
129 bind(&AutoregServer::onRegisterCommandFailure, this, notification, *prefix, _1, _2));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700130 }
131 }
132
133 void
Junxiao Shi15b12e72014-08-09 19:56:24 -0700134 onNotification(const FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700135 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700136 if (notification.getKind() == FACE_EVENT_CREATED &&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700137 !notification.isLocal() &&
138 notification.isOnDemand())
139 {
140 processCreateFace(notification);
141 }
142 else
143 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700144 std::cerr << "IGNORED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700145 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700146 }
147
148 void
149 signalHandler()
150 {
151 m_face.shutdown();
152 }
153
154
155
156 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700157 usage(std::ostream& os,
158 const po::options_description& optionDesciption,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700159 const char* programName)
160 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700161 os << "Usage:\n"
162 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
163 << "\n";
164 os << optionDesciption;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700165 }
166
167 void
168 startProcessing()
169 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700170 std::cerr << "AUTOREG prefixes: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700171 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
172 prefix != m_autoregPrefixes.end();
173 ++prefix)
174 {
175 std::cout << " " << *prefix << std::endl;
176 }
177
178 if (!m_blackList.empty())
179 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700180 std::cerr << "Blacklisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700181 for (std::vector<Network>::const_iterator network = m_blackList.begin();
182 network != m_blackList.end();
183 ++network)
184 {
185 std::cout << " " << *network << std::endl;
186 }
187 }
188
Junxiao Shidda0b462014-06-30 19:42:29 -0700189 std::cerr << "Whitelisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700190 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
191 network != m_whiteList.end();
192 ++network)
193 {
194 std::cout << " " << *network << std::endl;
195 }
196
Junxiao Shi15b12e72014-08-09 19:56:24 -0700197 m_faceMonitor.onNotification += bind(&AutoregServer::onNotification, this, _1);
198 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700199
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700200 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700201 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700202
203 m_face.processEvents();
204 }
205
206 int
207 main(int argc, char* argv[])
208 {
209 po::options_description optionDesciption;
210 optionDesciption.add_options()
211 ("help,h", "produce help message")
212 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
213 "prefix that should be automatically registered when new remote face is established")
214 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
215 "FIB cost which should be assigned to autoreg nexthops")
216 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
217 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
218 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
219 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700220 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700221 ;
222
223 po::variables_map options;
224 try
225 {
226 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
227 po::notify(options);
228 }
229 catch (std::exception& e)
230 {
231 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700232 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700233 return 1;
234 }
235
236 if (options.count("help"))
237 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700238 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700239 return 0;
240 }
241
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700242 if (options.count("version"))
243 {
244 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
245 return 0;
246 }
247
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700248 if (m_autoregPrefixes.empty())
249 {
250 std::cerr << "ERROR: at least one --prefix must be specified" << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700251 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700252 return 2;
253 }
254
255 if (m_whiteList.empty())
256 {
257 // Allow everything
258 m_whiteList.push_back(Network::getMaxRangeV4());
259 m_whiteList.push_back(Network::getMaxRangeV6());
260 }
261
262 try
263 {
264 startProcessing();
265 }
266 catch (std::exception& e)
267 {
268 std::cerr << "ERROR: " << e.what() << std::endl;
269 return 2;
270 }
271
272 return 0;
273 }
274
275private:
276 Face m_face;
277 Controller m_controller;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700278 FaceMonitor m_faceMonitor;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700279 std::vector<ndn::Name> m_autoregPrefixes;
280 uint64_t m_cost;
281 std::vector<Network> m_whiteList;
282 std::vector<Network> m_blackList;
283};
284
285} // namespace nfd
286
287int
288main(int argc, char* argv[])
289{
290 nfd::AutoregServer server;
291 return server.main(argc, argv);
292}