blob: 91f6a7af18aca308719d3863d3fc4d3357a61cfe [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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
akmhoque674b0b12014-05-20 14:33:28 -050038INIT_LOGGER("Lsa");
39
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
akmhoquefdbddb12014-05-02 18:35:19 -0500141CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
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
akmhoquefdbddb12014-05-02 18:35:19 -0500216AdjLsa::isEqualContent(AdjLsa& alsa)
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++);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500251 Adjacent adjacent(adjName, ndn::FaceUri(connectingFaceUri), linkCost,
Nick Gordone9733ed2017-04-26 10:48:39 -0500252 Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500253 addAdjacent(adjacent);
254 }
Nick Gordon0fa4c772017-10-23 13:33:03 -0500255 }
256 catch (const std::exception& e) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500257 NLSR_LOG_ERROR("Could not deserialize from content: " << e.what());
Nick Gordon0fa4c772017-10-23 13:33:03 -0500258 return false;
akmhoque53353462014-04-22 08:43:45 -0500259 }
260 return true;
261}
262
akmhoque53353462014-04-22 08:43:45 -0500263void
264AdjLsa::addNptEntries(Nlsr& pnlsr)
265{
Nick G97e34942016-07-11 14:46:27 -0500266 // Only add NPT entries if this is an adj LSA from another router.
akmhoque157b0a42014-05-13 00:26:37 -0500267 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500268 // Pass the originating router as both the name to register and
269 // where it came from.
akmhoque31d1d4b2014-05-05 22:08:14 -0500270 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500271 }
272}
273
274
275void
276AdjLsa::removeNptEntries(Nlsr& pnlsr)
277{
akmhoque157b0a42014-05-13 00:26:37 -0500278 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500279 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500280 }
281}
282
akmhoque674b0b12014-05-20 14:33:28 -0500283void
Nick Gordonae0a0472017-10-23 15:51:23 -0500284AdjLsa::writeLog() const
akmhoque674b0b12014-05-20 14:33:28 -0500285{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500286 NLSR_LOG_DEBUG(*this);
akmhoque53353462014-04-22 08:43:45 -0500287}
288
alvydce3f182015-04-09 11:23:30 -0500289std::ostream&
Nick Gordonae0a0472017-10-23 15:51:23 -0500290operator<<(std::ostream& os, const AdjLsa& lsa)
alvydce3f182015-04-09 11:23:30 -0500291{
Nick Gordonae0a0472017-10-23 15:51:23 -0500292 os << lsa.toString();
293 os << "-Adjacents:";
alvydce3f182015-04-09 11:23:30 -0500294
295 int adjacencyIndex = 1;
296
Nick Gordonae0a0472017-10-23 15:51:23 -0500297 for (const Adjacent& adjacency : lsa.m_adl) {
298 os << "--Adjacent" << adjacencyIndex++ << ":\n"
299 << "---Adjacent Name: " << adjacency.getName() << "\n"
300 << "---Connecting FaceUri: " << adjacency.getFaceUri() << "\n"
301 << "---Link Cost: " << adjacency.getLinkCost() << "\n";
alvydce3f182015-04-09 11:23:30 -0500302 }
303 os << "adj_lsa_end";
304
305 return os;
306}
307
Nick Gordon727d4832017-10-13 18:04:25 -0500308std::ostream&
Nick Gordonae0a0472017-10-23 15:51:23 -0500309operator<<(std::ostream& os, const CoordinateLsa& lsa)
310{
311 os << lsa.toString();
312 os << "--Hyperbolic Radius: " << lsa.m_corRad << "\n";
313 int i = 0;
314 for (const auto& value : lsa.m_angles) {
315 os << "---Hyperbolic Theta: " << i++ << ": " << value << "\n";
316 }
317 os << "cor_lsa_end";
318
319 return os;
320}
321
322std::ostream&
323operator<<(std::ostream& os, const NameLsa& lsa)
324{
325 os << lsa.toString();
326 os << "--Names:\n";
327 int i = 0;
328 auto names = lsa.m_npl.getNames();
329 for (const auto& name : names) {
330 os << "---Name " << i++ << ": " << name << "\n";
331 }
332 os << "name_lsa_end";
333
334 return os;
335}
336
337std::ostream&
Nick Gordon727d4832017-10-13 18:04:25 -0500338operator<<(std::ostream& os, const Lsa::Type& type)
339{
340 os << std::to_string(type);
341 return os;
342}
343
344std::istream&
345operator>>(std::istream& is, Lsa::Type& type)
346{
347 std::string typeString;
348 is >> typeString;
349 if (typeString == "ADJACENCY") {
350 type = Lsa::Type::ADJACENCY;
351 }
352 else if (typeString == "COORDINATE") {
353 type = Lsa::Type::COORDINATE;
354 }
355 else if (typeString == "NAME") {
356 type = Lsa::Type::NAME;
357 }
358 else {
359 type = Lsa::Type::BASE;
360 }
361 return is;
362}
363
Nick Gordonae0a0472017-10-23 15:51:23 -0500364std::string
365Lsa::toString() const
366{
367 std::ostringstream os;
368 os << "LSA of type " << getType() << ":\n-Origin Router: " << getOrigRouter()
369 << "\n-Sequence Number: " << getLsSeqNo() << "\n-Expiration Point: "
370 << getExpirationTimePoint() << "\n";
371 return os.str();
372}
373
alvydce3f182015-04-09 11:23:30 -0500374} // namespace nlsr
Nick Gordon727d4832017-10-13 18:04:25 -0500375
376namespace std {
377std::string
378to_string(const nlsr::Lsa::Type& type)
379{
380 switch (type) {
381 case nlsr::Lsa::Type::ADJACENCY:
382 return "ADJACENCY";
383 case nlsr::Lsa::Type::COORDINATE:
384 return "COORDINATE";
385 case nlsr::Lsa::Type::NAME:
386 return "NAME";
Nick Gordon9212bd42017-10-23 10:59:38 -0500387 case nlsr::Lsa::Type::MOCK:
388 return "MOCK";
Nick Gordon727d4832017-10-13 18:04:25 -0500389 default:
390 return "BASE";
391 }
392}
393
394} // namespace std