blob: 5930e99d25ed577f72f23500df7ca01baac1c129 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
Nick Gordonf14ec352017-07-24 16:09:58 -050022#include "lsa.hpp"
23#include "nlsr.hpp"
24#include "name-prefix-list.hpp"
25#include "adjacent.hpp"
26#include "logger.hpp"
27
akmhoque53353462014-04-22 08:43:45 -050028#include <string>
29#include <iostream>
30#include <sstream>
31#include <algorithm>
32#include <cmath>
33#include <limits>
akmhoque31d1d4b2014-05-05 22:08:14 -050034#include <boost/algorithm/string.hpp>
akmhoque53353462014-04-22 08:43:45 -050035
akmhoque53353462014-04-22 08:43:45 -050036namespace nlsr {
37
dmcoomescf8d0ed2017-02-21 11:39:01 -060038INIT_LOGGER(Lsa);
akmhoque674b0b12014-05-20 14:33:28 -050039
Nick Gordonfaf49f42017-10-23 12:36:28 -050040std::string
41Lsa::getData() const
42{
43 std::ostringstream os;
44 os << m_origRouter << "|" << getType() << "|" << m_lsSeqNo << "|"
45 << ndn::time::toIsoString(m_expirationTimePoint) << "|";
46 return os.str();
47}
48
akmhoque31d1d4b2014-05-05 22:08:14 -050049const ndn::Name
Nick Gordon22cc1a82017-10-23 13:06:53 -050050Lsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -050051{
Nick Gordon22cc1a82017-10-23 13:06:53 -050052 return ndn::Name(m_origRouter).append(std::to_string(getType()));
akmhoque53353462014-04-22 08:43:45 -050053}
54
Nick Gordon0fa4c772017-10-23 13:33:03 -050055bool
56Lsa::deserializeCommon(boost::tokenizer<boost::char_separator<char>>::iterator& iterator)
57{
58 m_origRouter = ndn::Name(*iterator++);
59 if (m_origRouter.size() <= 0)
60 return false;
61 if (*iterator++ != std::to_string(getType()))
62 return false;
63 m_lsSeqNo = boost::lexical_cast<uint32_t>(*iterator++);
64 m_expirationTimePoint = ndn::time::fromIsoString(*iterator++);
65 return true;
66}
67
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060068NameLsa::NameLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -050069 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -050070 NamePrefixList& npl)
akmhoque53353462014-04-22 08:43:45 -050071{
72 m_origRouter = origR;
akmhoque53353462014-04-22 08:43:45 -050073 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -050074 m_expirationTimePoint = lt;
Nick Gordonf14ec352017-07-24 16:09:58 -050075 for (const auto& name : npl.getNames()) {
76 addName(name);
akmhoque53353462014-04-22 08:43:45 -050077 }
78}
79
Nick Gordone98480b2017-05-24 11:23:03 -050080std::string
Nick Gordonfaf49f42017-10-23 12:36:28 -050081NameLsa::serialize() const
akmhoque53353462014-04-22 08:43:45 -050082{
Nick Gordonadad2492017-05-25 10:53:07 -050083 std::ostringstream os;
Nick Gordonfaf49f42017-10-23 12:36:28 -050084 os << getData() << m_npl.size();
Nick Gordonf14ec352017-07-24 16:09:58 -050085 for (const auto& name : m_npl.getNames()) {
Nick Gordonadad2492017-05-25 10:53:07 -050086 os << "|" << name;
akmhoque53353462014-04-22 08:43:45 -050087 }
Nick Gordonadad2492017-05-25 10:53:07 -050088 os << "|";
89 return os.str();
akmhoque53353462014-04-22 08:43:45 -050090}
91
92bool
Nick Gordon2f623382017-11-03 13:49:31 -050093NameLsa::deserialize(const std::string& content) noexcept
akmhoque53353462014-04-22 08:43:45 -050094{
95 uint32_t numName = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -050096 boost::char_separator<char> sep("|");
97 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
98 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
99 tokens.begin();
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600100
Nick Gordon0fa4c772017-10-23 13:33:03 -0500101 try {
102 if (!deserializeCommon(tok_iter))
103 return false;
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 numName = boost::lexical_cast<uint32_t>(*tok_iter++);
Nick Gordon0fa4c772017-10-23 13:33:03 -0500105 for (uint32_t i = 0; i < numName; i++) {
106 ndn::Name name(*tok_iter++);
107 addName(name);
108 }
akmhoque53353462014-04-22 08:43:45 -0500109 }
Nick Gordonadad2492017-05-25 10:53:07 -0500110 catch (const std::exception& e) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500111 NLSR_LOG_ERROR("Could not deserialize from content: " << e.what());
akmhoque53353462014-04-22 08:43:45 -0500112 return false;
113 }
akmhoque53353462014-04-22 08:43:45 -0500114 return true;
115}
116
Nick Gordon56d1fae2017-05-26 16:39:25 -0500117bool
118NameLsa::isEqualContent(const NameLsa& other) const
119{
120 return m_npl == other.getNpl();
121}
122
akmhoque53353462014-04-22 08:43:45 -0500123void
Nick Gordonae0a0472017-10-23 15:51:23 -0500124NameLsa::writeLog() const
akmhoque53353462014-04-22 08:43:45 -0500125{
Nick Gordonae0a0472017-10-23 15:51:23 -0500126 NLSR_LOG_DEBUG(*this);
akmhoque53353462014-04-22 08:43:45 -0500127}
128
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600129CoordinateLsa::CoordinateLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500130 const ndn::time::system_clock::TimePoint& lt,
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600131 double r, std::vector<double> theta)
akmhoque53353462014-04-22 08:43:45 -0500132{
133 m_origRouter = origR;
akmhoque53353462014-04-22 08:43:45 -0500134 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500135 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500136 m_corRad = r;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600137 m_angles = theta;
akmhoque53353462014-04-22 08:43:45 -0500138}
139
akmhoque53353462014-04-22 08:43:45 -0500140bool
Ryan Wickmanf3c79a62018-05-10 19:32:32 -0500141CoordinateLsa::isEqualContent(const CoordinateLsa& clsa) const
akmhoque53353462014-04-22 08:43:45 -0500142{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600143 if (clsa.getCorTheta().size() != m_angles.size()) {
144 return false;
145 }
146
147 std::vector<double> m_angles2 = clsa.getCorTheta();
148 for (unsigned int i = 0; i < clsa.getCorTheta().size(); i++) {
149 if (std::abs(m_angles[i] - m_angles2[i]) > std::numeric_limits<double>::epsilon()) {
150 return false;
151 }
152 }
153
akmhoque53353462014-04-22 08:43:45 -0500154 return (std::abs(m_corRad - clsa.getCorRadius()) <
akmhoque53353462014-04-22 08:43:45 -0500155 std::numeric_limits<double>::epsilon());
156}
157
Nick Gordone98480b2017-05-24 11:23:03 -0500158std::string
Nick Gordonfaf49f42017-10-23 12:36:28 -0500159CoordinateLsa::serialize() const
akmhoque53353462014-04-22 08:43:45 -0500160{
Nick Gordonadad2492017-05-25 10:53:07 -0500161 std::ostringstream os;
Nick Gordonfaf49f42017-10-23 12:36:28 -0500162 os << getData() << m_corRad << "|" << m_angles.size() << "|";
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600163 for (const auto& angle: m_angles) {
164 os << angle << "|";
165 }
Nick Gordonadad2492017-05-25 10:53:07 -0500166 return os.str();
akmhoque53353462014-04-22 08:43:45 -0500167}
168
169bool
Nick Gordon2f623382017-11-03 13:49:31 -0500170CoordinateLsa::deserialize(const std::string& content) noexcept
akmhoque53353462014-04-22 08:43:45 -0500171{
akmhoque31d1d4b2014-05-05 22:08:14 -0500172 boost::char_separator<char> sep("|");
173 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
174 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
175 tokens.begin();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600176
akmhoque31d1d4b2014-05-05 22:08:14 -0500177 try {
Nick Gordon0fa4c772017-10-23 13:33:03 -0500178 if (!deserializeCommon(tok_iter))
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600179 return false;
Nick Gordon0fa4c772017-10-23 13:33:03 -0500180 m_corRad = boost::lexical_cast<double>(*tok_iter++);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600181 int numAngles = boost::lexical_cast<uint32_t>(*tok_iter++);
Nick Gordon0fa4c772017-10-23 13:33:03 -0500182 for (int i = 0; i < numAngles; i++) {
183 m_angles.push_back(boost::lexical_cast<double>(*tok_iter++));
184 }
akmhoque53353462014-04-22 08:43:45 -0500185 }
Nick Gordonadad2492017-05-25 10:53:07 -0500186 catch (const std::exception& e) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500187 NLSR_LOG_ERROR("Could not deserialize from content: " << e.what());
akmhoque53353462014-04-22 08:43:45 -0500188 return false;
189 }
190 return true;
191}
192
akmhoque674b0b12014-05-20 14:33:28 -0500193void
Nick Gordonae0a0472017-10-23 15:51:23 -0500194CoordinateLsa::writeLog() const
akmhoque674b0b12014-05-20 14:33:28 -0500195{
Nick Gordonae0a0472017-10-23 15:51:23 -0500196 NLSR_LOG_DEBUG(*this);
akmhoque674b0b12014-05-20 14:33:28 -0500197}
198
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600199AdjLsa::AdjLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500200 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500201 uint32_t nl , AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -0500202{
203 m_origRouter = origR;
akmhoque53353462014-04-22 08:43:45 -0500204 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500205 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500206 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500207 std::list<Adjacent> al = adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500208 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500209 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500210 addAdjacent((*it));
211 }
212 }
213}
214
akmhoque53353462014-04-22 08:43:45 -0500215bool
Ryan Wickmanf3c79a62018-05-10 19:32:32 -0500216AdjLsa::isEqualContent(const AdjLsa& alsa) const
akmhoque53353462014-04-22 08:43:45 -0500217{
akmhoquefdbddb12014-05-02 18:35:19 -0500218 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500219}
220
Nick Gordone98480b2017-05-24 11:23:03 -0500221std::string
Nick Gordonfaf49f42017-10-23 12:36:28 -0500222AdjLsa::serialize() const
akmhoque53353462014-04-22 08:43:45 -0500223{
Nick Gordonadad2492017-05-25 10:53:07 -0500224 std::ostringstream os;
Nick Gordonfaf49f42017-10-23 12:36:28 -0500225 os << getData() << m_adl.size();
Nick Gordonadad2492017-05-25 10:53:07 -0500226 for (const auto& adjacent : m_adl.getAdjList()) {
Nick Gordone9733ed2017-04-26 10:48:39 -0500227 os << "|" << adjacent.getName() << "|" << adjacent.getFaceUri()
Nick Gordonadad2492017-05-25 10:53:07 -0500228 << "|" << adjacent.getLinkCost();
akmhoque53353462014-04-22 08:43:45 -0500229 }
Nick Gordonadad2492017-05-25 10:53:07 -0500230 os << "|";
231 return os.str();
akmhoque53353462014-04-22 08:43:45 -0500232}
233
234bool
Nick Gordon2f623382017-11-03 13:49:31 -0500235AdjLsa::deserialize(const std::string& content) noexcept
akmhoque53353462014-04-22 08:43:45 -0500236{
237 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500238 boost::char_separator<char> sep("|");
239 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
240 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
241 tokens.begin();
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600242
Nick Gordon0fa4c772017-10-23 13:33:03 -0500243 try {
244 if (!deserializeCommon(tok_iter))
245 return false;
246 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
247 for (uint32_t i = 0; i < numLink; i++) {
akmhoque778f81b2014-06-27 10:07:56 -0500248 ndn::Name adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500249 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500250 double linkCost = boost::lexical_cast<double>(*tok_iter++);
dulalsaurabd0816a32019-07-26 13:11:24 +0000251
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500252 Adjacent adjacent(adjName, ndn::FaceUri(connectingFaceUri), linkCost,
Nick Gordone9733ed2017-04-26 10:48:39 -0500253 Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500254 addAdjacent(adjacent);
255 }
Nick Gordon0fa4c772017-10-23 13:33:03 -0500256 }
dulalsaurabd0816a32019-07-26 13:11:24 +0000257 // Ignore neighbors with negative cost received from the Adjacent LSA data.
258 catch (const ndn::tlv::Error& e) {
259 NLSR_LOG_ERROR(e.what());
260 }
Nick Gordon0fa4c772017-10-23 13:33:03 -0500261 catch (const std::exception& e) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500262 NLSR_LOG_ERROR("Could not deserialize from content: " << e.what());
Nick Gordon0fa4c772017-10-23 13:33:03 -0500263 return false;
akmhoque53353462014-04-22 08:43:45 -0500264 }
265 return true;
266}
267
akmhoque53353462014-04-22 08:43:45 -0500268void
Nick Gordonae0a0472017-10-23 15:51:23 -0500269AdjLsa::writeLog() const
akmhoque674b0b12014-05-20 14:33:28 -0500270{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500271 NLSR_LOG_DEBUG(*this);
akmhoque53353462014-04-22 08:43:45 -0500272}
273
alvydce3f182015-04-09 11:23:30 -0500274std::ostream&
Nick Gordonae0a0472017-10-23 15:51:23 -0500275operator<<(std::ostream& os, const AdjLsa& lsa)
alvydce3f182015-04-09 11:23:30 -0500276{
Nick Gordonae0a0472017-10-23 15:51:23 -0500277 os << lsa.toString();
278 os << "-Adjacents:";
alvydce3f182015-04-09 11:23:30 -0500279
280 int adjacencyIndex = 1;
281
Nick Gordonae0a0472017-10-23 15:51:23 -0500282 for (const Adjacent& adjacency : lsa.m_adl) {
283 os << "--Adjacent" << adjacencyIndex++ << ":\n"
284 << "---Adjacent Name: " << adjacency.getName() << "\n"
285 << "---Connecting FaceUri: " << adjacency.getFaceUri() << "\n"
286 << "---Link Cost: " << adjacency.getLinkCost() << "\n";
alvydce3f182015-04-09 11:23:30 -0500287 }
288 os << "adj_lsa_end";
289
290 return os;
291}
292
Nick Gordon727d4832017-10-13 18:04:25 -0500293std::ostream&
Nick Gordonae0a0472017-10-23 15:51:23 -0500294operator<<(std::ostream& os, const CoordinateLsa& lsa)
295{
296 os << lsa.toString();
297 os << "--Hyperbolic Radius: " << lsa.m_corRad << "\n";
298 int i = 0;
299 for (const auto& value : lsa.m_angles) {
300 os << "---Hyperbolic Theta: " << i++ << ": " << value << "\n";
301 }
302 os << "cor_lsa_end";
303
304 return os;
305}
306
307std::ostream&
308operator<<(std::ostream& os, const NameLsa& lsa)
309{
310 os << lsa.toString();
311 os << "--Names:\n";
312 int i = 0;
313 auto names = lsa.m_npl.getNames();
314 for (const auto& name : names) {
315 os << "---Name " << i++ << ": " << name << "\n";
316 }
317 os << "name_lsa_end";
318
319 return os;
320}
321
322std::ostream&
Nick Gordon727d4832017-10-13 18:04:25 -0500323operator<<(std::ostream& os, const Lsa::Type& type)
324{
325 os << std::to_string(type);
326 return os;
327}
328
329std::istream&
330operator>>(std::istream& is, Lsa::Type& type)
331{
332 std::string typeString;
333 is >> typeString;
334 if (typeString == "ADJACENCY") {
335 type = Lsa::Type::ADJACENCY;
336 }
337 else if (typeString == "COORDINATE") {
338 type = Lsa::Type::COORDINATE;
339 }
340 else if (typeString == "NAME") {
341 type = Lsa::Type::NAME;
342 }
343 else {
344 type = Lsa::Type::BASE;
345 }
346 return is;
347}
348
Nick Gordonae0a0472017-10-23 15:51:23 -0500349std::string
350Lsa::toString() const
351{
352 std::ostringstream os;
353 os << "LSA of type " << getType() << ":\n-Origin Router: " << getOrigRouter()
354 << "\n-Sequence Number: " << getLsSeqNo() << "\n-Expiration Point: "
355 << getExpirationTimePoint() << "\n";
356 return os.str();
357}
358
alvydce3f182015-04-09 11:23:30 -0500359} // namespace nlsr
Nick Gordon727d4832017-10-13 18:04:25 -0500360
361namespace std {
362std::string
363to_string(const nlsr::Lsa::Type& type)
364{
365 switch (type) {
366 case nlsr::Lsa::Type::ADJACENCY:
367 return "ADJACENCY";
368 case nlsr::Lsa::Type::COORDINATE:
369 return "COORDINATE";
370 case nlsr::Lsa::Type::NAME:
371 return "NAME";
Nick Gordon9212bd42017-10-23 10:59:38 -0500372 case nlsr::Lsa::Type::MOCK:
373 return "MOCK";
Nick Gordon727d4832017-10-13 18:04:25 -0500374 default:
375 return "BASE";
376 }
377}
378
379} // namespace std