blob: fad8fc311270b8f49fc782f273e559b1427ab463 [file] [log] [blame]
Vince Lehmanc439d662015-04-27 10:56:00 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22#include "nlsrc.hpp"
23
24#include "version.hpp"
25#include "src/publisher/lsa-publisher.hpp"
26
27#include <ndn-cxx/face.hpp>
28#include <ndn-cxx/data.hpp>
29#include <ndn-cxx/interest.hpp>
30#include <ndn-cxx/encoding/block.hpp>
31#include <ndn-cxx/management/nfd-control-parameters.hpp>
32#include <ndn-cxx/management/nfd-control-response.hpp>
33#include <ndn-cxx/util/segment-fetcher.hpp>
34
35#include <iostream>
36
37namespace nlsrc {
38
39const ndn::Name Nlsrc::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr");
40const ndn::Name Nlsrc::LSDB_PREFIX = ndn::Name(Nlsrc::LOCALHOST_PREFIX).append("lsdb");
41const ndn::Name Nlsrc::NAME_UPDATE_PREFIX = ndn::Name(Nlsrc::LOCALHOST_PREFIX).append("prefix-update");
42
43const uint32_t Nlsrc::ERROR_CODE_TIMEOUT = 10060;
44const uint32_t Nlsrc::RESPONSE_CODE_SUCCESS = 200;
45
46Nlsrc::Nlsrc(ndn::Face& face)
47 : m_face(face)
48{
49}
50
51void
52Nlsrc::printUsage()
53{
54 std::cout << "Usage:\n" << programName << " [-h] [-V] COMMAND [<Command Options>]\n"
55 " -h print usage and exit\n"
56 " -V print version and exit\n"
57 "\n"
58 " COMMAND can be one of the following:\n"
59 " status\n"
60 " display NLSR status\n"
61 " advertise name\n"
62 " advertise a name prefix through NLSR\n"
63 " withdraw name\n"
64 " remove a name prefix advertised through NLSR"
65 << std::endl;
66}
67
68void
69Nlsrc::getStatus()
70{
71 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchAdjacencyLsas, this));
72 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchCoordinateLsas, this));
73 m_fetchSteps.push_back(std::bind(&Nlsrc::fetchNameLsas, this));
74 m_fetchSteps.push_back(std::bind(&Nlsrc::printLsdb, this));
75
76 runNextStep();
77}
78
79bool
80Nlsrc::dispatch(const std::string& command)
81{
82 if (command == "advertise") {
83 if (nOptions != 1) {
84 return false;
85 }
86
87 advertiseName();
88 return true;
89 }
90 else if (command == "withdraw") {
91 if (nOptions != 1) {
92 return false;
93 }
94
95 withdrawName();
96 return true;
97 }
98 else if (command == "status") {
99 if (nOptions != 0) {
100 return false;
101 }
102
103 getStatus();
104 return true;
105 }
106
107 return false;
108}
109
110void
111Nlsrc::runNextStep()
112{
113 if (m_fetchSteps.empty()) {
114 return;
115 }
116
117 std::function<void()> nextStep = m_fetchSteps.front();
118 m_fetchSteps.pop_front();
119
120 nextStep();
121}
122
123void
124Nlsrc::advertiseName()
125{
126 ndn::Name name = commandLineArguments[0];
127 ndn::Name::Component verb("advertise");
128 std::string info = "(Advertise: " + name.toUri() + ")";
129
130 sendNamePrefixUpdate(name, verb, info);
131}
132
133void
134Nlsrc::withdrawName()
135{
136 ndn::Name name = commandLineArguments[0];
137 ndn::Name::Component verb("withdraw");
138 std::string info = "(Withdraw: " + name.toUri() + ")";
139
140 sendNamePrefixUpdate(name, verb, info);
141}
142
143void
144Nlsrc::sendNamePrefixUpdate(const ndn::Name& name,
145 const ndn::Name::Component& verb,
146 const std::string& info)
147{
148 ndn::nfd::ControlParameters parameters;
149 parameters.setName(name);
150
151 ndn::Name commandName = NAME_UPDATE_PREFIX;
152 commandName.append(verb);
153
154 ndn::Interest interest(commandName.append(parameters.wireEncode()));
155 interest.setMustBeFresh(true);
156 m_keyChain.sign(interest);
157
158 m_face.expressInterest(interest,
159 std::bind(&Nlsrc::onControlResponse, this, info, _2),
160 std::bind(&Nlsrc::onTimeout, this, ERROR_CODE_TIMEOUT, "Timeout"));
161}
162
163void
164Nlsrc::onControlResponse(const std::string& info, const ndn::Data& data)
165{
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500166 if (data.getMetaInfo().getType() == ndn::tlv::ContentType_Nack) {
167 std::cerr << "ERROR: Run-time advertise/withdraw disabled" << std::endl;
168 return;
169 }
170
Vince Lehmanc439d662015-04-27 10:56:00 -0500171 ndn::nfd::ControlResponse response;
172
173 try {
174 response.wireDecode(data.getContent().blockFromValue());
175 }
176 catch (const std::exception& e) {
177 std::cerr << "ERROR: Control response decoding error" << std::endl;
178 return;
179 }
180
181 uint32_t code = response.getCode();
182
183 if (code != RESPONSE_CODE_SUCCESS) {
184 std::cerr << "Name prefix update error (code: " << code << ")" << std::endl;
185 return;
186 }
187
188 std::cout << "Applied Name prefix update successfully: " << info << std::endl;
189}
190
191void
192Nlsrc::fetchAdjacencyLsas()
193{
194 fetchFromLsdb<nlsr::tlv::AdjacencyLsa>(nlsr::AdjacencyLsaPublisher::DATASET_COMPONENT,
195 std::bind(&Nlsrc::recordAdjacencyLsa, this, _1));
196}
197
198void
199Nlsrc::fetchCoordinateLsas()
200{
201 fetchFromLsdb<nlsr::tlv::CoordinateLsa>(nlsr::CoordinateLsaPublisher::DATASET_COMPONENT,
202 std::bind(&Nlsrc::recordCoordinateLsa, this, _1));
203}
204
205void
206Nlsrc::fetchNameLsas()
207{
208 fetchFromLsdb<nlsr::tlv::NameLsa>(nlsr::NameLsaPublisher::DATASET_COMPONENT,
209 std::bind(&Nlsrc::recordNameLsa, this, _1));
210}
211
212template <class T>
213void
214Nlsrc::fetchFromLsdb(const ndn::Name::Component& datasetType,
215 const std::function<void(const T&)>& recordLsa)
216{
217 ndn::Name command = LSDB_PREFIX;
218 command.append(datasetType);
219
220 ndn::Interest interest(command);
221
222 ndn::util::SegmentFetcher::fetch(m_face,
223 interest,
224 ndn::util::DontVerifySegment(),
225 std::bind(&Nlsrc::onFetchSuccess<T>,
226 this, _1, recordLsa),
227 std::bind(&Nlsrc::onTimeout, this, _1, _2));
228}
229
230template <class T>
231void
232Nlsrc::onFetchSuccess(const ndn::ConstBufferPtr& data,
233 const std::function<void(const T&)>& recordLsa)
234{
235 ndn::Block block;
236 size_t offset = 0;
237
238 while (offset < data->size()) {
239 bool isOk = false;
240 std::tie(isOk, block) = ndn::Block::fromBuffer(data, offset);
241
242 if (!isOk) {
243 std::cerr << "ERROR: cannot decode LSA TLV" << std::endl;
244 break;
245 }
246
247 offset += block.size();
248
249 T lsa(block);
250 recordLsa(lsa);
251 }
252
253 runNextStep();
254}
255
256void
257Nlsrc::onTimeout(uint32_t errorCode, const std::string& error)
258{
259 std::cerr << "Request timed out (code: " << errorCode
260 << ", error: " << error << ")" << std::endl;
261}
262
263std::string
264Nlsrc::getLsaInfoString(const nlsr::tlv::LsaInfo& info)
265{
266 std::ostringstream os;
267 os << " info=" << info;
268
269 return os.str();
270}
271
272void
273Nlsrc::recordAdjacencyLsa(const nlsr::tlv::AdjacencyLsa& lsa)
274{
275 Router& router = getRouter(lsa.getLsaInfo());
276
277 std::ostringstream os;
278 os << " AdjacencyLsa:" << std::endl;
279
280 os << getLsaInfoString(lsa.getLsaInfo()) << std::endl;
281
282 for (const auto& adjacency : lsa.getAdjacencies()) {
283 os << " adjacency=" << adjacency << std::endl;
284 }
285
286 router.adjacencyLsaString = os.str();
287}
288
289void
290Nlsrc::recordCoordinateLsa(const nlsr::tlv::CoordinateLsa& lsa)
291{
292 Router& router = getRouter(lsa.getLsaInfo());
293
294 std::ostringstream os;
295 os << " Coordinate LSA:" << std::endl;
296
297 os << getLsaInfoString(lsa.getLsaInfo()) << std::endl;
298
299 os << " angle=" << lsa.getHyperbolicAngle() << std::endl;
300 os << " radius=" << lsa.getHyperbolicRadius() << std::endl;
301
302 router.coordinateLsaString = os.str();
303}
304
305void
306Nlsrc::recordNameLsa(const nlsr::tlv::NameLsa& lsa)
307{
308 Router& router = getRouter(lsa.getLsaInfo());
309
310 std::ostringstream os;
311 os << " Name LSA:" << std::endl;
312
313 os << getLsaInfoString(lsa.getLsaInfo()) << std::endl;
314
315 for (const auto& name : lsa.getNames()) {
316 os << " name=" << name << std::endl;
317 }
318
319 router.nameLsaString = os.str();
320}
321
322void
323Nlsrc::printLsdb()
324{
325 std::cout << "NLSR Status" << std::endl;
326 std::cout << "LSDB:" << std::endl;
327
328 for (const auto& item : m_routers) {
329 std::cout << " OriginRouter: " << item.first << std::endl;
330 std::cout << std::endl;
331
332 const Router& router = item.second;
333
334 if (!router.adjacencyLsaString.empty()) {
335 std::cout << router.adjacencyLsaString << std::endl;
336 }
337
338 if (!router.coordinateLsaString.empty()) {
339 std::cout << router.coordinateLsaString << std::endl;
340 }
341
342 if (!router.nameLsaString.empty()) {
343 std::cout << router.nameLsaString << std::endl;
344 }
345 }
346}
347
348Nlsrc::Router&
349Nlsrc::getRouter(const nlsr::tlv::LsaInfo& info)
350{
351 const ndn::Name& originRouterName = info.getOriginRouter();
352
353 const auto& pair =
354 m_routers.insert(std::make_pair(originRouterName, std::move(Router())));
355
356 return pair.first->second;
357}
358
359} // namespace nlsrc
360
361////////////////////////////////////////////////////////////////////////////////
362////////////////////////////////////////////////////////////////////////////////
363
364int
365main(int argc, char** argv)
366{
367 ndn::Face face;
368 nlsrc::Nlsrc nlsrc(face);
369
370 nlsrc.programName = argv[0];
371
372 if (argc < 2) {
373 nlsrc.printUsage();
374 return 0;
375 }
376
377 int opt;
378 while ((opt = ::getopt(argc, argv, "hV")) != -1) {
379 switch (opt) {
380 case 'h':
381 nlsrc.printUsage();
382 return 0;
383 case 'V':
384 std::cout << NLSR_VERSION_BUILD_STRING << std::endl;
385 return 0;
386 default:
387 nlsrc.printUsage();
388 return 1;
389 }
390 }
391
392 if (argc == ::optind) {
393 nlsrc.printUsage();
394 return 1;
395 }
396
397 try {
398 ::optind = 2; // Set ::optind to the command's index
399
400 nlsrc.commandLineArguments = argv + ::optind;
401 nlsrc.nOptions = argc - ::optind;
402
403 // argv[1] points to the command, so pass it to the dispatch
404 bool isOk = nlsrc.dispatch(argv[1]);
405 if (!isOk) {
406 nlsrc.printUsage();
407 return 1;
408 }
409
410 face.processEvents();
411 }
412 catch (const std::exception& e) {
413 std::cerr << "ERROR: " << e.what() << std::endl;
414 return 2;
415 }
416 return 0;
417}