blob: 21dbc8355d742135f2ae1c8c835d08ceaf670771 [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-face-event-notification.hpp>
30#include <ndn-cxx/management/nfd-controller.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;
46
Junxiao Shidda0b462014-06-30 19:42:29 -070047class AutoregServer : boost::noncopyable
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070048{
49public:
50 AutoregServer()
51 : m_controller(m_face)
52 // , m_lastNotification(<undefined>)
53 , m_cost(255)
54 {
55 }
56
57 void
Junxiao Shidda0b462014-06-30 19:42:29 -070058 onRegisterCommandSuccess(const FaceEventNotification& notification, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070059 {
Junxiao Shidda0b462014-06-30 19:42:29 -070060 std::cerr << "SUCCEED: register " << prefix << " on face "
61 << notification.getFaceId() << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070062 }
63
64 void
Junxiao Shidda0b462014-06-30 19:42:29 -070065 onRegisterCommandFailure(const FaceEventNotification& notification, const Name& prefix,
66 uint32_t code, const std::string& reason)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070067 {
Junxiao Shidda0b462014-06-30 19:42:29 -070068 std::cerr << "FAILED: register " << prefix << " on face " << notification.getFaceId()
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 /**
73 * \return true if auto-register should not be performed for uri
74 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070075 bool
76 isFiltered(const FaceUri& uri)
77 {
78 const std::string& scheme = uri.getScheme();
79 if (!(scheme == "udp4" || scheme == "tcp4" ||
80 scheme == "udp6" || scheme == "tcp6"))
81 return true;
82
83 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost());
84
85 for (std::vector<Network>::const_iterator network = m_blackList.begin();
86 network != m_blackList.end();
87 ++network)
88 {
89 if (network->doesContain(address))
90 return true;
91 }
92
93 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
94 network != m_whiteList.end();
95 ++network)
96 {
97 if (network->doesContain(address))
98 return false;
99 }
100
101 return true;
102 }
103
104 void
105 processCreateFace(const FaceEventNotification& notification)
106 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700107 FaceUri uri(notification.getRemoteUri());
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700108
109 if (isFiltered(uri))
110 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700111 std::cerr << "FILTERED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700112 return;
113 }
114
Junxiao Shidda0b462014-06-30 19:42:29 -0700115 std::cerr << "PROCESSING: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700116 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
117 prefix != m_autoregPrefixes.end();
118 ++prefix)
119 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700120 m_controller.start<RibRegisterCommand>(
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700121 ControlParameters()
122 .setName(*prefix)
123 .setFaceId(notification.getFaceId())
Junxiao Shidda0b462014-06-30 19:42:29 -0700124 .setOrigin(ROUTE_ORIGIN_AUTOREG)
125 .setCost(m_cost)
126 .setExpirationPeriod(time::milliseconds::max()),
127 bind(&AutoregServer::onRegisterCommandSuccess, this, notification, *prefix),
128 bind(&AutoregServer::onRegisterCommandFailure, this, notification, *prefix, _1, _2));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700129 }
130 }
131
132 void
133 onNotification(const Data& data)
134 {
135 m_lastNotification = data.getName().get(-1).toSegment();
136
137 // process
138 FaceEventNotification notification(data.getContent().blockFromValue());
139
Junxiao Shi6e694322014-04-03 10:27:13 -0700140 if (notification.getKind() == FACE_EVENT_CREATED &&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700141 !notification.isLocal() &&
142 notification.isOnDemand())
143 {
144 processCreateFace(notification);
145 }
146 else
147 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700148 std::cerr << "IGNORED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700149 }
150
151 Name nextNotification("/localhost/nfd/faces/events");
152 nextNotification
153 .appendSegment(m_lastNotification + 1);
154
155 // no need to set freshness or child selectors
156 m_face.expressInterest(nextNotification,
157 bind(&AutoregServer::onNotification, this, _2),
158 bind(&AutoregServer::onTimeout, this, _1));
159 }
160
161 void
162 onTimeout(const Interest& timedOutInterest)
163 {
164 // re-express the timed out interest, but reset Nonce, since it has to change
165
166 // To be robust against missing notification, use ChildSelector and MustBeFresh
167 Interest interest("/localhost/nfd/faces/events");
168 interest
169 .setMustBeFresh(true)
170 .setChildSelector(1)
171 .setInterestLifetime(time::seconds(60))
172 ;
173
174 m_face.expressInterest(interest,
175 bind(&AutoregServer::onNotification, this, _2),
176 bind(&AutoregServer::onTimeout, this, _1));
177 }
178
179 void
180 signalHandler()
181 {
182 m_face.shutdown();
183 }
184
185
186
187 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700188 usage(std::ostream& os,
189 const po::options_description& optionDesciption,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700190 const char* programName)
191 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700192 os << "Usage:\n"
193 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
194 << "\n";
195 os << optionDesciption;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700196 }
197
198 void
199 startProcessing()
200 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700201 std::cerr << "AUTOREG prefixes: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700202 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
203 prefix != m_autoregPrefixes.end();
204 ++prefix)
205 {
206 std::cout << " " << *prefix << std::endl;
207 }
208
209 if (!m_blackList.empty())
210 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700211 std::cerr << "Blacklisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700212 for (std::vector<Network>::const_iterator network = m_blackList.begin();
213 network != m_blackList.end();
214 ++network)
215 {
216 std::cout << " " << *network << std::endl;
217 }
218 }
219
Junxiao Shidda0b462014-06-30 19:42:29 -0700220 std::cerr << "Whitelisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700221 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
222 network != m_whiteList.end();
223 ++network)
224 {
225 std::cout << " " << *network << std::endl;
226 }
227
228 Interest interest("/localhost/nfd/faces/events");
229 interest
230 .setMustBeFresh(true)
231 .setChildSelector(1)
232 .setInterestLifetime(time::seconds(60))
233 ;
234
235 m_face.expressInterest(interest,
236 bind(&AutoregServer::onNotification, this, _2),
237 bind(&AutoregServer::onTimeout, this, _1));
238
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700239 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700240 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700241
242 m_face.processEvents();
243 }
244
245 int
246 main(int argc, char* argv[])
247 {
248 po::options_description optionDesciption;
249 optionDesciption.add_options()
250 ("help,h", "produce help message")
251 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
252 "prefix that should be automatically registered when new remote face is established")
253 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
254 "FIB cost which should be assigned to autoreg nexthops")
255 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
256 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
257 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
258 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700259 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700260 ;
261
262 po::variables_map options;
263 try
264 {
265 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
266 po::notify(options);
267 }
268 catch (std::exception& e)
269 {
270 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700271 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700272 return 1;
273 }
274
275 if (options.count("help"))
276 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700277 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700278 return 0;
279 }
280
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700281 if (options.count("version"))
282 {
283 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
284 return 0;
285 }
286
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700287 if (m_autoregPrefixes.empty())
288 {
289 std::cerr << "ERROR: at least one --prefix must be specified" << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700290 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700291 return 2;
292 }
293
294 if (m_whiteList.empty())
295 {
296 // Allow everything
297 m_whiteList.push_back(Network::getMaxRangeV4());
298 m_whiteList.push_back(Network::getMaxRangeV6());
299 }
300
301 try
302 {
303 startProcessing();
304 }
305 catch (std::exception& e)
306 {
307 std::cerr << "ERROR: " << e.what() << std::endl;
308 return 2;
309 }
310
311 return 0;
312 }
313
314private:
315 Face m_face;
316 Controller m_controller;
317 uint64_t m_lastNotification;
318 std::vector<ndn::Name> m_autoregPrefixes;
319 uint64_t m_cost;
320 std::vector<Network> m_whiteList;
321 std::vector<Network> m_blackList;
322};
323
324} // namespace nfd
325
326int
327main(int argc, char* argv[])
328{
329 nfd::AutoregServer server;
330 return server.main(argc, argv);
331}