blob: 9142774a0f3f0947c8a3e72f0f81c5e6e1670faf [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{
166 ndn::nfd::ControlResponse response;
167
168 try {
169 response.wireDecode(data.getContent().blockFromValue());
170 }
171 catch (const std::exception& e) {
172 std::cerr << "ERROR: Control response decoding error" << std::endl;
173 return;
174 }
175
176 uint32_t code = response.getCode();
177
178 if (code != RESPONSE_CODE_SUCCESS) {
179 std::cerr << "Name prefix update error (code: " << code << ")" << std::endl;
180 return;
181 }
182
183 std::cout << "Applied Name prefix update successfully: " << info << std::endl;
184}
185
186void
187Nlsrc::fetchAdjacencyLsas()
188{
189 fetchFromLsdb<nlsr::tlv::AdjacencyLsa>(nlsr::AdjacencyLsaPublisher::DATASET_COMPONENT,
190 std::bind(&Nlsrc::recordAdjacencyLsa, this, _1));
191}
192
193void
194Nlsrc::fetchCoordinateLsas()
195{
196 fetchFromLsdb<nlsr::tlv::CoordinateLsa>(nlsr::CoordinateLsaPublisher::DATASET_COMPONENT,
197 std::bind(&Nlsrc::recordCoordinateLsa, this, _1));
198}
199
200void
201Nlsrc::fetchNameLsas()
202{
203 fetchFromLsdb<nlsr::tlv::NameLsa>(nlsr::NameLsaPublisher::DATASET_COMPONENT,
204 std::bind(&Nlsrc::recordNameLsa, this, _1));
205}
206
207template <class T>
208void
209Nlsrc::fetchFromLsdb(const ndn::Name::Component& datasetType,
210 const std::function<void(const T&)>& recordLsa)
211{
212 ndn::Name command = LSDB_PREFIX;
213 command.append(datasetType);
214
215 ndn::Interest interest(command);
216
217 ndn::util::SegmentFetcher::fetch(m_face,
218 interest,
219 ndn::util::DontVerifySegment(),
220 std::bind(&Nlsrc::onFetchSuccess<T>,
221 this, _1, recordLsa),
222 std::bind(&Nlsrc::onTimeout, this, _1, _2));
223}
224
225template <class T>
226void
227Nlsrc::onFetchSuccess(const ndn::ConstBufferPtr& data,
228 const std::function<void(const T&)>& recordLsa)
229{
230 ndn::Block block;
231 size_t offset = 0;
232
233 while (offset < data->size()) {
234 bool isOk = false;
235 std::tie(isOk, block) = ndn::Block::fromBuffer(data, offset);
236
237 if (!isOk) {
238 std::cerr << "ERROR: cannot decode LSA TLV" << std::endl;
239 break;
240 }
241
242 offset += block.size();
243
244 T lsa(block);
245 recordLsa(lsa);
246 }
247
248 runNextStep();
249}
250
251void
252Nlsrc::onTimeout(uint32_t errorCode, const std::string& error)
253{
254 std::cerr << "Request timed out (code: " << errorCode
255 << ", error: " << error << ")" << std::endl;
256}
257
258std::string
259Nlsrc::getLsaInfoString(const nlsr::tlv::LsaInfo& info)
260{
261 std::ostringstream os;
262 os << " info=" << info;
263
264 return os.str();
265}
266
267void
268Nlsrc::recordAdjacencyLsa(const nlsr::tlv::AdjacencyLsa& lsa)
269{
270 Router& router = getRouter(lsa.getLsaInfo());
271
272 std::ostringstream os;
273 os << " AdjacencyLsa:" << std::endl;
274
275 os << getLsaInfoString(lsa.getLsaInfo()) << std::endl;
276
277 for (const auto& adjacency : lsa.getAdjacencies()) {
278 os << " adjacency=" << adjacency << std::endl;
279 }
280
281 router.adjacencyLsaString = os.str();
282}
283
284void
285Nlsrc::recordCoordinateLsa(const nlsr::tlv::CoordinateLsa& lsa)
286{
287 Router& router = getRouter(lsa.getLsaInfo());
288
289 std::ostringstream os;
290 os << " Coordinate LSA:" << std::endl;
291
292 os << getLsaInfoString(lsa.getLsaInfo()) << std::endl;
293
294 os << " angle=" << lsa.getHyperbolicAngle() << std::endl;
295 os << " radius=" << lsa.getHyperbolicRadius() << std::endl;
296
297 router.coordinateLsaString = os.str();
298}
299
300void
301Nlsrc::recordNameLsa(const nlsr::tlv::NameLsa& lsa)
302{
303 Router& router = getRouter(lsa.getLsaInfo());
304
305 std::ostringstream os;
306 os << " Name LSA:" << std::endl;
307
308 os << getLsaInfoString(lsa.getLsaInfo()) << std::endl;
309
310 for (const auto& name : lsa.getNames()) {
311 os << " name=" << name << std::endl;
312 }
313
314 router.nameLsaString = os.str();
315}
316
317void
318Nlsrc::printLsdb()
319{
320 std::cout << "NLSR Status" << std::endl;
321 std::cout << "LSDB:" << std::endl;
322
323 for (const auto& item : m_routers) {
324 std::cout << " OriginRouter: " << item.first << std::endl;
325 std::cout << std::endl;
326
327 const Router& router = item.second;
328
329 if (!router.adjacencyLsaString.empty()) {
330 std::cout << router.adjacencyLsaString << std::endl;
331 }
332
333 if (!router.coordinateLsaString.empty()) {
334 std::cout << router.coordinateLsaString << std::endl;
335 }
336
337 if (!router.nameLsaString.empty()) {
338 std::cout << router.nameLsaString << std::endl;
339 }
340 }
341}
342
343Nlsrc::Router&
344Nlsrc::getRouter(const nlsr::tlv::LsaInfo& info)
345{
346 const ndn::Name& originRouterName = info.getOriginRouter();
347
348 const auto& pair =
349 m_routers.insert(std::make_pair(originRouterName, std::move(Router())));
350
351 return pair.first->second;
352}
353
354} // namespace nlsrc
355
356////////////////////////////////////////////////////////////////////////////////
357////////////////////////////////////////////////////////////////////////////////
358
359int
360main(int argc, char** argv)
361{
362 ndn::Face face;
363 nlsrc::Nlsrc nlsrc(face);
364
365 nlsrc.programName = argv[0];
366
367 if (argc < 2) {
368 nlsrc.printUsage();
369 return 0;
370 }
371
372 int opt;
373 while ((opt = ::getopt(argc, argv, "hV")) != -1) {
374 switch (opt) {
375 case 'h':
376 nlsrc.printUsage();
377 return 0;
378 case 'V':
379 std::cout << NLSR_VERSION_BUILD_STRING << std::endl;
380 return 0;
381 default:
382 nlsrc.printUsage();
383 return 1;
384 }
385 }
386
387 if (argc == ::optind) {
388 nlsrc.printUsage();
389 return 1;
390 }
391
392 try {
393 ::optind = 2; // Set ::optind to the command's index
394
395 nlsrc.commandLineArguments = argv + ::optind;
396 nlsrc.nOptions = argc - ::optind;
397
398 // argv[1] points to the command, so pass it to the dispatch
399 bool isOk = nlsrc.dispatch(argv[1]);
400 if (!isOk) {
401 nlsrc.printUsage();
402 return 1;
403 }
404
405 face.processEvents();
406 }
407 catch (const std::exception& e) {
408 std::cerr << "ERROR: " << e.what() << std::endl;
409 return 2;
410 }
411 return 0;
412}